In the course of trying to educate myself on the use of the DS18B20 sensor and the OneWire library I have generated some small code which is supposed to indicate the time it takes for a temperature conversion at various output resolutions. There are some anomalies and I would appreciate some help homing in on where I have gone wrong. There is only one device on the OneWire bus.
The code is supposed to: 1. Reset the bus; 2. Issue a SkipRom command (0xCC); 3. Issue a Convert command (0x44); 4. Send a 'Start' text via the serial monitor; 5. Wait until the sensor allows input pin 9 to pull-up to a HIGH level; 6. Send a 'Completed' text to the serial monitor.
My problem is that it doesn't always work as anticipated. Sometimes the conversion delay is a textbook 50ms for 9-bit resolution, sometimes it indicates 0ms (clearly erroneous). An example serial monitor output is shown below.
19:19:33.802 -> start 53ms conversion time as expected
19:19:33.855 -> Completed
19:19:34.857 -> ***************************************
19:19:35.058 -> start Notice the start and completed timestamps are identical, even though
19:19:35.058 -> Completed there's a 25ms delay after the 'Start' text???
19:19:36.062 -> ***************************************
19:19:36.309 -> start 0ms again
19:19:36.309 -> Completed
19:19:37.312 -> ***************************************
19:19:37.512 -> start A very normal 53ms
19:19:37.565 -> Completed
19:19:38.569 -> ***************************************
19:19:38.769 -> start 0ms
19:19:38.769 -> Completed
19:19:39.819 -> ***************************************
19:19:40.019 -> start 0ms
19:19:40.019 -> Completed
19:19:41.022 -> ***************************************
19:19:41.223 -> start 54ms
19:19:41.277 -> Completed
19:19:42.281 -> ***************************************
19:19:42.483 -> start 47ms
19:19:42.530 -> Completed
19:19:43.488 -> ***************************************
I have tried adding delays of 25ms, 50ms, and 100ms after the reset and SkipRom commands, thinking it may be that the DS18B20 data line needs time to settle, but the erroneous results continue.
Where am I going wrong?
The purpose of all this is to educate myself first, and subsequently to write 'A guide to OneWire and DallasTemperature libraries - written by a beginner for beginners'.
The code is below, simplified to test only the 9 bit resolution setting.
Kind regards, GM
/* Reports the time when a 'convert' command 0x44 is sent to a single
* sensor and reports the time when conversion is complete.
DS18B20 Pinout (Left to Right, pins down, flat side toward you)
- Left = Ground
- Center = Signal (Pin 10): (with 3.3K to 4.7K resistor to +5 or 3.3 )
- Right = +5 or +3.3 V
*/
/*-----( Import needed libraries )-----*/
// Get 1-wire Library here: http://www.pjrc.com/teensy/td_libs_OneWire.html
#include <OneWire.h>
//Get DallasTemperature Library here: http://milesburton.com/Main_Page?title=Dallas_Temperature_Control_Library
#include <DallasTemperature.h>
// Wire (I2C) Library
#include <Wire.h>
/*-----( Declare Constants and Pin Numbers )-----*/
// Data wire is plugged into pin D9 on the Arduino (can be changed)
#define ONE_WIRE_BUS 9 // NOTE: No ";" on #define
/*-----( Declare objects )-----*/
// Setup a oneWire instance to communicate with any OneWire devices
// (not just Maxim/Dallas temperature ICs)
OneWire bus(ONE_WIRE_BUS);
// Pass address of our oneWire instance to Dallas Temperature.
DallasTemperature sensors(&bus); //bloody pointers!
/*-----( Declare Variables )-----*/
// Assign the addresses of your 1-Wire temp sensors.
// See the tutorial on how to obtain these addresses:
// https://arduinoinfo.mywikis.net/wiki/Brick-Temperature-DS18B20#Read%20individual
DeviceAddress Tester = { 0x28, 0xB0, 0x92, 0x79, 0xA2, 0x01, 0x03, 0x89 }; // Labelled Sensor 7 (encapsulated)
void setup() /****** SETUP: RUNS ONCE ******/
{
//------- Initialize the Temperature measurement library--------------
Serial.begin(38400);
}//--(end setup )---
void loop() /****** LOOP: RUNS CONSTANTLY ******/
{
sensors.setResolution(Tester, 9);
timeForConversion();
// sensors.setResolution(Tester, 10);
// timeForConversion();
// sensors.setResolution(Tester, 11);
// timeForConversion();
// sensors.setResolution(Tester, 12);
// timeForConversion();
Serial.println ("***************************************");
}//--(end main loop )---
///////////////////////////////////////////////////////////////////////////////////////////////////////////////
/*-----( Declare User-written Functions )-----*/
void timeForConversion()
{
bus.reset(); //reset the one wire bus. All devices wait for a command
delay (100);
bus.write(0xCC); //Skip ROM command. Next command applies to all devices
delay (100);
bus.write(0x44); //tell the sensor to convert the temperature
Serial.println ("start"); //and record the start time via serial monitor
delay (25); //it always takes longer than this, and this delay
//allows bus to settle before testing for completion.
while (!digitalRead (9)) //keep testing until conversion is done...
{ //because pin 9 will be pulled low until it's done
}
Serial.println ("Completed"); //record the end of conversion time.
delay (1000); //a 1 second delay before the next conversion
}





