Raspberry Pi 2 also provides serial communication as it has got inbuilt transmission and reception pins. Here we make use of an arduino board to demonstrate the serial communication in Raspberry Pi 2. A simple program is written on the arduino ie while receiving a character ‘A’, a string ‘OK’ will be transmitted. So a python script should be written to transmit a character ‘A’ through the transmission pin and to receive an ‘OK’ through the reception pin and it should be print on the python shell.
The figure shown above represents how an arduino board is connected to a Raspberry Pi serially. Pin no. 8 is the transmission pin and pin no. 10 is the reception pin of the raspberry Pi 2. Digital pin 0 is the reception pin and digital pin 1 is the transmission pin in the arduino board. The serial interface between both the modules are done by connecting the transmission pin of the arduino board in to the reception pin of the raspberry pi. Similarly the reception pin of the arduino board should be connected to the transmission pin of the raspberry pi. Also the ground pins must be shorted as shown in the figure.
Given below is the arduino code for serial communication which transmit an ‘OK’ string when a character ‘A’ is received.
After writing the code power the arduino board using a 12volt adapter. Also power the Raspberry Pi and open the python editor and write the code shown below.
import RPi.GPIO as GPIO
import os, time
port = serial.Serial("/dev/ttyAMA0", baudrate=9600, timeout=0.5)
rcv = port.read(10)
ttyAMA0 is the name of the serial port used in Raspberry Pi.
Run this python code and check whether the string ‘OK’ is being received in the python shell. There is a chance of generating an error due to serial exceptions. When this error occurs open the lxterminal and enter in to configuration settings by typing raspi-config. Then follow the instructions as shown in the figures given below.
After performing the above operations run the same code and check the results.
Shop With Us
- Click here to buy Raspberry Pi 2 from RhydoLabz.
- An open source website by Raspberry Pi foundation.
- RPi Serial Connection.
- Get started with Python programming.
Frequently Asked Questions (FAQs)
Q: ttyAMA0 is the name of the serial port used in Raspberry Pi. What is this AMA stands for ?
Ans: The driver used for the RPi’s UART which is the short form of AMBA (Advanced Microcontroller Bus Architecture). AMBA is is an open-standard, on-chip interconnect specification for the connection and management of functional blocks in system-on-a-chip (SoC) designs.
Q: What is the name of the USB serial port in Raspberry Pi ?
Ans: ttyUSB0, ttyUSB1, and so on.