Every hardware engineer and IoT enthusiast has interacted with a number of micro-controllers and embedded devices, the most common one being the Arduino micro-controller. Well, it's a good fit for noobs who want to get their feet wet but the only caveat is the C/C++ hybrid language used in the Arduino IDE as most devs find it quite intimidating.
Today, I'm going to take you down the rabbit hole with me by showing you how to use Python to program your Arduino micro-controller.
For starters, you'll need the following hardware:
- Arduino (UNO, Mega2560...etc)
- Arduino USB Cable
- Push Button
- 10kOhm Resistors
Well, the only software requirement is the Arduino IDE
The theory behind what we are trying to do is communicating with the Arduino using a serial connection between our computer and the micro-controller itself. There are a number of ways this can be achieved; in Python for example, the PySerial library can come in handy, effectively capturing serial data transmitted between the board and the computer. But that's an article for another day.
In this article we are going to exploit the Firmata protocol which is already available by default in the Arduino IDE post-installation. This protocol establishes a serial communication format that allows you to read digital and analog inputs, as well as send information to digital and analog outputs.
Let's dive right in and see what we can do. The Firmata sketch is (as I said earlier) already available in the Arduino IDE so on your desktop, launch the IDE and navigate to File then Examples then Firmata and click on the StandardFirmata sketch.
An alternative window will be opened upon which you should select your appropriate board type and port after connecting your board to your computer via a USB cable. Compile the sketch and upload.
Now let's play with some Python code. You will need to install the
pyFirmata package through the Python Package manager,
pip. Now, for *nix systems i.e (MacOS/Linux) launch your terminal and for Windows users, launch your command prompt then run the following command:
pip install pyfirmata
After installation, launch your favorite text editor, in my case, I'll write the Python code on an open-source version of VS Code. We'll now write some simple Python code equivalent to the Blink Sketch (The Hello World of Arduino😉).
Now let's crack every bit of the code (pun intended😉).
So we first import the library we need to establish a serial connection between our board and the computer. Our plan is to control the inbuilt LED connected to pin 13 on the board so it will be prudent to use the time module in order to control how long we will turn the LED on/off.
Since communication is through a serial protocol, we need to define our port. You can get this info from the Arduino IDE and for my case it is as stipulated above. For Windows users, it may be
COMx where x is any integer.
We then instantiate our board object (
myBoard) and pass the port as an argument to
The next step is to run the main block of code in a loop just like in the Arduino syntax. So let's use a
while loop for that...
board.digital is a Python list containing all the digital pins of the Arduino but in our case we are using pin 13. The
.write() method is used to write the state to the digital pin. The state is represented as either 1 or 0 for
LOW respectively. The former representing 5V and the latter 0V hence high or low.
HIGH state provides 5V to the LED and turns it on while
LOW state provides 0V hence turning it off. The
.sleep() method from the
time module is responsible for handling how long each action takes; just like the
delay() function in the Arduino syntax.
We can now run the code by launching a terminal and executing:
You should however note that the proper syntax is:
Now wasn't that simple. Let's go even deeper by adding a push button that will change the state of our digital pin...
The Button is connected to a 10k pull down resistor so as to ensure the voltage is completely pulled down to 0V on the digital pin.
We are conversant with line 1 through 5. Line 6 is used to define an iterator that will constantly read the state of our digital pins. Line 7 only instantiates or activates it.
Line 9 explicitly declares digital pin 10 as output through the
pyfirmata.INPUT method. This is necessary since the default config use for digital pins is as output and not input.
We then go to our loop and define our switch pin. The rest is as intuitive as it gets.
The final step is obviously running our Python script and Voila! Some action.
Micro-controllers and Embedded Devices are on the rise in numerous applications especially in the Internet of Things(IoT). Enthusiasts, hobbyists, Engineers, and other developers are getting the chance to explore numerous possibilities by building projects and contributing to others.
For the Arduino fans, this article is only a tip of the iceberg. The journey is a deep dark rabbit hole that once you get yourself into, there's no turning back. This also applies to Python programmers who just don't like C/C++.
For those interested in learning more, there is excellent documentation on Pyfirmata here that will be of great use.
For those who want to do IoT natively in Python, then there are other Micro-controllers such as MicroPython and the renowned RaspberryPi. But baby steps first😅; we all start from somewhere.
Anyway, that's all for now. I hope you enjoyed.