Dear [bernd2700].
I am new to this forum, so I only read the description of your problems in this thread today. Since they are similar to mine, which I successfully solved, I will attach here my machine-translated (my English is poor) post from another local forum. I think it will be bananas for many of the colleagues here, but it may also be helpful to you and many others.
Arduino - how I saved my unstable 1-Wire 1Wire OW OneWire
How did I achieve this ? In a very simple way: I divided my "network" into parts, connected each of them to a different GPIO pin of my Arduino and, of course, adjusted the code accordingly.
Simple right ? But it took me 2 years of constant trial and failure to arrive at this solution. A little about my installation: 1-Wire is used to collect temperature data from about 15 DS18B20 sensors located in various places in and around the house. It's a sizable installation with a weight of about 100m and a radius of about 20m (according to the terminology used in the studies below), working for several years, which became unstable after another expansion. Long-lasting, absurd or erroneous readings began to appear on some sensors (-273.15 or -127 or 85 *C and others, or lack thereof - depending also supposedly on the manufacturer of the sensor (original/clone)), generally leading eventually to the blockage of the entire bus. And worst of all: This phenomenon was unpredictable, occurred with varying frequency: from a few hours, to a few days, and only subsided after powering down the entire system for a few seconds (reset was not enough), so it didn't really yield to software support. First, of course, I started by replacing individual sensors - the ones that most often "died" on other copies, then I replaced the whole set of them, because this was the advice I read somewhere on the web (in addition to many other pieces of advice, boiling down to one: let go of the 1-wire, because you will not come to terms with this chimerical granddaddy anyway). It was only at this stage (like probably many) that I began to "study" the problems of 1-Wire, and I dug into the source study by Maxim-Dallas *1 (links at the end), which I recommend for study to anyone interested in this thread. From this study it became clear that the causes of the phenomenon could be various - I will not cite them here again referring to the source, and also that I had committed the "original sin", building a network in which the star topology prevails (this was the most convenient in my layout). At this stage, I began to maneuver the size, number, and position of pullUp resistors, as well as resistors that suppress individual lines. Some of these treatments seemed to improve the situation, but after a while - see the nature of the phenomenon above - they proved to be completely ineffective. The next move was to convert the topology of my network from the unrecommended star (3 arm), to the recommended line. The practical implementation of this was cumbersome and lengthy, and when it, too, finally failed to produce the desired result, I began to think about how to implement those online tips which said: let go ...
But here, too, problems arose. To make a long story short: Other digital sensors, such as BME/BMP280 or DHT (also sometimes unstable) and others fell off due to their excessive size or my requirement that they be airtight, and any analog sensors fell off after my experience with the LM35, which on longer lines (already on the order of 15 mb) did not work for me.
I was "on the dot." The full operation of my installation (about which at the end) depended on whether I personally could respond to an alert about a non-functioning temperature measurement system and turn off the power for a few seconds - disaster ! So I went back to reading the study *1. I paid attention to the thread described in the section Switched networks and the diagram:

I decided to test this practically. I divided my 1-Wire line again, this time into 2 parts and connected both of them to the 1-Wire GPIO pin via the NO and NC connectors of an ordinary electromagnetic relay in such a way that they alternated between being connected to the input and being read by the master (controller) in an appropriately selected cycle. Such a (primitively) switched circuit worked and functioned without failure for more than 2 weeks, which gave me the grounds to not to look any further for a defect in sensors, wires, connections, configurations, etc. The next step was to write code using the tried-and-true 1-wire and ds18b20 libraries in a way that would support more than one 1-wire line, connected to a correspondingly increased number of GPIO pins, freeing me from the need to use other hardware enhancements and specialized controllers (also described in item *1). The solution turned out to be the creation of a library instance (fortunately, because I had another, but far less "elegant" solution in reserve). I split my 1-Wire once again, restoring it to its original form, but now as 3 independent lines connected to 3 GPIO pins. This circuit has been working for more than 2 months now without any interference, so I consider the concept and its implementation to have worked 100%.
Finally, I will recommend another study (link *2) treating the construction of 1-Wire, which I found recently, and in which, in addition to taking up and confirming some of the theses contained in *1, develops the very important issue of wires for the construction of 1-Wire and the details of their use. And then there are hints from my own experience: I confirm the sensitivity of 1-Wire lines to foreign electromagnetic fields. Usually in this context one talks about higher-power electric motors. I would add that this also applies to devices that produce weaker fields, such as relays. I suggest that 1-Wire lines should be at least 10cm away from them and also that wires for controlling relays or other inductive loads should not be run in the same twisted pair or harness as the 1-Wire bus data lines.
I wish all those interested in adapting this solution to their needs,
and to the inquisitive one who will call out: "This is a dig about which Copernicus already wrote in his blog !" I will say: it's a pity you didn't tell me about it earlier, because after all ... it's a waste of life for idle activities,
and to the purists who will state that "After all, this is no longer a 1-Wire network !" I will say: It is, and even 3 and it does !, so ... pity the life for idle activities,
and to those who find that they lack additional GPIOs, I say: the Mega 2560 has more than 60 of them (which is usually enough for a really big project), and costs ~ as much as 2xUno, Nano, etc., so ... pity the life for idle activities.
The attached very simple and short code supports 3 independent 1-Wire lines connected to 3 (digital) GPIO pins of any Arduino. What's obvious, you have to change the unique addresses of DS18B20 sensors in it (also stored in arrays if you prefer it), in an arbitrary (and obvious) way you can change the number of 1-Wire lines and pins used to support them - as many pins as lines, as well as the number of sensors in each line.
/*
Many DS18B20 sensors on 3 lines 1-Wire connected to pins: 2, 11, 12.
What is obvious, you need to change the unique addresses of the DS18B20
sensors, in any (and obvious) way you can change the number of 1-Wire
lines and the pins to support them (as many pins as lines), as well as
the pin numbers and the number of sensors on each line.
by zbylan@vp.pl , january 2023
*/
#include <OneWire.h>
#include <DS18B20.h>
// pin declaration for individual instances/lines
// deklaracja pin dla poszczególnych instancji/linii
OneWire onewire1(2);
OneWire onewire2(11);
OneWire onewire3(12);
DS18B20 Line1(&onewire1);
DS18B20 Line2(&onewire2);
DS18B20 Line3(&onewire3);
// here necessarily put the addresses of your DS18B20 sensors
// tu koniecznie wstaw adresy swoich sensorów DS18B20
byte adrRoom[8] = {0x28, 0xBE, 0x0, 0x7, 0xD6, 0x1, 0x3C, 0x9D};
byte adrPool[8] = {0x28, 0xBA, 0xCC, 0x7, 0xD6, 0x1, 0x3C, 0x42};
byte adrBath[8] = {0x28, 0x96, 0xF, 0x96, 0xF0, 0x1, 0x3C, 0xCE};
float tempRoom;
float tempPool;
float tempBath;
void setup() {
while (!Serial);
Serial.begin(9600);
onewire1.reset_search();
onewire2.reset_search();
onewire3.reset_search();
// sensor resolution declarations may be different for each line
// deklaracje rozdzielczości sensorów mogą być różne dla poszczególnych linii
Line1.begin(12);
Line2.begin(12);
Line3.begin(12);
}
void loop() {
if (Line1.available() ) {
tempPool = Line1.readTemperature(adrPool);
tempBath = Line1.readTemperature(adrBath);
Line1.request();
}
if (Line2.available()) {
tempRoom = Line2.readTemperature(adrRoom);
Line2.request();
}
if (Line3.available()) {
// here read the temperature from the sensors on line 3
// tu odczytaj temperaturę z czujników na linii 3
Line3.request();
}
Serial.println(" ===============================================================");
Serial.print(" tempBath: ");
Serial.print(tempBath);
Serial.print(" tempRoom: ");
Serial.print(tempRoom);
Serial.print(" tempPool: ");
Serial.println(tempPool);
delay(500); // needed only for friendly observation of the serial monitor
// DS18B20 is read asynchronously (not dependent on loop circulation time)
}
Note: The code was checked in Arduino compiler v.1.6.11 and v.1.8.12 (which is unlikely to matter) and with OneWire.h libraries version 2.3.5 and DS18B20.h by Mathias Munk Hansen, which may matter. If you have verified that it works correctly in another environment - please: let me know about it.
Links to the studies cited in the text:
*1 Guidelines for Reliable Long Line 1-Wire Networks | Analog Devices
*2 https://ntronic.pl/jak-projektowac-niezawodna-siec-1wire/ (PL language version)
P.S. As an aside: My entire project referenced here is a smart home installation built on an Arduino Mega 2560 with such autonomous functions as: control of central heating of the building (weather, daily and weekly) with pool water reheating and control of the solar system used also for heating the pool - hence such a large number of temperature measurement points (DSs), in addition to more than 20 different sensors to control other quantities, states and parameters, as well as stepper motors and relays controlling the gas boiler, pumps, valve drives, etc..., also carrying out many other functions in safety, comfort, control and information.
Regards - zbylan
zbylan@vp.pl
Don't look for me on social media. I don't hang out there (... it's a waste of life for idle activities
).
Translated with DeepL Translate: The world's most accurate translator (free version)
2024.10.15
Regarding post #54 of September 2023 and subsequent posts, I must report a correction to my above post.
The cause of my sketch's compilation errors (three 1-wire lines on three processor pins) was the indication of the wrong library DS18B20.h
I don't know how this is possible, but my Arduino IDE points to DS18B20.h by Mathias Munk Hansen as the currently installed, while the compiler uses a different library that was previously installed on the system. This looks to me like some kind of library management error by the Aruino IDE.
By trial and error, I found the library DS18B20.h, with which my sketch compiles.
The relevant part of the above entry should therefore read as follows:
Note: The code was checked in the Arduino compiler v.1.6.11 and v.1.8.12 (which should rather not matter) and with the libraries OneWire.h version 2.3.5 and NOTE, because it definitely matters: DS18B20.h which is reported by the Arduino IDE as Unknown Version, which I obtained in a zip version (I don't remember the source), and which I can send you via e-mail (my e-mail address e-mail you will find at the end). If you have verified that it works correctly in another environment - please: let me know about it.
Regards zbylan@vp.pl