public
Created

Arduino based LED digital thermometer based on DS18B20

  • Download Gist
LedThermometer.ino
Arduino
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
// An Arduino sketch for continually reading the temperature from
// a Dallas DS18B20 digital thermometer and turning on or off red,
// yellow or green LEDs to indicate the temperature range
 
// OneWire 2 library (an improved version of the original OneWire)
// http://www.pjrc.com/teensy/td_libs_OneWire.html
#include <OneWire.h>
 
// Dallas Temperature Control library from
// http://milesburton.com/Dallas_Temperature_Control_Library
#include <DallasTemperature.h>
 
// The LEDs are connected to digital outputs 4, 5 and 6
enum { RED = 4, YELLOW = 5, GREEN = 6 };
 
// Data wire is plugged into digital input 7 on the Arduino
const int ONE_WIRE_BUS = 7;
 
// Setup a oneWire instance to communicate with any OneWire devices (not just Maxim/Dallas temperature ICs)
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);
 
DeviceAddress insideThermometer = { 0x28, 0xEA, 0x10, 0x72, 0x03, 0x00, 0x00, 0xCF };
 
void setup(void)
{
pinMode(RED, OUTPUT);
pinMode(YELLOW, OUTPUT);
pinMode(GREEN, OUTPUT);
Serial.begin(9600);
Serial.println("Dallas Temperature IC Control Library Demo");
 
Serial.print("Locating devices...");
sensors.begin();
Serial.print("Found ");
Serial.print(sensors.getDeviceCount(), DEC);
Serial.println(" devices.");
 
Serial.print("Parasite power is: ");
if (sensors.isParasitePowerMode()) Serial.println("ON");
else Serial.println("OFF");
Serial.print("Device 0 Address: ");
printAddress(insideThermometer);
Serial.println();
 
// set the resolution to 9 bit (Each Dallas/Maxim device is capable of several different resolutions)
sensors.setResolution(insideThermometer, 9);
Serial.print("Device 0 Resolution: ");
Serial.print(sensors.getResolution(insideThermometer), DEC);
Serial.println();
}
 
void printTemperature(DeviceAddress deviceAddress)
{
float tempC = sensors.getTempC(deviceAddress);
Serial.print(tempC);
if (tempC < 20.0) {
digitalWrite(RED, LOW);
digitalWrite(YELLOW, LOW);
digitalWrite(GREEN, HIGH);
}
else if (tempC >= 20.0 && tempC < 25.0) {
digitalWrite(RED, LOW);
digitalWrite(YELLOW, HIGH);
digitalWrite(GREEN, LOW);
}
else if (tempC >= 25.0 && tempC <= 50.0) {
digitalWrite(RED, HIGH);
digitalWrite(YELLOW, LOW);
digitalWrite(GREEN, LOW);
}
else {
digitalWrite(RED, HIGH);
digitalWrite(YELLOW, HIGH);
digitalWrite(GREEN, HIGH);
}
}
 
void loop(void)
{
sensors.requestTemperatures();
printTemperature(insideThermometer);
Serial.println();
}
 
void printAddress(DeviceAddress deviceAddress)
{
for (uint8_t i = 0; i < 8; i++)
{
if (deviceAddress[i] < 16) Serial.print("0");
Serial.print(deviceAddress[i], HEX);
}
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.