Arduino and DS18B20 basic warning

In a previous example at arduino ds18b20 example we showed you a basic usage of a DS18B20 which outputted a 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

#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, DANGER, DANGER”);
}
else
{
Serial.print(sensors.getTempCByIndex(0));
Serial.println(” degrees C”);
}
}

In action

DS18b20 warning

Related posts

VEML6070 ultraviolet light sensor on an Arduino

Programming an Attiny44 using an Arduino

Programming an Attiny85 using an Arduino