Home Code DS18B20 basic warning example

DS18B20 basic warning example

by shedboy71

In a previous example we showed you a basic usage of a DS18B20 which outputted the temperature to the Serial output.

A simple addition to the code can show a practical example, in this case if the temperature exceeds a certain value then a message is displayed. In real life this could be an alarm of some direction.

 

Code
[c]
#include <OneWire.h>
#include <DallasTemperature.h>

//the pin you connect the ds18b20 to
#define DS18B20 2

OneWire ourWire(DS18B20);
DallasTemperature sensors(&ourWire);

void setup()
{
Serial.begin(9600);
delay(1000);
//start reading
sensors.begin();
}

void loop()
{
//this sets the max temp
int maxTemp = 30;
//read temperature and output via serial
sensors.requestTemperatures();
//if temperature exceeds maxTemp value
if(sensors.getTempCByIndex(0)>maxTemp)
{
Serial.println(“DANGER”);
}
else
{
Serial.print(sensors.getTempCByIndex(0));
Serial.println(” degrees C”);
}
}

[/c]

Share

You may also like