Arduino halting when reading from 2* DS18B20

Hello i'm having the following issue.
My arduino halts (both the lcd and the serial monitor) when it reads the temperature from two DS18B20. When i read only the one, is stable as rock.When i connect the second one, after 2-3 loops it halts. Already tried a bunch of delays in random points.
My code:

#include <Wire.h> 
#include <LiquidCrystal_I2C.h>
#include <OneWire.h> 
#include <DallasTemperature.h>
#define LED_G  9  // The pin the LED is connected to
#define LED_Y  11  // The pin the LED is connected to
#define LED_R  10  // The pin the LED is connected to
#define ONE_WIRE_BUS 6
LiquidCrystal_I2C lcd(0x27, 16, 2);
  OneWire oneWire(ONE_WIRE_BUS); 
  DallasTemperature sensors(&oneWire);
int cr_flag=0;
int tflag=0;
void setup()
{
  Serial.begin(9600); 
    pinMode(LED_G, OUTPUT); // Declare the LED as an output
    pinMode(LED_Y, OUTPUT); // Declare the LED as an output
    pinMode(LED_R, OUTPUT); // Declare the LED as an output
	lcd.begin();
    for(int i=0; i<140; i++){
      analogWrite(LED_G, i);
      delay(2);
    }
  lcd.backlight();
	  lcd.clear();
    lcd.setCursor(1,0);
    lcd.print("RTelemetry");
    for(int i=0; i<255; i++){
      analogWrite(LED_Y, i);
      delay(2);
    }
    lcd.setCursor(1,1);
    lcd.print("Version 1.2");
    for(int i=0; i<255; i++){
      analogWrite(LED_R, i);
      delay(2);
    }
    for(int i=255; i>0; i--){
      analogWrite(LED_R, i);
      delay(2);
    }
    digitalWrite(LED_R,LOW);
    for(int i=255; i>0; i--){
      analogWrite(LED_Y, i);
      delay(2);
    }
    digitalWrite(LED_Y,LOW);
    for(int i=140; i>0; i--){
      analogWrite(LED_G, i);
      delay(2);
    }
    digitalWrite(LED_G,LOW);  
  sensors.begin(); 
  Serial.begin(9600);
}

void loop()
{

 if (cr_flag==0)
 {
  if (tflag==0)
  {
    lcd.clear();
    lcd.setCursor(2,0);
    lcd.print("TEMPERATURE");
    delay(1000);
    lcd.setCursor(6,1);
    lcd.print("MODE");
    delay(2000);
    tflag++;
    temperature();
  }
  else{
    temperature();
  }
 }
}





void temperature()
{ sensors.requestTemperatures();
  int t1= sensors.getTempCByIndex(0);
  delay(1200);
  int t2= sensors.getTempCByIndex(1);
  lcd.clear();
	lcd.setCursor(1,0);
	lcd.print("temp1");
  lcd.setCursor(8,0);
  lcd.print(t1);
  lcd.setCursor(1,1);
	lcd.print("temp2");
  lcd.setCursor(8,1);
  lcd.print(t2);
  if ((t1+t2)/2<25)
  {
     analogWrite(LED_G, (5-(25-(t1+t2)/2))*28.8);

  }
  else if ((t1+t2)/2>25|(t1+t2)/2<30 ){
    analogWrite(LED_G,144);
    analogWrite(LED_Y, (5-(30-(t1+t2)/2))*28.8);
  }
  delay(100); 
  Serial.println(t1);
    Serial.println(t2);
}

And my cables are kind like that:


Custom made PCB with two rows of 3 pins, common ground and +5V from arduino and common data wire with 4.7k resistor. Simple as that. What can i change?

I don't understand your wiring-diagram
looks fancy but is hard to understand.

Draw a schematic by freehand not a doublesided PCB-layout

I can see multiple 4,7 resistors.
You need only one single 4,7 resistor as pullup from data to +5V.

to narrow down the problem comment out all code related to the LCD
and just watch the serial monitor
Add serial debug-output to see where the hang really occurs

best regards Stefan

ok, as far as it seems, my code was wrong.
Currently this is my code:

`
#include <Wire.h> 
#include <LiquidCrystal_I2C.h>
#include <OneWire.h> 
#include <DallasTemperature.h>
#define LED_G  9  // The pin the LED is connected to
#define LED_Y  11  // The pin the LED is connected to
#define LED_R  10  // The pin the LED is connected to
#define ONE_WIRE_BUS 6
LiquidCrystal_I2C lcd(0x27, 16, 2);
  OneWire oneWire(ONE_WIRE_BUS); 
  DallasTemperature sensors(&oneWire);
int cr_flag=0;
int tflag=0;
void setup()
{
  Serial.begin(9600); 
    pinMode(LED_G, OUTPUT); // Declare the LED as an output
    pinMode(LED_Y, OUTPUT); // Declare the LED as an output
    pinMode(LED_R, OUTPUT); // Declare the LED as an output
	lcd.begin();
    for(int i=0; i<140; i++){
      analogWrite(LED_G, i);
      delay(2);
    }
  lcd.backlight();
	  lcd.clear();
    lcd.setCursor(1,0);
    lcd.print("RTelemetry");
    for(int i=0; i<255; i++){
      analogWrite(LED_Y, i);
      delay(2);
    }
    lcd.setCursor(1,1);
    lcd.print("Version 1.2");
    for(int i=0; i<255; i++){
      analogWrite(LED_R, i);
      delay(2);
    }
    for(int i=255; i>0; i--){
      analogWrite(LED_R, i);
      delay(2);
    }
    digitalWrite(LED_R,LOW);
    for(int i=255; i>0; i--){
      analogWrite(LED_Y, i);
      delay(2);
    }
    digitalWrite(LED_Y,LOW);
    for(int i=140; i>0; i--){
      analogWrite(LED_G, i);
      delay(2);
    }
    digitalWrite(LED_G,LOW);  
  sensors.begin(); 
  Serial.begin(9600);
}

void loop()
{

 if (cr_flag==0)
 {
  if (tflag==0)
  {
    lcd.clear();
    lcd.setCursor(2,0);
    lcd.print("TEMPERATURE");
    delay(1000);
    lcd.setCursor(6,1);
    lcd.print("MODE");
    delay(2000);
    tflag++;
    temperature();
  }
  else{
    temperature();
  }
 }
}





void temperature()
{ sensors.requestTemperatures();
  int t1= sensors.getTempCByIndex(0);
  delay(1200);
  int t2= sensors.getTempCByIndex(1);
  lcd.clear();
	lcd.setCursor(1,0);
	lcd.print("LEFT");
  lcd.setCursor(8,0);
  lcd.print(t1);
  lcd.setCursor(1,1);
	lcd.print("RIGHT");
  lcd.setCursor(8,1);
  lcd.print(t2);
  if ((t1+t2)/2<=20)
  {
     analogWrite(LED_G, 2);
      lcd.setCursor(11,0);
      lcd.print("COLD");
  }
  ///////////////////////////////////////////////////////////////////////
      if (((t1+t2)/2 >= 20) && ((t1+t2)/2 < 25)) 
  {
     analogWrite(LED_G, (5-(25-(t1+t2)/2))*28.8);
      lcd.setCursor(11,0);
      lcd.print("COLD!");
  }
 //////////////////////////////////////////////////////////////////////     
      if (((t1+t2)/2 >= 25) && ((t1+t2)/2 <= 30)) 
  {
     analogWrite(LED_G, 144);
     analogWrite(LED_Y, (5-(30-(t1+t2)/2))*51);
      lcd.setCursor(11,0);
      lcd.print("OK!");
  }
  /////////////////////////////////////////////////////////////////////
  
  delay(100); 
  Serial.println(t1);
    Serial.println(t2);
}
`

And arduino hangs at this time:

`      if (((t1+t2)/2 >= 20) && ((t1+t2)/2 < 25)) 
  {
     analogWrite(LED_G, (5-(25-(t1+t2)/2))*28.8);
      lcd.setCursor(11,0);
      lcd.print("COLD!");
  }`

at random times.
There has to be a bug somewhere there and i can't seem to find it.

The hardware is ok, it's working perfectly without this part of the code.

So it is the LCD screen with I2C bus that halts the sketch ?

My best guess is not the lcd but the if statement. There has to be a bug that i cannot find out.serial print is stuck also

adding more serial output
to see where the hang really happens
Do you understand now the principle of adding Serial-debug-output to narrow down
to the line of code that causes the crash?

Wild guess: Is your GND wire to the Green led shared in some way with the GND to the display ? Or is the power to the Green led (the PWM signal) next to the wires to the display ?

Will try it later today and post results, tks

You are correct, there is a 8 pin dupont connector in a pcb, that goes as follows:
1)+led G 2)+led Y,3)+led R,4)Not used <- first row
5)+Lcd, 6)RX,7)TX, 8)Common ground for all leds and lcd. <- second row.

I want to emphasize that it does it only on ifs that are more complex (contain &&). On simpler types (like the first if) it works stable as rock.

add debugoutput that print the parts and print combinations like shown above

best regards Stefan

Ok status update:
Current code:

#include <Wire.h> 
#include <LiquidCrystal_I2C.h>
#include <OneWire.h> 
#include <DallasTemperature.h>
#define LED_G  9  // The pin the LED is connected to
#define LED_Y  11  // The pin the LED is connected to
#define LED_R  10  // The pin the LED is connected to
#define ONE_WIRE_BUS 6
LiquidCrystal_I2C lcd(0x27, 16, 2);
  OneWire oneWire(ONE_WIRE_BUS); 
  DallasTemperature sensors(&oneWire);
int cr_flag=0;
int tflag=0;
void setup()
{
  Serial.begin(9600); 
    pinMode(LED_G, OUTPUT); // Declare the LED as an output
    pinMode(LED_Y, OUTPUT); // Declare the LED as an output
    pinMode(LED_R, OUTPUT); // Declare the LED as an output
	lcd.begin();
    for(int i=0; i<140; i++){
      analogWrite(LED_G, i);
      delay(2);
    }
  lcd.backlight();
	  lcd.clear();
    lcd.setCursor(1,0);
    lcd.print("RTelemetry");
    for(int i=0; i<255; i++){
      analogWrite(LED_Y, i);
      delay(2);
    }
    lcd.setCursor(1,1);
    lcd.print("Version 1.2");
    for(int i=0; i<255; i++){
      analogWrite(LED_R, i);
      delay(2);
    }
    for(int i=255; i>0; i--){
      analogWrite(LED_R, i);
      delay(2);
    }
    digitalWrite(LED_R,LOW);
    for(int i=255; i>0; i--){
      analogWrite(LED_Y, i);
      delay(2);
    }
    digitalWrite(LED_Y,LOW);
    for(int i=140; i>0; i--){
      analogWrite(LED_G, i);
      delay(2);
    }
    digitalWrite(LED_G,LOW);  
  sensors.begin(); 
  Serial.begin(9600);
}

void loop()
{

 if (cr_flag==0)
 {
  if (tflag==0)
  {
    lcd.clear();
    lcd.setCursor(2,0);
    lcd.print("TEMPERATURE");
    delay(1000);
    lcd.setCursor(6,1);
    lcd.print("MODE");
    delay(2000);
    tflag++;
    temperature();
  }
  else{
    temperature();
  }
 }
}





void temperature()
{ sensors.requestTemperatures();
Serial.println("1");
  int t1= sensors.getTempCByIndex(0);
  int t2= sensors.getTempCByIndex(1);
Serial.println("2");
  //lcd.clear();
	//lcd.setCursor(1,0);
	//lcd.print("LEFT");
 // lcd.setCursor(8,0);
 // lcd.print(t1);
 // lcd.setCursor(1,1);
//	lcd.print("RIGHT");
 // lcd.setCursor(8,1);
 // lcd.print(t2);
Serial.println("3");
  int t3=t1+t2;
Serial.println("4");
  if (t3/2<=30)
  {
     analogWrite(LED_G, 2);
     // lcd.setCursor(11,0);
     // lcd.print("COLD");
  }
Serial.println("5");
  ///////////////////////////////////////////////////////////////////////
if ((t3/2 >= 20) && (t3/2 <= 25)) 
  { 
    
     analogWrite(LED_G, (5-(25-t3/2))*28.8);
     Serial.println("5.1");
     // lcd.setCursor(11,0);
      Serial.println("5.2");
     // lcd.print("COLD!");
      Serial.println("5.3");
  }
Serial.println("6");
/////////////////////////////////////////////////////////////////     
if ((t3/2 >= 25) && (t3/2 <= 30)) 
  { 
    Serial.println("6.1");
     //analogWrite(LED_G, 140);
     analogWrite(LED_Y, (5-(30-t3/2))*51);
     Serial.println("6.2");
    //  lcd.setCursor(11,0);
      Serial.println("6.3");
     // lcd.print("COLD!");
      Serial.println("6.4");
  }
  /////////////////////////////////////////////////////////////////////
Serial.println("7");
  delay(100); 
  Serial.println(t1);
    Serial.println(t2);
Serial.println("8");
}

As it stands currently it's stable af. When i de-comment the lcd.** it bugs at serial print 2 or 4 so far. More testing needed, but as it stands now it bugs in the lcd, and only when it goes to second part:

///////////////////////////////////////////////////////////////////////
if ((t3/2 >= 20) && (t3/2 <= 25)) 
  { 
    
     analogWrite(LED_G, (5-(25-t3/2))*28.8);
     Serial.println("5.1");
     // lcd.setCursor(11,0);
      Serial.println("5.2");
     // lcd.print("COLD!");
      Serial.println("5.3");
  }
/////////////////////////////////////////////////////////////////     
if ((t3/2 >= 25) && (t3/2 <= 30)) 
  { 
    Serial.println("6.1");
     //analogWrite(LED_G, 140);
     analogWrite(LED_Y, (5-(30-t3/2))*51);
     Serial.println("6.2");
    //  lcd.setCursor(11,0);
      Serial.println("6.3");
     // lcd.print("COLD!");
      Serial.println("6.4");
  }

Also, it hangs after 2-4 loops,it's not standard.

Does "it#s not standard mean. You can have sometimes 2 loops working and on the third it hangs
and in other cases you have it 4 times working and then it hangs?

You should use more and more precise words.

Such short postings are the opposite of fast,
They slow down communication because your helpers have to ask back!

Sounds like a hang of the I2C-bus.
If a single logic-level-change LOW/HIGH on the I2C-bus does not occur in the correct manner this can make the I2C-bus hang infinitely.

If I remember right you can set a timeout for the I2C-bus
I have not used this option yet.

I'm sorry, English is my second language.

So, yes sometimes it hangs at 2 loops, some at first some at forth. Never after 5 or six loops though.
It's the only I2C device currently, as my sensors use onewire.

Currently my code is as follows:

#include <Wire.h> 
#include <LiquidCrystal_I2C.h>
#include <OneWire.h> 
#include <DallasTemperature.h>
#define LED_G  9  // The pin the LED is connected to
#define LED_Y  11  // The pin the LED is connected to
#define LED_R  10  // The pin the LED is connected to
#define ONE_WIRE_BUS 6
LiquidCrystal_I2C lcd(0x27, 16, 2);
  OneWire oneWire(ONE_WIRE_BUS); 
  DallasTemperature sensors(&oneWire);
int cr_flag=0;
int tflag=0;
void setup()
{
  Serial.begin(9600); 
    pinMode(LED_G, OUTPUT); // Declare the LED as an output
    pinMode(LED_Y, OUTPUT); // Declare the LED as an output
    pinMode(LED_R, OUTPUT); // Declare the LED as an output
	lcd.begin();
    for(int i=0; i<140; i++){
      analogWrite(LED_G, i);
      delay(2);
    }
  lcd.backlight();
	  lcd.clear();
    lcd.setCursor(1,0);
    lcd.print("RTelemetry");
    for(int i=0; i<255; i++){
      analogWrite(LED_Y, i);
      delay(2);
    }
    lcd.setCursor(1,1);
    lcd.print("Version 1.2");
    for(int i=0; i<255; i++){
      analogWrite(LED_R, i);
      delay(2);
    }
    for(int i=255; i>0; i--){
      analogWrite(LED_R, i);
      delay(2);
    }
    digitalWrite(LED_R,LOW);
    for(int i=255; i>0; i--){
      analogWrite(LED_Y, i);
      delay(2);
    }
    digitalWrite(LED_Y,LOW);
    for(int i=140; i>0; i--){
      analogWrite(LED_G, i);
      delay(2);
    }
    digitalWrite(LED_G,LOW);  
  sensors.begin(); 
  Serial.begin(9600);
}

void loop()
{

 if (cr_flag==0)
 {
  if (tflag==0)
  {
    lcd.clear();
    lcd.setCursor(2,0);
    lcd.print("TEMPERATURE");
    delay(1000);
    lcd.setCursor(6,1);
    lcd.print("MODE");
    delay(2000);
    tflag++;
    temperature();
  }
  else{
    temperature();
  }
 }
}





void temperature()
{ sensors.requestTemperatures();
Serial.println("1");
  int t1= sensors.getTempCByIndex(0);
  int t2= sensors.getTempCByIndex(1);
Serial.println("2");

Serial.println("3");
  int t3=t1+t2;
Serial.println("4");
  if (t3/2<=20)
  {
     analogWrite(LED_G, 2);
     // lcd.setCursor(11,0);
     // lcd.print("COLD");
  }
Serial.println("5");
  /////////////////////////////***COLD 1*****//////////////
if ((t3/2 >= 20) && (t3/2 <= 25)) 
  { 
    
     analogWrite(LED_G, (5-(25-t3/2))*28.8);
     Serial.println("5.1");
      Serial.println("5.2");
      Serial.println("5.3");
                    lcd.clear();
                  delay(20);
                  Serial.print(2.1);
                  lcd.setCursor(1,0);
                  delay(20);
                    Serial.print(2.2);
                  lcd.print("LEFT");
                  delay(20);
                    Serial.print(2.3);
                  lcd.setCursor(8,0);
                delay(20);
                  Serial.print(2.4);
                  lcd.print(t1);
                delay(20);
                  Serial.print(2.5);
                  lcd.setCursor(1,1);
                delay(20);
                  Serial.print(2.6);
                  lcd.print("RIGHT");
                delay(20);
                  Serial.print(2.7);
                  lcd.setCursor(8,1);
                delay(20);
                  Serial.print(2.8);
                  lcd.print(t2);
                delay(20);
                  Serial.print(2.9);
              lcd.setCursor(11,0);
                Serial.println("2.10");
              lcd.print("cold");
                Serial.println("2.11");

  }
Serial.println("6");
////////////////////////***OK*****////////////////////////////////////     
if ((t3/2 > 25) && (t3/2 <= 30)) 
  { 
    Serial.println("6.1");
     analogWrite(LED_G, 140);
     analogWrite(LED_Y, (5-(30-t3/2))*51);
     Serial.println("6.2");

                    lcd.clear();
                  delay(20);
                  Serial.print(0.1);
                  lcd.setCursor(1,0);
                  delay(20);
                    Serial.print(0.2);
                  lcd.print("LEFT");
                  delay(20);
                    Serial.print(0.3);
                  lcd.setCursor(8,0);
                delay(20);
                  Serial.print(0.4);
                  lcd.print(t1);
                delay(20);
                  Serial.print(0.5);
                  lcd.setCursor(1,1);
                delay(20);
                  Serial.print(0.6);
                  lcd.print("RIGHT");
                delay(20);
                  Serial.print(0.7);
                  lcd.setCursor(8,1);
                delay(20);
                  Serial.print(0.8);
                  lcd.print(t2);
                delay(20);
                  Serial.print(0.9);
              lcd.setCursor(11,0);
                Serial.println("0.10");
              lcd.print("ok");
                Serial.println("0.11");

  }
  /////////////////////////////////////////////////////////////////////
Serial.println("7");
  delay(100); 
  Serial.println(t1);
    Serial.println(t2);
Serial.println("8");
}

All the hangs occur in this area:

////////////////////////***OK*****////////////////////////////////////     
if ((t3/2 > 25) && (t3/2 <= 30)) 
  { 
    Serial.println("6.1");
     analogWrite(LED_G, 140);
     analogWrite(LED_Y, (5-(30-t3/2))*51);
     Serial.println("6.2");

                    lcd.clear();
                  delay(20);
                  Serial.print(0.1);
                  lcd.setCursor(1,0);
                  delay(20);
                    Serial.print(0.2);
                  lcd.print("LEFT");
                  delay(20);
                    Serial.print(0.3);
                  lcd.setCursor(8,0);
                delay(20);
                  Serial.print(0.4);
                  lcd.print(t1);
                delay(20);
                  Serial.print(0.5);
                  lcd.setCursor(1,1);
                delay(20);
                  Serial.print(0.6);
                  lcd.print("RIGHT");
                delay(20);
                  Serial.print(0.7);
                  lcd.setCursor(8,1);
                delay(20);
                  Serial.print(0.8);
                  lcd.print(t2);
                delay(20);
                  Serial.print(0.9);
              lcd.setCursor(11,0);
                Serial.println("0.10");
              lcd.print("ok");
                Serial.println("0.11");

  }
  /////////////////////////////////////////////////////////////////////

Thank you for your time.

I'm absolutely and total serious with this:
If you have difficulties to explain with precision in english use google-translate!"
I'm really serious!

Does only indicate the place where in the code the crash happends
You should modify the serial prints to show the values of the following command.

Maybe you are calculating strange values that or a array boundary violation is modifying RAM that should'nt be modified.

You have some serial prints but not yet in front of really ALL lcd-function calls
Only if you have them right before EACH and EVERY lcd-function-call you will be able to narrow down it to a single line of code or recognising it happends in different lines of code with no recognisable pattern and then this observation would speak for
bad I2C-bus-signals

Ok i get it, but i'm doing my best here mate.
I know that in this field precision is key.

Now for a new update.
It seems like this: analogWrite(LED_Y, (5-(30-t3/2))*51); creates issues.
When i comment it, there are no faults.
Also i tried with LED_R and i don't have any problems.
I'm thinking about hardware problem. Will post schema in a while

1 Like

Then me personal I would be interested in each and every detail of this calculation
(5-(30-t3/2))*51)

Serial.print("t3/2=");
Serial.println(t3/2);

Serial.print("-t3/2=");
Serial.println(-t3/2);

Serial.print("30-t3/2=");
Serial.println(30-t3/2);

Serial.print("5-(30-t3/2)=");
Serial.println(5-(30-t3/2) );

Serial.print("(5-(30-t3/2)*51=");
Serial.println( (5-(30-t3/2)*51) );

Serial.print("LED_Y=");
Serial.println(LED_Y);

This is just almost "dumb" typing-work to add the serial-debug-output

Can we look better into the hardware ?
Do you have a schematic of your project ? You may also draw it on a piece of paper and make a photo of it. Show all the power lines as well.
Where does the power of the LCD display and the leds come from ?
Would it be possible to connect the 4 wires of the LCD display directly to the Arduino board, and have those wires not connected to something else ?
How long are the wires to the LCD display ? Do you know that SDA should not be next to SCL ? I think you have to fix the I2C bus to the LCD display and probably add extra pullup resistors.

We should be talking about the I2C bus from the start, so we have some catching up to do.

Have you told us which Arduino board you use ?
I hope that you did not design your own ATmega328P board and forgot about decoupling capacitors.


There are two pcbs,one is like a shield for arduino (will post relevant schema later on) and this is the one with the lcd and the leds. Please dont mind the w_button, currently is empty. There is an utp cable with dupont connectors,around 1.5m, and currently i have remade twice the LCD pcb, tested all wires, no short circuits and no "power" lines come near close to sda and scl.There is common ground for everything, and lcd has standalone +5v straight from arduino

There are resistors for the leds

I'm sorry, but I have no clue.

Do you have an Arduino board ? Which one ?
Did you design a shield for the Arduino board ? With connectors ? Is the LCD display directly on the shield without wires and without a cable directly attached to a Arduino board ?
How can I see which pin of which Arduino board is used to power the LCD display ?

When you designed your own Arduino board and forgot about decoupling capacitors, then that is where to start. Someone on this forum replaced a cheap Arduino Mega clone with a genuine Arduino Mega, and that solved the problem.

i'm having an arduino uno r3 smd genuine.
I designed a pcb that fits on top, kind like a shield. No schematics for that currently.
I have an lcd with an I2C module, and i created another one pcb that connects with that module and also has places to add leds. The schematics for this pcb,are the ones i upload.