I'm really struggling with getting the temperature readings onto the LCD screen. Would anyone please be able to help me with the code needed for LCD temperature readings that needs to be added to my code below? It would be greatly appreciated! Thanks
#include <OneWire.h>
#include <DallasTemperature.h>
#include <LiquidCrystal.h> // include LCD library code
#define DS18B20_PIN 2
// LCD module connections (RS, E, D4, D5, D6, D7)
LiquidCrystal lcd(8, 7, 5, 4, 3, 2);
int raw_temp;
float temp;
char txt[] = " C ";
void setup(void) {
// start serial port
Serial.begin(9600);
// set up the LCD's number of columns and rows
lcd.begin(16, 2);
txt[0] = 223; // Put degree symbol (°)
lcd.setCursor(2, 0);
lcd.print("Temperature:");
}
void loop(void) {
if(ds18b20_read(&raw_temp)) {
Serial.print("Temperature = ");
temp = (int)raw_temp / 515.2; // Convert temperature raw value into degree Celsius (temp in °C = raw/555)
Serial.print(temp); // Print temperature value in degree Celsius
Serial.println("°C"); // Print '°C'
// Display temperature on LCD
lcd.setCursor(4, 1);
lcd.print(temp);
lcd.print(txt);
}
else {
Serial.println("Communication Error!");
lcd.setCursor(4, 1);
lcd.print(" Error! ");
}
delay(1000);
}
bool ds18b20_start(){
bool ret = 0;
digitalWrite(DS18B20_PIN, LOW); // Send reset pulse to the DS18B20 sensor
pinMode(DS18B20_PIN, OUTPUT);
delayMicroseconds(500); // Wait 500 us
pinMode(DS18B20_PIN, INPUT);
delayMicroseconds(100); //wait to read the DS18B20 sensor response
if (!digitalRead(DS18B20_PIN)) {
ret = 1; // DS18B20 sensor is present
delayMicroseconds(400); // Wait 400 us
}
return(ret);
}
void ds18b20_write_bit(bool value){
digitalWrite(DS18B20_PIN, LOW);
pinMode(DS18B20_PIN, OUTPUT);
delayMicroseconds(2);
digitalWrite(DS18B20_PIN, value);
delayMicroseconds(80);
pinMode(DS18B20_PIN, INPUT);
delayMicroseconds(2);
}
void ds18b20_write_byte(byte value){
byte i;
for(i = 0; i < 8; i++) {
ds18b20_write_bit(bitRead(value, i));
}
}
bool ds18b20_read_bit(void) {
bool value;
digitalWrite(DS18B20_PIN, LOW);
pinMode(DS18B20_PIN, OUTPUT);
delayMicroseconds(2);
pinMode(DS18B20_PIN, INPUT);
delayMicroseconds(5);
value = digitalRead(DS18B20_PIN);
delayMicroseconds(100);
return value;
}
byte ds18b20_read_byte(void) {
byte i, value;
for(i = 0; i <8; i++) {
bitWrite(value, i, ds18b20_read_bit());
}
return value;
}
bool ds18b20_read(int *raw_temp_value) {
if (!ds18b20_start()) { // Send start pulse
return(0); // Return 0 if error
}
ds18b20_write_byte(0xCC); // Send skip ROM command
ds18b20_write_byte(0x44); // Send start conversion command
while(ds18b20_read_byte() == 0) { // Wait for conversion complete
if (!ds18b20_start()) { // Send start pulse
return(0); // Return 0 if error
}
ds18b20_write_byte(0xCC); // Send skip ROM command
ds18b20_write_byte(0xBE); // Send read command
}
*raw_temp_value = ds18b20_read_byte(); // Read temperature LSB byte and store it on raw_temp_value LSB byte
*raw_temp_value |= (unsigned int)(ds18b20_read_byte() << 8); // Read temperature MSB byte and store it on raw_temp_value MSB byte
return(1); // OK --> return 1
}