/*
DS18B20 temperature sensor demo
- - - - - - - - - - - - - - - - - -
The LCD circuit:
* LCD pin 1 (gnd) to gnd
* LCD pin 2 (Vcc) to +5v
* LCD pin 3 (VO) to wiper of 20k pot between Vcc and gnd (contrast adj)
* LCD pin 4 (RS) to digital pin 9
* LCD pin 5 (R/W) to gnd
* LCD pin 6 (clock/enable) to digital pin 8
* LCD pin 7 (D0) UNUSED
* LCD pin 8 (D1) UNUSED
* LCD pin 9 (D2) UNUSED
* LCD pin 10 (D3) UNUSED
* LCD pin 11 (D4) to digital pin 7
* LCD pin 12 (D5) to digital pin 6
* LCD pin 13 (D6) to digital pin 5
* LCD pin 14 (D7) to digital pin 4
http://www.arduino.cc/en/Tutorial/LiquidCrystal
- - - - - - - - - - - - - - - - - -
The DS18B20 circuit:
* DS18B20 pin 1 to gnd (black)
* DS18B20 pin 2 to digital pin 2 (yellow)
* DS18B20 pin 3 to Vcc (red)
http://www.hobbytronics.co.uk/ds18b20-arduino
- - - - - - - - - - - - - - - - - -
*/
// include the library code for LCD:
#include <LiquidCrystal.h>
#include <SPI.h>
#include "printf.h"
// include library code for DS18B20
#include <OneWire.h>
#include <DallasTemperature.h>
const
uint8_t num_rows = 2;
uint8_t num_cols = 16;
uint8_t scan_wide = (num_rows - 1) * num_cols;
// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(7, 6, 5, 4, 3, 2);
// Data wire is plugged into pin 2 on the Arduino
#define ONE_WIRE_BUS 2
// Setup a oneWire instance to communicate with any OneWire devices
// (not just Maxim/Dallas temperature ICs)
OneWire oneWire(ONE_WIRE_BUS);
// Pass our oneWire reference to Dallas Temperature.
DallasTemperature sensors(&oneWire);
// Pin 13 has an LED connected on most Arduino boards.
// give it a name:
int led = 13;
bool bLedOn = false;
// function to print a device address
void printAddress(DeviceAddress deviceAddress)
{
for (uint8_t i = 0; i < 8; i++)
{
if (deviceAddress[i] < 16) lcd.print("0");
lcd.print(deviceAddress[i], HEX);
}
}
#define TEMPERATURE_PRECISION 11
DeviceAddress tempDeviceAddress; // We'll use this variable to store a found device address
void SetRes(int i)
{
if(sensors.getAddress(tempDeviceAddress, i))
{
int maxRes = sensors.getResolution(tempDeviceAddress);
do
{
// try the next highest number of bits
sensors.setResolution(tempDeviceAddress, maxRes+1);
// if it doesn't get set correctly
if (sensors.getResolution(tempDeviceAddress) != maxRes+1)
{
// must have gone too far
break;
}
// That's OK, let's try for 1 more
maxRes++;
}
while (1==1); // loop forever
// set to the highest found
sensors.setResolution(tempDeviceAddress, maxRes);
}
}
float temp;
void setup()
{
// set up the LCD's number of columns and rows:
lcd.begin(num_cols, num_rows);
// Print a message to the LCD.
lcd.print("DS18B20 Demo");
// initialize the digital pin as an output.
pinMode(led, OUTPUT);
// Start up the sensor library
sensors.begin();
lcd.setCursor(0, 1);
SetRes(0);
sensors.requestTemperatures(); // Send the command to get temperatures
temp = sensors.getTempCByIndex(0);
}
void loop()
{
// set the cursor to column 0, line 1
// (note: line 1 is the second row, since counting begins with 0):
lcd.setCursor(0, 1);
sensors.requestTemperatures(); // Send the command to get temperatures
temp = (temp*4 + sensors.getTempCByIndex(0))/5;
lcd.print(temp);
lcd.print("/");
lcd.print(sensors.getResolution(tempDeviceAddress), DEC);
digitalWrite(led, bLedOn);
bLedOn = !bLedOn;
}