Arduino Uno + DS18B20 temp sensor

Have set up the DS18B20 sensor connected to Arduino Uno + WiFi shield and my problem is that the Cayenne dashboard value displayed is static at 1.00 C or 33.8 F rather than the true temperature. I have some experience of the DS18B20 with RasPi and have a python-based internal/external 2-sensor system working for some time, but I’m not as familiar with Arduino. The DS18B20 is an Ltech model with two integrated resistors (1K and 10K) on the board, one for the LED and one for pull down I presume. There are 3 pins on the board: + (to 5V) - (to GND) and DG to pin 2 on Arduino (which is the pin specified in sketch (const int tmpPin = 2;) and on Cayenne widget settings.

Any ideas/???

@bbrohan

Welcome to the Cayenne Community!

My first thought is to look at the Serial Monitor and see what Arduino is sending out.

Any chance you could post the sketch? I have some Arduino’s and DS18B20’s laying around and I could give it a go.

Again, welcome,

-Ian

Hi Ian,
thanks for quick response …sketch follows:
…Brendan

/*
Cayenne DS18B20 Example
This sketch shows how to send temperature data to a DS18B20 Sensor in the Cayenne Dashboard.
The Cayenne Library is required to run this sketch. If you have not already done so you can install it from the Arduino IDE Library Manager.
Steps:
1. Install the OneWire library (http://www.pjrc.com/teensy/td_libs_OneWire.html) from the Arduino Library Manager.
2. Install the DallasTemperature library (http://milesburton.com/Main_Page?title=Dallas_Temperature_Control_Library#Code.2FLibrary) from the Arduino Library Manager.
3. In the Cayenne Dashboard add a new DS18B20 widget.
4. Set the widget to Value Display.
5. Select Virtual Pins and a virtual pin number.
6. Set VIRTUAL_PIN to the pin number you selected.
7. Attach a DS18B20 to an digital pin on your Arduino.
   Schematic:
   [Ground] -- [DS18B20] -- [4.7k resistor] -- [5V]
                   |______________|
                   |
              Digital Pin
8. Set the tmpPin variable to match the pin used to connect the DS18B20.
9. Set the token variable to match the Arduino token from the Dashboard.
10. Compile and upload this sketch.
11. Once the Arduino connects to the Dashboard it should automatically update the DS18B20 widget with data.
*/

#define CAYENNE_PRINT Serial  // Comment this out to disable prints and save space
#include <OneWire.h>
#include <DallasTemperature.h>
#include <CayenneEthernet.h>

// Virtual Pin of the DS18B20 widget.
#define VIRTUAL_PIN V1

// Digital pin the DS18B20 is connected to. Do not use digital pins 0 or 1 since those conflict with the use of Serial.
const int tmpPin = 2;

OneWire oneWire(tmpPin);
DallasTemperature sensors(&oneWire);

// Cayenne authentication token. This should be obtained from the Cayenne Dashboard.
char token[] = "xxxxxxxxx";

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

void loop()
{
  Cayenne.run();
}

// This function is called when the Cayenne widget requests data for the Virtual Pin.
CAYENNE_OUT(VIRTUAL_PIN)
{
  // Send the command to get temperatures.
  sensors.requestTemperatures();
  // This command writes the temperature in Celsius to the Virtual Pin.
  Cayenne.celsiusWrite(VIRTUAL_PIN, sensors.getTempCByIndex(0));
  // To send the temperature in Fahrenheit use the corresponding code below.
  //Cayenne.fahrenheitWrite(VIRTUAL_PIN, sensors.getTempFByIndex(0));
}

Thanks Ian,

going offline for the evening, will check it out in the am… Bren

Brendan,
Sorry I missed you, so far I’m not having much more luck…

Going to tear into it -

-Ian

Well I’m making a little progress. I was able to determine that the Arduino and DS18B20 were capable of playing nice together. I loaded up the sketch that comes with the OneWire library found here-

Hooked up the DS18B20 to pin 10 on the Arduino and was able to get appropriate output on the serial monitor.

This was good since I’ve never had a DS18B20 on an Arduino before, and the sensor was a new one from China which are always suspect.

Now to sort out where the breakdown is with the Cayenne code…

-Ian

Hi Ian,

I ran the example script for OneWire+DS18B20 and, like you, I also get the correct temperature output via the serial monitor. So I can conclude that I have a properly functioning Uno + sensor - so it seems to be the Cayenne interfacing that is the issue. Unfortunately I’m out of my depth trouble-shooting this.
…Bren

Hang on! Mine is working now, wasn’t yesterday, not sure what changed? I’m putting everything together now, I’ll have it posted in a couple minutes.

Ian

Hi Ian,
as you can see I’ve got it working. I resorted to removing the Uno from my dashboard and re-adding it, then adding the sensor. I don’t know what the root cause of the problem was but suspect it was lack of communication between my Uno and the cloud. Thanks for your input.
…Bren

So, I’m not sure what happened, but it seems things that didn’t work yesterday ARE working today.

Here’s the set up-


Arduino Uno clone, W5100 ethernet shield, waterproof DS18B20 with 4.7kΩ pu attached to digital pin 2.

Here’s the code-

/*
Cayenne DS18B20 Example
This sketch shows how to send temperature data to a DS18B20 Sensor in the Cayenne Dashboard.
The Cayenne Library is required to run this sketch. If you have not already done so you can install it from the Arduino IDE Library Manager.
Steps:
1. Install the OneWire library (http://www.pjrc.com/teensy/td_libs_OneWire.html) from the Arduino Library Manager.
2. Install the DallasTemperature library (http://milesburton.com/Main_Page?title=Dallas_Temperature_Control_Library#Code.2FLibrary) from the Arduino Library Manager.
3. In the Cayenne Dashboard add a new DS18B20 widget.
4. Set the widget to Value Display.
5. Select Virtual Pins and a virtual pin number.
6. Set VIRTUAL_PIN to the pin number you selected.
7. Attach a DS18B20 to an digital pin on your Arduino.
   Schematic:
   [Ground] -- [DS18B20] -- [4.7k resistor] -- [5V]
                   |______________|
                   |
              Digital Pin
8. Set the tmpPin variable to match the pin used to connect the DS18B20.
9. Set the token variable to match the Arduino token from the Dashboard.
10. Compile and upload this sketch.
11. Once the Arduino connects to the Dashboard it should automatically update the DS18B20 widget with data.
*/

#define CAYENNE_PRINT Serial  // Comment this out to disable prints and save space
#include <OneWire.h>
#include <DallasTemperature.h>
#include <CayenneEthernet.h>

// Virtual Pin of the DS18B20 widget.
#define VIRTUAL_PIN V1

// Digital pin the DS18B20 is connected to. Do not use digital pins 0 or 1 since those conflict with the use of Serial.
const int tmpPin = 2;

OneWire oneWire(tmpPin);
DallasTemperature sensors(&oneWire);

// Cayenne authentication token. This should be obtained from the Cayenne Dashboard.
char token[] = "5mk7xgvwuf";

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

void loop()
{
  Cayenne.run();
}

// This function is called when the Cayenne widget requests data for the Virtual Pin.
CAYENNE_OUT(VIRTUAL_PIN)
{
  // Send the command to get temperatures.
  sensors.requestTemperatures();
  // This command writes the temperature in Celsius to the Virtual Pin.
  Cayenne.celsiusWrite(VIRTUAL_PIN, sensors.getTempCByIndex(0));
  // To send the temperature in Fahrenheit use the corresponding code below.
  //Cayenne.fahrenheitWrite(VIRTUAL_PIN, sensors.getTempFByIndex(0));
}

@bbrohan, I don’t think this is any different than what you posted yesterday. Again, what didn’t seem to work yesterday seems to today.

Used this method to add the widget-


And got a seemingly well behaved widgit-

I am able to use the settings cog to change the unit of measure °C, °F, °K all appear to work properly.

The one thing I did note is the polling rate seems to be a little slow as you can see here-


With the exception of the bump around 07:38 it seems to be polling about once a minute.

-Ian

I was having exactly the same problems you were yesterday. I don’t think the code I was using yesterday that didn’t work is any different than the code that is working today. I’m gonna blame the ‘cloud’ :slight_smile:.

Glad to hear you got things working!

-Ian

Brendan,
I changed the status of this thread to ‘Helped’. Please let us know if you continue to have difficulties, and we’ll move it back to an ‘open’ status.

Thank you for taking the time to contribute to the group discussion!

And a personal thanks from me for prompting me to get my favorite sensor working on the Arduino!

-Ian

2 Likes

I’m having the same problem. I’ve tried everything. I can get a reading on the monitor with the temp. Sensor but through the app and the web page I get 33.8. Any clue? I’ve deleted and reinstalled everything. I’m at a loss but I’m also new to this as well.

I have exact the same problem. Yesterday it was working fine with my sensor with another arduino UNO today it is showing -127° C always. Please help.


have a look at this topic One wire probe problem. there are a couple of suggestion. give them a try and let me know.