RS-485 serial communication between Arduino Uno and Raspberry Pi

The selection of the communication protocol used for communication between the microcontroller and the peripheral devices is an important part of the embedded system. This is important because the overall performance of any embedded application depends on the means of communication as it pertains to cost reduction, faster data transfer, long distance coverage, etc.
So far, we have written about RS485 serial communication between Arduino Uno and Arduino Nano . Today we will focus on RS-485 communication between Arduino UNO and Raspberry Pi.
RS485 serial communication protocol
RS-485 is an asynchronous serial communication protocol that does not require a clock signal. It uses a technique called differential signaling to transmit binary data from one device to another.
So what is the differential signal transmission method?
The differential signaling method works by creating a differential voltage using positive and negative 5V. It provides half-duplex communication when two wires are used, while full-duplex requires 4 wires.
In this project, the servo motor is connected with the Arduino UNO development board, and the Raspberry Pi sends the angle value to the Arduino UNO through RS-485 serial communication to control the angle of the motor. The Raspberry Pi is used as the host (Master), and the Arduino UNO and the servo motor are slaves. The Arduino development board is also connected with an LCD 1602 display to display the angle value received from the Raspberry Pi.
Required Components
● Raspberry Pi 3 B+ (with Raspbian OS installed)
● Arduino UNO development board
● MAX485 TTL to RS485 conversion module (2)
● SG-90 servo motor
● 1602 LCD
● 10K potentiometer
● Breadboard
● Connecting cable
MAX-485 TTL to RS-485 Converter Module Pinouts and Features

The MAX-485 TTL to RS-485 conversion module has the following features:
● Working voltage: 5V
● Onboard MAX485 chip
● RS485 communication has low power consumption
● 5.08mm pitch 2P terminal
● Convenient RS-485 communication wiring
● Board size: 44 x 14mm
● It allows serial communication over long distances up to 1200 meters
Connecting RS-485 Module with Raspberry Pi 3 B+
To connect the MAX485 TTL and RS-485 conversion module to the Raspberry Pi, you need to use the following UART pins of the Pi (GPIO14, GPIO15).

Connecting RS-485 Module with Arduino UNO
To connect the MAX485 TTL and RS-485 Converter Module to Arduino UNO, use the following UART pins (0, 1) of UNO.

Circuit connection between RS-485 module and Raspberry Pi 3 B+ (Master):
Circuit connection between RS-485 and Arduino UNO development board (slave):
This completes all the necessary circuit connections between all the components. Now start writing the Master and Slave code for Raspberry Pi and Arduino UNO.
Programming the Raspberry Pi as a Master using Python
In the host Raspberry Pi, the angle values (0, 10, 45, 90, 135, 180, 135, 90, 45, 10, 0) are sent to the RS-485 module through the serial port of Pi, and then the values are sent to the Arduino UNO and the servo motor is controlled according to the angle values. So, to use the serial port in Raspberry Pi, the UART serial port must be enabled.
Enable UART (Serial Port) pins in Raspberry Pi:
Before you use the UART pins in Raspberry Pi, you need to enable it. Follow the steps given below to enable UART (Serial) pins in Raspberry Pi.
1. Open a terminal and type sudo raspi-config
2. Select “Interfacing Options “

3. Then select the serial port

4. Then click “No” (this is used to disable the Linux UART console)

5. Then exit raspi-config
6. Reboot the Pi
Now you can use the serial port.
Important: Before writing values to the RS-485 module, pins DE and RE must be made high.
So, now let’s take a closer look at the Python coding on the master side.
Initially, all the libraries are imported for the peripherals used. The important libraries here are Time, Serial (for serial communication), GPIO for accessing GPIO and Sleep.
Below, the GPIO.BOARD option specifies that you are referring to the pin by the pin number in the board.
GPIO pin number 7 on the Raspberry Pi goes high because the Pi’s pin 7 is connected to the DE & RE of the RS-485. It goes high since it makes the RPi send a value to the RS-485.
Starts the serial class on pins GPIO14 and GPIO 15 (Serial 0 port) which contains various information like serial port, baud rate, parity and stop bits.
A variable ‘i’ is defined with an array of angle values which will be sent over serial communication.
The function send.write(str(x)) when executed continuously, writes the values of the serial port to RS-485 one by one inside the while loop. The values are sent with a delay of 1.5 seconds.
This completes the code for the Raspberry Pi, which acts as a master in RS485 based serial communication.
Programming Arduino UNO (Slave)
The slave side is Arduino UNO which receives the angle value from the master. The servo motor connected to the Arduino rotates according to the received value and the value is displayed on the LCD display. So, LCD display library and servo motor library are used in Arduino programming.
Use the Arduino IDE development environment to program the Arduino UNO.
Just like the master, we have several peripherals and include the necessary libraries, similarly, the slave side has peripherals like servo motors and 1602 LCD display, so first include the libraries for these peripherals.
Next, the 1602 LCD display pins used with Arduino UNO are defined and then the Servo object is also created.
A display message is initially shown, can be changed per item, and then cleared to show the next message.
Serial communication starts at baud rate 9600.
Since the Arduino RS-485 receives values from the host, pin 2 of (EnablePin) becomes low to make it in input mode and makes pins DE and RE of RS-485 low to read the values from the host Raspberry Pi .
The Servo Motor PWM pin is connected to Arduino PWM pin 3.
While the serial port to which the RS485 module is connected has a value, the while loop is executed.
The Serial.paseInt() function is used to receive an integer value (Angle) from the serial port sent by the Raspberry Pi
Write the received angle value to the servo motor to rotate the servo motor shaft from (0 to 180).
Finally, use the corresponding LCD function to display the angle value on the LCD display.
The above completes the programming of the slave side. In addition, by uploading the code in Raspberry Pi and Arduino UNO, both controllers can be demonstrated for working.
Testing RS485 Serial Communication with Raspberry Pi and Arduino UNO
When the circuit connections are complete and the code is uploaded to Arduino UNO, run the python code in Raspberry Pi using the terminal. The angle value is sent from Raspberry Pi to Arduino Uno to control the servo motor angle through RS-485 serial communication.
1. Angle: 0

2. Angle: 90

3. Angle: 135

4. Angle: 180

The above is the complete tutorial for RS485 serial communication using Raspberry Pi. If you have any questions or suggestions, please leave a comment below.
Code
The complete code used in this article is as follows:
main.zip




