Question 14.5: You want to use PyFirmata, but over the serial connection (R......

You want to use PyFirmata, but over the serial connection (RXD and TXD on the GPIO connector) rather than by USB.

Step-by-Step
The 'Blue Check Mark' means that this solution was answered by an expert.
Learn more on how do we answer questions.

Use a level converter to connect the RXD pin of the Raspberry Pi to the Tx pin of the Arduino, and the TXD pin of the Raspberry Pi to the Rx pin of 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)
• 270Ω and 470Ω resistors (see “Resistors and Capacitors” on page 380) or a four-way bidirectional level converter (see “Modules” on page 381)

If you are using the level converter module, then connect the breadboard as shown in Figure 14-7.

If, on the other hand, you are using the pair of resistors, then connect the breadboard as shown in Figure 14-8.

The Arduino Rx input is fine with just 3.3V from the Raspberry Pi TXD pin; however, the 5V coming from the Arduino Tx pin must be dropped to the 3V expected by the Raspberry Pi.

You will need to set up PyFirmata—see Recipe 14.3. The Arduino side of the project remains exactly the same as Recipe 14.4, where USB is used instead of the serial connection. There is one change that you need to make to the Python program running on the Raspberry Pi—change the device name from /dev/ttyACM0 to /dev/ttyAMA0, as the serial port has a different device name from the USB interface.

The following Python script makes the LED blink at a rate of about 1 Hz. 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.rasp berrypicookbook.com, where it is called ardu_flash_ser.py.

import pyfirmata
import time
board = pyfirmata.Arduino(‘/dev/ttyAMA0’)
led_pin = board.get_pin(‘d:13:o’)

while True:
led_pin.write(1)
time.sleep(0.5)
led_pin.write(0)
time.sleep(0.5)

Discussion

The level conversion is necessary because the Raspberry Pi serial port connections, RXD and TXD, operate at 3.3V, whereas the Arduino Uno operates at 5V. While it is OK for the 5V Arduino to use a 3V signal, the reverse is not true and a 5V signal connected to the 3V RXD pin is likely to damage the Raspberry Pi.

See Also

You can also easily adapt the other examples that use PyFirmata (Recipes 14.6 through 14.9) to use a serial rather than USB connection, simply by changing the device name in the Python program.

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
14.7
14.8

Related Answered Questions

Question: 14.11

Verified Answer:

The I2C bus has the concept of master and slave de...
Question: 14.10

Verified Answer:

Use a level converter or pair of resistors to conn...
Question: 14.9

Verified Answer:

Use PyFirmata to send commands to an Arduino to ge...
Question: 14.8

Verified Answer:

Use PyFirmata to send commands to an Arduino to ge...
Question: 14.7

Verified Answer:

Use PyFirmata to read an analog input on the Ardui...
Question: 14.6

Verified Answer:

Use PyFirmata to read a digital input on the Ardui...
Question: 14.14

Verified Answer:

Use an aLaMode interface board. You may find that ...
Question: 14.15

Verified Answer:

Follow this detailed tutorial to set up the Arduin...