Introduction: Temperature Sensor Using Arduino Uno
Using a DS18b20 sensor with Arduino to take the temperature and turn on an LED
Step 1: MATERIALS YOU WILL NEED
1. One Breadboard
2. One Arduino Uno
3. One DS18b20 Temperature Sensor
4. One LED
5. Two 4.7k resistor
6. 5 jumper wires
Step 2: TEMP SENSOR CIRCUT
This is a very simple digital sensors to hookup. It has a power and ground as well as a single digital signal pin that we will be connecting to digital pin 2 on our arduino. It also needs a 4.7k resistor between the signal and power pin. See image for example.
Step 3: ADDING AN LED
For this I decided I wanted to add and LED. I wanted the LED to turn on when it got below a certain temperature and turn off when the temperature became warmer again.
For this you will need to place your LED in line with your temperature sensor on your bread board. The negative (flat side) leg will be closest to the temperature sensor. You will need to run a jumper wire from the positive leg of the LED to Ground. The Negative leg needs a jumper wire that runs to pin 13. You also need to place the other resistor at the negative pin of the led and the wire going back to pin 13 on your Arduino.
Step 4: CODE
#include
int DS18S20_Pin = 2; //DS18S20 Signal pin on digital 2 int ledPin = 2; //Temperature chip i/o OneWire ds(DS18S20_Pin); // on digital pin 2
void setup(void) { Serial.begin(9600); digitalWrite(ledPin,HIGH); delay(500); digitalWrite(ledPin, LOW); }
void loop(void) { float temperature = getTemp(); //will take about 750ms to run Serial.println(temperature);
if (temperature <= 24) { digitalWrite(ledPin, HIGH); } else { digitalWrite(ledPin, LOW); } }
float getTemp(){ //returns the temperature from one DS18S20 in DEG Celsius
byte data[12]; byte addr[8];
if ( !ds.search(addr)) { //no more sensors on chain, reset search ds.reset_search(); return -1000; }
if ( OneWire::crc8( addr, 7) != addr[7]) { Serial.println("CRC is not valid!"); return -1000; }
if ( addr[0] != 0x10 && addr[0] != 0x28) { Serial.print("Device is not recognized"); return -1000; }
ds.reset(); ds.select(addr); ds.write(0x44,1); // start conversion, with parasite power on at the end delay(750); // Wait for temperature conversion to complete
byte present = ds.reset(); ds.select(addr); ds.write(0xBE); // Read Scratchpad
for (int i = 0; i < 9; i++) { // we need 9 bytes data[i] = ds.read(); } ds.reset_search(); byte MSB = data[1]; byte LSB = data[0];
float tempRead = ((MSB << 8) | LSB); //using two's compliment float TemperatureSum = tempRead / 16; return TemperatureSum; }