Temperature reading

Im using a Mega 2560, 16x2 LCD and a DS18b20 sensor and Im trying to make a sketch that will switch a relay when the temp meets a certain temp. Everything’s is wired up on breadboard and at the moment the reading just hovers aroung 155 Celsius. Im very new but nothing jumps out at me. Can someone please point out the obvious.

// Attempting to control a Chiller and Heater 
    #include <LiquidCrystal.h>
    #include <OneWire.h>
    int reading = 0;
    int sensorPin =A0; // Temperature data pin
    int relay =2; // Relay pin
    // initialize the library with the numbers of the interface pins
    LiquidCrystal lcd(8, 9, 4, 5, 6, 7);
    void setup() {
    // set up the LCD's number of columns and rows:
    lcd.begin(16, 2);
    void loop() {
    reading = analogRead(sensorPin); 
    int celsius = reading/2;
    lcd.setCursor(0, 0);
    lcd.print("Temperature: ");	
    lcd.print(celsius, DEC);
    if (celsius >28) {
    } else {


The sketch you have provided is written for an analog temperature sensor. These provide temperature measurement by just setting their output to a voltage, dependant on the temperature.

However, you say that you have a DS18B20 sensor. This is a digital device that is a tad more sophisticated. It uses a single wire to send and receive data as a digital stream of bits.

Have a look at this page to get you started http://playground.arduino.cc/Learning/OneWire

I knew it was something simple, I hadnt thought of that. Thanks Ken


at the moment the reading just hovers aroung 155 Celsius.

that is quite a temperature!

We had a cool change, it was 511 8)

I recommend that you download the Dallas Temperature library as well to use this device.

Your program will contain #include <DallasTemperature.h>

There are good examples with the library, and it will make programming much more simple, unless of course you want the challenge of using OneWire alone and doing basically everything the library does to get a temperature out of the DS18B20.

Thanks Cattledog. Im having a bit of trouble including it so it reads correctly still. Its doing my head in, but I'll persevere


I found this the best tutorial