DEV Community

Cover image for Using Python for Arduino Projects
William Otieno
William Otieno

Posted on

Using Python for Arduino Projects

INTRODUCTION

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.

REQUIREMENTS

For starters, you'll need the following hardware:

  • Arduino (UNO, Mega2560...etc)
  • Arduino USB Cable
  • LEDs
  • Push Button
  • 10kOhm Resistors

Well, the only software requirement is the Arduino IDE

EXPLANATION

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.

Uploading the Firmata Sketch

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.
Alt Text
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.

Python Fun

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

You should get some result similar to this...
Alt Text

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😉).
Alt Text

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 pyfirmata.Arduino()

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 HIGH or LOW respectively. The former representing 5V and the latter 0V hence high or low.
The 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.

Running the Code

We can now run the code by launching a terminal and executing:
python pyduino.py
You should however note that the proper syntax is:
python [filename.py]

Now wasn't that simple. Let's go even deeper by adding a push button that will change the state of our digital pin...

More Hands-on Experience

We shall first connect our circuit as follows:-
Alt Text

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.

Our code will only change a bit and it will be just as simple as the other one:
Alt Text

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.

CONCLUSION

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.
Thank you.

Top comments (2)

Collapse
 
edkb profile image
Eduardo Binotto

Hello William, nice project! I know another lib that do a similar thing, but also has support for other features natively, like servo control, i'ts called nanpy (github.com/nanpy/nanpy). The main downside is that it looks abandoned :( but a think is worth to give a try.

Collapse
 
williamotieno profile image
William Otieno

Yeah, I know about it. I've used it with the Raspberry Pi to make arduino a slave to it. It's also really good.