Arduino and Raspberry Pi Working Together

DZone 's Guide to

Arduino and Raspberry Pi Working Together

Wish you could blend the benefits of an Arduino and Raspberry Pi? All you need is a USB cable and a bit of code to start building an app.

· IoT Zone ·
Free Resource

Basically, everything we can do with Arduino it can be done with a Raspberry Pi (and vice versa). Sure, there are things that are easy to do with Arduino (connecting sensors for example), but other things (such as working with REST servers and databases) are “complicated” with Arduino and C++ (they are possible but require a lot of low-level operations). Meanwhile, those difficult tasks are pretty straightforward with Raspberry Pi and Python (at least for me and because of my background).

With this small project, I want to use an Arduino board and Raspberry Pi together. The idea is blinking two LEDs. One (a green one) will be controlled by the Raspberry Pi directly via GPIO, and the other one (a red one) will be controlled by the Arduino board. The Raspberry Pi will be the “brain” of the project and will tell the Arduino board when to turn on/off its LED. Let’s show the code.

import RPi.GPIO as gpio
import serial
import time
import sys
import os

def main():
    gpio.setup(12, gpio.OUT)

    s = serial.Serial('/dev/ttyACM0', 9600)
    status = False

    while 1:
        gpio.output(12, status)
        status = not status
        print status
        s.write("1\n" if status else "0\n")

if __name__ == '__main__':
    except KeyboardInterrupt:
        print 'Interrupted'
        except SystemExit:

As we can see, the script is a simple loop and blinks the LED (using pin 12) with an interval of one second. Our Arduino board is connected directly to the Raspberry Pi via USB cable and we send commands via serial interface.

Finally, the Arduino program:

#define LED  11

String serialData = "";
boolean onSerialRead = false; 

void setup() {
    // initialize serial:

void procesSerialData() {
    Serial.print("Data " + serialData);
    if (serialData == "1") {    
        Serial.println(" LED ON");
        digitalWrite(LED, HIGH);
    } else {
        Serial.println(" LED OFF");
        digitalWrite(LED, LOW);
    serialData = "";
    onSerialRead = false;

void loop() {
    if (onSerialRead) {

void serialEvent() {
    while (Serial.available()) {
        char inChar = (char)Serial.read();
        if (inChar == '\n') {
            onSerialRead = true;
        } else {
            serialData += inChar;

Here, our Arduino board is listening to the serial interface (with serialEvent), and each time we receive “\n”, the main loop will turn on/off the LED depending on the value (1 = on, 0 = off).

We can use I2C and other ways to connect an Arduino and a Raspberry Pi, but in this example, we’re using the simplest way to do it: A USB cable. We only need an A/B USB cable. We don’t need any other extra hardware (such as resistors) and the software part is pretty straightforward also.


  • Arduino UNO
  • Raspberry Pi 3
  • Two LEDs and two resistors

The code is in my GitHub account.

arduino, iot, iot app development, raspberry pi, tutorial

Published at DZone with permission of Gonzalo Ayuso , DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}