I am using the DS18B20 for some temperature readings.
I am using (I think) the standard example code (see below).
However I only seem to be getting 0.5 Deg resolution from readtemp12
Looking at the Raw read for DSdata shows it in steps of 8, i.e
192 = 12.0
200 = 12.5
208 = 13.0
Am I missing something? or is my DS18B20 a cheap knock off without true 12bit?
I don't understand the code in DS18B20.h well enough to see where the resolution is set.
If someone can confirm they have been getting 12bit reads that would help, or I will need to dig deeper.
Thanks
SubGET_DS18B20' The function readtemp12 returns the raw value of the sensor. 'Thesensorisreadasa12bitvaluethereforeeachunitequatesto0.0625ofadegreeDSdata=readtemp12HSerPrintDSdataHSerPrint","DS18B20_SIGNBIT=DSdata/256/128IfDS18B20_SIGNBIT=0ThengotoPositiveendif' its negative! DSdata = ( DSdata # 0xffff ) + 1 'taketwoscompPositive:' Convert value * 0.0625. Mulitple value by 6 then add result to multiplication of the value with 25 then divide result by 100. DS18B20_TempC_100 = DSdata * 6 DSdata = ( DSdata * 25 ) / 100 DS18B20_TempC_100 = DS18B20_TempC_100 + DSdata DS18B20_WHOLE = DS18B20_TempC_100 / 100 ;Get Whole number part DS18B20_FRACT = DS18B20_TempC_100 % 100 ;Get remainder or decimal If DS18B20_SIGNBIT = 0 Then goto DisplayTemp end if hserprint "-" DisplayTemp: hserprint str(DS18B20_WHOLE) hserprint "." 'ToensurethedecimalpartistwoDS18B20_DIGitsDS18B20_DIG=DS18B20_FRACT/10hserprintDS18B20_DIGDS18B20_DIG=DS18B20_FRACT%10hserprintDS18B20_DIGhserprint"C"HSerSend13endsub
Last edit: Anobium 2020-04-16
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
The library sets a global integer variable called DSDATA. Calling DS18B20DeviceRead() will aslo set the DSDATA varialbe. That is a raw read of the IC.
The example code simply converts the raw data in to strings for display. In the format of Wholenumber.Decimalnumber.
Therefore, it is a simple change to this code to display greater accurancy. To ensure the decimal part is two DS18B20_DIGits
hserprint str(DS18B20_WHOLE)
hserprint "."
' To ensure the decimal part is two DS18B20_DIGits
DS18B20_DIG = DS18B20_FRACT / 10
hserprint DS18B20_DIG
DS18B20_DIG = DS18B20_FRACT % 10
hserprint DS18B20_DIG
hserprint "C"
HSerSend 13
So, start after these lines of code shown below. As as this point DS18B20_TempC_100 is set per Table 1 in the DS18B20 datasheet.
Convert value * 0.0625. Mulitple value by 6 then add result to multiplication of the value with 25 then divide result by 100. DS18B20_TempC_100 = DSdata * 6
Summary... the example code displays a two digit decimale temperature. You need to adapt to show a higher resolution using DS18B20_TempC_100 as your starting point.
Anobium
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
I did not write the DS18B20 library, but, there is chance that the DS18B20 is not operating in the default 12bit mode. But, if the temperature is correct (apart from the displayed resolution) then it will be operating in the default 12bit mode.
I have just hooked up the Training Video 16 (recent YouTube video) and at about 25C I got a value of 402 for DSData, and ( 404 * 0.0625 ) =25.25C... so, if that value was return as 405 that would be calcaulated as 25.3125C
So, I think the read is correct. This is just a display formatting thing to be fixed.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Yes, it does display a 2 digit decimal, but it is only 9Bit resolution or 0.5 Deg increments
I am only seeing the DSdata Raw in steps of 8;
192 = 12.00
200 = 12.50
208 = 13.00
How is R1 and R2 set for 12bit results as per datasheet Table 2?
I may have missed it, but I can't see any setup for 12bit in DS18B20.h
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
' Convert value * 0.0625. Mulitple value by 6 then add result to multiplication of the value with 25 then divide result by 100. DS18B20_TempC_100 = DSdata * 6
The mutliple by 6 is the constraint. Make that 625 to fully factorise. Where TempC_100 will need to be change to a LONG.
Yes, quite right... There is nothing wrong with the Math or method.
The problem is can not get a raw value of 191. it is in steps of 8.
A rising temperature will only give steps of Raw value;
186 = 11.50
192 = 12.00
200 = 12.50
A Raw value of 191 for example never occurs.
I will dig through DS18B20.h some more, get my head around that and see if I can set the resolution according to the datasheet.
I think for now I will take it as the problem is my device's default settings, rather than a problem with the example code
-Thanks
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Here you go. Larger factorisation meant I had to change some of the variables from bytes to words, and the formatting of the decimal needed to left padded.
Evan
#chip 16f18313#config MCLR=ON#option Explicit#include <ds18b20.h>'Generated by PIC PPS Tool for Great Cow Basic'PPS Tool version: 0.0.6.1'PinManager data: v1.79.0'Generated for 16f18313''Template comment at the start of the config file'#startup InitPPS, 85#define PPSToolPart 16f18313SubInitPPS'Module: EUSARTRA5PPS=0x0014'TX > RA5EndSub'Template comment at the end of the config file'USART settings for USART1#define USART_BAUD_RATE 115200#define USART_TX_BLOCKING#define USART_DELAY OFF#define LCD_IO 107 'K107;-----Constants' DS18B20 port settings#define DQ RA4;-----QuickCommandReference:'''Set LCD_10 to 10 for the YwRobot LCD1602 IIC V1 or the Sainsmart LCD_PIC I2C adapter '''SetLCD_10to12fortheYwmjkdzI2Cadapterwithpotbentovertopofchip;-----VariablesdimTempC_100asLONG' a variabler to handle the temperature calculationsDimDSdata,WHOLE,FRACT,DIGaswordDimCCOUNT,SIGNBITasByte;-----Mainbodyofprogramcommenceshere.ccount=0CLSprint"GCBasic 2020"locate1,0print"DS18B20 Demo"wait2sCLSdoforever' The function readtemp returns the integer value of the sensorDSdata=readtemp' Display the integer value of the sensor on the LCDlocate0,0printhex(ccount)print" Ceil"locate0,8printDSdataprintchr(223)+"C"' Display the integer and decimal value of the sensor on the LCD' The function readtemp12 returns the raw value of the sensor.' The sensor is read as a 12 bit value therefore each unit equates to 0.0625 of a degreeDSdata=readtemp12SignBit=DSdata/256/128IfSignBit=0ThengotoPositive' its negative!DSdata=(DSdata# 0xffff ) + 1 ' take twos compPositive:' Convert value * 0.0625 by factorisationTempC_100=DSdata*625Whole=TempC_100/10000Fract=TempC_100%10000IfSignBit=0ThengotoDisplayTempPrint"-"DisplayTemp:Locate3,0PrintWholePrint"."Printleftpad(str(Fract),4,"0")wait2sccount++loop
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Thanks.
The output from your code. Note I changed the output of DSdata to raw so you can see whats going on.
CCOUNT Dsdata (Raw) Temp C
0 184 11.5000 C
1 176 11.0000 C
2 176 11.0000 C
3 176 11.0000 C
4 176 11.0000 C
5 176 11.0000 C
6 176 11.0000 C
7 192 12.0000 C
8 248 15.5000 C
9 304 19.0000 C
10 352 22.0000 C
11 392 24.5000 C
12 424 26.5000 C
13 456 28.5000 C
14 480 30.0000 C
15 504 31.5000 C
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Here is code, see the sub, to set the resolution to 9,10,11 or 12 bits. Take the .h replace the existing one in your include folder.
New library sub is:
DS18B20SetResolution ( in DS18B20SetResolutionValue )
where
D18B20SetResolution ( DS18B20_TEMP_9_BIT | DS18B20_TEMP_10_BIT, DS18B20_TEMP_11_BIT, DS18B20_TEMP_12_BIT ) are valid
Sorry. You may have been stuck in 9bit mode. This will set to 12bit but if it does not work then you have duff a DS18B20 - as this does work on test here, and, I can see the data transmission using the one-wire logic analyser.
Evan
#chip 16f18313#config MCLR=ON#option Explicit#include <ds18b20.h>'Generated by PIC PPS Tool for Great Cow Basic'PPS Tool version: 0.0.6.1'PinManager data: v1.79.0'Generated for 16f18313''Template comment at the start of the config file'#startup InitPPS, 85#define PPSToolPart 16f18313SubInitPPS'Module: EUSARTRA5PPS=0x0014'TX > RA5EndSub'Template comment at the end of the config file'USART settings for USART1#define USART_BAUD_RATE 115200#define USART_TX_BLOCKING#define USART_DELAY OFF#define LCD_IO 107 'K107;-----Constants' DS18B20 port settings#define DQ RA4;-----QuickCommandReference:'''Set LCD_10 to 10 for the YwRobot LCD1602 IIC V1 or the Sainsmart LCD_PIC I2C adapter '''SetLCD_10to12fortheYwmjkdzI2Cadapterwithpotbentovertopofchip;-----VariablesdimTempC_100asLONG' a variabler to handle the temperature calculationsDimDSdata,WHOLE,FRACT,DIGaswordDimCCOUNT,SIGNBITasByte;-----Mainbodyofprogramcommenceshere.ccount=0CLSprint"GCBasic 2020"locate1,0print"DS18B20 Demo"wait2sCLSDS18B20SetResolution(DS18B20_TEMP_12_BIT)doforever' The function readtemp returns the integer value of the sensorDSdata=readtemp' Display the integer value of the sensor on the LCDlocate0,0printhex(ccount)print" Ceil"locate0,8printDSdataprintchr(223)+"C"' Display the integer and decimal value of the sensor on the LCD' The function readtemp12 returns the raw value of the sensor.' The sensor is read as a 12 bit value therefore each unit equates to 0.0625 of a degreeDSdata=readtemp12SignBit=DSdata/256/128IfSignBit=0ThengotoPositive' its negative!DSdata=(DSdata# 0xffff ) + 1 ' take twos compPositive:' Convert value * 0.0625 by factorisationTempC_100=DSdata*625Whole=TempC_100/10000Fract=TempC_100%10000IfSignBit=0ThengotoDisplayTempPrint"-"DisplayTemp:Locate3,0PrintWholePrint"."Printleftpad(str(Fract),4,"0")wait2sccount++loop
Thanks very much for that... that did the trick. All good now, and getting better than 9 bit resolution.
You weren't to know my devices were in 9 bit mode by default. :-)
I have been using a HX711 strain guage amplifier and determining its output drift with temperature. The Staircase in the attached graph is due to the 0.5 Deg resolution that I can now improve on. - thanks
Excellent news. I will move the .h to the release.
Be good of you to post a table of reading for a range of temps (like the one posted previously) with 9bit thru 12bit results. It will help others and I can include in the Help.
Anobium
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Typical outputs for 9bit, 10bit, 11bit, 12bit attached.
Help guide is missing the 'S' in D18B20SetResolution ?
Maybe in the table of CONSTANT and Resolution add another coloum for temperature resolution of 0.5 Deg C, 0.25 Deg C, 0.125 Deg C, 0.0625 Deg C
RESOLVED - THE DS18B20 LIBRARY HAS BEEN UPDATED TO SUPPORT THE SETTING OF THE OPERATING RESOLUTION. See https://github.com/Anobium/Great-Cow-BASIC-Help/blob/master/source/ds18b20setresolution.adoc
Original post follows.
I am using the DS18B20 for some temperature readings.
I am using (I think) the standard example code (see below).
However I only seem to be getting 0.5 Deg resolution from readtemp12
Looking at the Raw read for DSdata shows it in steps of 8, i.e
192 = 12.0
200 = 12.5
208 = 13.0
Am I missing something? or is my DS18B20 a cheap knock off without true 12bit?
I don't understand the code in DS18B20.h well enough to see where the resolution is set.
If someone can confirm they have been getting 12bit reads that would help, or I will need to dig deeper.
Last edit: Anobium 2020-04-16
Hi.
The library sets a global integer variable called DSDATA. Calling DS18B20DeviceRead() will aslo set the DSDATA varialbe. That is a raw read of the IC.
The example code simply converts the raw data in to strings for display. In the format of Wholenumber.Decimalnumber.
Therefore, it is a simple change to this code to display greater accurancy.
To ensure the decimal part is two DS18B20_DIGitsSo, start after these lines of code shown below. As as this point
DS18B20_TempC_100is set per Table 1 in the DS18B20 datasheet.Summary... the example code displays a two digit decimale temperature. You need to adapt to show a higher resolution using
DS18B20_TempC_100as your starting point.Anobium
I did not write the DS18B20 library, but, there is chance that the DS18B20 is not operating in the default 12bit mode. But, if the temperature is correct (apart from the displayed resolution) then it will be operating in the default 12bit mode.
I have just hooked up the Training Video 16 (recent YouTube video) and at about 25C I got a value of 402 for DSData, and ( 404 * 0.0625 ) =25.25C... so, if that value was return as 405 that would be calcaulated as 25.3125C
So, I think the read is correct. This is just a display formatting thing to be fixed.
Yes, it does display a 2 digit decimal, but it is only 9Bit resolution or 0.5 Deg increments
I am only seeing the DSdata Raw in steps of 8;
192 = 12.00
200 = 12.50
208 = 13.00
How is R1 and R2 set for 12bit results as per datasheet Table 2?
I may have missed it, but I can't see any setup for 12bit in DS18B20.h
The core is the display and factorisation.
So, focus on the following.
The mutliple by 6 is the constraint. Make that 625 to fully factorise. Where TempC_100 will need to be change to a LONG.
Now TempC_100 is now factorised to cater. Remember we using integer maths.
So, old calc v new calc
OLD = a value 191 = yielded 1193. Factor was 100 therefore 11.93
NEW = a value of 191 = now yields 119376. Factor 10000 therefore 11.9376
So, now you have to show (I think - untested)
Yes, quite right... There is nothing wrong with the Math or method.
The problem is can not get a raw value of 191. it is in steps of 8.
A rising temperature will only give steps of Raw value;
186 = 11.50
192 = 12.00
200 = 12.50
A Raw value of 191 for example never occurs.
I will dig through DS18B20.h some more, get my head around that and see if I can set the resolution according to the datasheet.
I think for now I will take it as the problem is my device's default settings, rather than a problem with the example code
-Thanks
It could be that your device is in 9-bit mode... but, I would try the code I posted that displays to 4 decimal places.
It is not a library issue.
Let me knock up the code.
Here you go. Larger factorisation meant I had to change some of the variables from bytes to words, and the formatting of the decimal needed to left padded.
Evan
Of course Dallas only claim "is accurate to ±0.5°C over the range of -10°C to +85°C."
Thanks.
The output from your code. Note I changed the output of DSdata to raw so you can see whats going on.
CCOUNT Dsdata (Raw) Temp C
0 184 11.5000 C
1 176 11.0000 C
2 176 11.0000 C
3 176 11.0000 C
4 176 11.0000 C
5 176 11.0000 C
6 176 11.0000 C
7 192 12.0000 C
8 248 15.5000 C
9 304 19.0000 C
10 352 22.0000 C
11 392 24.5000 C
12 424 26.5000 C
13 456 28.5000 C
14 480 30.0000 C
15 504 31.5000 C
Well... thats not correct.
Here is code, see the sub, to set the resolution to 9,10,11 or 12 bits. Take the .h replace the existing one in your
includefolder.New library sub is:
DS18B20SetResolution ( in DS18B20SetResolutionValue )where
D18B20SetResolution ( DS18B20_TEMP_9_BIT | DS18B20_TEMP_10_BIT, DS18B20_TEMP_11_BIT, DS18B20_TEMP_12_BIT )are validSorry. You may have been stuck in 9bit mode. This will set to 12bit but if it does not work then you have duff a DS18B20 - as this does work on test here, and, I can see the data transmission using the one-wire logic analyser.
Evan
A debug of the D18B20SetResolution on the wire.
Last edit: Anobium 2020-04-15
Thanks very much for that... that did the trick. All good now, and getting better than 9 bit resolution.
You weren't to know my devices were in 9 bit mode by default. :-)
I have been using a HX711 strain guage amplifier and determining its output drift with temperature. The Staircase in the attached graph is due to the 0.5 Deg resolution that I can now improve on. - thanks
Excellent news. I will move the .h to the release.
Be good of you to post a table of reading for a range of temps (like the one posted previously) with 9bit thru 12bit results. It will help others and I can include in the Help.
Anobium
And, can you scan/improve the Help? See https://github.com/Anobium/Great-Cow-BASIC-Help/blob/master/source/ds18b20setresolution.adoc
Typical outputs for 9bit, 10bit, 11bit, 12bit attached.
Help guide is missing the 'S' in D18B20SetResolution ?
Maybe in the table of CONSTANT and Resolution add another coloum for temperature resolution of 0.5 Deg C, 0.25 Deg C, 0.125 Deg C, 0.0625 Deg C
Otherwise Excellent :-)
Excellent . I have updated the Help. I have added your recommended information - a good idea.
Thanks
Oooh, I forgot... and add your extra decimal resolution (above) to the example code for the next release?
Many thanks for this :-)
Just done it. That was a good idea.
Anobium