Projects / RS-485 multi-drop sensor network

RS-485 multi-drop sensor network

Intermediate3 hGuided buildReviewed

Build it step by step

0 of 8 done
  1. NANORS485DHT2221.5°C

    Wire one slave

    Connect a Nano, MAX485 module and DHT22 and upload the slave sketch with address 1.

  2. NANO

    Wire the master

    Connect the master Nano and module and upload the master sketch.

  3. NANO

    Join A to A and B to B

    Use a twisted pair and a shared ground wire.

  4. NANO

    Read node 1

    The serial monitor shows temperature and humidity with a checksum check.

  5. NANO

    Add node 2

    Upload the slave sketch with address 2 and join it to the same pair.

  6. NANO

    Add terminators

    Fit one at the master and one at the last slave.

  7. NANO

    Lengthen the cable

    Try longer runs and count bad checksums.

  8. NANO

    Cause an error

    Swap A and B on one node and see it fail, then fix it.

In short

How do you build an RS-485 multi-drop sensor network?

Three nodes share one twisted pair using RS-485, each answering when the master calls its address. You need Arduino Nano × 3, RS485 to TTL module (MAX485) × 3, DHT22 Sensor × 2, Resistor 10 kΩ × 2, Multi-core shielded cable 4-core and Resistor 100 Ω × 2. It takes 8 steps and about 180 minutes.

Level
Intermediate
Time
About 180 min
Parts
6

Parts list

Tick what you already own. It saves to My Garage, so “Build with what I have” knows.

Wiring

FromToWhy
Each MAX485 module VCCNano 5VPower
Each MAX485 module GNDNano GNDGround
Each module RONano D10Receive data
Each module DINano D11Transmit data
Each module DE and RE joinedNano D3High to transmit, low to listen
Master ASlave 1 ASlave 2 A, the A line of the twisted pair
Master BSlave 1 BSlave 2 B, the B line of the twisted pair
Terminator resistorA and B at the master, and A and B at the last slaveTwo terminators only, one at each end
All Nano GNDCable ground wireCommon reference
DHT22 data (slave 1 and 2)Nano D4 with 10 kΩ resistor to 5VTemperature and humidity
DHT22 VCC and GNDNano 5V and GNDPower

Code

Every node runs the same wiring; only the master has no DHT22. The master sends a request like ?1 followed by two hex characters (an XOR checksum), and only the slave with that address answers. Only one node ever talks at a time, which is why DE and RE are switched with D3. Use a twisted pair for A and B, with a terminator at each end only. On a bench with short wires the terminators matter little, but for 100 m they do. Sensor data is read with 9600 baud SoftwareSerial, which is slow but robust. Arduino IDE, board "Arduino Nano" (processor ATmega328P, or ATmega328P (Old Bootloader) for clones).

Runs on a virtual Arduino Nano in your browser, with sliders for the sensors. Happy with it? Verify and upload to your real board from the same page (Chrome or Edge on a computer).

// Master. Libraries: SoftwareSerial (built in).
#include <SoftwareSerial.h>

SoftwareSerial bus(10, 11);   // RX, TX
const byte DE_RE = 3;

byte xorSum(const String &s) {
  byte c = 0;
  for (unsigned i = 0; i < s.length(); i++) c ^= s[i];
  return c;
}

String ask(char addr) {
  String q = String("?") + addr;
  char h[3]; sprintf(h, "%02X", xorSum(q));
  q += h;
  digitalWrite(DE_RE, HIGH);            // transmit
  bus.print(q); bus.print('\n');
  delay(3);                             // let the last byte leave
  digitalWrite(DE_RE, LOW);             // listen
  String r = "";
  unsigned long t = millis();
  while (millis() - t < 300) {
    while (bus.available()) {
      char c = bus.read();
      if (c == '\n') return r;
      r += c;
    }
  }
  return "";                            // timeout
}

bool valid(const String &r) {
  int star = r.indexOf('*');
  if (star < 1 || r.length() != (unsigned)star + 3) return false;
  char h[3]; sprintf(h, "%02X", xorSum(r.substring(0, star)));
  return r.substring(star + 1) == String(h);
}

void setup() {
  Serial.begin(9600);
  bus.begin(9600);
  pinMode(DE_RE, OUTPUT);
  digitalWrite(DE_RE, LOW);
}

void loop() {
  for (char a = '1'; a <= '2'; a++) {
    String r = ask(a);
    if (r.length() == 0) { Serial.print("node "); Serial.print(a); Serial.println(" no answer"); }
    else if (!valid(r)) { Serial.print("node "); Serial.print(a); Serial.println(" bad checksum"); }
    else { Serial.print("node "); Serial.println(r); }
    delay(500);
  }
  delay(2000);
}

/* Slave. Upload to each slave with MY_ADDR set to '1' and '2'.
   Library: "DHT sensor library" by Adafruit.
#include <SoftwareSerial.h>
#include <DHT.h>

const char MY_ADDR = '1';
const byte DE_RE = 3;
SoftwareSerial bus(10, 11);
DHT dht(4, DHT22);
String line = "";

byte xorSum(const String &s) { byte c = 0; for (unsigned i = 0; i < s.length(); i++) c ^= s[i]; return c; }

void handle(const String &l) {
  if (l.length() != 4 || l[0] != '?' || l[1] != MY_ADDR) return;
  char h[3]; sprintf(h, "%02X", xorSum(l.substring(0, 2)));
  if (l.substring(2) != String(h)) return;
  float t = dht.readTemperature(), hu = dht.readHumidity();
  String body = String(MY_ADDR) + "," + String(t, 1) + "," + String(hu, 1);
  char k[3]; sprintf(k, "%02X", xorSum(body));
  digitalWrite(DE_RE, HIGH);
  bus.print(body); bus.print('*'); bus.print(k); bus.print('\n');
  delay(3);
  digitalWrite(DE_RE, LOW);
}

void setup() {
  bus.begin(9600);
  dht.begin();
  pinMode(DE_RE, OUTPUT);
  digitalWrite(DE_RE, LOW);
}

void loop() {
  while (bus.available()) {
    char c = bus.read();
    if (c == '\n') { handle(line); line = ""; }
    else if (line.length() < 12) line += c;
  }
}
*/

What the serial monitor should show

Open the serial monitor at the baud rate in the code. This is what our simulator prints for a virtual Arduino Nano with its sensors at their default values. Your numbers will follow your real readings, but the format should match.

node 1 no answer
node 2 no answer
node 1 no answer
node 2 no answer
node 1 no answer

Test checklist

What you should see at each check. If a reading is far off, stop and use the troubleshooting list.

CheckExpected
One slave connectedAnswers every poll
Two slavesEach answers only to its own address
Cable lengthened to tens of metresStill answers, with few or no checksum errors
A and B swappedNo answer from that node

If it does not work

No answer

Check DE and RE are tied to D3 and A and B are not swapped.

Bad checksum

Check both ends use the same baud rate and keep wires twisted.

Two nodes answer together

Check each slave has a different address.

DHT reads nan

Check the 10 kΩ pull-up on its data line.

Still stuck? Find the fault step by step with your multimeter →

Ask Lark AI

Common questions

What parts do I need for the RS-485 multi-drop sensor network?

Arduino Nano × 3, RS485 to TTL module (MAX485) × 3, DHT22 Sensor × 2, Resistor 10 kΩ × 2, Multi-core shielded cable 4-core and Resistor 100 Ω × 2. Resistor 100 Ω × 2: Use 120 Ω terminators if you have them, 100 Ω works for a short bench test.

How long does the RS-485 multi-drop sensor network take to build?

About 180 minutes for a intermediate maker, in 8 steps.

How do I know the RS-485 multi-drop sensor network works?

One slave connected: Answers every poll. Two slaves: Each answers only to its own address. Cable lengthened to tens of metres: Still answers, with few or no checksum errors.

No answer: what should I check?

Check DE and RE are tied to D3 and A and B are not swapped.

Bad checksum: what should I check?

Check both ends use the same baud rate and keep wires twisted.

Two nodes answer together: what should I check?

Check each slave has a different address.

DHT reads nan: what should I check?

Check the 10 kΩ pull-up on its data line.