Skip to content Skip to sidebar Skip to footer

Read Ardunio Output to Raspberry Pi Input

Choosing a communication protocol for communication between microcontrollers and peripheral devices is an important part of embedded system. It is important considering the overall performance of any embedded application depends on communication means as it is related to toll reduction, faster data transfer, long distance coverage etc.

So far, we take seen RS485 Serial Communication betwixt Arduino Uno and Arduino Nano, today in this nosotros will see RS-485 advice between a Raspberry Pi and Arduino UNO.

RS485 Serial Advice Protocol

RS-485 is an asynchronous series communication protocol which doesn't not require clock. It uses a technique called differential bespeak to transfer binary data from one device to another.

So what is differential signal transfer method?

Differential signal method works past creating a differential voltage by using a positive and negative 5V. It provides a Half-Duplex advice when using two wires and Full-Duplex requires 4 fours wires.

In this tutorial the bending of the servo motor connected with Arduino UNO is controlled by sending angle values from Raspberry Pi to Arduino UNO through RS-485 Serial advice. Raspberry Pi is used every bit Master and the Arduino UNO with servo motor is used as slave. Too it has a LCD 16x2 display to evidence the angle value that is received from Raspberry Pi.

Components Required

  • Raspberry Pi three B+ (With Raspbian OS installed)
  • Arduino UNO
  • MAX485 TTL to RS485 Converter Module (2)
  • SG-90 Servo Motor
  • 16x2 LCD
  • 10K Potentiometer
  • Breadstuff Board
  • Connecting Wires

Pin-Out & Features of MAX-485 TTL to RS-485 converter module

Pinout of RS-485

Pin Proper name Pin Clarification
VCC 5V
A Not-inverting Receiver Input Not-Inverting Commuter Output
B Inverting Receiver Input Inverting Driver Output
GND GND (0V)
R0 Receiver Out (RX pin)
RE Receiver Output (LOW-Enable)
DE Driver Output (Loftier-Enable)
DI Driver Input (TX pin)

MAX-485 TTL to RS-485 converter module has following features:

  • Operating voltage: 5V
  • On-board MAX485 bit
  • A low power consumption for the RS485 communication
  • Slew-rate limited transceiver
  • 5.08mm pitch 2P final
  • User-friendly RS-485 communication wiring
  • Board size: 44 10 14mm
  • It allows serial communication over long distance of 1200 meters

Connecting RS-485 Module with Raspberry Pi 3 B+

To connect the MAX485 TTL to RS-485 Converted Module to Raspberry Pi the following UART pins of Pi is used (GPIO14, GPIO15).

Raspberry Pi UART Pins

Connecting RS-485 Module with Arduino UNO

To connect the MAX485 TTL to RS-485 Converter Module to Arduino UNO the post-obit UART pins of UNO is used (0,ane).

Circuit Diagram for RS-485 Serial Communication between Raspberry Pi and Arduino UNO

Circuit Connection betwixt one RS-485 and Raspberry Pi 3 B+ (Chief):

RS-485

Raspberry Pi 3 B+

DI

GPIO14 (TX)

DE

RE

GPIO4

R0

GPIO15(RX)

VCC

5V

GND

GND

A

To A of Slave RS-485

B

To B of Slave RS-485

Circuit Connection between 1 RS-485 and Arduino UNO (Slave):

RS-485

Arduino UNO

DI

1 (TX)

DE

RE

ii

R0

0 (RX)

VCC

5V

GND

GND

A

To A of Master RS-485

B

To B of Principal RS-485

Arduino UNO with Servo Motor SG-90:

Servo Motor (SG-90)

Arduino UNO

Ruddy

+5V

Orangish (PWM)

3

Brown

GND

Circuit Connection betwixt a 16x2 LCD and Arduino UNO:

16x2 LCD

Arduino UNO

VSS

GND

VDD

+5V

V0

To potentiometer centre pivot for contrast control of LCD

RS

viii

RW

GND

E

9

D4

10

D5

11

D6

12

D7

thirteen

A

+5V

K

GND

This finishes all the necessary circuit connections between all components. Now start programming the Raspberry Pi and Arduino UNO with the Principal and Slave code.

Programming Raspberry Pi as Master using Python

In the Master Raspberry Pi, the angle value of range (0,ten,45,90,135,180,135,90,45,10,0) is sent to the RS-485 module via serial port of Pi that sends value to the Arduino UNO and controls the servo motor according to that. So, for using Series port in Raspberry Pi the UART Serial port must be enabled.

Enabling the UART (Series Port) pins in Raspberry Pi: only bold

Earlier using UART pins in Raspberry Pi, it needs to be enabled. Follow the steps below to enable the UART (Serial) Pins in Raspberry Pi board.

ane. Open up a concluding and type sudo raspi-config

2. Select Interfacing options

Enabling UART pins in Raspberry Pi

3. And then select serial

Select Serial Communication in Raspberry Pi

four. Then click on 'No' (This is used to disable Linux UART panel)

Disable Linux UART Console in Raspberry Pi

five. After that exit the raspi-config

half-dozen. Reboot the Pi

Now Serial port is gear up to be used.

IMPORTANT: Before writing values to the RS-485 module the pins DE & RE must exist fabricated Loftier.

And then, let's see in detail virtually Python coding at master side now.

Initially, all the libraries are imported for peripherals used. The libraries which are of import hither are time, series(for serial communication), GPIO for accessing GPIO and slumber.

          import time          import serial          import RPi.GPIO as GPIO          from time import sleep        

Below, GPIO.Board pick specifies that yous are referring to the pins by the number of pin in the board.

          GPIO.setmode(GPIO.Board)        

The GPIO pin number 7 on the Raspberry Pi is fabricated HIGH because the pin 7 of Pi is connected to DE & RE of RS-485.It is fabricated Loftier because it makes RPi to send values to RS-485.

          GPIO.setup(seven, GPIO.OUT, initial=GPIO.High)        

Initiate the serial course at the pins GPIO14 & GPIO 15 (Serial0 Port) with various information like which serial port, baud rate, parity and stop $.25.

          ship = serial.Serial(                      port='/dev/serial0',                      baudrate = 9600,                      parity=series.PARITY_NONE,                      stopbits=serial.STOPBITS_ONE,                      bytesize=serial.EIGHTBITS,                      timeout=1          )        

The variable 'i' with array of angle values is defined, these values will be sent via serial communication.

          i = [0,10,45,ninety,135,180,135,90,45,10,0]        

The function send.write(str(x)) sends the values to serial port to the RS-485 one by one written inside the while loop as it executes continuously. The values are sent with a delay of one.5 seconds.

          while True:                      for x in i:                      ship.write(str(x))                      print(x)                      time.sleep(one.5)        

This finishes the code for Raspberry Pi which is interim every bit main in RS485 based serial advice.

Programming Arduino UNO (Slave)

At the Slave side which is Arduino UNO, the values are received from the Master. The servo motor connected to Arduino is rotated co-ordinate to the value received, and also the value is displayed in LCD display. So, in Arduino programming LCD brandish library and Servo motor library used.

IMPORTANT

As the Slave RS-485 of Arduino UNO receives value, the pins DE & RE must be made Low.

Arduino IDE is used for programming Arduino UNO.

But like for master we had several peripherals and included necessary libraries, similarly the slave side has peripherals such every bit servo motor and 16X2 LCD display, then start with including libraries for these peripherals.

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

Next the 16X2 LCD display pins that are to exist used with the Arduino UNO are divers and then the Servo object is likewise created.

          LiquidCrystal lcd(8,9,10,eleven,12,xiii);     // Define LCD display pins RS,East,D4,D5,D6,D7          Servo servo;        

Initially a display bulletin is displayed which tin be changed according to the project and then information technology is cleared for next message.

                      lcd.begin(16,2);                      lcd.impress("CIRCUIT Digest");                      lcd.setCursor(0,1);                      lcd.print("RS_485");                      filibuster(3000);                      lcd.clear();        

The serial communication is started at baud rate of 9600.

          Serial.begin(9600);                  

As Arduino RS-485 receives value from master, then the pin 2 of (EnablePin) is made Low to brand it in input manner and likewise to brand pin DE & RE of RS-485 LOW to read value from Master Raspberry Pi.

          digitalWrite(enablePin, LOW);                  

The Servo Motor PWM pin is continued to the Arduino PWM pin 3.

                      servo.attach(3);                  

The while loop executes when there is a value bachelor at serial port where RS485 module is continued.

The Series.paseInt() function is used to receive the integer value (Angle) from series port that is sent from Raspberry Pi

          int angle = Serial.parseInt();                  

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

          servo.write(angle);                  

And finally, the angle value is displayed in LCD display using the corresponding LCD functions.

                      lcd.setCursor(0,0);                      lcd.print("Angle From RPi ");                      lcd.setCursor(0,ane);                      lcd.print(angle);        

This finishes the slave side programming. As well with uploading the codes in Raspberry Pi ad Arduino UNO, both the controllers are fix for a working demo. Complete lawmaking with a Demo Video is given at the end of this tutorial.

Testing the RS 485 Serial advice with Raspberry Pi and Arduino UNO

When excursion connections are complete and code is uploaded to Arduino UNO, then utilize terminal to run the python code in Raspberry Pi. The Bending value is sent from Raspberry Pi to Arduino Uno to control the Servo Motor angle via RS-485 Serial Advice.

1. At Bending: 0

Pi to Arduino to Control Servo angle to 0 via RS-485 Serial Communication

2. At Bending: 90

Pi to Arduino to Control Servo angle to 90 via RS-485 Serial Communication

3. At Angle:135

Pi to Arduino to Control Servo angle to 135 via RS-485 Serial Communication

4. At Angle:180

Pi to Arduino to Control Servo angle to 180 via RS-485 Serial Communication

This finishes the complete tutorial on RS485 serial advice using Raspberry Pi. If you have any dubiousness or suggestions then please comment below.

Code

Primary Raspberry Pi Code:

import time
import series
import RPi.GPIO as GPIO
from time import slumber

GPIO.setwarnings(False)
GPIO.setmode(GPIO.Lath)
GPIO.setup(seven, GPIO.OUT, initial=GPIO.HIGH)

send = serial.Serial(
port='/dev/serial0',
baudrate = 9600,
parity=serial.PARITY_NONE,
stopbits=serial.STOPBITS_ONE,
bytesize=series.EIGHTBITS,
timeout=ane
)

i = [0,10,45,ninety,135,180,135,ninety,45,10,0]

while Truthful:
for x in i:
transport.write(str(ten))
print(x)
time.sleep(1.5)

Slave Arduino Code:

#include <LiquidCrystal.h>              //Include LCD library for using LCD display functions
#include <Servo.h>                //For using Servo functions
int enablePin = two;

LiquidCrystal lcd(8,9,x,xi,12,thirteen);      // Define LCD display pins RS,E,D4,D5,D6,D7

Servo servo;

void setup()
{
lcd.begin(sixteen,2);
lcd.print("CIRCUIT DIGEST");
lcd.setCursor(0,1);
lcd.print("RS_485");
delay(3000);
lcd.clear();
Series.begin(9600);                   // initialize serial at baudrate 9600:
pinMode(enablePin, OUTPUT);
filibuster(10);
digitalWrite(enablePin, LOW);        //  (Pin 2 always Low to receive value from Master)
servo.adhere(3);                     //  (Servo PWM pivot connected to Pin 3 PWM pin of Arduino)
}

void loop()

{
while (Serial.available())                   //While have data at Serial port this loop executes
{

        lcd.clear();
int angle = Series.parseInt();            //Receive INTEGER value from Master throught RS-485
servo.write(angle);                       //Write received value to Servo PWM pin (Setting Angle)
lcd.setCursor(0,0);
lcd.print("Angle From RPi ");
lcd.setCursor(0,1);
lcd.print(angle);                        //Displays the Angle value

    }
}

legersuation.blogspot.com

Source: https://circuitdigest.com/microcontroller-projects/rs485-serial-communication-between-arduino-and-raspberry-pi

إرسال تعليق for "Read Ardunio Output to Raspberry Pi Input"