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).
Project Overview
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 UNO
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.
Hardware Requirements
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.)
- Breadboard
Hardware Connection
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.
Project Demo
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 linkgit clone https://github.com/suryasundarraj/rpi-series.gitStep 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 linkgit clone https://github.com/suryasundarraj/rpi-series.gitStep 2: Under the "rpi-series" navigate to "uart_communication". Open the uart_communication folder using the following command
cd rpi-series cd uart_communicationStep 3: Run the python script
python uart_rpi.pyWith 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.
Disclosure: * Denotes an affiliate link – if you click and make a purchase we may receive a small commission.