Integrate your Arduino projects with the power of Raspberry Pi

Learn how to connect an Arduino board to a Raspberry Pi and control it using Python code. …


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 board to a Raspberry Pi and control it using Python code.

Requirements:

  • Raspberry Pi (any model)
  • Arduino Uno or compatible board
  • USB cable (Type A to Micro B)
  • Breadboard
  • Jumper wires
  • Power supply for both devices

Step 1: Connecting the devices

To connect the Arduino and Raspberry Pi, you will need to use a USB cable. The Type A connector should be plugged into the Raspberry Pi and the Micro B connector into the Arduino board. The other end of the cable can be left unconnected or connected to the power supply.

Step 2: Wiring up the Arduino

First, connect your Raspberry Pi to a breadboard using some jumper wires. Then, connect the following pins on the Arduino board to the corresponding pins on the breadboard:

  • VCC (5V) to the positive rail of the breadboard
  • GND to the negative rail of the breadboard
  • D2 (digital pin 2) to a jumper wire, which will be connected to Raspberry Pi GPIO pin 18 (using another jumper wire)
  • GND to another jumper wire, which will also be connected to Raspberry Pi GND pin

Step 3: Set up the Raspberry Pi

Next, you need to set up your Raspberry Pi for Python development. If you haven’t already done so, install the latest version of Raspbian on your Raspberry Pi and update it using the following commands in a terminal window:

sudo apt-get update
sudo apt-get upgrade

Then, install the necessary libraries to communicate with the Arduino:

sudo apt-get install python3-rpi.gpio python3-serial

Step 4: Write the Python code

Create a new file called arduino_control.py and open it in your favorite text editor. Add the following code to control the Arduino from the Raspberry Pi using Python:

import serial, time
import RPi.GPIO as GPIO
from gpiozero import LED

# Set up GPIO pin for Arduino connection
GPIO.setmode(GPIO.BCM)
GPIO.setup(18, GPIO.OUT)
led = LED(18)

# Connect to the Arduino using serial communication
ser = serial.Serial('/dev/ttyACM0', 9600)
time.sleep(2) # wait for connection to establish

# Control the Arduino
while True:
    command = input("Enter command (on, off): ")
    if command == 'on':
        led.on()
        ser.write(b'1')
    elif command == 'off':
        led.off()
        ser.write(b'0')

This code sets up the Raspberry Pi to communicate with the Arduino using serial communication and allows you to control an LED connected to digital pin 2 on the Arduino via the terminal. Replace '/dev/ttyACM0' with the correct port for your Arduino if necessary.

Step 5: Test the connection

Upload a simple blink program to your Arduino board using the Arduino IDE. This will make sure the connection between the devices is working properly and the LED on your Arduino board will blink when you send the ‘1’ command from the Raspberry Pi.

Now, run the arduino_control.py script in a terminal window:

python3 arduino_control.py

Type “on” or “off” in the terminal to turn on or off the LED connected to your Arduino board. You should see the LED blinking accordingly.

That’s it! You have successfully connected an Arduino to a Raspberry Pi and controlled it using Python code. Now you can integrate your Arduino projects with the power of Raspberry Pi and create amazing IoT solutions. Happy coding!