Thermometer using an 18b20 temperature sensor.

Hello there guys,

This is the first big project that I am currently doing (I have just done some simple LED traffic lights before this) and I have a code that I found online. Now the code works 100% flawlessly, but I am trying to get it to read in 0.1 degree increments and not 0.5 degree increments. I have heard that it is possible by changing the resolution, but I am unsure on how I can achieve this. My current code is as follows:

#include <OneWire.h>
#include <DallasTemperature.h>
#include <LiquidCrystal.h>
 
//DS18b20 connected to D38
#define DS18B20 38
// Connections: Sainsmart LCD/Keypad shield
LiquidCrystal lcd(8, 9, 4, 5, 6, 7);
OneWire oneWire(DS18B20);
DallasTemperature sensors(&oneWire);

float TempInC ;
 
void setup()
{
//Serial.begin(9600);
delay(1000);
//start reading
sensors.begin();
//setup the LCD
lcd.begin(16, 2); // start the library
lcd.setCursor(0,0);
lcd.print("TEMPERATURE");
}
 
void loop()
{
//read temperature and output via LCD
sensors.requestTemperatures();
lcd.setCursor(0,1);
lcd.print(sensors.getTempCByIndex(0));
lcd.setCursor(6,1);
lcd.print("celsius");
}

Thank you in advanced!

Jnrdingo

Check the library documentation, how to get temperatures of the required resolution.

When the sensor resolution is already set to 12 bit (default), you need some sensor with a higher resolution.

The resolution of the temperature sensor is user-configurable to 9, 10, 11, or 12 bits, corresponding to increments of 0.5°C, 0.25°C, 0.125°C, and 0.0625°C, respectively. The default resolution at power-up is 12-bit, so, since you are not setting the resolution, you should be getting readings down to 0.0625°C.

You say the code is working 100% flawlessly, but I think you are either lying or lucky. You may also be thinking it is 100% kosher because it looks like it is but it is actually all wrong.

For starters, the DS18B20 needs time to do it's job and, the more the resolution, the more time is needed - 0.75 secs for the default resolution. You have allowed no time at all, and I'm surprised you get any useful reading, let alone a low resolution one. Maybe you got lucky because because the LCD takes a finite time to print the value and the word "celcius", i.e. this is what determines the time of your loop. It could be that what you are really seeing is an old reading being repeated.

If you insert a delay(1000); in the loop, you may find it will work OK.

If it doesn't, there are two more possibilities.

If you are using parasitic power, cease and desist immediately. Irrespective of any settings or defaults, parasitic power forces the sensor into 9-bit resolution. And I bet you don't have good reason to use parasitic power anyway. It is a measure of last resort.

I believe the same applies to the "by index" procedure for one-wire operation. I can't see the sense in using this method anyway, but I guess that is just a personal opinion.

You can get a better information and methodology here

Nick_Pyner:
The resolution of the temperature sensor is user-configurable to 9, 10, 11, or 12 bits, corresponding to increments of 0.5°C, 0.25°C, 0.125°C, and 0.0625°C, respectively. The default resolution at power-up is 12-bit, so, since you are not setting the resolution, you should be getting readings down to 0.0625°C.

You say the code is working 100% flawlessly, but I think you are either lying or lucky. You may also be thinking it is 100% kosher because it looks like it is but it is actually all wrong.

For starters, the DS18B20 needs time to do it's job and, the more the resolution, the more time is needed - 0.75 secs for the default resolution. You have allowed no time at all, and I'm surprised you get any useful reading, let alone a low resolution one. Maybe you got lucky because because the LCD takes a finite time to print the value and the word "celcius", i.e. this is what determines the time of your loop. It could be that what you are really seeing is an old reading being repeated.

If you insert a delay(1000); in the loop, you may find it will work OK.

If it doesn't, there are two more possibilities.

If you are using parasitic power, cease and desist immediately. Irrespective of any settings or defaults, parasitic power forces the sensor into 9-bit resolution. And I bet you don't have good reason to use parasitic power anyway. It is a measure of last resort.

I believe the same applies to the "by index" procedure for one-wire operation. I can't see the sense in using this method anyway, but I guess that is just a personal opinion.

You can get a better information and methodology here

http://www.hacktronics.com/Tutorials/arduino-1-wire-tutorial.html

Thanks for the advice, and sorry for the late reply, I've been really busy as of late.

I ended up finding out what the issue was. The sensor was set at factory to a resolution of 9. I used code from here: Code Name: Domoticz (hmm ... :)): Configure DS18B20 resolution and save it to EEPROM to set the resolution to 12. I also added in a 1 second delay in the loop before I tried the code in this link, but it made absolutely no difference. I have now got it set to 0.0625 of a degree and I would like to thank you for your advice :slight_smile:

jnrdingo:
The sensor was set at factory to a resolution of 9. I used code from here: Code Name: Domoticz (hmm ... :)): Configure DS18B20 resolution and save it to EEPROM to set the resolution to 12.

I don't believe any of this but, if you have gotten what you want, that doesn't matter. The DS18B20 runs at 12 bit resolution by default. While you say you have a DS18B20, I rather suspect you have a DS18S20. I'm not familiar with the DS18S20, nobody is because nobody uses them, but I believe it is 9 bit by default. Same goes for http://mydomotic.blogspot, he doesn't know what he's got either. Check the proper data sheets from Maxim.

If you do have a DS18S20, I don't think there is any need to change it. I understand that it is designed as a replacement for an older device but it is not inferior to the DS18B20.

While the 1 sec delay did not fix your immediate problem, it would be most unwise not to include it if you have 12 bit resolution, irrespective of the device you really have.

If DS18S20 - resolution greater than 9 bits must be calculated from the counter registers (it is in the scratchpad).