Uncategorized

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

RS-485 serial communication between Arduino Uno and Raspberry Pi

Pin Name
Pin Description
VCC
5V
A
Non-inverting receiver input, non-inverting driver output
B
Inverting receiver input, inverting driver output
GND
GND (0V)
R0
Receiver Output (RX Pin)
RE
Receiver output (active low)
DE
Driver output (high level enable)
DI
Driver Input (TX Pin)

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).

RS-485 serial communication between Arduino Uno and Raspberry Pi

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):

RS-485
Raspberry Pi 3 Model B+
DI
GPIO14 (TX)
DE
GPIO4
RE
R0
GPIO15 (RX)
VCC
5V
GND
GND
A
RS485 slave module A
B
RS485 slave module B

Circuit connection between RS-485 and Arduino UNO development board (slave):

RS-485
Arduino UNO
DI
1 (TX)
DE
2
RE
R0
0 (RX)
VCC
5V
GND
GND
A
RS485 host module A
B
RS485 host module B

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.

  1. import time
  2. import serial
  3. import RPi.GPIO as GPIO
  4. from time import sleep

Copy code

Below, the GPIO.BOARD option specifies that you are referring to the pin by the pin number in the board.

  1. GPIO.setmode(GPIO.BOARD)

Copy code

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.

  1. GPIO.setup(7, GPIO.OUT, initial=GPIO.HIGH)

Copy code

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.

  1. send = serial.Serial(
  2.     port=’/dev/serial0′,
  3.     baudrate = 9600,
  4.     parity=serial.PARITY_NONE,
  5.     stopbits=serial.STOPBITS_ONE,
  6.     bytesize=serial.EIGHTBITS,
  7.     timeout=1
  8. )

Copy code

A variable ‘i’ is defined with an array of angle values ​​​​which will be sent over serial communication.

  1. i = [0,10,45,90,135,180,135,90,45,10,0]

Copy code

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.

  1. while True:
  2. for x in i:
  3.      send.write(str(x))
  4.      print(x)
  5.      time.sleep(1.5)

Copy code

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.

  1. #include <LiquidCrystal.h>            
  2. #include <Servo.h>  

Copy code

Next, the 1602 LCD display pins used with Arduino UNO are defined and then the Servo object is also created.

  1. LiquidCrystal lcd(8,9,10,11,12,13); // Define LCD display pins RS,E,D4,D5,D6,D7
  2. Servo servo;

Copy code

A display message is initially shown, can be changed per item, and then cleared to show the next message.

  1.   lcd.begin(16,2);
  2.   lcd.print(“CIRCUIT DIGEST”);
  3.   lcd.setCursor(0,1);
  4.   lcd.print(“RS_485”);
  5.   delay(3000);
  6.   lcd.clear();

Copy code

Serial communication starts at baud rate 9600.

  1. Serial.begin(9600);     

Copy code

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 .

  1. digitalWrite(enablePin, LOW);      

Copy code

The Servo Motor PWM pin is connected to Arduino PWM pin 3.

  1. servo.attach(3);            

Copy code

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

  1. int angle = Serial.parseInt();

Copy code

Write the received angle value to the servo motor to rotate the servo motor shaft from (0 to 180).

  1. servo.write(angle);

Copy code

Finally, use the corresponding LCD function to display the angle value on the LCD display.

  1.   lcd.setCursor(0,0);
  2.         lcd.print(“Angle From RPi “);
  3.         lcd.setCursor(0,1);
  4.         lcd.print(angle);

Copy code

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

Back to top button
error: Content is protected !!