The Stalker sensor platform : Part 2 - Reading a digital temperature sensor

Previously - The Stalker sensor platform : Part 2 - Reading a digital temperature sensor

DS18B20_

Last time we've seen how to connect the Stalker and how to upload a first sketch. This time we'll learn how to communicate through serial communication with the computer, reading analogue Voltage values and how to read in a digital "One Wire" temperature sensor.

We need for this tutorial:

     1. Read an analogue Value

Connect the Stalker like we did in the last chapter. Now let's start to write some code to read in an analogue value and to send it through Serial Communication to the Serial Monitor in the Arduino IDE. You can use the example code coming with the Arduino IDE:  File --> Examples --> Basics --> AnalogReadSerial

 void setup() {
// initialize serial communication at
// 9600 bits per second:
Serial.begin(9600);
}

// the loop routine runs forever:
void loop() {
// read the input on analog pin 0:
int sensorValue = analogRead(A0);
// print out the value you read:
Serial.println(sensorValue);
// delay in between reads for stability
delay(1);
}

analogread_serial_monitor Serial Monitor Arduino IDE

!!!Please remember that for the Stalker board the input Voltage should not exceed 3.3V!!!

We can test this code by connecting 0 or 3.3V to the Pin 0 of our Stalker, you should see the scaled Voltage value (0-3.3V = 0-1023) in the Serial Monitor (Tools --> Serial Monitor).

 

     2. Connect a digital temperature sensor

The DS18B20 is a widely used digital temperature sensor with a special "One Wire Bus" which supports supplying the sensor through the data line (Parasitic Power).

 

DS18B20_pinout_schematic DS18B20 wiring diagram

Connect the sensor according the schematic using a Breadboard.

#include

OneWire ds(8); // pin 8

void setup() {
Wire.begin();
Serial.begin(9600);
}

void loop() {
Serial.print(temp());
}

float temp() {
byte i;
// lower and upper temperature data byte
byte data[2];
// 64bit address, every sensor has a unique address
byte addr[8];
float celsius;

ds.search(addr);
ds.reset();
ds.select(addr);
// A/D to scratchpad, takes up to 750ms
ds.write(0x44,1);
delay(1000);
ds.reset();
// select sensor
ds.select(addr);
// read Scratchpad comand
ds.write(0xBE);
// get lower temperature byte
data[0] = ds.read();
// get upper temperature byte
data[1] = ds.read();
// shift upperbyte 8 digits left
// and or it with lowerbyte
int raw = (data[1] << 8 ) | data[0];
// positive temp value
if (raw < 0x7FF)
//conversion sensor data to Celsius
celsius = (float)raw / 16.0;
// negative temp value
else
//conversion sensor data to Celsius
celsius = (((float)raw-0xFFFF)/16);
return celsius;
}

To get this sensor working, we have to import the OneWire library for Arduino. Just copy the unzipped folder into the library folder of the arduino IDE and the OneWire library should appear in:  Sketch --> Import Library... --> OneWire Note: The library will not be recognized by the Arduino IDE if you just copy and paste this code (even if you've installed the library). You have to add it in:  Sketch --> Import Library... --> OneWire

DS18B20_read_serial_monitor Serial Monitor with temperature data

You can try now simply to touch the sensor to create a temperature increase.

Next:

The Stalker sensor platform : Part 3 - Getting time stamped temperature data