Python programming for Arduino
In this tutorial you will learn how to use Python to program an Arduino board. We will use Tkinter to create message box to interact with the components on the breadboard.
What do you need :
-Arduino Uno & LED
-Example code that we will study below -> Python & Arduino
- Connect the external LED
Before starting we have to connect the LED to the Arduino board on pin 10, don’t forget to add a resistor around 220Ω.
- Setup the Arduino Board
The first-time setup requires the uploading of a Firmata program to the board. Open the Arduino IDE software and go to File -> Examples -> Firmata -> StandardFirmata. Don’t change the code and just upload it directly in the board by clicking “Upload” or use the shortcut “CTRL+U”. Now your Arduino board is ready.
- Initialize the board with Python
When writing a new Python program, we have to import the correct libraries that we will use. You will need to make sure that all the necessary libraries have been installed.
Tkinter will allow us to generate an small interface to interact directly with the board. Pyfirmata will link between Python, PySerial and Arduino.
To begin, we will need to associate the ports and board with pyFirmata. Our board is on “COM8” but the port’s location might be different according to your computer configuration. To know which port your Arduino board is using, for Windows, go to “Control Panel -> Device Manager -> Ports (COM & LPT)” then find your Arduino Uno. For Mac OS X, it should approximately look like this : ‘/dev/cu.usbmodemfa1331’. In the case of the Linux operating system, use the following line of code : ‘/dev/ttyACM0’.
Then pyfirmata.Arduino(port) will know, thanks to the port, which board your are using. You have to make a break in the program with a sleep() to let the time to the board to be initialized.
After we will initialize the pins that you need. In our example, we will only need pin 10. There are different ways to setup a pin, you will use board.get_pin(). The string in the parenthesis is composed with 3 elements, pin type (“a” for analog & “d” for digital), pin number, pin mode (“i” for input, “o” for output, “p” for PWM). They are separated by a colon (:) operator. For example, if we want the digital pin 10 like PWM, we will write : “d:10:p”.
- Initalize main windows with Tkinter
Now we will initialize the main message box, we will use the module named Tkinter. As below, you can define the title and the size of the window.
- Create a text field entry
With Tkinter you can create different GUI functionality for example buttons, sliders, entry value boxes etc. In this tutorial we will create a text field entry.
In the first line of the code snippet above we setup the size of the text field and the variable to store the value timePeriodEntry. Then we will choose the place of this text field in the window using timePeriodEntry.grid(column=1, row=1). The window will be split into columns (vertical) & rows (horizontal) as below:
- Create a button
Using the same method as above, we can create a button to execute an action. We will create two different buttons; the first one to switch on the LED using the function that we will define below, and the second one to quit the program and close the window.
The string ‘Start’ will appear in the button under the variable we created startButton. Everytime this button is pressed it will execute the function onStartButtonPress. For the start button we will call the function and for the exit button we will use an already registered function top.quit, “top” is the name of our windows that we define before when we create the windows. The final thing is to choose where to place the buttons in our Tkinter window by defining the column and row for each button.
- Create a Slider
We can create a slider, we just have to setup the start point and the end point and choose the orientation. Type in the code below:
brightnessScale = Tkinter.Scale(top, from_=0, to=100, orient=HORIZONTAL) brightnessScale.grid(column=2, row=1)
- Create a function to switch on/off LED
We will create a small function which, when the start button is clicked, the LED will switch on during the timePeriod defined in the field text entry and with the brightness from the scale. We have to use get() to collect the information from the text field and the value from the slider. These values will be stored in our variables ledBrightness and timePeriod. The second block of code below switches on the LED, waits for the established time period, and then switches off. This will keep on looping until the arduino is switched off or the code or variables changed. When the button is clicked and the LED is still on, we change the state of the button as “disabled”, to avoid to click again and again on it.
- Set the opening of the windows
At the end of the Python program, don’t forget to open the windows.