Introduction: RS485 Between Arduino and Raspberry Pi
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.
Step 1: Needed
Parts:
- Raspberry PI (I use a 3B+)
- MAX485 module
- USB to RS485 interface
- some jumper wires
- arduino uno
Step 2: 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
Step 3: Wiring the Arduino
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.
Step 4: 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.
Step 5: 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
Attachments
Step 6: Testing the Script
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.
Step 7: 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

Participated in the
Raspberry Pi Contest 2020
7 Comments
Question 3 years ago on Step 7
Hi Lazy
I have plugged a max485 to an arduino Nano and i run your code. In the same time i have plugged another Max485 to a raspberrypi 4 (OS= Buster) and i run your Python code. The raspberrypi 4 and the Nano are therefore on a Modbus by A and B pins throught there Max485. The second Max 485 is linked to the Raspberrypi with Txd and Rxd pins without any USB to RS232 adapter. My serial device on my Pi 4 is ttyAMA0: i have tested the serial communication without any Max485 and it is ok. When I run your codes (Raspberrypi and Nano), i can, for example enter 1 in the raspberrypi console and i see the Led "on" on the Nano but nothing comes back into the RaspberryPi and i get this message:
File "..../minmalmodbus.py" , line 518, in write_register payloadformat = _PAYLOADFORMAT_REGISTER,
File "..../minmalmodbus.py" , line 1170, in _generic_command_ payload_from_slave = self._perform_command(functioncode, payload_to_slave)
File "..../minmalmodbus.py", line 1240, in _perform_command response=self._communicate(request,number_of_bytes_to_read)
File "..../minmalmodbus.py", line 1406, in _communicate raise NoResponseError: No communication with the instrument (no answer)
I have checked the minimalmodbus.py to those lines: i have changed in the def write _PAYLOADFORMAT_REGISTER in "register" as it is written at the beginning of the minimalmodbus.py file if you are using python 3 (which is my case) but nothing has changed.
I am working for an industrial project consisting of driving the antennas orientations in a telecommunicaton tower from a raspberrypi and the Nanos are the slaves.
Any idea please? I have been trying to solve this problem for a copple of days :-(
Thanks a lot.
Best regards.
Laurent
Reply 3 years ago
Laurent,
I haven't tried it with connecting directly to the raspberry pi. However the raspberry pi works at 3.3v did you use a level shifter?
kind regards
Reply 3 years ago
Lazy
I have plugged an usb (FTDi) to RS232 instead of using directely the Rxd and Txd RaspberryPi pins: same result: when i enter '1' with raspberrypi keyboard the led plugged to the Nano is 'ON' but no led state is coming back to RaspberryPi with the same messages i have posted yesterday. I cannot read anything from the nano slave: it raises an exception which interrupts the execution.
Reply 3 years ago
Can you quickly sketch me the cabling on the raspberry side? Because I really think you are trying to receive 5v in spite of 3.3v. If I don't see an error there I will get my raspberry from under the dust and try it myself without USB. An alternative fritzing will be nice to add to the instructable.
Reply 3 years ago
Hi
Here is the PCB for the first test ( test 1): with Txd and Rxd of the Raspi directly connected to the MAX485. I have ttyAMA0 in the python code and I have switched off ( config file and linux shell commands) the UART for the Bluetooth on RaspberryPi so that the UART is only for serial port: I have then tested the Serial port (Txd and Rxd connected and with a python code I have sent some bytes and I have received those bytes by Rxd pin) and it was ok. For the second test (test2), I have cut off Rxd and Txd wires from the pi and place a USB to RS232 adapter to the USB port of the raspberry and connect Txd and Rxd of the adapter to RI and Ro of the Max 485. In the python code I have replaced ttyAMA0 by ttyUSB0 but same issue!! Note that with the test 1 I have checked ( with an oscilloscope) signals Txd Raspberry, Rxd (Arduino Nano), TxD (Arduino Nano) A and B pins of the Modbus and RxD of the raspberry when I run the python code. Everything is ok except ( I see the frame) except on the Rxd pin of the raspberryPi: There are some glitches during a delay but not a clear signal: The Rxd pin of the nano is ok with right signal level and right frame. I can understand that the 3.3 volt is border line for electronics components on TTL level to understand it as a high level ('1') signal but the nano receives the led level.... I have ordered today a usb to max 485 adapter: I should get it on next monday. I 'm impatient to know what you get if you try the test 1.
Reply 3 years ago
I should add that I have also try to connect RE and DE (connected to gather) (from the MAX485) to a digital pin of the Raspberry and change there level ( GPIO Python code) before reading and writing but I get the same issue. I have even try to connect those DE and RE pin to 5 volt directly when the master (raspi) only write to avoid the 3.3 volts problem in comparison with TTL level: I have done those tests before writing to you the first time. By the way I do not understand why those pins are not connected to the master to change there level depending of the operation (writing or reading) which is done?? before using this raspberryPi in master I have use a Nano in master and another nano in slave and I declare those pins and there level in my master code. Best regards. Laurent
Reply 3 years ago
Hi Laurent
Did you solve your problem?
Best regards, Matheus