Example Analysis of Serial Communication between Raspberry Pie and arduino
Editor to share with you a sample analysis of raspberry pie and arduino serial communication. I hope you will get something after reading this article. Let's discuss it together.
Serial communication between raspberry pie and arduino
Step 1: first set the hardware serial port to assign to the GPIO serial port
Enter the sudo raspi-config command to enter the raspberry pie system configuration interface and select the third Interfacing Options
Go in and choose Serial Port.
Then choose to turn off the serial login function and turn on the hardware serial port debugging function.
Modify the configuration file
Open the / boot/config.txt file, type sudo vim / boot/config.txt, and add two lines at the end: 1, dtoverlay=pi3-miniuart-bt 2, force_turbo=1
Then restart the raspberry pie, ls / dev-al to check the serial port
Serial port 0 corresponds to ttyAMA0 and serial port 1 corresponds to ttyS0, which means that the hardware serial port can communicate with others through GPIO at this time.
Step 2: write a test program
Raspberry pie:
Import serial import time port = "/ dev/ttyAMA0" ser = serial.Serial (port,115200,timeout=1) # / / Open the serial port and connect to ser.flushInput () # / / clear the input buffer while True: # ser.write ("7" .encode () ser.write ("s" .encode ()) Size = ser.inWaiting () # get the buffer character if size! = 0: response = ser.read (size); print (response) time.sleep (3)
Arduino:
Void setup () {Serial.begin (115200); / / define baud rate} void loop () {while (Serial.available () > 0) {char teststring = Serial.read (); Serial.println (teststring); if ('s'= = teststring) Serial.println ("Hello Raspberry,I am Arduino.");}}
Screenshots of test results:
The wiring is, R for raspberry pie to T for Arduino, T for raspberry pie to R for Arduino, and then connect to GND.
There are also programs in C language, so I don't bother to sort them out.
After reading this article, I believe you have a certain understanding of "sample analysis of raspberry pie and arduino serial communication". If you want to know more about it, welcome to follow the industry information channel, thank you for reading!