CODE 1
#include <OneWire.h>
#include <DallasTemperature.h>
// 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);
int val = 0;
int lightval = 0;
int potPin = 0;
void setup(void)
{
// start serial port
delay (3000);
Serial.begin(57600);
Serial.println(“Dallas Temperature IC Control Library
Demo”);
// Start up the library
sensors.begin();
}
// celcius to fahrenheit conversion
float c2f(float val) {
float aux = (val 9 / 5);
return (aux + 32);
}
void loop(void)
{
lightval = analogRead(potPin);
// read the value from
// the sensor
// call sensors.requestTemperatures() to issue a global
// temperature request to all devices on the bus
// Serial.print(“Requesting temperatures...”);
sensors.requestTemperatures();
// Send the command to get temperatures
// Serial.println(“DONE”);
// Serial.print(“Temperature for Device 1 is: “);
Serial.print(“c0001|”);
Serial.print(lightval);
Serial.print(“|”);
Serial.print(c2f(sensors.getTempCByIndex(0)));
// You can have more than one IC on the same bus.
// 0 refers to the first IC on the wire
Serial.print(“|”);
Serial.print(c2f(sensors.getTempCByIndex(1)));
Serial.print(“|”);
Serial.println(c2f(sensors.getTempCByIndex(2)));
delay (20000);
// 20 second delay between data line output
}
38
December 2010
Make note of which sensor corresponds
to each data value in your output data
stream.
My projects focus on capturing log
data, then pushing it to some type of
plotting system. Taking a temperature
reading from about once a second up to
once every five minutes works well
enough for my purposes.
The readings are packaged up using
print statements and sent over the serial
line to my Linux notebook. I capture the
data from the serial line using a simple
"cat /dev/ttyUSB0 > rob.txt &" command
line on the notebook. The kst program
then plots the data using the rob.txt file as
input. Figure 5 shows a screenshot of
sample captured temperature data using
kst.
Getting the data from the Arduino to
my notebook is via a pair of 2 mw Xbee
digital radios acting as a serial connection.
You can also connect a USB cable
between the Arduino and notebook. The
cable configuration is typically used for
programming and troubleshooting.
Naturally, the serial connection (digital
pins 0 and 1) should be disconnected
between the Arduino and Xbee when
hooking up the USB cable. Otherwise,
you'll get an error when attempting to
upload a program from the Arduino IDE. I
upped the communication rate from the
default of 9600 to 57600 bits/second.
Standard Design
Practices
Effectively using the Dallas sensors
means installing them in your projects, so
that they report the desired temperature
in a predictable and reliable way.
There are four main challenges to
successfully adding the Dallas sensor to
your project. They include placement in
your device, shielding, connections, and
protecting the device.
Placement and Shielding
My design for the compost
temperature probe called for a sensor to
measure ambient air temperature, two
sensors to measure temperatures down in
the compost, and a photocell to record
the amount of sunlight falling on the heap.
I originally mounted the air