Build a simple UART communication circuit between a Raspberry Pi and Arduino UNO and learn how to build a Raspberry Pi UART interface for communicating between the two devices.
Universal asynchronous receiver/transmitter (UART) is a block of circuitry responsible for implementing asynchronous serial communication. It takes bytes of data and transmits the individual bits in a sequential fashion. At the other end, a receiver receives the data and reassembles the bits into complete bytes of data. Communication may be simplex (in one direction only, with no provision for the receiving device to send information back to the transmitting device), full duplex (both devices send and receive at the same time) or half duplex (devices take turns transmitting and receiving).
In this blog post, we will build a simple circuit for Raspberry Pi UART transmitter operation and demonstrate how to achieve UART communication with Arduino UNO as receiver.
For this demo, we have chosen Raspberry Pi & Arduino UNO as both these boards support UART Serial Communication.
Raspberry Pi 3
Raspberry Pi is the credit card sized computer which has rich peripherals to interface various sensors. Here is a recap of the Raspberry Pi 3 pin functions.
Arduino/Genuino Uno is a microcontroller board based on the ATmega328P (datasheet). It has 14 digital input/output pins (of which 6 can be used as PWM outputs), 6 analog inputs, a 16 MHz quartz crystal, a USB connection, a power jack, an ICSP header and a reset button.
You will need the following hardware to execute this project.
- Raspberry Pi 3 Board*
- Arduino UNO*
- Push Buttons (2 nos.)
- Resistors, 220 Ohms ( 4 nos.), 1k Ohms (1 no.), 2.2k Ohms ( 1 no.)
- 5mm LED (2 nos.)
You can also get the electronic kit* that contains all the passive components such as resistors, buttons, connecting wires and breadboard.
Before getting into the specifics of the hardware connection, let us define the role that each of the devices play in this demo.
- Arduino UNO – Acts as the UART receiver. Receives data at the UART interface, sent from the Raspberry Pi and toggles the LED.
- Raspberry Pi – Acts as the UART transmitter. Transmits data upon the press of a button.
We are going to demonstrate UART communication between the Raspberry Pi and Arduino UNO by transmitting certain commands on the press of a button, and blinking of LED to indicate the reception of those commands. For each button press from the Raspberry Pi, there is a command data sent to the Arduino UNO’s UART.
Here is the complete hardware connection between the Raspberry Pi and Arduino UNO including the interface connections for the button and LED. We have used two buttons and two LEDs (Check the color codes for resistors. The Vcc and ground connections are represented by red and black wires respectively).
Before powering the circuit, we have to be aware of voltage level shifting from Arduino to Raspberry Pi. Since Arduino works with 5V and Raspberry Pi works with 3.3V. We can achieve level shifting from 5V to 3.3V using voltage divider circuit.
Why do we need Voltage Dividers?
A voltage divider is a simple circuit which turns a large voltage into a smaller one. Using just two series resistors and an input voltage, we can create an output voltage that is a fraction of the input.Voltage dividers are one of the most fundamental circuits in electronics.
With this, the data transmitted from the Arduino UNO is brought down to 3.3v using voltage divider resistors.
Note: Make sure to follow the circuit and schematic illustration above. Also please cross check the circuit before powering on the devices. If the voltage at Raspberry Pi end is not brought down to 3.3 volts then this can permanently damage the board.
Power on the hardware setup and make sure that you can access Arduino UNO via the USB of your computer and can login to Raspberry Pi terminal (via ssh or direct display connection). Your Raspberry Pi should also have the Python interpreter installed.
Arduino Software Build
Before proceeding, we need to install Arduino IDE on a computer to which the Arduino is connected.
Installing the Arduino IDE
– Download and Install Latest Version of Arduino IDE
– Start Arduino IDE and Plug the Development Board
Step 1: Clone the GitHub repository under one directory on the computer, using the following link
git clone https://github.com/suryasundarraj/rpi-series.git
Step 2: Open the file rpi-series/uart_communication/uart_communication/uart_communication.ino from this repo in Arduino IDE
Step 3: Select the Board from Tools – > Board – > Arduino UNO
Step 4: Select the USB Port from Tools – > Port – > COMXX
Step 5: Upload the Code to Arduino UNO board.
With this, the UART receiver program is flashed on Arduino UNO and it is ready to receive data.
Raspberry Pi UART Python Script
Step 1: Log into the Raspberry pi console and clone the GitHub repository under the home directory, using the following link
git clone https://github.com/suryasundarraj/rpi-series.git
Step 2: Under the “rpi-series” navigate to “uart_communication”. Open the uart_communication folder using the following command
cd rpi-series cd uart_communication
Step 3: Run the python script
With this, the Raspberry Pi is set up to trigger UART communication at the press of the button. The Arduino UNO is already listening on UART so when we press the button, this is what happens.
I encourage you to explore the source code for the Arduino UNO program and the Raspberry Pi python script to understand the logic. Pay attention to the functions that are used to initialize the serial interface and for configuring the baud rate.
Try your hands on this demo. And please share your experiences or any particular challenges that you might have faced in executing this setup. If u have any technical doubts then you can post your comment below and I will get back to you.
I will be back soon with yet another important hardware interface supported by Raspberry Pi.
Disclosure: * Denotes an affiliate link – if you click and make a purchase we may receive a small commission.