loading

Both the Raspberry Pi and Arduino Uno are very powerful devices, good at different things. The Arduino boards are awesome at reading inputs and outputs from various different things. The Raspberry Pi is basically a mini, open-source Linux computer. If you put these two together, your options are limitless.

That's what this tutorial is about. Putting these two together, over USB and Serial Communication! Let's get started.

Step 1: Materials List

In order to get a good understanding of this project, you will need the following materials:

  • Raspberry Pi B+
  • Arduino Uno
  • Breadboard
  • Jumper Cables (4)
  • LEDs (3)
  • 220 OHM resistors (3)

You can use the same colored LEDs if you wish, but different colors are easier to visualize.

Step 2: The Circuit

The circuit itself is very simple. Follow the diagram and you'll be good to go.

Step 3: Arduino Code

In order to install run Arduino on your Raspberry, you will need to type the following into the LXTerminal. You could also try to download the Linux ARM file, but I couldn't get it to open on the Raspberry pi.

sudo apt-get update && sudo apt-get install arduino

note: you must have an internet connection

(source: http://playground.arduino.cc/Linux/Raspbian )



Now that you have Arduino on the Raspberry Pi, copy and paste this code into your sketch:

char receivedChar;
boolean newData = false;

void setup() {

Serial.begin(9600);

pinMode(3, OUTPUT); pinMode(5, OUTPUT); pinMode(6, OUTPUT); }

void loop() {

recvInfo(); lightLED(); }

void recvInfo() {

if (Serial.available() > 0) {

receivedChar = Serial.read(); newData = true; } }

void lightLED() {

int led = (receivedChar - '0');

while(newData == true) {

digitalWrite(led, HIGH); delay(2000); digitalWrite(led, LOW);

newData = false; } }

Plug your Arduino into the Raspberry Pi via USB and upload the code!

Step 4: The Raspberry Code

Now, it's time to make everything fall into place. Enter the following code to set up serial communication between the Arduino and Raspberry. Go to the Desktop and open the IDLE application, and type the code in there.

 >>> import serial
 >>> ser = serial.Serial('/dev/ttyACM0', 9600)

Once that code is entered, it is time for us to begin communication!

Step 5: Communication!

As you know, the circuit includes three LEDs. The red LED is connected to pin 3, the green connected to 5, and the yellow connected to 6. You can use different colors if you like, but those are what I chose. Type these commands into the IDLE program.

Turn red LED on:

>>> ser.write('3')

Turn green LED on:

>>> ser.write('5')

Turn yellow LED on:

>>> ser.write('6')

When a command is entered, the LED told to turn on will light for 2 seconds, and turn back off! Now that you know how to do this, expand this idea into other things like servos, LCD screens, or whatever you can think of that communicates with Arduino!

Step 6: Troubleshooting and Useful Links!

Raspberry Pi doesn't recognize the Serial port '/dev/ttyACM0' ?

The serial port could have a different name! To check....

1. Go to LXTerminal.

2. Unplug your Arduino.

3. Type in

ls /dev/tty*

4. Plug in Arduino.

5. Repeat step 3.

6. Check for a new directory file between the two times. The one that only shows up the second time is what you should use!

LEDs don't light when they should?


Check your wiring. Are your LEDs facing the right direction? Are your LEDs connected to the right pins? Always double check!

Serial Communication with Arduino - http://forum.arduino.cc/index.php?topic=396450

Other Projects - http://www.seeedstudio.com/recipe/166-basic-pi-lt-gt-arduino-communication-over-usb.html

<p>Great for learning the basics of getting the Pi to talk to the Arduino with minimum effort.</p>
<p>can someone tell me where in the breadboard the jumper wires are going? I can't read it from the picture, and yes i'm a beginner so I think it would be logic but I don't understand ha ha</p>
I want to connect two arduino or more to a raspberry pi using serial commmunication via usb gor my latest project .....<br><br>Sir plz plz plz help me ..... coz i m a begginer ?????
<p>For this simple example, you're actually making it a bit more complicated than you need to. Instead of getting into python, all you need is echo &quot;1&quot; &gt;/dev/ttyACM0 (note, /dev/tty is not a directory, e.g. /dev/ttyACM0 not /dev/tty/ACM0). You should also look at http://playground.arduino.cc/Interfacing/LinuxTTY for info on how to easily configure the arduino port characteristics, e.g baud rate (note, I add -hupcl to the string listed to keep the arduino from rebooting each time the device is opened). Note also that sometimes arduinos appear as /dev/ttyUSB0 instead of /dev/ttyACM0. It's also handy to know there is a symbolic link at /dev/serial/by-id that includes the usb id in the file name. This is really useful if you have more than one arduino attached to your system because ACM0, ACM1, etc. can change if you plug and unplug the arduino but the usb id can't.</p>
<p>Thank you for that link, I will check it out!</p><p>And as for the name of the USB port, there's instructions on how to find out what it's called in the Troubleshooting step of this. </p>
True. It's just that the way I usually work is to copy example source code and run it. Because of the extra &quot;/&quot; that code won't work. Errors like this leave me confused and frustrated. What you are illustrating here is something I think is really cool so I really want it to be easy and work for all those newbies trying this stuff for the first time.

About This Instructable

43,008views

216favorites

License:

Bio: Building and coding is what I do.
More by KNEXnCoding:How to Install and Use OpenEmu Easiest way to control your Arduino with a Mobile Device! Controlling a DC Motor With Arduino 
Add instructable to: