Hello. I have ordered 3 rs485 laser distance sensors that I would like to connect to an arduino Giga R1 and be able to do 2 functions from the arduino. First is to re-zero each sensor, and second is to request a distance reading from each sensor and receive that distance reading. I have looked at a lot of tutorials, but haven't come across any that use a sensor with 6 wires. Also the arduino Giga has 4 UARTS, but do I only need 2 serial ports when using addressable rs485 sensors?
The shields I have got are:
https://www.aliexpress.com/item/1005006007545162.html?spm=a2g0o.productlist.0.0.1a49mNAamNAaTw&mp=1
The sensor details are:
Brown DC12~24V
Black switch output 1 (Not sure where to connect this)
Pink MF (Not sure where to connect either)
Grey RS485(A+) (connect to Shield----> Arduino)
Shielded RS485(B-) (connect to Shield----> Arduino)
Blue 0V
The full specs are here: https://www.gangwhooscam.com/laser_sensor.pdf
And the code (using TX0 1 and RX0 0)
#include <ModbusMaster.h>
#define MAX485_DE 1
#define MAX485_RE_NEG 0
// instantiate ModbusMaster objects
ModbusMaster node1;
ModbusMaster node2;
ModbusMaster node3;
void preTransmission()
{
digitalWrite(MAX485_RE_NEG, 1);
digitalWrite(MAX485_DE, 1);
}
void postTransmission()
{
digitalWrite(MAX485_RE_NEG, 0);
digitalWrite(MAX485_DE, 0);
}
void setup()
{
pinMode(MAX485_RE_NEG, OUTPUT);
pinMode(MAX485_DE, OUTPUT);
// Init in receive mode
digitalWrite(MAX485_RE_NEG, 0);
digitalWrite(MAX485_DE, 0);
// Modbus communication at 115200 baud
Serial.begin(115200);
// Sensor1 Modbus slave ID 1
node1.begin(1, Serial);
node1.preTransmission(preTransmission);
node1.postTransmission(postTransmission);
}
// Sensor2 Modbus slave ID 2
node2.begin(2, Serial);
node2.preTransmission(preTransmission);
node2.postTransmission(postTransmission);
}
// Sensor3 Modbus slave ID 3
node3.begin(3, Serial);
node3.preTransmission(preTransmission);
node3.postTransmission(postTransmission);
}
bool state1 = true;
bool state2 = true;
bool state3 = true;
// Toggle the coils at address 0x0005 (Rezero)
node1.writeSingleCoil(0x0005, true);
node2.writeSingleCoil(0x0005, true);
node3.writeSingleCoil(0x0005, true);
void loop()
{
uint8_t result1;
uint8_t result2;
uint8_t result3;
// Toggle the coils at address 0x0002 (Distance request)
result1 = node.writeSingleCoil(0x0002, state1);
state1 = !state1;
result2 = node.writeSingleCoil(0x0002, state2);
state2 = !state2;
result3 = node.writeSingleCoil(0x0002, state3);
state3 = !state3;
// Read the results - I think this may be wrong
result1 = node1.readInputRegisters();
result2 = node2.readInputRegisters();
result3 = node3.readInputRegisters();
if (result1 == node1.ku8MBSuccess)
{
Serial.print("sensor1 value: ");
Serial.println(result1);
}
if (result2 == node2.ku8MBSuccess)
{
Serial.print("sensor2 value: ");
Serial.println(result2);
}
if (result3 == node3.ku8MBSuccess)
{
Serial.print("sensor3 value: ");
Serial.println(result3);
}
delay(1000);
}