DS18b20 Working Example?


#1

Hi,
is there somewhere an actual working example to read multiple Dallas DS18b20 temperature sensors and maybe print them to the serial port?

I’ve read several threads so far and tried 4 different ports example codes but none of them where working on my Spark Core. There were always include errors and others. I fixed some of them but I just couldn’t figure out others. Heck, even the Spark Web IDE provided “Spark-Dallas-Temperature” library throws errors at me?!

Coming from the Arduino, what frustrates me most with the Spark Core is the lack of simple, working examples. (I’m still a beginner) Yes there’s quite a bit of community by now but it’s so cumbersome to find all the bits and pieces in forum threads. I think it would be really helpful to have some kind of database with small tutorials. The project page goes in the right direction but there are several that not even have any details but the general description.

Anyway, some help with original question would be greatly appreciated.


#2

I had a lot of trouble getting the DS18b20 working as well but it can be done! What errors are you getting?

Here’s a stripped down version of my code: https://gist.github.com/harrisonhjones/3a8f81e99a40c4a28167

Note, you do need to add the OneWire library to your project.

Let me know if you have additional questions/problems


#3

I also get errors when I copy & paste your code in the Web IDE and verify it. It seems to be a combination of WiFi drivers (CC3000) and some other stuff. Maybe there’s something else wrong? (The Spak Core is not even connected at this time)

ERROR:
In file included from ../../core-common-lib/CC3000_Host_Driver/cc3000_common.h:38:0,
from ../../core-common-lib/SPARK_Firmware_Driver/inc/hw_config.h:35,
from ../inc/main.h:37,
from ../inc/spark_utilities.h:30,
from ../inc/spark_wiring.h:33,
from OneWire/OneWire.h:8,
from OneWire/OneWire.cpp:131:
../../core-common-lib/CC3000_Host_Driver/data_types.h:53:0: note: this is the location of the previous definition
#define FALSE (0)
^
In file included from OneWire/OneWire.cpp:131:0:
OneWire/OneWire.h:32:0: warning: "TRUE" redefined [enabled by default]
#define TRUE 1
^
In file included from ../../core-common-lib/CC3000_Host_Driver/cc3000_common.h:38:0,
from ../../core-common-lib/SPARK_Firmware_Driver/inc/hw_config.h:35,
from ../inc/main.h:37,
from ../inc/spark_utilities.h:30,
from ../inc/spark_wiring.h:33,
from OneWire/OneWire.h:8,
from OneWire/OneWire.cpp:131:
../../core-common-lib/CC3000_Host_Driver/data_types.h:57:0: note: this is the location of the previous definition
#define TRUE (!FALSE)
^
temp.cpp:2:0: warning: ignoring #pragma SPARK_NO_PREPROCESSOR [-Wunknown-pragmas]
#pragma SPARK_NO_PREPROCESSOR
^
temp.cpp:5:31: fatal error: DallasTemperature.h: No such file or directory
#include "DallasTemperature.h"
^
compilation terminated.
make: *** [temp.o] Error 1

Error: Could not compile. Please review your code.

#4

Ah, I’m sorry. The gist I linked to had two additional files “DallasTemperature.cpp” and “DallasTemperature.h”. You need to include both of those in your project. You do this by clicking the “+” symbol near the top of the editor and inputting the filename “DallasTemperature”. It will automatically create a .cpp ad .h file. Once those are created copy and paste the content of the files from the gist into the files in the WebIDE.


#5

I’ve still no luck, I added your DallasTemperature.cpp and .h but still a lot of errors when I verify.



Error:
In file included from …/inc/spark_wiring.h:29:0,
from …/inc/application.h:29,
from DallasTemperature.h:26,
from DallasTemperature.cpp:12:
…/…/core-common-lib/SPARK_Firmware_Driver/inc/config.h:12:2: warning: #warning “Defaulting to Release Build” [-Wcpp]
#warning “Defaulting to Release Build”
^
In file included from DallasTemperature.h:27:0,
from DallasTemperature.cpp:12:
OneWire/OneWire.h:31:0: warning: “FALSE” redefined [enabled by default]
#define FALSE 0
^
In file included from …/…/core-common-lib/CC3000_Host_Driver/cc3000_common.h:38:0,
from …/…/core-common-lib/SPARK_Firmware_Driver/inc/hw_config.h:35,
from …/inc/main.h:37,
from …/inc/spark_utilities.h:30,
from …/inc/spark_wiring.h:33,
from …/inc/application.h:29,
from DallasTemperature.h:26,
from DallasTemperature.cpp:12:
…/…/core-common-lib/CC3000_Host_Driver/data_types.h:53:0: note: this is the location of the previous definition
#define FALSE (0)
^
In file included from DallasTemperature.h:27:0,
from DallasTemperature.cpp:12:
OneWire/OneWire.h:32:0: warning: “TRUE” redefined [enabled by default]
#define TRUE 1
^
In file included from …/…/core-common-lib/CC3000_Host_Driver/cc3000_common.h:38:0,
from …/…/core-common-lib/SPARK_Firmware_Driver/inc/hw_config.h:35,
from …/inc/main.h:37,
from …/inc/spark_utilities.h:30,
from …/inc/spark_wiring.h:33,
from …/inc/application.h:29,
from DallasTemperature.h:26,
from DallasTemperature.cpp:12:
…/…/core-common-lib/CC3000_Host_Driver/data_types.h:57:0: note: this is the location of the previous definition
#define TRUE (!FALSE)
^
In file included from …/inc/spark_wiring.h:29:0,
from OneWire/OneWire.h:8,
from OneWire/OneWire.cpp:131:
…/…/core-common-lib/SPARK_Firmware_Driver/inc/config.h:12:2: warning: #warning “Defaulting to Release Build” [-Wcpp]
#warning “Defaulting to Release Build”
^
In file included from OneWire/OneWire.cpp:131:0:
OneWire/OneWire.h:31:0: warning: “FALSE” redefined [enabled by default]
#define FALSE 0
^
In file included from …/…/core-common-lib/CC3000_Host_Driver/cc3000_common.h:38:0,
from …/…/core-common-lib/SPARK_Firmware_Driver/inc/hw_config.h:35,
from …/inc/main.h:37,
from …/inc/spark_utilities.h:30,
from …/inc/spark_wiring.h:33,
from OneWire/OneWire.h:8,
from OneWire/OneWire.cpp:131:
…/…/core-common-lib/CC3000_Host_Driver/data_types.h:53:0: note: this is the location of the previous definition
#define FALSE (0)
^
In file included from OneWire/OneWire.cpp:131:0:
OneWire/OneWire.h:32:0: warning: “TRUE” redefined [enabled by default]
#define TRUE 1
^
In file included from …/…/core-common-lib/CC3000_Host_Driver/cc3000_common.h:38:0,
from …/…/core-common-lib/SPARK_Firmware_Driver/inc/hw_config.h:35,
from …/inc/main.h:37,
from …/inc/spark_utilities.h:30,
from …/inc/spark_wiring.h:33,
from OneWire/OneWire.h:8,
from OneWire/OneWire.cpp:131:
…/…/core-common-lib/CC3000_Host_Driver/data_types.h:57:0: note: this is the location of the previous definition
#define TRUE (!FALSE)
^
ds18b20.cpp:1:0: warning: ignoring #pragma SPARK_NO_PREPROCESSOR [-Wunknown-pragmas]
#pragma SPARK_NO_PREPROCESSOR
^
In file included from …/inc/spark_wiring.h:29:0,
from …/inc/application.h:29,
from DallasTemperature.h:26,
from ds18b20.cpp:4:
…/…/core-common-lib/SPARK_Firmware_Driver/inc/config.h:12:2: warning: #warning “Defaulting to Release Build” [-Wcpp]
#warning “Defaulting to Release Build”
^
In file included from DallasTemperature.h:27:0,
from ds18b20.cpp:4:
OneWire/OneWire.h:31:0: warning: “FALSE” redefined [enabled by default]
#define FALSE 0
^
In file included from …/…/core-common-lib/CC3000_Host_Driver/cc3000_common.h:38:0,
from …/…/core-common-lib/SPARK_Firmware_Driver/inc/hw_config.h:35,
from …/inc/main.h:37,
from …/inc/spark_utilities.h:30,
from …/inc/spark_wiring.h:33,
from …/inc/application.h:29,
from DallasTemperature.h:26,
from ds18b20.cpp:4:
…/…/core-common-lib/CC3000_Host_Driver/data_types.h:53:0: note: this is the location of the previous definition
#define FALSE (0)
^
In file included from DallasTemperature.h:27:0,
from ds18b20.cpp:4:
OneWire/OneWire.h:32:0: warning: “TRUE” redefined [enabled by default]
#define TRUE 1
^
In file included from …/…/core-common-lib/CC3000_Host_Driver/cc3000_common.h:38:0,
from …/…/core-common-lib/SPARK_Firmware_Driver/inc/hw_config.h:35,
from …/inc/main.h:37,
from …/inc/spark_utilities.h:30,
from …/inc/spark_wiring.h:33,
from …/inc/application.h:29,
from DallasTemperature.h:26,
from ds18b20.cpp:4:
…/…/core-common-lib/CC3000_Host_Driver/data_types.h:57:0: note: this is the location of the previous definition
#define TRUE (!FALSE)
^
ds18b20.cpp: In function ‘void loop()’:
ds18b20.cpp:51:14: error: ‘atTempAnnounced’ was not declared in this scope
if((!atTempAnnounced) && (spaInTempC > spaTargetTemp) && (spaTargetTemp != 0.0))
^
ds18b20.cpp:51:48: error: ‘spaTargetTemp’ was not declared in this scope
if((!atTempAnnounced) && (spaInTempC > spaTargetTemp) && (spaTargetTemp != 0.0))
^
make: *** [ds18b20.o] Error 1


#6

@Exposure, we are in the same boat and probably been reading the same posts. I have a sketch from arduino that uses the extra funcionality of the dallas library and still haven´t figured out how to use it here. I get sensor readings using the provided one wire library, but I haven´t managed to use the dallas temperature from the IDE. Tried to copy paste this code using the (+) option on the IDE without success. It appears that it has to be loaded via USB, I don´t know.

@harrisonhjones, thanks for your input, I´ll try your code as soon I get home and post back.


#7

Hey, I’ve update my gist so it should compile for you. There actually aren’t that many errors, most your error output are warnings in fact.

Using the code:
Copy and paste the updated example code
Wire up two dallas one wire DS18B20s
Edit the top lines which define the device addresses to match your devices address
Flash the code with the core connected to your computer via a usb cable
Open your serial console/monitor and connect to the core. You should see temperature readings


#8

It did compile, thanks a lot man! Your code by the way is what I was aiming for, giving a label to the sensors and then requesing temperatures by name.
I lost half an hour getting 0.00 readings because in the code I forgot to change the pin to D3, as is in my wiring.

I´m still getting the “out” temperature -127.00, gonna check my wiring and address. (EDIT: was a typo in the addres). Worst part is over :smile:

Again, thanks a lot, this was dragging me a lot.


#9

I got it compiled and after a bit of trouble with the serial monitoring (I had to take out this part: while(!Serial.available()) SPARK_WLAN_Loop(); ) I get readings now.

Unfortunately always -127.00C
I’m pretty certain I got my DS18b20 address right (it’s also terminated with a resistor). It’s connected to D3 and I changed the ONE_WIRE_BUS to D3.

Any ideas?

Also to double check, is there a Spark Core code to read out addresses of connected One Wire devices? I tried to use some Arduino code but failed to make it work.


#10

You can try this snippet:

https://github.com/kennethlimcp/Spark-Snippets/blob/master/Snippets/0006%20-%20I2C_scanner.ino


#11

That’s I2C so I guess it won’t work for 1-Wire devices? Also I would probably need application.h in case it does.


#12

Here is the code I use. One classic misstep in moving from Arduino to Spark is that on Arduino int is 16-bits but on Spark it is 32-bits. This means you should use a type like int16_t when combining two bytes like the return values from one wire. I was checking CRCs but on my breadboard they were always right so I took that out.

void getTemp() {
    // Get the ROM address
    one.reset();
    one.write(0x33);
    one.read_bytes(rom, 8);
    // Get the temp
    one.reset();
    one.write(0x55);
    one.write_bytes(rom,8);
    one.write(0x44);
    delay(10);
    one.reset();
    one.write(0x55);
    one.write_bytes(rom, 8);
    one.write(0xBE);
    one.read_bytes(resp, 9);
    
    byte MSB = resp[1];
    byte LSB = resp[0];
    
    int16_t intTemp = ((MSB << 8) | LSB); //using two's compliment 16-bit
    tempC =   ((double)intTemp)/16.0;
    tempF = (( tempC*9.0)/5.0+32.0);
}

#13

I stripped this code down to the code below to get a copy-able format of the address, without caring to take temperatures.
It will get the ROM of all the chips on the wire, so I suggest wiring one sensor at a time to get its ROM. It may take up to 3 seconds to show in serial because I wanted it to go slow thus allowing me to copy the address.
You´ll need to include only the onewire library from the IDE. Note I´m not using dallas library here.
Output:

Code:

// This #include statement was automatically added by the Spark IDE.
#include "OneWire/OneWire.h"



OneWire ds = OneWire(D3);  // on pin 10 (a 4.7K resistor is necessary)
unsigned long lastUpdate = 0; 
void setup() {
  Serial.begin(9600);
}
 
void loop() {

 unsigned long now = millis();
    if((now - lastUpdate) > 3000)
    {
        lastUpdate = now;
        byte i;
        byte present = 0;
        byte addr[8];
 
      if ( !ds.search(addr)) {
        Serial.println("No more addresses.");
        Serial.println();
        ds.reset_search();
        //delay(250);
        return;
      }
            // the first ROM byte indicates which chip
      switch (addr[0]) {
        case 0x10:
          Serial.println("Chip = DS18S20");  // or old DS1820
          break;
        case 0x28:
          Serial.println("Chip = DS18B20");
          break;
        case 0x22:
          Serial.println("Chip = DS1822");
          break;
        default:
          Serial.println("Device is not a DS18x20 family device.");
          return;
      }
      
     
      Serial.print("ROM = ");
      Serial.print("0x");
        Serial.print(addr[0],HEX);
      for( i = 1; i < 8; i++) {
        Serial.print(", 0x");
        Serial.print(addr[i],HEX);
      }
     
      if (OneWire::crc8(addr, 7) != addr[7]) {
          Serial.println("CRC is not valid!");
          return;
      }
      
     
    Serial.println();
      ds.reset();
        
    }
}

#14

It’s working, yay!
Thanks everyone for helping!

My address was wrong hence the -127.0 and the converted code I’ve tried might indeed have failed because of the int 16 problem. I did use rodrigdjodi to read out the addresses.

Is there a way to calibrate the DS18B20? Mine shows 20C while I’m prety sure it’s more 17-18C, it feels colder and another device shows 17.5C.


#15

Hi @exposure

Glad you got it working!

The DS18B20’s normally do not need any calibration and are usually quite accurate. One thing to note that has come up before is that the Spark core runs warm and can easily warm up a whole breadboard a few degrees.

Try moving the sensor to another breadboard or use longer wires to move it away from the core.


#16

So I am finally playing with the Spark Core for the first time after a year. Before this I just played with tinker and blinked some leds.

Well I just got a DS18B20 temp sensor and thought I would try the library. So I included the one wire library which looks like it is all setup to read the temp from one of these temp sensors.

I hit the verify button but I got a compiler error.

    ../../core-common-lib/SPARK_Firmware_Driver/inc/config.h:12:2: warning: #warning "Defaulting to Release Build" [-Wcpp]
 #warning  "Defaulting to Release Build"
  ^
   
In file included from OneWire/OneWire.cpp:131:0:
OneWire/OneWire.h:31:0: warning: "FALSE" redefined [enabled by default]
 #define FALSE 0
 ^
In file included from ../../core-common-lib/CC3000_Host_Driver/cc3000_common.h:38:0,
                 from ../../core-common-lib/SPARK_Firmware_Driver/inc/hw_config.h:35,
                 from ../inc/main.h:37,
                 from ../inc/spark_utilities.h:30,
                 from ../inc/spark_wiring.h:33,
                 from OneWire/OneWire.h:8,
                 from OneWire/OneWire.cpp:131:
../../core-common-lib/CC3000_Host_Driver/data_types.h:53:0: note: this is the location of the previous definition
 #define FALSE         (0)
 ^
In file included from OneWire/OneWire.cpp:131:0:
OneWire/OneWire.h:32:0: warning: "TRUE" redefined [enabled by default]
 #define TRUE  1
 ^
In file included from ../../core-common-lib/CC3000_Host_Driver/cc3000_common.h:38:0,
                 from ../../core-common-lib/SPARK_Firmware_Driver/inc/hw_config.h:35,
                 from ../inc/main.h:37,
                 from ../inc/spark_utilities.h:30,
                 from ../inc/spark_wiring.h:33,
                 from OneWire/OneWire.h:8,
                 from OneWire/OneWire.cpp:131:
../../core-common-lib/CC3000_Host_Driver/data_types.h:57:0: note: this is the location of the previous definition
 #define TRUE          (!FALSE)
 ^
In file included from ../inc/spark_wiring.h:29:0,
                 from ../inc/application.h:29,
                 from readtemp.cpp:2:
../../core-common-lib/SPARK_Firmware_Driver/inc/config.h:12:2: warning: #warning "Defaulting to Release Build" [-Wcpp]
 #warning  "Defaulting to Release Build"
  ^

   
 readtemp.cpp:2:21: fatal error: OneWire.h: No such file or directory
 #include "application.h"
                     ^
   
compilation terminated.
make: *** [readtemp.o] Error 1

So my question is do I have control over the application.h or is there something goofed up with this library? Or am I just doing this completely wrong.


#17

It looks like the version of the library that is being used by the WebIDE is out of date?

The problem is in OneWire.h

#define FALSE 0
#define TRUE  1

These should not be there (and looking through github history last year, they were removed, and still is removed in the latest version.)

The version of the library in github is 1.1.0, but the version in the WebIDE is 1.0.0.

Can anyone that’s familiar with the library shed some light on this?


#18

I got it to compile by changing the include for the onewire.h so that it is now:

#include "OneWire/OneWire.h"

Using this onewire example I am getting an output from the sensor but it is wrong.

ROM = 28 9C 4E DE 6 0 0 5C
  Chip = DS18B20
  Data = 1 1 4B 46 7F FF 1 10 14 FF  CRC=A7
  Temperature = 1200.06 Celsius, 2192.11 Fahrenheit
No more addresses.

So I was reading through this thread and it looks like I should be using the spark-dallas-temperature library but there is no example ds18b20.ino file like exposure has in post #5.


#19

I did figure out how to get the right temp using the spark-Dallas-temperature library. I had to write my own application since there wasn’t an example.

The other issue I had was that I was trying to work on it way to late with my wife continuing to remind me that it was bed time.

Turns out I put the wrong port in for the data pin (on the Dallas example I had to create).


#20

I have been working on getting the examples working in the core+photon port of OneWire and DallasTemperature. The port works but the examples are taking a little longer. So far I have “Multiple” and “Alarm” compiling. Note if you want to try and fork these examples you have to add the Particle-OneWire library through the webIDE. That is import both libraries, then fork either “Alarm.ino” or “Multiple.ino” then include Particle-OneWire library. I will try and get the others up and running soon.

To import a library from GitHub:

  1. Go to the library tab in the WebIDE
  2. click"Contribute Library"
  3. paste the link above
  4. click import (ignore the warning that another user already has a library by this name)
    5)Whala

OneWire library throwing a boatload of errors