I'm relatively new to microcontroller programming, so please excuse me if my question is dumb. I've created a simple device that allows me to control 4 relays based on temperature. I bought four DS18B20 sensors, 4 relay board and Wemos D1. Everything works fine - I'm able to read the temperature of each sensor based on their address.

But I started wondering how I can replace a single DS18B20 without recompiling the code; currently this isn't possible. After searching a bit I found a couple of questions about the order of 1Wire sensors. For example, this one. I have no control over the order in which sensors are detected.

Is there a best practice when it comes to configuring DS18B20 sensors? I would like to set up my device. By set up, I mean that I would like to assign a specific DS18B20 to a specific relay.

I imagine it like so:

  1. Take a new DS18B20, connect it to a device and power the device.
  2. The device should scan for DS18B20 sensors. If it finds one then it should assign it to relay 1 and store its address in EEPROM.
  3. Then unplug the device from power, plug the second DS18B20 and power the device again.
  4. The device again scans for DS18B20 sensors. It finds 2 devices. The first one is known (the address was stored in EEPROM) so it is skipped. The second is assigned to relay 2 and the address is stored in EEPROM.
  5. The same procedure is applied to the third and fourth sensors.
  6. If four sensors are connected (the addresses are from EEPROM), then the device can run.
  7. When the user wants to replace a non-functioning sensor, the same procedure can be used.

As I wrote, I'm new to microcontroller programming, so if there is a better way please let me know. I'm mostly learning by following examples and tutorials, so if anyone knows of an example showing such a configuration procedure I'll be grateful for any materials.

  • How many ds18b20 sensor are you going to break? There is indeed no control over the order when you use byIndex. Every ds18b20 has a unique identifier, you can store that in eeprom. That is the only way to be sure to read the temperature from a specific sensor. You can do what you wrote, but you could also use a button to scan for a new sensor. The more complex you make the code, the more chance for a bug. It is easier when there is a display or a serial port or a webinterface.
    – Jot
    Commented Jan 18, 2019 at 21:16
  • personally, I'd go with a web interface for configuration - though, if it's easy enough to connect to a PC if you need to replace a DS18B20, then a simple serial interface would suffice Commented Jan 18, 2019 at 23:29
  • @Jot my idea is to build a device that I can give to my friends and put on GitHub so everyone can use. Idea is to remove step where the user must read DS18B20 addresses and put them into code. I want to use the same method to set up a new device or replace a broken sensor.
    – Misiu
    Commented Jan 22, 2019 at 7:50
  • @JaromandaX I'm planning to add web interface, but I was plannign to use it only to show readings and relays statuses. I wasn't thinking about using it to setup sensors. I must look into this :)
    – Misiu
    Commented Jan 22, 2019 at 7:51

1 Answer 1


If you can easily disconnect the sensors one at a time then your way is fine.

If you can't easily disconnect them then you can maybe use readings to set the order, although this depends on the environment. If, for example, all the sensors are generally room temperature then you have the code check the temp of all the sensors at start up. If it finds one sensor at less than 0C then assign that sensor to the least-recently-replaced relay index (also stored in EEPROM).

Then when you want to put a sensor into the working set, hold an ice cube on it for a few seconds before powering up the controller. There are other combinations of this technique you can imagine that use a button or jumper wires to specifically set the index of the out-of-range-at-startup sensor.

Alternately if you have an extra pin on the Arduino, then you can make a procedure where you connect a new sensor to that pin before attaching it to the string. You can make a rule like "if you ever see a sensor on pin X then remember its device ID and assign it to relay Y". There are lots of UI ways to pick Y. Note that you do not even need any extra hardware for this.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.