Garbage value from temperature sensors

Hallo Everyone,
I am collecting temperature sensors data ,but whenever I start my serial monitor ,I get some initial value(garbage or previous values) and afterward i get values as per program ...
I have connected temperature sensor with one wire bus ..
Looking forward to help from you ..


/* Multiple DS18B20 1-Wire digital temperature sensors with Arduino example code. More info: https://www.makerguides.com */

// Include the required Arduino libraries:
#include <OneWire.h>
#include <DallasTemperature.h>


// Define to which pin of the Arduino the 1-Wire bus is connected:
#define ONE_WIRE_BUS 4

// Create a new instance of the oneWire class to communicate with any OneWire device:
OneWire oneWire(ONE_WIRE_BUS);

// Pass the oneWire reference to DallasTemperature library:
DallasTemperature sensors(&oneWire);

int deviceCount = 0;
float tempC;
float tempF;
bool label = true;
void setup() {
  // Begin serial communication at a baud rate of 9600:
  sensors.begin();
  Serial.begin(19200);
  // Start up the library:
  

  // Locate the devices on the bus:
  
  deviceCount = sensors.getDeviceCount();
 
}

void loop() {
  // Send the command for all devices on the bus to perform a temperature conversion:

  String sensor_name="sensor";
  while(label){
    for (int i = 0;  i < deviceCount;  i++) {
       String Sensor_Label= String(sensor_name + String(i));
      Serial.print(Sensor_Label);
      Serial.print(",");
      
      }
      Serial.println();
    label = false;
    }
  sensors.requestTemperatures();

  // Display temperature from each sensor
  for (int i = 0;  i < deviceCount;  i++) {
   
    tempC = sensors.getTempCByIndex(i);
    
    Serial.print(tempC);
    Serial.print(",");
    

  }

  Serial.println();
  delay(1);

}

and output on serial monitor look like below:

Hi, @roshan1
Welcome to the forum.

What model Arduino controller are you using?

Have you tried a different baud rate, like 115200?

Thanks.. Tom... :smiley: :+1: :coffee: :australia:

Thanks Tom..
I am using Arduino Mega R3 Board 2560 ..
I have tried with different baud rate also..

if the serial monitor is open and the OP does a rest do the "garbage values" show up immediately after a reboot?

Or has the thing been running with Serial Monitor closed, opens serial monitor and poof garbage values show up upon the serial monitor open?

Post a schematic, post an image of the project.

How many devices are connected?

What is the sensor?

and this tells me the entire code was not posted.

In a new message post the whole code.

-Actually when i turn off and turn on serial monitor , then I face problem of garbage value .
-Circuit is similar like this and update post for program

-There are three sensors are connected and resistor of 4.7k ohm is used
-I do not have exact model of temperature sensor but it suppose to be dallas sensor ,because it works with dallas library

What do you mean by garbage values?

Assuming a DB18B20 sensor, and 12bit resolution, you need to leave around 750ms between requesting a temperature conversion and reading the result.

You have no sensible timing control. Stick a delay(1000); at the start of the loop and see if things improve.

I assume the rest of your code is (reasonably) kosher, and the monitor baud rate matches, but this is not a good start.

There is no reason to suspect the wiring.

Garbage value or Previous values means:
Whenever I restart my serial monitor , it gives me previous value or sometime no value .
I have checked with by adding some delay in between requesting value but it is not working for me ...

No need to bother asking why you are so secretive, I guess, but "some" is a relative term, and not the same as enough. If you don't have enough, you deserve all the grief you are getting.

Also, just in case you are seeing it, 85 is not garbage, it is a reminder for you to read the book of instructions.

Can you show your code with the timing in it.

OK curiosity begged. This is where the code came from. I did fix a few small issues. I also noticed as pointed out:

// Begin serial communication at a baud rate of 9600:
  sensors.begin();
  Serial.begin(19200);

Yes, a bit strange. I did use 9600 and also 19200 with no difference. So yes a typo. Either data rate works. Using an Arduino UNO. I also had a problem with:
#define ONE_WIRE_BUS 4
With the UNO I had to change that to:
#define ONE_WIRE_BUS D4
I did not try the MEGA only the UNO.

I experienced the very same issue as the thread starter. Maybe a quirk with the serial monitor? Nope, I tried Tera Term and the old Windows Hyper Term, which runs fine on Windows 10. Three serial monitors all did the same exact issue. I also added additional delay and same result. Here is the last attempt.

#include <OneWire.h>
#include <DallasTemperature.h>


// Data wire is plugged into port 2 on the Arduino
#define ONE_WIRE_BUS 2


// Setup a oneWire instance to communicate with any OneWire devices
OneWire oneWire(ONE_WIRE_BUS);

#include <OneWire.h>
#include <DallasTemperature.h>

// Data wire is plugged into port 2 on the Arduino
#define ONE_WIRE_BUS D2

// Setup a oneWire instance to communicate with any OneWire devices
OneWire (ONE_WIRE_BUS);

// Pass our oneWire reference to Dallas Temperature.
DallasTemperature sensors(&oneWire);

// Addresses of 3 DS18B20s
uint8_t sensor1[8] = { 0x28, 0x9E, 0x8F, 0x49, 0xF6, 0xF5, 0x3C, 0x4C };
uint8_t sensor2[8] = { 0x28, 0x97, 0x79, 0x49, 0xF6, 0x1C, 0x3C, 0x27 };
uint8_t sensor3[8] = { 0x28, 0x37, 0x61, 0x49, 0xF6, 0xA6, 0x3C, 0xDC };

void setup(void)
{
  Serial.begin(9600);
  sensors.begin();
}

void loop(void)
{
  sensors.requestTemperatures();
  
  Serial.print("Sensor 1: ");
  printTemperature(sensor1);
  
  Serial.print("Sensor 2: ");
  printTemperature(sensor2);
  
  Serial.print("Sensor 3: ");
  printTemperature(sensor3);
  
  Serial.println();
  delay(1000);
}

void printTemperature(DeviceAddress deviceAddress)
{
  float tempC = sensors.getTempC(deviceAddress);
  Serial.print(tempC);
  //Serial.print((char)176);
  Serial.print(" C  |  ");
  Serial.print(DallasTemperature::toFahrenheit(tempC));
  //Serial.print((char)176);
  Serial.println(" F");

  delay(1000);
}

As soon as the serial monitor opens the old previous readings are there. Maybe increase the delays? Increasing the last Delay seems to have made the issue go away. I closed the serial port monitor right after the last reading of sensor #3 and when I opened it again it started as a clean sheet with no artifact from last time. Beats me why it was doing that.

Ron

You need the delay after this statement, but before the reads (sensors.getTempC(deviceAddress)).

As per the datasheet... it can take 750ms to do a conversion... if you try reading the temperature before that time is up, then you will likely get the previous value.

Many thanks. Have a few things going but will pick up on that in the morning. Again, many thanks. Let's hope it helps the thread starter. Then too, if I close the serial port monitor and open it again 10 seconds later I wonder if I will still see the old data still there? Oh well, tomorrow is another day. Thanks again :slight_smile:

Ron

Indeed. All that is required is that the baud rate in the monitor matches the rate declared in the Arduino programme, and 115200 should be fine.
Your code appears to be kosher but, with such a short setup, it would be better to have the delay at the start of the loop.

@red_car
Though code is little bit updated ,It similar as above

// Include the required Arduino libraries:
#include <OneWire.h>
#include <DallasTemperature.h>


// Define to which pin of the Arduino the 1-Wire bus is connected:
#define ONE_WIRE_BUS 4

// Create a new instance of the oneWire class to communicate with any OneWire device:
OneWire oneWire(ONE_WIRE_BUS);

// Pass the oneWire reference to DallasTemperature library:
DallasTemperature sensors(&oneWire);

int deviceCount = 0;
float tempC;
float tempF;
bool label = true;
void setup() {
  // Begin serial communication at a baud rate of 9600:
  sensors.begin();
  Serial.begin(19200);
  // Start up the library:
  

  // Locate the devices on the bus:
  
  deviceCount = sensors.getDeviceCount();
  Serial.println("reading is started");
 
}

void loop() {
  // Send the command for all devices on the bus to perform a temperature conversion:
  
  String sensor_name="sensor";
  while(label){
    for (int i = 0;  i < deviceCount;  i++) {
       String Sensor_Label= String(sensor_name + String(i));
      Serial.print(Sensor_Label);
      Serial.print(",");
      
      }
      Serial.println();
    label = false;
    }
  delay(1000);
  sensors.requestTemperatures();
  delay(1000);
 
  // Display temperature from each sensor
  for (int i = 0;  i < deviceCount;  i++) {
   
    tempC = sensors.getTempCByIndex(i);
    
    Serial.print(tempC);
    Serial.print(",");
    

  }

  Serial.println();
  

}

What results do you actually get?

share_2

Problem is not solved and it is too much slower!!!

Calm down...

Do you think that could because you now have twice as many delay statements?

Are the temperature values actually correct?

Hi,

What is your project?
What is the application?

What construction type are your sensors?
Can you post an image of your project?

Why do you need speed?
Those sensors do not change reading instantly their environment does.

Tom... :smiley: :+1: :coffee: :australia: