To control your arduino card with python, there a library called Pyfirmata. This library make the link between Python language and your arduino card and permits you to control every pins of your arduino card.
There are others libraries to control your Arduino card from python but Pyfirmata is the easiest to use.
To install the Pyfirmata library, you have to write !pip install pyfirmata in the python consol.
You have on the right picture an example of a succeed installation of the Pyfirmata library:
For this tutorial, we will use Anaconda as a python interpreter.
The next step to use Pyfirmata library is to go to Arduino ide and upload on your Arduino card the firmata program. You can find this program in the example bar. It’s allow to create a connection between your card and python.
We have installed StandardFirmata as you can see on the picture:
This little program is useful to verify that everything is working. Furthermore you only need an Arduino card to make it works.
It consist to blink 10 times the micro-led linked to the pin 13 of your arduino card.
Remark: when you will do the test, you will see that the Rx blink also because it’s shows an interaction between the Python ide and the arduino card.
Remark: the program can run at the infinite by replacing the loop for by “While True”.
In order to see your program working, you have to specify which port COM your arduino card is linked to.
We will see how to find it.
To blink the led you have to take the first program we have done and change the pin with the one where you have plug your led. In our example we have plug the led on the pin 8 of the Arduino card:
Here is the sketch of the circuit.
We use a resistor of 220 ohms in it.
The temperature sensor detect a temperature and transmitted it in a voltage tension. This sensor is composed of 3 pins: Vcc, GND and signal one.
In this program we will display the temperature on the python console:
This is the sketch of the circuit:
The servomotor is a motor able to maintain a position to an effort and it’s position is known and corrected if needed.
In the program below, you can enter an angle and the servomotor will change the position according to the angle you have entered.
This is the sketch of the circuit:
This is what we can see by running the program:
In this parts, we will show how to display the value of any components. To do that you have to change some lines to specify if it’s an input or an output.
import pyfirmata
import time
port = ‘COM3’
board = pyfirmata.Arduino(port) # To open the port COM linked to the Arduino card
your_component_pin = board.get_pin(‘a:0:i’) # To initialize the pin used
iterator = pyfirmata.util.Iterator(board) # to initialize the link between the card and python
iterator.start() # To start the connection
your_component_pin.enable_reporting() # to read the values
while your_component_pin.read() == None: None # As long as there is no value
try:
while True:
print (“The value is : “,your_component_pin.read()) # Read and display the values
time.sleep(1) # Delay between two measures
except:
your_component_pin.disable_reporting()
board.exit()
Here is the modification you have to do make a program for your component:
| Input (sensor, button …) | Output (motor, led …) |
d => digital | (d : N° pin : i) | (d : N° pin : o) |
a => analog | (a : N° pin : i) | (a : N° pin : o) |
1) (d : N° pin: p) -> p means pulse width modulation (pwm), which permits you to vary the brightness of a led for example.
2) ( d : N° pin: s) -> s means servomotor.You have to use it only if your component is a servomotor.