Subscribe To Our List

We help electronics hobbyists to create projects related to Micro-controllers. Join our newsletter to get access to new projects first.
Email address
Secure and Spam free...

In this Raspberry Pi tutorial, you are going to learn about controlling Arduino and Raspberry Pi serial communication. Arduino is a micro-controller which is very popular because of its ability to control the hardware. Raspberry PI have 26 GPIO pins and if you require more input/output pins then the Arduino will come in handy for you. The Arduino also have the built-in ADC which means that you can get the analog inputs with it.

In the first example, we will simply write the code in the Arduino IDE to blink the LED on pressing the push button. In the second example, we will do the bidirectional serial communication and we will send the data both ways at the same time (From Arduino to Raspberry Pi and from Raspberry Pi to Arduino).

Installing the Arduino IDE

  1. First of all update your Raspberry Pi to have the latest features by typing the below command in the terminal.
    sudo apt-get update
  2. Then type the below command in the terminal to install the Arduino IDE.
    sudo apt-get install arduino

Running a simple Example

In the first example, we are going to control the LED with the Arduino. To do it, follow the below steps

  1. Make the connections as follows
    Connect the positive lead of LED to the pin 8 of Arduino and the other lead of LED to the ground through the 220 ohm resistor.
    Then Place your push button on the breadboard and connect the upper left or lower left pin to the pin 7 of Arduino and also connect a 10k resistor at this pin. Connect the other end of this resistor to ground. After that, connect the upper right or lower right pin of push button to the 5V of Arduino.
    If you will see the internal circuit of push button, then you will see that the upper left and lower left pins of push buttons are shorted with each other and also the upper right and lower right pins of push buttons are shorted with each other.
  2. Plug in the cable of Arduino in the Raspberry Pi and type the following command in the terminal to open up the Arduino.
    arduino

    Then go to the tools -> board and make sure that the right board is selected. I am using Arduino Uno, so I have selected Arduino Uno.

    Then go to tools -> serial port and make sure that the port is marked tick.  Remember the port, you will need it in the second example. In my case, the port number is “/dev/tty/ACM1”.

  3. Type the following code in the Arduino IDE and upload it. The LED should light up on pressing the push button.
    int led_pin = 8; //Initializing pin 8 of Arduino as the led pin
    int button_pin = 7;  //Initializing pin 7 of Arduino as the button pin
    int button_state = 0;  //Initializes a variable for storing the button state
    
    void setup(){
      pinMode(led_pin, OUTPUT); //Declaring led pin as output pin
      pinMode(button_pin, INPUT); //Declaring button pin as the input pin.
    }
    
    void loop(){
      button_state = digitalRead(button_pin); //Reading the state of button
      digitalWrite(led_pin, button_state);  //Light up or light down the LED depending on the state of button
    }

Bidirectional Communication between Raspberry Pi and Arduino over serial

In this example, we are going to do the bidirectional communication. When we will press the push button on the Arduino side, the led connected to the Raspberry Pi will light up and when we will press the push button on the Raspberry Pi side, the led connected to the Arduino will light up.

The circuit diagram for Arduino is same as it was in the first example and make the circuit for Raspberry Pi as shown below.

Arduino and Raspberry Pi serial communication Arduino and Raspberry Pi serial communication

Arduino Code

Everything is explained with the comments.

int led_pin = 8; //Initializing pin 8 of Arduino as the led pin
int button_pin = 7;  //Initializing pin 7 of Arduino as the button pin
int button_state = 0;  //Initializes a variable for storing the button state
int incoming_state = 0;  //Initializes a variable for storing the incoming data from Arduino

void setup(){
  Serial.begin(9600);  //Started the serial communication at 9600 baudrate
  pinMode(led_pin, OUTPUT); //Declaring led pin as output pin
  pinMode(button_pin, INPUT); //Declaring button pin as the input pin.
}

void loop(){
  if (Serial.available() > 0){  //Looking for incoming data
    incoming_state = Serial.read() - '0';  //Reading the data
    digitalWrite(led_pin, incoming_state);  //Making the LED light up or down
  }
  
  button_state = digitalRead(button_pin); //Reading the button state
  Serial.println(button_state);  //Sending the data over serial to Raspberry pi
}

Raspberry Pi

Everything is explained with the comments.

import serial  # Importing the serial library to communicate with Arduino
import RPi.GPIO as GPIO # Importing the GPIO library to use the GPIO pins of Raspberry pi
from time import sleep # Importing the time library to provide the delays in program

led_pin = 21  # Initializing pin 21 for led
button_pin = 20 # Initializing pin 20 for button

GPIO.setmode(GPIO.BCM) # Using BCM numbering
GPIO.setup(led_pin, GPIO.OUT) # Declaring the led pin as output pin
GPIO.setup(button_pin, GPIO.IN) # Declaring the button pin as input pin

# Defined the port and the baudrate at which we will communicate with Arduino.
# It should be same on the Arduino side.
# If you don't know the port at which Arduino is connected. Read the step 2 of the first example.
ser = serial.Serial('/dev/ttyACM1', 9600) 

while True:
    try:
        # Reading and storing the data coming from Arduino
        incoming_data = ser.read() 
        
        # Light up or light down the led depending on the incoming data
        if incoming_data == '1':
            GPIO.output(led_pin, 1)
        elif incoming_data == '0':
            GPIO.output(led_pin, 0)
        
        # Reading the button state
        button_state = GPIO.input(button_pin)
        if button_state == 1:
            ser.write('1') # Sending data to the Arduino
        elif button_state == 0:
            ser.write('0')
        
    except KeyboardInterrupt:
        GPIO.cleanup()

If you have any questions, feel free to ask me in the comment section.

JOIN OUR NEWSLETTER
We help electronics hobbyists to create projects related to Micro-controllers. Join our newsletter to get access to new projects first.
We hate spam. Your email address will not be sold or shared with anyone else.

Leave a Reply

Your email address will not be published.

Subscribe To Our List

We help electronics hobbyists to create projects related to Micro-controllers. Join our newsletter to get access to new projects first.
Email address
Secure and Spam free...
Skip to toolbar