Raspberry Pi to Arduino connection

Wiring Raspberry Pi to Arduino (5v)

Connecting a Raspberry Pi to an Arduino requires a voltage divider or level control board to change 5v to 3.3v for the Pi.
Note that some Arduinos are 3.3v and do not require any voltage controls, but there are not many that do this.

The Raspberry Pi will power the Arduino so only the Pi needs connecting to power. The Arduino should not be powered separately!

As shown in the diagram, the transmit line from the Arduino should have a couple of resistors attached to drop the voltage to 3.3 or slightly lower. 

Talking to the Arduino

Picocom is a quick tool to use for serial text communication.
Install with:
sudo apt-get install picocom


Connect to the Arduino using the connected /dev port. In the case of a Pi Zero it's /dev/serial0
picocom /dev/serial0

This can be used as an Arduino terminal. Anything typed will go across the serial connection and be received by the Ardunio code.

Building and Uploading Code

You have to be very careful attaching the Arduino USB to another computer to upload new code. Connecting the USB will also provide power to the Raspberry Pi. This is fine as long as the Pi is also not powered or attached to other hardware which could draw more power than the Arduino can handle. 

The Raspberry Pi can provide a way to build and upload the code to the Arduino as it's already attached to the serial lines. The only issue is the auto reset to trigger the boot-loader to upload, but there's a trick to doing this manually.

The Arduino IDE can be downloaded from https://www.arduino.cc/. It can be run on the Raspberry Pi desktop by directly hooking up to HDMI monitor or via VNC connection. For a headless Raspberry Pi then the Arduino IDE can also be launched from the command line. 

Triggering the upload

During the build and upload process from the IDE (or command line - see below) the code will be sent to the Arduino. As long as the Arduino board has a reset switch then it's just a matter of pressing this reset button when the words Uploading... appear.
Example from command line:

If you don't reset then 10 chances will be given by avrdude and it will give up. It provides ample time to respond to the upload so fast reflexes are not required.

Headless setup

Download the IDE and extract to your home directory, or a location you prefer such as /usr/local/.
I'll assume it's in the home directory for the sake of this example.

Extract the tar ball. In this example it creates /home/pi/arduino-1.8.8
Add a path to the executable by typing 
export PATH="$HOME/arduino-1.8.8/:$PATH"

You can permanently add this to your bash profile by editing ~/.profile and adding
if [ -d "$HOME/arduino" ] ; then
    PATH="$HOME/arduino:$PATH"
fi

Building from the command line is a bit more involved than from the IDE.
New sketches must live in their own separate directories.
An example command line would look like the following to build a Blink.ino
arduino --upload --board arduino:avr:pro:cpu=16MHzatmega328 --port /dev/serial0 Blink.ino

--upload specifies that the code should build and upload over serial. Another option is --verify which just builds the code without upload
--board specifies the hardware and is required on the command line. This example builds for a Pro Mini 328 5v. Deciphering the required string is a bit tricky as I couldn't find references for the boards. See further note below to find out other values
--port provides the serial port path
Blink.ino is the program name. Replace with the file to build

-v option (not shown above) provides debug and verbose build information. This is useful to identify any environment upload or build issues.

Finding board names

On Windows the IDE saves the last config in:
C:\Users\<YOU>\AppData\Local\Arduino15\preferences.txt
The values required for a command line should be in the format
arduino:avr:<board>:cpu=<custom_cpu>

Where the <board> and <custom_cpu> are taken from entries in the preferences.txt file. 


No comments:

Post a Comment