DS18B20 Arduino User Manual – Part 4 – Alarms

Introduction to Using DS18B20 Alarms on an Arduino

18B20 Temp Sensor AlarmsOne of the cooler things about the DS18B20 sensor is that you have the ability to establish alarm set points.   These set points can come in handy in terms of creating temperature protection and/or control within your projects.

In this tutorial, we’ll examine how to set alarm set points,  how to determine what the set points are and finally, how to check to see if the sensor is in an alarm state.

To run this tutorial, you will need to include the OneWire and Dallas Temperature libraries.    You can find a link to the Dallas Temperature library HERE.

DS18B20 Arduino Alarm Tutorial

Connect the DS18B20 to the Arduino

This is the same set up we’ve used in previous tutorials.

DS18B20 Ardunio Connections

Copy, Paste and Upload the DS18B20 Arduino Alarm Sketch

The sketch below makes use of the following functions:

  • setHighAlarmTemp – This function sets a set point that, when exceeded by the measured temperature, raises an alarm state.
  • setLowAlarmTemp – This function sets a set point that is raised when the measured temperature is less than the set point.
  • getHighAlarmTemp – Returns the High Alarm set point
  • getLowAlarmTemp – Returns the Low Alarm set pont

Be sure to substitute the address of your DS18B20 into the sketch.  If you don’t know your address, you can learn how to acquire it HERE.

#include <OneWire.h>
#include <DallasTemperature.h>

// Create a Onewire Referenca and assign it to pin 10 on your Arduino
OneWire oneWire(10);

// declare as sensor referenec by passing oneWire reference to Dallas Temperature. 
DallasTemperature sensors(&oneWire);

// declare your device address
// YOUR ADDRESS GOES HERE!!!!
DeviceAddress tempSensor = {0x28, 0xFF, 0x2B, 0x45, 0x4C, 0x04, 0x00, 0x10};

//Alarm Set Points
char lowSetPoint = 28;
char hiSetPoint = 30;

// A Variable to hold the temperature you retrieve
float tempC;

void setup(void)
{
  char alarmTemp;
  // start your serial port
  Serial.begin(9600);
  // Start up the DallasTemperature library
  sensors.begin();
  
  // alarm when temp is higher than value assigned to hiSetPoint
  sensors.setHighAlarmTemp(tempSensor, hiSetPoint);  
  // alarm when temp is lower than value assigned to lowSetPoint
  sensors.setLowAlarmTemp(tempSensor, lowSetPoint);
  
  // Print the alarm set points to the serial monitor
  
  // Set Measurement Resolution
  sensors.setResolution(tempSensor, 12);
  
  alarmTemp = sensors.getHighAlarmTemp(tempSensor);
  Serial.print("High Alarm is set to ");
  Serial.print(alarmTemp, DEC);
  Serial.println(" degrees C");
  
  alarmTemp = sensors.getLowAlarmTemp(tempSensor);  
  Serial.print("Low Alarm is set to ");
  Serial.print(alarmTemp, DEC);
  Serial.println(" degrees C");
}


void loop(void)
{ 
  // tell your sensor to measure the temperature
  
  sensors.requestTemperaturesByAddress(tempSensor); // Send the command to get temperatures
  Serial.print("Current Temperature is ");
  Serial.print(sensors.getTempC(tempSensor),4);
  Serial.print(" degrees C \t");
  
  // See if the temperature is above the high set point or below the low set point
  
  if (sensors.hasAlarm(tempSensor))
  {
    Serial.println("Alarm");
  }
  else{
    Serial.println("No Alarm");    
  }
  delay(1000);
}

Open Your Serial Monitor and View the Results

Your output should look like the picture below.

DS18B20 Arduino Tutorial 4 Output

Obviously,  you will need to vary the temperature to see the device move in and out of an alarm state.   Something cold and something warm along with a little set point tweaking will do the trick.

2 Comments

  1. capnfatz@gmail.comAuthor July 3, 2016
  2. qazerty June 30, 2016

Add a Comment

Your email address will not be published. Required fields are marked *