How to Set up serial communication between Raspberry pi and Arduino using GPIO
How to Set up serial communication between Raspberry pi and Arduino using GPIO
Disable getty:
To use pi's serial port we need to disable getty.
Login as administrator by this command sudo startx
Using Filemanager open etc/inittab (it will open like a file)
Add comment infront of the following line
0:23:respawn:/sbin/getty -L ttyAMA0 115200 vt100
Click on save.
Remove the console statement carefully:
Using Filemanager open /boot/cmdline.txt
Remove the statements console=ttyAMA0,115200
Then click on save
Note: The cmdline.txt file cannot open like a normal file.so kindly open with vi editor.
Minicom installation:
Install minicom (serial monitor)
Open the Lxterminal and type sudo apt-get install minicom
To allow installation kindly type 'y'
Hardware connections
1.Arduino signals +5v and 0 ,Raspberry pi signals are +3.3v and 0.Raspberry pi GPIO pin are very sensitive.If 5v may drop on that it will get damaged.Inorder to avoid that we may use voltage divider logic
2.Take a breadboard and do connections as mentioned below
3.Take 1 kilo ohm resistor connect the one end with arduino Transmitter(Tx) and other end with Raspberry pi receiver(Rx) in series.
4.Connect another 1 kilo ohm resistor with Raspberry pi receiver(Rx) and other end of resistor should be grounded
5.Connect the both ground pins with ground rail of breadboard
6.Connect the Raspberry pi transmitter(Tx) with Arduino Receiver(Rx)
7.Raspberry pi
8.Make a check your pi already installed Pyserial
9.If yes,Kindly follow the below steps,If No,Kindly install Pyserial with the help of Interfacing_Between_Arduino_and_Raspberry_pi document
10.Open the Lxterminal and type sudo idle
11.Open File->New Window and type the following Program
#!/usr/bin/env python
import serial
import string
rot13 = string.maketrans(
"ABCDEFGHIJKLMabcdefghijklmNOPQRSTUVWXYZnopqrstuvwxyz",
"NOPQRSTUVWXYZnopqrstuvwxyzABCDEFGHIJKLMabcdefghijklm")
test=serial.Serial("/dev/ttyAMA0",9600)
test.open()
try:
while True:
line = test.readline(eol='\r')
test.write(string.translate(line, rot13))
except KeyboardInterrupt:
pass # do cleanup here
test.close()
-
Then click on save to save the module
-
Then click on Run to Run module
-
Open the minicom to view the data which is sent/received in serial communication by the following command
-
sudo minicom -b 9600 -o -D /dev/ttyAMA0 type the command in the Lxterminal
Arduino
-
Connect your arduino with your pc using usb cable
-
Open Arduino IDE as administrator
-
Type the following program
void setup() {
Serial.begin(9600);
}
void loop() {
int incomingByte;
if(Serial.available() > 0) {
// read the incoming byte:
incomingByte = Serial.read();
// echo
Serial.write(incomingByte);
}
}
-
Click to save icon to save the file
-
Click to verify icon to compile the file
-
Click to upload icon to upload the file
-
Open the serial monitor in the arduino IDE to view the data which is sent/received in serial communication
Note: Kindly disconnect the Arduino Tx and Arduino Rx with Raspberry pi Tx and Raspberry pi Rx unless you will get an error while uploading your program.
Output:
Type your data (anything) in the minicom that will shown in arduino serial monitor
(i.e) YourRaspberry pi transmittes the data and your arduino receives the data via serial communication