One good solution that comes bundled with the Arduino is
Hans-Christoph Steiner’s Firmata (bit.ly/1CXFnqF), an all-
purpose serial protocol that is simple and human-readable.It may
not be perfect for all applications, but it is a good place to start.
Here’s a quick example:
1. SelectFileExamplesFirmataStandardFirmata in the
Arduino IDE; this will open the sample Firmata code that will
allow you to send and receive messages to the Arduino and get
information about all of the pins.
2. Upload that code to the Arduino the same way you did in pre-
vious examples.
3. You’ll need a bit of Python code on the Pi to send commands
to the Arduino to query or change its state. The easiest way is to
use the pyFirmata module. Install it using Pip (see “EasyModule
Installs with Pip” on page 71):
pip install --user pyrmata
pip3 install --user pyrmata
4. Because Firmata is a serial protocol, you talk to the Arduino
from Python in the same way as in previous examples, but using
pyFirmata instead of pySerial. Use the write() method to make
a digital pin high or low on the Arduino:
from pyrmata import Arduino
from time import sleep
board = Arduino('/dev/ttyACM0')
while (1):
board.digital[13].write(1)
print("on")
sleep(1)
board.digital[13].write(0)
print("off")
sleep(1)
This code makes the Raspberry Pi
blink the LED on the Arduino
Uno
board!
The full module is documented on the pyFirmata GitHub
page (github.com/tino/pyFirmata).
Arduino and the Pi 87
GSW_RASPI_4ED_FIN.indd 87GSW_RASPI_4ED_FIN.indd 87 10/28/21 10:54 AM10/28/21 10:54 AM
Going Further
The nitty-gritty of serial protocols is beyond the scope of this
book, but there are a lot of interesting examples of how oth-
er people have solved problems in the “Interfacing with Soft-
ware” (bit.ly/1o17nGY) section of the Arduino Playground (www.
arduino.cc/playground). In addition, you may want to try:
MIDI
If your project is musical, consider using MIDI commands as
your serial protocol. MIDI is (basically) just serial, so it should
just work.
Arduino-compatible Raspberry Pi shields
There are tons of shields, or PHATs (Pi HATs—Hardware
Attached on Top) on the market that connect the GPIO pins on
the Raspberry Pi with an Arduino-compatible microcontroller.
WyoLum’s AlaMode (bit.ly/1EylgRM) shield is a good solution
and offers a few other accessories, including a real-time clock.
Talk over a network
Finally, you can ditch the serial connection altogether and talk to
the Arduino over a network. A lot of really interesting projects are
using the WebSocket (www.websocket.org) protocol along with
the Node.js (nodejs.org) JavaScript platform.
Using the serial pins on the Raspberry Pi header
The header on the Raspberry Pi pulls out a number of input and
output pins, including two that can be used to send and receive
serial data bypassing the USB port. To do that, you’ll first need
to cover the material in Chapter 7, and make sure that you have
a level shifter to protect the Raspberry Pi 3.3V pins from the
Arduino’s 5Vpins.
If you’re looking to get deeper into making physical devices com-
municate, a good starting point is
Making Things Talk, 3rd Edition
(www.makershed.com/products/making-things-talk-third-edi-
tion), by Tom Igoe.
88 Getting Started with Raspberry Pi
GSW_RASPI_4ED_FIN.indd 88GSW_RASPI_4ED_FIN.indd 88 10/28/21 10:54 AM10/28/21 10:54 AM
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset