Aquarium Automation Multiple DS18B20

Hello all,

I need some advice please, I have a current system setup using multiple DS18B20s current about 20 but I’m increasing this to 45 shortly, a bit of an explanation. The 20 i have installed I’m identifying via unique ID but I don’t want to redo all this to add the extra ones so am looking for a better solution.

I have 15 aquariums and each will have 3 sensors I then average this reading and turn heaters on or off depending upon the settings in my node red web interface and all works well (except the cheap relay boards I’m using are failing so I’m redoing it all with solid state relays).

I have re written my code to setup a one wire sensor on each port like so…

#define ONE_WIRE_BUS_T1 22
DallasTemperature SENSORS_T1(&ONE_WIRE_T1);

#define ONE_WIRE_BUS_T2 23
DallasTemperature SENSORS_T2(&ONE_WIRE_T2);

My first question is should I just be using one instance of DallasTemperature Like so.

DallasTemperature SENSORS(&ONE_WIRE_T1);
DallasTemperature SENSORS(&ONE_WIRE_T2);

Should I have all the positive and negative common with the data cable in each pin, or should i have the data common to pin 4, the negative common and the positive in each pin? then I could turn each input on and off as needed? I’ve seen lots of options from existing posts where other people have had these issues.

Many thanks in advance and hope this made sense.

How did You connect the previous sensors? Why not use the same concept, strategy?

I connected them all in parallel but it meant sitting with each sensor and putting it into a glass of ice so I could monitor and log the correct ID then manually entering the ID into my logic on the node red dashboard, total pain and not easy to maintain.

Quoting: "not easy to maintain. "

Don't ask for "easy"! Ask for predictable, precision. That time is nothing compared to get bad readings, fault finding etc.

Yes I suppose your right it is predictable but extremely prone to human error. I would think looking at pre defined pin for the reading would be so much more reliable and modular? Using the Mega I would like to have 3 sensors per pin using 15 pins so 45 sensors, then 15 pins would be used to trigger the Solid State Relay to switch the heaters on or off, also one pin to switch the lighting on or off based on time.

Split the question into adopting sensors and what will be done as output.
Your problem looks more like a logistic problem then a technical.
Doing the same work multiple times calls for disciplin. No way around that I think. It's faster then any "gambling method".

I don't really have much issue doing the mundane task of logging the IDs its not that bad of a job, it just seems very redundant. I was just checking here to see if others had done something similar with success, i have a working system at the moment but will shortly be redoing it all when i change to solid state relays and adding more sensors so I'm still in the planning, my code may work and I may have no issues using it on a per pin basis. the other thought is as I add more tanks i could just plug into a new pin and be setup without the need to log IDs.

Then figure out a way to make Your system flexible, easy to expand. Create a "test sketch" to find addresses etc. Then just increase the variable "nr_of_sensors"..... having a code prepaired for that.

Thanks for your help, i will try various configs and see which work best.

Look for Random Nerd tutorial, there is an example using multiple DS18B20's in one string and importantly code to read the individual addresses for each sensor.

Use the OneWire address finder sketch and label each sensor.

// This sketch looks for 1-wire devices and
// prints their addresses (serial number) to
// the UART, in a format that is useful in Arduino sketches
// Tutorial: 

#include <OneWire.h>

OneWire  ds(3);  // Connect your 1-wire device to pin 3

void setup(void) {

void discoverOneWireDevices(void) {
  byte i;
  byte present = 0;
  byte data[12];
  byte addr[8];
  Serial.print("Looking for 1-Wire devices...\n\r");
  while( {
    Serial.print("\n\rFound \'1-Wire\' device with address:\n\r");
    for( i = 0; i < 8; i++) {
      if (addr[i] < 16) {
      Serial.print(addr[i], HEX);
      if (i < 7) {
        Serial.print(", ");
    if ( OneWire::crc8( addr, 7) != addr[7]) {
        Serial.print("CRC is not valid!\n");
  Serial.print("\n\r\n\rThat's it.\r\n");

void loop(void) {
  // nothing to see here

it meant sitting with each sensor and putting it into a glass of ice so I could monitor and log the correct ID

This sounds like you are just trying to make things hard. There must be a swag of address sniffer programmes that don't require you to put anything in ice. For all that, if you want to use sensors on a bus, you need to know which one is where. This is not difficult to do. Some sticky labels and a pencil can work wonders here.

Thanks all, I have visual program that shows me all the temps and the corresponding ID/address. The only way to know which is which while I have 20+ plugged in is to lower the temp on one. I just figured that if I could attach a sensor to a pin then it would make more sense. I could read the pin for the data. I'm fine with getting temperature by address which is what I'm doing now but the way I imagine it would make more sense. Will it not work? Every post on this thread seems to discourage it but why? Why mess with addresses when I could just reference a pin?

Get a Nano, program it with the DS18B20 ID reading code and connect the sensors ONE at a time and read their ID.
Write the ID on some tape and stick it to the lead.
Now you know where each sensor is, by looking at its position , and its ID by reading the tape label.

Simple, a Nano is cheap and good to have if you need to swap out a sensor, or extend your system.

Tom.... :slight_smile:

Yes sounds like a plan. The 20 i have so far are all labelled and such, it works I was just looking at redesigning it, I'm looking for someone to tell me why my alternative idea will or won't work, what is actually wrong on a design basis to put 3 sensors on a pin over 15 pins?

Yes sounds like a plan. The 20 i have so far are all labelled and such, it works I was just looking at redesigning it, I'm looking for someone to tell me why my alternative idea will or won't work, what is actually wrong on a design basis to put 3 sensors on a pin over 15 pins?

There's nothing fundamentally wrong with it, although if you put three sensors on a pin, you'll have to know the addresses of the sensors anyway. You could address them by index, but that will mean retesting if you ever need to replace a sensor.

The reason people are trying to discourage you is that the ability to put multiple sensors on a bus is generally seen as an advantage, but if you want to expend a larger number of pins, go ahead. It may be that you would have been forced to do that anyway to some extent - I'm not sure whether a 45 sensor arrangement would be reliable.

Putting three sensors on a pin fifteen times just makes you look like you are having two bob each way and don't know what you are doing. Further, you still have to check the address of every sensor, so for godsake don't show it to your mother. If you really insist on not going about the job in a sensible manner, you CAN throw that technology you paid good money for away to the wind, and go for sensors on individual pins.

If you are only using a single DS18B20, I think this is the better way to go.

Each pin corresponding to a tank, each tank has 3 sensors, I won't need to use addresses and a simple loop via device count will do.

So device count pin 5 (tank5) loop and read the 3 temps on that pin, combine then divide by device count. Then logic to decide if to trigger the relay and turn heater on.

I have it set up using addresses and it works, in my mind it would just be cleaner and with the length of runs I'm already using a pull up at each ds18b20.