Display temperature on I2C LCD using DS18B20

Status
Not open for further replies.

a.sparrow

Newbie level 1
Joined
Apr 21, 2013
Messages
1
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
1,292
Hi everyone,
I am a newbie programmer and I am hoping that someone could guide me on the right path to completing this project.
I am using a PIC24HJ64GP502 chip to display the current temperature on an 20X4 I2C LCD screen. The temperature sensor I am using is a DS18B20 and I will be using a 5V power supply with a 4.7k ohm resistor. I am having trouble on how to approach this project. So far, I have used functions found in the libraries from the sensor and the lcd to print out the temperature. I have not run this on the actual board yet. I am not sure if the configuration part of the lcd is correct. I got it from a sample code project from the LCD's website. If anyone could help me find a tutorial on I2C initialization/configuration that would be helpful. I want to know if by using these functions, will I be able to print it onto the LCD.
Also, do I need to convert the temperature values into characters, in order to see it on the screen? I saw that implementation and I am not sure if it is necessary. Thank you in advance!


Code:
#include<OneWire.h> 
#include<DallasTemperature.h> // temperature sensor
#include<LiquidCrystal_I2C.h> // lcd screen


// Data wire is plugged into pin 4 on the Pic
#define ONE_WIRE_BUS 4

// Setup a oneWire instance to communicate with any OneWire devices
OneWire oneWire(ONE_WIRE_BUS); 

int DS18S20_Pin = 4; //DS18S20 Signal pin on analog 2 (pin4)
OneWire ds(DS18S20_Pin); 

DallasTemperature sensors(&oneWire); // Pass our oneWire reference to Dallas Temperature. 


// http://www.hacktronics.com/Tutorials/arduino-1-wire-address-finder.html
// Need to use the above url to determine the address of our 1-Wire temp sensor! Below has incorrect addresses. 
DeviceAddress insideThermometer = { 0x28, 0x94, 0xE2, 0xDF, 0x02, 0x00, 0x00, 0xFE };

#define BACKLIGHT_PIN     13  [COLOR="#FF0000"]// I am only using VCC, GND, SCL, and SDA pins. Should I be using other pins as well? [/COLOR]

LiquidCrystal_I2C lcd(0x38);  // Set the LCD I2C address

//LiquidCrystal_I2C lcd(0x38, BACKLIGHT_PIN, POSITIVE);  // Set the LCD I2C address


// Creat a set of new characters
const uint8_t charBitmap[][8] = {
   { 0xc, 0x12, 0x12, 0xc, 0, 0, 0, 0 },
   { 0x6, 0x9, 0x9, 0x6, 0, 0, 0, 0 },
   { 0x0, 0x6, 0x9, 0x9, 0x6, 0, 0, 0x0 },
   { 0x0, 0xc, 0x12, 0x12, 0xc, 0, 0, 0x0 },
   { 0x0, 0x0, 0xc, 0x12, 0x12, 0xc, 0, 0x0 },
   { 0x0, 0x0, 0x6, 0x9, 0x9, 0x6, 0, 0x0 },
   { 0x0, 0x0, 0x0, 0x6, 0x9, 0x9, 0x6, 0x0 },
   { 0x0, 0x0, 0x0, 0xc, 0x12, 0x12, 0xc, 0x0 }
   
};


void setup(void)
{
  //pinMode(backLight, OUTPUT);
  //digitalWrite(backLight, HIGH); // turn backlight on. Replace 'HIGH' with 'LOW' to turn it off.
  lcd.begin(20, 4);              // rows, columns for a 20x4 LCD
  lcd.clear();                   // start with a blank screen
  lcd.setCursor(0,0);            // set cursor to column 0, row 0 (upper left hand corner)

  Serial.begin(9600); // start serial port
  Serial.println("DS18B20 Demo");
  sensors.begin();
  sensors.setResolution(insideThermometer, 10); //sensor precision is 10 bits (can be 9,10,11,or 12 bits)

  delay(2000);
  lcd.print("Requesting temperature..."); 
  sensors.requestTemperatures(); // command to get the temperatures
  lcd.print("COMPLETE");

}

void loop(void)
{ 
  //Print the temperature
  lcd.clear();
  lcd.setCursor(0,0);
  lcd.print("Current Temperature is: "); 
  lcd.setCursor(0,1); // set cursor at the second row on the lcd. 
  lcd.print(sensors.getTempC(0)); 
  lcd.print(" °C  "); // there will be a small space between the two temperature readings
  lcd.print(sensors.getTempF(0)); 
  lcd.print(" °F");
  delay(1000);
}
 
Last edited:

Status
Not open for further replies.

Similar threads

Cookies are required to use this site. You must accept them to continue using the site. Learn more…