Install Arduino IDE on Raspberry Pi
In terminal, run the following commands to install the Arduino IDE on you Raspberry Pi.
$ sudo apt-get update && sudo apt-get upgrade
$ sudo apt-get install arduino
Upload Pyfirmata to Arduino
In the toolbars, click File ->Examples -> Firmata -> StandardFirmata, in the newly opened window for the file, click Upload. Now you have already set your Arduino up, and it’s now time to configure Python environment for Firmata.
Install Pyfirmata
$ git clone https://github.com/tino/pyFirmata
$ cd pyFirmata
$ sudo python3 setup.py install
After the installation, you should be able to import pyfirmata:
$ python3
>>> import pyfirmata
You should get no error if the installation process is right.
A simple Application
This is an application when connecting Arduino Mega to my Raspberry Pi.
# file: test_pyfirmata.py
import pyfirmata
from time import sleep
# equivalent to:
# reference 8 & 9
# board = pyfirmata.Arduino('/dev/ttyUSB0')
# board.setup_layout(pyfirmata.BOARDS['arduino_mega'])
board = pyfirmata.ArduinoMega('/dev/ttyUSB0')
# A dictionary for defining the board layout(see reference 3)
# This might vary among different Arduino models
# e.g. the default setting only defines 6 analog ports,
# which works for uno, but not for mega.
# mega = {
# 'digital' : tuple(x for x in range(54)),
# 'analog' : tuple(x for x in range(16)),
# 'pwm' : tuple(x for x in range(2,14)),
# 'use_ports' : True,
# 'disabled' : (0, 1, 14, 15) # Rx, Tx, Crystal
#}
## for uno, corresponding to Line 151 in pyfirmata.py
# 'disabled': (0, 1, 6, 7) # Rx, Tx, Crystal
# The implemenation of this function can be found in reference 4.
# board.setup_layout(mega)
## INPUT
# number of sensors
nSensors = 8
# Some issues solved compared with other tutorials you
# may have seen on the Internet(see reference 5).
for i in range(nSensors):
board.analog[i].mode = pyfirmata.INPUT
# using iterators to refresh GPIO input,
# sleep 0.1s for Aduino preparation,
# one of the 2 above missing will lead to
# the result of always getting None.
it = pyfirmata.util.Iterator(board)
it.start()
sleep(0.1)
# Ouput the analog signal in range (0, 1) returned by the sensor
# forever. This can be your main loop in your applications.
while True:
for i in range(nSensors):
print(board.analog[i].read())
# an equivalent implementation
while True:
for i in range(nSensors):
# 1st parameter: a for 'analog', d for 'digital'
# 2nd parameter: GPIO number
# 3rd parameter: i for 'input', o for 'output', p for 'pwm'
# 's' for servo.
# e.g., when you want to control a servo: 'd:1:s'
print(board.get_pin('a:{}:i'.format(i)))
## output
# This is an easy one compared with input, you can reference the
# 7th link below.
Updates
2019.03.15 board info update
Update the easier way for getting your Arduino board information.
References
- Install Arduino:https://www.raspberrypi.org/magpi/program-arduino-uno-raspberry-pi/
- Install Pyfirmata: https://github.com/tino/pyFirmata#installation
- Define
Arduino Board Layout: https://github.com/tino/pyFirmata#board-layout - Board method setup_layout: https://github.com/tino/pyFirmata/blob/05881909c4d7c4e808e9ed457144670b2136706e/pyfirmata/pyfirmata.py#L125
- Solving some minor issues: https://github.com/tino/pyFirmata/issues/39
- Controlling Servos with Pyfirmata: https://letsmakearobot.blogspot.com/2014/09/servo-control.html
- More
pyfirmata usage: https://github.com/tino/pyFirmata#usage - Board Info: https://github.com/tino/pyFirmata/blob/master/pyfirmata/boards.py#L9
- Arduino Init Shortcuts: https://github.com/tino/pyFirmata/blob/master/pyfirmata/init.py#L25