Anth's Computer Cave Tutorials

Connect an Arduino to a Raspberry Pi via a serial connection

Many of the articles in the cave involve using a Raspberry Pi and an Arduino together.

Rather than repeat the basics in each article I thought I'd create a short guide here.

Connections

We will be using a serial connection between the devices.

There are several ways to create a serial connection between the Pi and Arduino, but this guide uses the safest and easiest method, the Aruino's own USB connection.

Configure serial connection

You need to configure your serial connection on both the Arduino and the Pi.

Arduino sketch

The code relating to the serial connection (pictured below) goes inside the void setup() part of your Arduino sketch.

  Serial.begin(9600);
  while (!Serial) {
  }
  Serial.println("Serial Connected");
	

The complete sketch would look like this:

void setup() {
  Serial.begin(9600);
  while (!Serial) {
  }
  Serial.println("Serial Connected");
}

void loop() {
}	
	

Note that this sketch doesn't actually do anything other than sending a single serial message to the Raspberry Pi to test your connection. If you look at some of the other projects in the Cave you will find various things you can do over the serial connection. This is just intended as a simple guide to get you started.

Connect your Arduino to your normal computer first. Open the Arduino IDE and paste the code in. Then upload the sketch to the Arduino.

At this point you can test the Arduino side of the connection. In your Arduino IDE select tools, then Serial Monitor.

In a few seconds you should see "Serial Connected" printed to the display.

If so you have your Arduino configured correctly.

Raspberry Pi Python program

The first thing you need to do in your Python program on the Raspberry Pi is import the Serial library.

import serial

You then need to initiate a connection.

arduino = serial.Serial('/dev/ttyUSB0', 9600)

You may need to change the "ttyUSB0" part of the above code.

I am using a cheap, knock-off Uno board from Ebay. My Raspberry Pi connects to this over the "ttyUSB0" com port.

Your board may connect on a different tty port. I found the port on my setup using the dmesg command. There may be better ways but this works for me. Using dmesg on its own, however, will give you far too much information.

The port name you are looking for almost certainly begins with "tty". To narrow the results down you can pipe the output through grep using tty as an argument.

Open a terminal and type:

dmesg | grep tty

That returns the following output on my Pi.

The results from using dmesg to determine Arduino port. Picture: Anthony Hartup

You'll notice that grep has found two tty devices: ttyAMA0 and ttyUSB0. The first port, ttyAMA0, is the default port for the Pi's own serial communication. You will see this on your Pi as well. The second port is the one you want, in my case ttyUSB0.

If your system is also using ttyUSB0 you are good to go. Otherwise you will need to change the serial-connect line in your Python code. Let's say you found your system was using ttyACM0. Instead of using this:

arduino = serial.Serial('/dev/ttyUSB0', 9600)

You would use this:

arduino = serial.Serial('/dev/ttyACM0', 9600)

Below is some Python code to test your connection.

Create a file on your raspberry Pi and name it serial_test.py. Copy and paste the code into the file.

import serial
import time

def connect():
    #Initiate a connection
    arduino = serial.Serial('/dev/ttyUSB0', 9600)
    #Read confirmation from Arduino
    while True:
        try:
            time.sleep(0.01)
            #read the message from the Arduino
            raw_message = str(arduino.readline())
            #remove EOL characters from string
            message = raw_message.rstrip()
            return message
        except:
            pass
            KeyboardInterrupt
	

Open a terminal and navigate to the folder containing your serial_test.py file and type:

sudo python

When Python loads, type:

import serial_test

After several seconds you should see "Serial Connected" printed on the screen. If so, you are good to go.

If the program does not print "Serial Connected", you may need to try again. Repeat the steps mentioned earlier with dmesg | grep tty to check your tty port.

Cheers

Anth

Previous: Use a distance sensor with Arduino

Related:Connect a relay to Arduino

_____________________________________________


Comments

Leave a comment.

There are currently no comments for this article.

Leave a comment on this article