Connect Your Raspberry Pi and Arduino Uno!

129,572

247

24

Introduction: Connect Your Raspberry Pi and Arduino Uno!

About: Building and coding is what I do.

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

Be the First to Share

    Recommendations

    • Recycled Speed Challenge

      Recycled Speed Challenge
    • Make it Move Contest 2020

      Make it Move Contest 2020
    • Build a Tool Contest

      Build a Tool Contest

    24 Discussions

    0
    yumadome
    yumadome

    Question 1 year ago on Step 6

    Hi! On another blog they commented that tying the two devices directly together by the serial port was risky because the Rasberry PI IS 3.3V and the Arduino was 5V, thereby risking damage to the Pi... is this an issue or an outdated problem?

    0
    JamieB226
    JamieB226

    Answer 9 months ago

    It looks like they have them connected over USB and USB is always 5V. the Pi should be fine. I imagine the other site was talking about connecting the GPIO pins.

    0
    theguymasamato
    theguymasamato

    1 year ago

    Thanks for this Instructables.
    Very helpful.

    0
    eduardonobresilva
    eduardonobresilva

    1 year ago

    Excelente, mas não se esqueçam de instalar a serialport (npm install serialport@6.2.2 --save).

    IMG_20190522_154550.jpgIMG_20190522_154557.jpg
    0
    amtanaam22
    amtanaam22

    1 year ago

    why i could not upload these programs, can you help me ,please ?

    Screenshot (51).pngScreenshot (52).png
    0
    MUKESHBADIGINENI
    MUKESHBADIGINENI

    Question 1 year ago on Introduction

    I would like to send data from Arduino to raspberry pi using Bluetooth. Someone kindly guide me....

    0
    shivakumar1995
    shivakumar1995

    3 years ago

    the command " ser.write('3') " is not working. I checked the leds they are fine and their polarity is also correct. what might be the problem here?

    0
    ryki2658
    ryki2658

    Reply 1 year ago

    add "str.encode" before the ('3').
    should look like this:
    ser.write(str.encode('3'))

    0
    LouieL21
    LouieL21

    Reply 3 years ago

    Use Python 2 (IDLE). Run the module file then enter 'ser.write('3')' in the shell.

    0
    Jjss90up
    Jjss90up

    Question 1 year ago on Step 4

    Hello
    beautiful tuturial
    but I have a question
    how can i read the status of the arduino inputs on rastberry?
    and why is it that when I dial an output with the number above 9 it does not turn on?
    thank you

    0
    MalluS1
    MalluS1

    2 years ago

    sir greatwork that u have done same i too did ..can u turn on the LED through your voice by using input as Microphone.LED should be turned only after recogning your can we try this.can u guide me can u tell me how to send the commands from raspberrypi to arduion

    0
    MortenB14
    MortenB14

    Question 2 years ago

    pi@raspberrypi:~ $ sudo apt-get install arduino

    Reading package lists... Done

    Building dependency tree

    Reading state information... Done

    E: Unable to locate package arduino

    is there a diffrent command i can type in to install the arduino ide on the raspberry pi?

    0
    KalpanJ
    KalpanJ

    2 years ago

    +1! Great, simple tutorial

    0
    andremvilar
    andremvilar

    2 years ago

    Hello,
    Thanks for share this project, it was so easy and also elegant to make this connection I am very happy with the results, I was looking for such connections but only find difficult and not cristal clear projects. This one realy works. Thanks.

    0
    genesisquilala
    genesisquilala

    2 years ago

    Dude. Please help me . We have a thesis. How can i connect rasbery pi to arduino? is there any application i can used? Heeelp me i need to run arduino using rasbery pi please here is my fb. cexz.quilala and my email @cexz.quilala. Please i will pay if u want please asap. jut pm me

    0
    twicejiggled
    twicejiggled

    Reply 2 years ago

    You need to do this for a thesis? This is easy man, and there are tons of guides online of multiple ways to do it! Spend more then 10 minutes googling and i'm sure you'll find a a few step by step or video guides.

    0
    tonys_0
    tonys_0

    2 years ago

    Hi. I am reading this prior to buying an Arduino board, so I can not see, what happens live.

    I actually do not see, how one does:

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

    but I guess it will be obvious after performing

    sudo apt-get install arduino

    Will it? Or do I have to start some kind of UI on Raspberry?

    My other question is: Will all this work with Leonardo or other boards like Mini, Micro, Pico or whatever?

    Thank you for answer.

    0
    Doppelbock
    Doppelbock

    3 years ago

    Great for learning the basics of getting the Pi to talk to the Arduino with minimum effort.

    0
    TessaV10
    TessaV10

    3 years ago

    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