0

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 or each sensor based on their address.

But I started wondering how can I replace 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.

My question is:
Is there a best practice when it comes to configuring DS18B20 sensors? I would like to setup my device. By setup, I mean that I would like to assign specific DS18B20 to specific relay.

I imagine it like so:

  1. take new DS18B20, connect it to 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. the unplug 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 (address was stored in EEPROM) so it is skipped, the second is assigned to relay 2 and address is stored in EEPROM.
  5. the same procedure is applied to the third and fourth sensor.
  6. if four sensors are connected (addresses are from EEPROM) then the device can run.
  7. when the user wants to replace the 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 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 Jan 18 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 – Jaromanda X Jan 18 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 Jan 22 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 Jan 22 at 7:51
0

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, privacy policy and cookie policy

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