Integrating IoT devices with the Raspberry Pi for smarter living.

Learn how to connect an Arduino to a Raspberry Pi and create a smart home system using these two popular microcontroller boards. …


Updated October 4, 2023

Need help with your Raspberry Pi?
Contact Me!

Do you love silly Raspberry Pi Projects?
Check out my this YouTube Channel!


Learn how to connect an Arduino to a Raspberry Pi and create a smart home system using these two popular microcontroller boards.

Requirements:

  • Raspberry Pi 4 or higher (any model)
  • Arduino Uno or similar board
  • USB cable
  • Breadboard
  • Jumper wires
  • Power supplies for both devices

Step 1: Prepare the hardware

First, gather your components and prepare the hardware. Here’s how to set up each device:

Raspberry Pi Setup:

  1. Connect the power supply to the Raspberry Pi and insert the SD card with your preferred OS (e.g., Raspbian or Ubuntu).
  2. If you haven’t already, enable SSH on the Raspberry Pi so you can access it remotely. This will make it easier to work with the device later.
  3. Connect a keyboard, mouse, monitor, and ethernet cable to set up your Raspberry Pi.
  4. Once the setup is complete, disable the graphical desktop and boot directly into command line mode for better performance. You can do this by editing the /boot/config.txt file on the SD card and adding startx=0 at the end of the file.

Arduino Setup:

  1. Connect the power supply to your Arduino board.
  2. Use a breadboard to set up your circuit with jumper wires connecting the necessary components (e.g., sensors, LEDs, motors).
  3. Make sure you have a stable connection between the Raspberry Pi and Arduino using the USB cable.

Step 2: Connect the devices

Now that the hardware is ready, it’s time to connect the Raspberry Pi and Arduino boards. Here are the steps:

  1. Plug the USB cable into the Arduino board and the other end into a USB port on the Raspberry Pi. This will provide power to the Arduino and allow for data transfer between the devices.
  2. Open a terminal window on your Raspberry Pi and check that the Arduino is recognized by running ls /dev/tty*. You should see a device named /dev/ttyACM0 or similar, which indicates that the connection has been established.
  3. To test the connection, run sudo screen /dev/ttyACM0 9600. This will open a serial terminal at 9600 baud rate. Type some text into the terminal and check if it appears on the Arduino’s serial monitor. If everything works correctly, you should see your input echoed back to you.
  4. To close the serial connection, press Ctrl+A followed by K and then Y.

Step 3: Write code for the Raspberry Pi

Now that we have connected the two devices, let’s write some code to make them communicate with each other. On the Raspberry Pi, you can use Python and its built-in serial library to read data from the Arduino and perform actions based on it. Here’s an example script:

import serial
import time

ser = serial.Serial('/dev/ttyACM0', 9600) # establish serial connection
time.sleep(2) # give the connection some time to settle

while True:
    data = ser.readline()[:-2] # read a line of data from Arduino, remove newline characters
    if data == b'ON': # check if the data received is "ON" (as bytes)
        print("Light on")
        # add code to turn on the light here
    elif data == b'OFF': # check if the data received is "OFF" (as bytes)
        print("Light off")
        # add code to turn off the light here
    time.sleep(1) # wait for a second before checking again

In this script, we read data from the Arduino continuously and check if it’s equal to “ON” or “OFF”. Based on the received data, you can trigger actions like turning on or off a light in your smart home system. Save this file as arduino_connect.py and run it with python3 arduino_connect.py.

Step 4: Write code for the Arduino

On the Arduino side, you can write a simple program that sends “ON” or “OFF” signals based on some input, such as a button press. Here’s an example sketch:

const int buttonPin = 2; // set up pin 2 for the button
const int ledPin = 13; // set up pin 13 for the LED
int buttonState = 0; // variable to store button state

void setup() {
  Serial.begin(9600); // start serial communication at 9600 baud rate
  pinMode(buttonPin, INPUT); // set up button as input
  pinMode(ledPin, OUTPUT); // set up LED as output
}

void loop() {
  buttonState = digitalRead(buttonPin); // read the state of the button
  
  if (buttonState == HIGH) { // check if the button is pressed
    Serial.println("ON"); // send "ON" to Raspberry Pi
    digitalWrite(ledPin, HIGH); // turn on the LED
  } else {
    Serial.println("OFF"); // send "OFF" to Raspberry Pi
    digitalWrite(ledPin, LOW); // turn off the LED
  }
  
  delay(100); // wait for a moment before checking again
}

Upload this code to your Arduino board and test it by pressing the button. You should see “ON” or “OFF” appearing on the Raspberry Pi’s terminal when you press the button, and the LED should turn on and off as well.

Conclusion:

Now you have successfully connected an Arduino to a Raspberry Pi and established a two-way communication channel between them. With this foundation, you can build more complex IoT projects that sense and control various devices in your home or workspace. The possibilities are endless - explore different sensors, actuators, and programming languages to create the smart home of your dreams!