Python

Python is one of the most popular coding languages, especially among students. BrainPad gives Python developers access to the physical world.


DUE Link Pre-Setup

DUE Link allows devices to be connected to a PC that is programmed is any programming language, including python. To prep you BrainPad to work with Python, it needs to be loaded with DUE Link software.

Visit the DUE Link Console and click firmware option in the top menu.

Note that you do not need to use the console. You are only using it for the firmware update feature. This step is only necessary if the board is not already loaded with the latest DUE Link firmware and only needs to be done once. Come back here to continue with Python.


Blink LED

Python programs are codded in a development software (IDE) that helps coders in writing and debugging code. There are several options for Python, too many actually! Some options are very simple and beginner-friendly, like Thonny.

We like Thonny because it is a very small download and works on Windows, Mac, and Linux. It took us 5 minutes to download, install, and run a single line of code print(“Hello BrainPad”)


An excellent alternative is JetBrains PyCharm. PyCharm is a robust IDE, tailored specifically for Python development

Another good option is Microsoft Visual Studio Code. VC Code is very powerful and supports many languages. There is more setup needed for a starter but it is worth the investment in the long run.

We will show you how to get started with all IDEs but you can use any other IDE similarly.


Thonny

Start by visiting the thonny.org website to download and install the latest version. Open Thonny and add this simple line of code print(“Hello BrainPad”) then run the program by clicking the green circle with next to the “bug”! If all is good, you should see Hello BrainPad show in the shell window.

Go ahead and save the file as test.py. We need to install the libraries needed to access the BrainPad, which you loaded with the DUE Link firmware earlier. Go to Tools ⮞ Open the system shell…

In the shell, type pip install DUELink

Close the shell and modify the code. Make sure your BrainPad is plugged in and run the program. The LED will blink 10 times. Can you modify the code to make the LED blink faster, or make it blink 20 times instead? Note that Python is case-sensitive. This means BrainPad is not the same as Brainpad.

from DUELink.DUELinkController import DUELinkController
print("Hello BrainPad!")
availablePort = DUELinkController.GetConnectionPort()
BrainPad = DUELinkController(availablePort)
# Flash the LED 20 times (on for 200ms and off for 800ms)
BrainPad.Led.Set(200,800,10)
print("Bye BrainPad!")

Debugging is the process of running the program step by step to try to find programming errors. If you haven’t yet, save the file as test.py and click on the “bug” icon. This will bring execution to the first line of code. Start now by clicking on step over till you reach the line that starts with BrainPad. Open the variables window from View ⮞ Variables.

The variable availableport at this point will hold the name of the currently available port to which the BrainPad is connected. In our case it is COM4 but yours may be different.


PyCharm

PyCharm, crafted by JetBrains, stands as a comprehensive IDE tailored exclusively for Python development.

Boasting intelligent code assistance, a robust debugger, and integrated testing tools, PyCharm ensures an efficient coding experience with minimized errors. It seamlessly integrates with version control systems like Git, incorporates database tools, and extends support to web development technologies, making it a versatile choice for developers at all levels. To kickstart your PyCharm journey, download and install it from jetbrains.com/pycharm/download, create a Python file, run your code, and delve into the extensive tutorials available on the JetBrains website to unlock the full potential of PyCharm for your Python projects from here PyCharm: the Python IDE for Professional Developers by JetBrains.

Run pip install DUELink command in the terminal window to install the DUE Link library and modify the code.

Run the program to verify the system is set properly. The LED will blink 10 times.


VS Code

This is a full-blown IDE with an ever-growing list of features. Please note that VS Code can be used with many other languages. A simple IDE, like Thonny, will only work with Python.

Instead of listing detailed instructions here that can change quickly, please visit code.visualstudio.com to download and install. Then create a file called blinky.py in a folder. Open the file or the folder in VS Code modify the code to print(“Hello BrainPad”) and run it. The web has hundreds of videos and tutorials in case you have any problems. Here is one of the options code.visualstudio.com/docs/python/python-tutorial

Run the program and observe Hello BrainPad showing in the terminal window.

Run pip install DUELink command in the terminal window to install the DUE Link library and modify the code.

from DUELink.DUELinkController import DUELinkController
print("Hello BrainPad!")
availablePort = DUELinkController.GetConnectionPort()
BrainPad = DUELinkController(availablePort)
# Flash the LED 20 times (on for 200ms and off for 800ms)
BrainPad.Led.Set(200,800,10)
print("Bye BrainPad!")

Run the program to verify the system is set properly. The LED will blink 10 times.

Start modifying the code and try debugging, which is stepping through code and inspecting variables. Familiarize yourself with VS Code before continuing.


Get Coding!

At this point, your PC, your BrainPad, and you are ready to start venturing into Python coding, especially physical coding things. And while you are at it, why not learn C# as well?

Content Licensing
Newsletter

Twitter Feed
Hot off the press!
January 30, 2024
December 14, 2023
December 11, 2023
December 8, 2023
November 24, 2023