ArduinoPi

,

This is a story of true romance; a match made in silicon heaven; a story of an unlikely match up between two very different single board computers (SBCs): a Raspberry Pi 2 model B, and an Arduino Pro Mini. It’s a story that could be told about any Raspberry Pi model paired with any Arduino model.

Arduino SBCs are really great at interfacing with the real, analog, world. They can consume analog data sources, sampling them with 10 bits (or more) of resolution, at frequencies of perhaps 60 samples per second (or even greater). They can also produce (low amperage) simulated analog output signals using the Pulse Width Modulation (PWM) technique. This page has some info about analog versus digital signals, PWM, and related topics. Unfortunately though, the computing capability of Arduinos is minimal. They typically have very small RAM memory (a few KB), and the maximum size of the programs stored in their flash memories is very small (a few tens of KB). Many of the Arduinos are not even capable of communicating over common interfaces like USB.

Raspberry Pi SBCs are very powerful Linux hosts for their size, the latest ones have 1.4GHz quad-core 64bit CPUs, powerful GPUs, and 1GB of RAM. They also have one or more USB ports, HDMI and stereo audio output ports, video camera inputs, and dozens of (digital) GPIO pins. Unfortunately they are unable to consume a simple analog input signal, and their ability to produce analog output is very constrained (usually with just one hardware PWM GPIO pin).

When a Romeo Arduino meets a Juliet Pi, their matchup enables the world of analog I/O to join the world of modern software. Of course, Arduinos can hook up other Arduinos too. And a Raspberry Pi can be mated to any other Raspberry Pi too. Even combinations of more than two are possible. But I digress. This page is about one Arduino mating with one Raspberry Pi. Let’s take a look at how we can make this beautiful matchup happen.

Parts List:
Raspberry Pi 3 (or Pi 2), model B (or A+ or B+)
2.5A or greater USB (5V) power supply and cord
microSD flash card (I like to use 32GB size, but probably 8GB or larger could work)
Arduino Pro Mini (5V)
USB-Serial Adapter (5V)
10K ohm potentiometer (as an example analog input source)
LED with 200 ohm resistor in series (as an example analog output)
Prototyping breadboard (optional)
A few short wires suitable for breadboard use

PLEASE NOTE: You need to purchase an appropriate USB-Serial adapter for the particular Arduino you buy. Above I listed 5V models, but you could instead purchase the 3.3V models. The Arduino logic level voltage and the USB-serial adapter logic level voltage (which also supplies power to the Arduino) must match. E.g., a 3.3V USB-Serial adapter will not work correctly with a 5V Arduino Pro Mini. Some USB-Serial adapters (like the one in the Amazon link above) are able to work with either 3.3V or 5V devices. If you purchase one of those, make sure the toggle switch is set to the appropriate one of those voltages before you connect power to these devices or you may damage the components.

You may also need to solder the pins onto the Arduino (the ones I buy always come with the pins unattached, so they require soldering). If so, you will at least need some small diameter (e.g., 0.3mm) flux or rosin core (not acid core) solder, and a soldering pen of at least 25W (I personally prefer to use a 60W solder pen, because too hot is easier to deal with than too cold). There are many online tutorials (like this one) that can teach you how to solder if you have never done it before. Be careful. There be monsters here.

Wiring:

Let’s start with the Arduino/analog side of the circuitry.

Connect the example analog input and output devices to the Arduino as shown in the Fritzing output below:

Next, connect the USB-Serial adapter to the Arduino, as follows:
connect GND on the adapter to the pin labelled BLK on the Arduino
connect DTR on the adapter to the pin labelled GRN on the Arduino
connect RXD on the adapter to TX0 on the Arduino
connect TXD on the adapter to RX1 on the Arduino
connect 5V (or 3.3V) on the adapter to VCC on the Arduino

Once that is done, you can connect the USB connector to your Raspberry Pi, and the hardware (wiring) will be all done. The photo at the top of this article shows the completed hardware. Now let’s move on to look at the two software of this project (i.e., both the Arduino and the Raspberry Pi require software for this).

Arduino Software:

The code discussed in this section is available in the Examples/AnalogIO directory of the DarlingEvil git repo.

To load software onto your Arduino you need to run the Arduino Integrated Development Environment (IDE) software on some other host. It is possible to run this software directly on the Raspberry Pi, but I don’t recommend that. It is better to use it in Windows or MacOS. My long time preference is to use MacOS, but in the latest versions of MacOS I always seem to have trouble with the Arduino IDE not being able to communicate with my Arduinos. So, with regrets I recommend using Windows if you have access to a relatively recent Windows machine. I personally always have a Windows machine hanging around somewhere. Most of the time I try not to get any Windows on me, but when it comes to Virtual Reality (VR), or first person (3D) games, or programming Arduinos, it’s still the best choice. If you can’t use Windows then you might want to try the new Arduino “web editor” a web-based version of the IDE (details at the link above).

Once you have the Arduino IDE installed, connect the USB-Serial adapter (with the Arduino and additional hardware attached) to one of the USB ports of the machine where you are running the IDE, then launch the IDE. When the IDE has come up, open the HdwrTest.ino file int the Examples/AnalogIO directory of the git repo. Now from the Tools menu, configure the Port, and Board (select “Arduino Pro or Pro Mini“). Then tap the “Upload” button (the one with the rightward-pointing arrow). The program should then compile, and begin uploading to the Arduino. When it is finished uploading, it will automatically start running. You should interact with the potentiometer and observe the output LED increasing and decreasing in brightness as you do so. With that, your hardware testing is complete. Note that this program has been uploaded into the flash memory of the Arduino, so it is now persistent there. You could disconnect the Arduino from the IDE host, and power it from any 5V source, and it will run that saved program.

Now let’s install the software that will enable the Arduino to act as a slave (i.e., as a peripheral) of the Raspberry Pi. There are many ways that a Raspberry Pi and an Arduino can communicate, but I find this approach to be simple and elegant.

Make sure your Arduino is still connected to the IDE machine. Now open the “Standard Firmata” program from the “File/Examples/Firmata/Standard Firmata” menu item in the IDE. You should see a file come open in the IDE. This is the program that will enable your Arduino to act as a slave of your Raspberry Pi. Next, make sure that the appropriate Port and Board are configured (as before). And finally, tap the upload button (right arrow) to compile, upload and run the Standard Firmata program.

Once that has completed successfully, your Arduino is ready to go! Now let’s look at what’s needed on the Raspberry Pi side of things to control the Arduino.

Raspberry Pi Software:

The code discussed in this section is available in the Examples/AnalogIO directory of the DarlingEvil git repo.

Note that the Firmata software supports many different language bindings on the client side to use when controlling an Arduino (including Java, Go, Ruby, Python). I chose the Python client to illustrate as an example here but the others are very similar to use.

Begin by connecting the USB-Serial adapter (with the Arduino and additional hardware attached) to one of the USB ports of the Raspberry Pi.

If you haven’t already done so, you need to flash a Linux operating system onto your micro SD card and power up the Raspberry Pi. Here are some instructions for doing that. If you are doing a headless (no monitor, keyboard, or mouse) setup, here are some instructions for that.

Once you have managed to get things set up, login (e.g., over ssh) to a Linux shelland change the default password:
passwd

In the linux shell, make sure basic Python development tools are installed:
sudo apt-get install -y build-essential python-dev python-pip git

Install the Python library that enables serial communication over USB
sudo apt-get install -y python-serial

Install “pyFirmata” (the Python client library for Firmata)
git clone https://github.com/tino/pyFirmata
cd pyFirmata/
sudo python setup.py install

You should now be able to control your Arduno from your Raspberry Pi using Python.

In your favorite test editor, open the AnalogIO.py example program in the Examples/AnalogIO directory of the git repo. Take a look at this small example program to get a feel for how it controls the Arduino. This program behaves exactly the same as the “HdwrTest.ino” program you ran earlier on the Arduino, but it is written in Python. More significantly, it runs on the Raspberry Pi (not on the Arduino). The Arduino is only running the Standard Firmata program and communicating back and forth with the Python client library that this example Python program uses.

Run the Python program, and then interact with the hardware as you did before to observe the LED brightness changing as you turn the potentiometer:
sudo python AnalogIO.py

Note that you may need to used sudo (as shown above) to run the program if GPIO access requires root level access.

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.