I could use some help reading a soil moisture sensor with RS485 output (not modbus). I bought a DFrobot DFR0259 rs485 shield from you but am having trouble making heads or tails of their wiki and figuring out how to communicate with the sensor.
The sensor communicates with ASCII characters using parameters “9600 8N1 (9600baud, 8 databits, no parity, 1 stopbit)” in half duplex.
Basically I need to send a ready request string like “OP 1\r”, receive a response “OK\r” then send a string like “GN\r” and receive the sensor reading like “009000\r” where “\r” is the CR or 0x0D, if I understand correctly.
The board I want to use is based on an Arduino Zero and has two hardware serial ports although I think they are used for the USB to the IDE and to the LoRaWan, so probably would need to use software serial. I also could use a LoPy that I have but I’m less comfortable with python. Also willing to buy whatever hardware I might need since time is ticking on my project.
So I’m guessing what I need is to set up digital pins on the board for RX and TX, send a character string through the shield to the sensor and read the response into a string variable. Is that correct? Any idea how to program that or any hints on things to read to help me figure it out? Thanks.
The DFRobot rs485 hat is designed to sit on top of an Arduino Uno, and basically connects the serial port of the Uno to an rs485 driver on the hat.
The switches on the hat connect/disconnect the serial port of the Uno to the rs485 driver chip to allow the Uno to be programmed via standard USB/Uart methods. Write a program to have the Uno communicate using it’s serial port, and then change the switch to take the hat out of program mode, and the serial port now goes to the rs485 converter.
Pin 2 of the Uno is connected to the Enable pin of the rs485 driver chip via the 2nd switch.
Hope this helps.
Thanks, sounds like the rs485 hat could work but I’m trying to figure out how to monitor the sensor readings as I’m testing my programming with the usb serial monitor disconnected. Is there a way to do that? I was hoping to get the sensor reading working before trying to send them to The Things Network with LoRaWan.
Your original post stated the board you are using is based on the Arduino MKRzero. The MKRzero only has one serial port.
The SAMD21 chip at the heart of the MKRzero has six SERCOM units, but I don’t know how your board has configured these units (Uart / I2C / SPI etc.)
If you’re not using the Uno, why use a Uno hat? Why not use something like this:
Can you post a link to the actual board in use?
To monitor the readings, try some of the following:
- Log the info to an SD card or datalogger.
- Display the info on a LED display
Thanks for the input,
The board is a Seeeduino LoRaWan, which has the same processor and many of the same features as the Arduino Zero (not the MKRzero) and the same headers as the Uno. I believe the 2nd serial is used for the LoRa radio. It has many features I need but hasn’t been the easiest to work with.
I can send the output through the LoRaWAN and I’ll look into an LED display.
So my understanding is that, to use this shield I just send and receive through the Serial port. I guess I will also look for other options to communicate with the sensor.
My bad on the mkr. You’re right, it’s just zero.
To use the USB UART (which I hope is connected to Uno rx/tx pins) specify
and not serial in the sketch and use the switches on the hat appropriately.
And I also think the 2nd serial port is dedicated to the LoraWAN.
Also something for you to investigate.
The SeeedUino board has a number of Grove connectors. What are the capabilities of these? Can they be configured as a hardware serial port? Even allowing for I2C, SPI, and two serial ports, there are still a couple of unused SERCOM units on the SAMD21.
It looks like the Grove connectors just pull the existing I/O to standard connectors and don’t really add functionality.
I have an LCD display hooked up through I2C but am still having trouble understanding how to send and receive strings through the RS485 board. Not finding their wiki very helpful. I’m guessing that I need to set the transceiver mode switch to automatic for half-duplex.
Do I then just use serial.write and serial.read for the I/O? Do I need to read the input character by character, or is there a way to read the whole string in at once?
I appreciate your help.
An update on my progress, or lack of progress. (kind of long).
I have tried to simplify things by putting the shield on a genuine Arduino Uno to avoid anything odd that may result from using the LoRaWAN board.
I have worked with the two example programs from the Dfrobot wiki page. They did not work as written. These programs take input from the serial monitor send and send a reply back to the serial monitor. I have to assume that strings are actually sent to the RS485 cable and appropriately mimic a reply coming back through the cable. I don’t have a good way of checking that right now (no oscilloscope). There appears to be a flicker in voltage between the RS485 ground and the communication pins if I use my multimeter, but it is too fast to read. Interesting to note that this seems to mean that when the switch on the shield is flipped to operation mode, the serial monitor on the computer still is in operation and the signals are sent to both it and the RS485.
I figured out that the difference between “automatic” and “manual” mode is that in manual mode a pin is used to switch between send and receive but automatic mode switches based on signal timing in a way that isn’t clear to me. In any case it is not working as written but I managed to get both working with some changes.
The automatic mode program works as written if I have the transceiver mode switch in manual position, not the automatic position. In the automatic position, text I enter on the serial monitor causes an led to flash on the Arduino Uno but does not toggle the led on the shield and the shield does not send a reply.
To make the manual mode demo program work I had to swap the setting of the enable pin. The program has the pin set to HIGH for the sending state and LOW for receiving. It only works for me if the pin is LOW for sending and HIGH for receiving. I have no idea how the manufacturer would have got that wrong unless there is something wrong with my board.
I still can’t communicate with my sensor using the modified programs. So there seem to be 3 possibilities:
- My shield is not operating correctly - can someone at Core check to see if they get the same behavior with the demo programs?
- My sensor is not working correctly - it is brand new and I want to wait to rule out other possibilities before I try and possibly damage a different one
- My communications parameters are still messed up so the sensor is not reading my requests properly - I will continue to explore this but the only thing I can think of is to get second board or an RS234 to USB cable so I can send communications to my PC and see what is actually being sent. Maybe Core could try this, too.
I’m open to other ideas and really appreciate the help.