Raspberry Pi has a plethora of applications and a huge range over which it can be put to use, for some of these applications we require to connect it to external sensors or devices. To do so, we need to first understand how to connect the Pi to read and write from the peripheral device. Using serial communication with your Raspberry Pi is a simple and efficient way to read and write from and to an external device. To do this, we use the GPIO pins provided on the board, and by the end of this article, you should be able to connect your Raspberry Pi to any serial device.
What is serial communication?
Serial communication is a widely used approach to transfer information between a system and peripherals connected to it. It is also a simple, yet effective method of communication and is easy to learn and master for beginners. Raspberry Pi uses UART (Universal Asynchronous Receiver/Transmitter) is a serial communication protocol in which data is transferred serially i.e. bit by bit. Asynchronous serial communication is widely used for byte oriented transmission.
Hardware requirements:
- Raspberry Pi with OS installed (available on the official website)
- Camera module
- Power cable
- Monitor
- HDMI connector
- USB or Bluetooth mouse
- USB or Bluetooth keyboard
Setup:
Hardware
The RS232 to TTL adapter will have four connections which will be labelled: GND (Ground power-supply pin), RX (Received Data), TX (Transmitted Data), and VCC (Power-supply pin). To connect pins to the Raspberry Pi board, you can use either female to female wires to directly connect them or use a breadboard as in this tutorial. The GPIO pins on the board are assigned as:
The connection between the wires should be given as below (you can also refer the diagram)
- GND connects to Pin 6.
- RX connects to Pin 10.
- TX connects to Pin 8.
- VCC connects to Pin 4.
Software:
- First, open the terminal and use the following commands to update and upgrade your Pi:
sudo apt-get update sudo apt-get upgrade
2. next, we use the raspi-config tool. This will allow us to enable and disable the serial input/output interface. To open the Raspberry Pi configuration tool type:
sudo raspi-config
3. This command will load up the Raspberry Pi configuration screen. For more details on this check out: https://iot4beginners.com/raspberry-pi-configuration-settings/ . Use the arrow keys to select ‘Interfacing Options’ and click enter
4. On the next screen, use the arrow keys again to select “Serial“, and hit enter
5. You will then be prompted to choose whether you want the login shell to be accessible over serial. Select No and press enter. Select Yes when prompted to make use of Serial Port Hardware and press enter.
6. Once the Raspberry Pi has made the changes, you should see the following text appear on your screen.
“The serial login shell is disabled
The serial interface is enabled“.
7. After this, make sure to restart the Raspberry Pi. To do so, you will either be prompted, or you can type
sudo reboot
Prerequisites
We will be using a RS232 to TTL Adapter to create a loop back to the Raspberry Pi. Have your USB-Serial adapter plugged into the RS232 adapter, and the USB end of the USB-Serial adapter to be plugged into your Raspberry Pi’s USB Ports. In a practical application, you will be connecting your serial connection to an actual device
1. Once you have connected your USB-Serial adapter up and it is plugged into the Raspberry Pi, type the following in the terminal
dmesg | grep tty
2. In the output of this command, take note the USB specified.
For example, my own converter was attached to ttyUSB0 as we have shown in our output below. Make a note of what your USB device was attached to as you will need this to complete the tutorial.
ch341-uart converter now attached to ttyUSB0
In the text above, it has been connected to ttyUSB0
Programming for Serial Write
The first line of code is there to tell the operating system what it should try running the file with. Otherwise, it will likely attempt to run it as a standard bash script. We then import the libraries needed to run the code
#!/usr/bin/env python import time import serial
We then define the serial class. Here, port defines the serial port that the object will read and write over. Baudrate is the rate at which information is transferred. Parity chooses whether we should be doing parity checking. Stopbits indicates the end of data transmission. Bytesize is the number of data bits. Timeout is the time that serial commands should wait for before timing out.
ser = serial.Serial( port='/dev/ttyS0', #Replace ttyS0 with ttyAM0 for Pi1,Pi2,Pi0 baudrate = 9600, parity=serial.PARITY_NONE, stopbits=serial.STOPBITS_ONE, bytesize=serial.EIGHTBITS, timeout=1 ) counter=0
We then write a loop to continually writing the text “Write Counter: n”. Where n is the current counter number to the serial port. It means that any device at the receiving end can display that text.
while 1: ser.write('Write counter: %d \n'%(counter)) time.sleep(1) counter += 1
Programming for Serial Read
Programming for Serial Readis very similar to Serial Write, except for one difference – we use the port that we got a few steps ago, which in my case this was ttyUSB0.
!/usr/bin/env python import time import serial ser = serial.Serial( port='/dev/ttyUSB0', baudrate = 9600, parity=serial.PARITY_NONE, stopbits=serial.STOPBITS_ONE, bytesize=serial.EIGHTBITS, timeout=1 ) while 1: x=ser.readline() print x
This function reads a line until it hits a line that ends in \n. Anything after that will be omitted. Once it reads the value it stores it into our variable. We also print the value.
To view the output of your codes, open two terminals – in one run the serial write and in the other run the serial read. You will see the output in the serial read terminal, as the data will be written.
This will give you an insight into using the Raspberry Pi for Serial communication with peripheral devices and you can explore a wide range of applications using this!