Skip to content

Instantly share code, notes, and snippets.

@rob-smallshire
Created November 17, 2012 19:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rob-smallshire/4099336 to your computer and use it in GitHub Desktop.
Save rob-smallshire/4099336 to your computer and use it in GitHub Desktop.
Arduino based LED digital thermometer based on DS18B20
// 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);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment