Page 98 - Programming the Raspberry Pi Getting Started with Python
P. 98
sketch or the Python program on the Raspberry Pi to carry out more complex tasks.
This example assumes you are familiar with the Arduino. If you are not, you may want to read some
of my other books on the Arduino, including Programming Arduino: Getting Started with Sketches and
30 Arduino Projects for the Evil Genius.
Arduino and Pi Talk
To get the Arduino and Pi to talk, we are going to connect them using a USB port on the Raspberry Pi.
Because the Arduino only draws about 50mA and in this case has no external electronics attached to it,
it can be powered by the Pi.
The Arduino Software
All you need to do is load the following Arduino sketch onto the Arduino. You will probably want to
do this with your regular computer, because at the time of writing, only a very old version of the
Arduino software is available for the Raspberry Pi. The following sketch is available in the downloads
package and is called PiTest.ino:
This very simple sketch contains just three functions. T h e setup function initializes serial
communications and sets pin 13 on the LED to be an output. This pin is attached to the LED built into
the Arduino. The loop function is invoked repeatedly until the Arduino is powered down. It first sends
the message “Hello Pi” to the Raspberry Pi and then checks to see whether there is any incoming
communication from the Pi. If there is (it expects a single digit), it flashes the LED on and off that