You want to read Arduino digital inputs from Python on a Raspberry Pi.
Use PyFirmata to read a digital input on the Arduino.
To make this recipe, you need:
• Arduino Uno (see “Modules” on page 381)
• Breadboard and jumper wires (see “Prototyping Equipment” on page 380)
• 1kΩ resistor (see “Resistors and Capacitors” on page 380)
• Tactile push switch (see “Miscellaneous” on page 382)
Connect the breadboard, holding the components to the Arduino as shown in Figure 14-9.
If you haven’t already done so, follow Recipe 14.3 to set up PyFirmata.
The following Python script prints out a message every time the switch is pressed. It’s very similar to the program in Recipe 11.1 (switch.py). Open an editor (nano or IDLE) and paste in the following code. As with all the program examples in this book, you can also download the program from the Code section of http://www.raspberrypicook book.com, where it is called ardu_switch.py. Just follow the link to this book and then click Code.
import pyfirmata import time board = pyfirmata.Arduino(‘/dev/ttyACM0’) switch_pin = board.get_pin(‘d:4:i’) it = pyfirmata.util.Iterator(board) it.start() switch_pin.enable_reporting() while True: input_state = switch_pin.read() if input_state == False: print(‘Button Pressed’) time.sleep(0.2) |
When you run it, nothing will happen for a second or two, while the Firmata sketch starts and establishes communication with the Raspberry Pi. But, once it starts up, each time you press the button, a message will appear.
$ sudo python ardu_switch.py Button Pressed Button Pressed Button Pressed |
Discussion
PyFirmata uses the concept of an Iterator to monitor the Arduino input pin. The reasons for this are bound up in the implementation of Firmata. This means that you can’t simply read the value of an Arduino input pin on demand; instead, you have to create a separate Iterator thread that manages the reading of the switch using the commands:
it = pyfirmata.util.Iterator(board) it.start() |
You then also have to enable reporting for the pin you are interested in using the command:
switch_pin.enable_reporting() |
A side effect of this mechanism is that when you press Ctrl-C to exit the program, it won’t exit properly. There is no nice way to kill the Iterator thread other than to open another Terminal window or SSH session and kill the process (Recipe 3.24).
If the only Python process running is this program, you can kill it with the command:
$ sudo killall python |
Simply disconnecting the Arduino from the Raspberry Pi, breaking the communication link, will also cause the Python program to exit.
See Also
This is very similar to connecting a switch directly to a Raspberry Pi (Recipe 11.1), and if you just have one switch, there is no real benefit in using an Arduino like this.
Table A-8. Modules | |
Raspberry Pi camera module | Adafruit: 1367, MCM: 28-17733, CPC: SC13023 |
Arduino Uno | SparkFun: DEV-11021, Adafruit: 50, CPC: A000066 |
Level converter, four-way | SparkFun: BOB-11978, Adafruit: 757 |
Level converter eight-way | Adafruit: 395 |
LiPo boost converter/charger | SparkFun: PRT-11231 |
PowerSwitch tail | Adafruit: 268 |
16-channel servo controller | Adafruit: 815 |
Motor driver 1A dual | SparkFun: ROB-09457 |
RaspiRobot board | Sparkfun: KIT-11561, raspirobot.com |
PiFace digital interface board | MCM: 83-14472, CPC: SC12827 |
Humble Pi | MCM: 83-14637, CPC: SC12871 |
Pi Plate | Adafruit: 801 |
Gertboard | MCM: 83-14460, CPC: SC12828 |
Breakout board with paddle terminals | MCM: 83-14876, CPC: SC12885 |
PIR motion detector | Adafruit: 189 |
Venus GPS module | SparkFun: GPS-11058 |
Methane sensor | SparkFun: SEN-09404 |
Gas sensor breakout board | SparkFun: BOB-08891 |
ADXL335 triple-axis accelerometer | Adafruit: 163 |
4×7-segment LED with I2C backpack | Adafruit: 878 |
Bicolor LED square-pixel matrix with I2C backpack | Adafruit: 902 |
PiLite interface board | Ciseco, CPC: SC13018 |
aLaMode interface board | Makershed: MKWY1, Seeedstudio: ARD10251P |
Freetronics Arduino LCD shield | www.freetronics.com |
RTC module | Adafruit: 264 |
16 x 2 HD44780 compatible LCD module | SparkFun: LCD-00255, Adafruit: 181 |
Table A-3. Prototyping equipment | |
Description | Suppliers |
M-M jumper wires | SparkFun: PRT-08431, Adafruit: 759 |
M-F jumper wires | SparkFun: PRT-09140, Adafruit: 825 |
F-F jumper wires | SparkFun: PRT-08430, Adafruit: 794 |
Half-sized breadboard | SparkFun: PRT-09567 Adafruit: 64 |
Pi Cobbler | Adafruit: 1105 |
Table A-4. Resistors and capacitors | |
270Ω 0.25W resistor | Mouser: 293-270-RC |
470Ω 0.25W resistor | Mouser: 293-470-RC |
1kΩ 0.25W resistor | Mouser: 293-1k-RC |
3.3kΩ 0.25W resistor | Mouser: 293-3.3k-RC |
4.7kΩ 0.25W resistor | Mouser: 293-4.7k-RC |
10 kΩ trimpot | Adafruit: 356, SparkFun: COM-09806, Mouser: 652-3362F-1-103LF |
Photoresistor | Adafruit: 161, SparkFun: SEN-09088 |
220nF capacitor | MCM: 31-0610, Mouser: 80-C322C224M5U5HA |
Table A-9. Miscellaneous | |
1200mAh LiPo battery | Adafruit: 258 |
5V relay | SparkFun: COM-00100 |
5V panel meter | SparkFun: TOL-10285 |
Servo motor | SparkFun: ROB-09065, Adafruit: 1449 |
5V 1A power supply | Adafruit: 276 |
Low power 6V DC motor | Adafruit: 711 |
0.1 inch header pins | SparkFun: PRT-00116, Adafruit: 392 |
5 V 5-pin unipolar stepper motor | Adafruit: 858 |
12 V, 4-pin bipolar stepper motor | Adafruit: 324 |
Magician chassis with gearmotors | SparkFun: ROB-10825 |
Tactile push switch | SparkFun: COM-00097, Adafruit: 504 |
Miniature slide switch | SparkFun: COM-09609, Adafruit: 805 |
Rotary encoder | Adafruit: 377 |
4×3 keypad | SparkFun: COM-08653 |
Piezo buzzer | SparkFun: COM-07950, Adafruit: 160 |