RS485 Between Arduino and Raspberry Pi

by lazygarden in Circuits > Raspberry Pi

12593 Views, 7 Favorites, 0 Comments

RS485 Between Arduino and Raspberry Pi

rs485_raspberry.jpg

For school I need to make a project. I choose to make a smart greenhouse controller completely controlled by a raspberry pi. The sensors will be powered by an arduino uno. During the months that follow I will post the making of this project step by step on instructables so you too can make it.
I needed serial communication that can be used for longer distances. RS485 is perfect for this. RS485 supports speeds upto 10 Mbit/s and 1200 meter distance. Depending on the cable length you need to lower the speed you're sending. Look at this table to know the maximum speed per distance. To read and set the values on the RS485 slave I will use the python language.

Needed

Parts:

Installing the Needed Software Onto the Raspbian

I won't discuss howto install raspbian onto your raspberry. There are already some instructables describing this.
Instead I'm going to describe howto install the needed software.

First update your raspberry:

apt update

Then install pip:

apt-get install python3-pip

Click enter for Yes

Then install minimalmodbus:

pip3 install -U minimalmodbus

Wiring the Arduino

rs485.JPG

In the image above you can see how to wire the arduino to the RS485 interface. The second RS485 represents a USB to RS485 adapter.

Coding the Arduino

First import this library via sketch, use library and add zip library.
Then upload the sketch that I included as an attachment. This is the code for the arduino slave node that makes it possible to control the onboard led on pin 13 of the arduino.

Downloads

Programming RS485 on the Raspberry

Now we are going to code the raspberry pi as a master.

  • Open a terminal on your raspberry pi.
  • Create a new file modbus.py

vi modbus.py

  • type i for insert
  • paste in the code in the file
  • press the escape key
  • type :wq
  • press the enter key

Downloads

Testing the Script

raspberry.JPG
Type in the command: python3 modbus.py Now give in 1 or 0 and you will see the led on the arduino go on and off.

Conclusion

This was the first step in order to make my complete greenhouse controller. Via RS485 I can turn on my valves and read the sensorvalues. I hope you enjoy this instructable.

For those who speak dutch you can follow my project overhere. When the project is finished I will make an extended instructable of my complete project