User Tools

Site Tools


This is an old revision of the document!


This tutorial is currently being edited. Please do not update until this statement has be removed.

Temperature Sensing and the 1-Wire Bus

This tutorial demonstrates how to use the Oak to read temperature data from the DS18B20 1-Wire Digital Thermometer and publish that data to the Dashboard log. It will also demonstrate how to use the deep sleep capability of the Oak platform to minimize power requirements (if desired) between temperature readings.


  • How to connect a 1-Wire DS18B20 digital thermometer to the Oak platform.
  • How to read DS18S20 temperature data.
  • How to publish temperature data to the Dashboard log.
  • How to use deep sleep capabilities of the Oak to minimize power usage between readings.

What's required:

  1. An active account []
  2. Your Oak configured with the latest firmware
  3. Your Oak claimed as a device on your account
  4. A D18B20 1-Wire Digital Thermometer []
  5. A 4.7K Ohm resister (3.3K and 2.2K can be substituted)
  6. OneWire library supporting the ESP8622

Connecting the DS18B20 device to the Oak

The 1-Wire DS18B20 digital thermometer can operate at either 3.3 volts or 5 volts. Since the Oak is a 3.3V device, in this tutorial we will use the 3.3V supplied by the Oak's VCC 3.3V pin to power the DS18B20. The DS18B20 Data Input/Output lead (DQ) must be pulled up to 3.3V using a 4.7K resister. While the DS18B20 does support a parasitic mode, this will not be demonstrated in this tutorial. Parasitic mode does not require the DS18B20 VDD lead to be connected to external power, but it does require a strong pullup on the 1-Wire bus. To learn more about operating the DS18B20 in parasitic mode, see the DS18B20 device data sheet (

Publishing Temperature Data

The provided example sketch (see below) uses the function Particle.publish() to post temperature readings to the Dashboard log. In order to do this you must have an active account, a functional Oak running the latest firmware, and that Oak claimed as an active device on

The actual command used is: Particle.publish(“Temp (F)”, tempFstring, 60, PRIVATE); where “Temp(F): is the event name and tempFstring is the actual temperature reading in string format.

For more information and other examples for how to use the Particle.publish function and other Particle device firmware Cloud functions, see

Oak Deep Sleep function

The Oak is capable of entering a deep sleep to minimize power requirements. This may be desirable if, for example, your Oak will be battery powered. The provided example sketch demonstrates this mode of operation but will also support operating in a non-sleeping mode.

To indicate to the sketch that sleep mode is desired, we use pin 5 (pulled HIGH) as essentially as “switch”. If pin 5 is HIGH when read in setup(), the sketch operate in sleep mode. This will be considered the default operating mode and as such pin 5 will be defined as INPUT_PULLUP.

To facilitate the Oak's deep sleep and wake-up capabilities, you must also connect pin 10 (WAKE) to the reset (RST) pin. This allows the Oak to trigger a RESET after a user specified sleep period has expired. While in sleep mode, the sketch will enter and run setup after each time a RESET occurs. In this mode it never enters loop().

If you prefer to operate the Oak in non-sleep mode, or if you want to prevent it from sleeping (when in sleep mode) to allow for new sketch uploads, pin 5 simple needs to be connected to ground (GND) thus pulling pin 5 LOW. When pin 5 is LOW at power up the sketch will not sleep and does enter loop() where it remains and repeatedly reads and publishes data from the DS18B20 device. This mode consumes more power than does operating in a sleep mode.

If pin 5 is HIGH at power up (sleep mode), the example sketch will stay in sleep mode as long as pin 5 remains HIGH. If at anytime pin 5 is pulled LOW, the sketch will enter non-sleep mode following the next RESET. Once in non-sleep mode, it will remain in this mode at long as the Oak is powered up regardless of the state of pin 5 (HIGH or LOW).

Sleep Mode configuration:

  1. Oak powered through USB or VIN/GND
  2. The DS18B20 is powered by Oak's VCC (3.3V)
  3. Black wires - Ground connections
  4. Red wires - VCC 3.3V connections
  5. Yellow wire - pin 10 (WAKE) to RESET (RST)
  6. White wire – pin 2 to DS18B20 DQ (note: 4.7K pull-up resister)


Sleep Mode Configuration

In Sleep Mode (after reading and publishing temperature data) the Oak enters deep sleep and delays for a user specified number of seconds. When sleep time has expired, a RESET is triggered via pin 10 (WAKE). Following a RESET, the sketch starts from the beginning and never enters loop().

Non-Sleep Mode configuration (or how to prevent Sleep Mode after RESET)

  1. Oak powered through USB or VIN/GND
  2. The DS18B20 is powered by Oak's VCC (3.3V)
  3. Black wires - Ground connection
  4. Red wires - VCC 3.3V
  5. Yellow wire - pin 10 (WAKE) to RESET (RST)
  6. White wire – pin 2 to DS18B20 DQ (note: 4.7K pull-up resister)
  7. Blue wire – pin 5 to GND (ground)


Non-Sleep Mode Configuration

Note the addition of the Blue wire connecting pin 5 to ground. In non-sleep mode the Oak enters setup() and reads pin 5 to determine the desired mode. Since pin 5 is pulled to ground (LOW) the sketch will enter loop() where it continually requests temperature data from the DS18B20 and publishes that date to the Dashboard log. Data is requested at intervals set by “sleepTimeS” which specifies the number of seconds to delay between readings.

If the Oak was started in Sleep Mode (pin 5 HIGH), it can be forced into non-sleep mode by simply connecting pin 5 to ground. At the next wake-up / reset, the sketch will see pin 5 high and enter Non-Sleep mode. This mode will allow new sketches to be uploaded.

  Digistump Oak example reading the DS18B20 digital thermometer with
  periodic calls to particle.Publish() to log temperature readings.
   * account
   * Digistump Oak with current firmware and active on
   * DS18B20 OneWire temperature probe
   * 4.7K ohm resistor
   * OneWire library with ESP8266 support
   * Periodic temperature reads - frequency controlled by sleepTimeS
   * Publishes readings to
   * Enters deep sleep to conserve power when Sleep_pin = HIGH 
     WARNING: Pin 10-WAKE must be connected to RESET pin for wakeup to occur
     else you will enter and endless sleep.
   * Watches Sleep_pin to determine whether or not to go into deep sleep. 
     HIGH = sleep, LOW enters loop() and non sleep mode
     NOTE: Non sleep mode allows for new sketch uploads
#include <OneWire.h> // Using OneWire lib from the Oak package

int Sleep_pin = 5;   // (Configure as INPUT_PULLUP to default HIGH)
int DS18S20_Pin = 2; // DS18S20 Signal on pin 2
int OakLEDpin = 1;   // Oak onboard LED pin 1

int sleepTimeS = 30; // 30 seconds - adjust a needed

float tempF = 0;     // Temperature in degrees F
char tempFstring[6]; // Used to store a temperature value as a string

//Temperature chip i/o
OneWire ds(DS18S20_Pin);

void setup(void) {
  pinMode(Sleep_pin, INPUT_PULLUP); // Use pullup mode to default HIGH
  pinMode(OakLEDpin, OUTPUT);
  digitalWrite(OakLEDpin,HIGH); // Turn on onboard LED
  //Particle.publish("Oak Setup", "Started", 60, PRIVATE);
  if (!ds.reset()) {
     Particle.publish("Oak Setup", "DS18B20 not found", 60, PRIVATE);
  else {
     Particle.publish("Oak Setup", "DS18B20 present", 60, PRIVATE);
     // Discard first reading (sometimes in error after power up)
     tempF = getTemp(); // Ignore this one
     tempF = getTemp(); // Use this reading 
     sprintf(tempFstring, "%d", (int)tempF); // Convert to string
     Particle.publish("Temp (F)", tempFstring, 60, PRIVATE);
  //Particle.publish("Oak Setup", "Complete", 60, PRIVATE);
  delay(1000); // Pluse stretch onboard LED

  // Check to see if sleep mode selected
  if (digitalRead(Sleep_pin) == HIGH) {
     Particle.publish("Oak Setup", "Entering Deep Sleep", 60, PRIVATE);
     ESP.deepSleep(sleepTimeS*1000000, WAKE_RF_DEFAULT); // Sleep
  else {
     Particle.publish("Oak Setup","Entering Non Sleep Mode",60,PRIVATE);
} //END setup()

void loop(void) {

  // We are here because Non Sleep Mode selected. 
  // Will remains in loop() until reset or power cycle occurs.

  digitalWrite(OakLEDpin,HIGH); // Turn on onboard LED
  tempF = getTemp();
  sprintf(tempFstring, "%d", (int)tempF); // Convert to string

  // Check for errors (-1000 no device, -2000 CRC error, -3000 device not recognized)
  if(tempF <= -1000) {
    Particle.publish("DS18B20 Error", tempFstring, 60, PRIVATE);
  else {
    Particle.publish("Temp (F)", tempFstring, 60, PRIVATE);
  digitalWrite(OakLEDpin,LOW); // Turn off onboard LED

  // Delay before next read
  for(int i=0; i<sleepTimeS; i++) {
     delay(1000); // This delay * sleepTimes slows updates to log
} //END loop()

// OneWire DS18B20 temperature reader with conversion to degrees F
float getTemp(){
  //returns the temperature from one DS18S20 in DEG Fahrenheit

  byte data[12];
  byte addr[8];

  if ( ! {
      //no more sensors on chain, reset search
      //Serial.println("No more addresses.");
      return -1000;

  if ( OneWire::crc8( addr, 7) != addr[7]) {
      //Serial.println("CRC is not valid!");
      return -2000;

  if ( addr[0] != 0x10 && addr[0] != 0x28) {
      //Serial.print("Device is not recognized");
      return -3000;

  ds.write(0x44,1); // start conversion, with parasite power on at the end

  byte present = ds.reset();;    
  ds.write(0xBE); // Read Scratchpad

  for (int i = 0; i < 9; i++) { // we need 9 bytes
    data[i] =;
  byte MSB = data[1];
  byte LSB = data[0];

  float tempRead = ((MSB << 8) | LSB); //using two's compliment
  float TemperatureSum = tempRead / 16;
  // Convert to degrees Fahrenheit
  TemperatureSum = TemperatureSum * 9 / 5 + 32;
  return TemperatureSum; 
} //END getTemp()


This tutorial has demonstrated how to connect and read temperature data from the DS18B20 thermometer. It provided an example of how to publish data to the Dashboard log. And finally, it demonstrated the deep sleep capabilities of the Oak.

While the example sketch may be more complicated that necessary, the hope is that it has served as a useful example for creating new sketches with the Oak platform. Feel free to use and modify this example to create your own sketches using the concepts demonstrated here.

Finally, we hope that you have found this tutorial to be both useful and informative.

oak/tutorials/onewiretemp.1458512059.txt.gz · Last modified: 2016/03/20 15:14 by exeng