Temperature Sensor with LCD

Hi Everyone,

This is my first post and I would really appreciate your advice! I'm doing a project where I want the DS18B20 temperature sensors to appear on the LCD screen and I am really struggling with the code as it keeps coming up with errors. Attached is a diagram of my circuit and my code. Would anyone who has experience with this please help!

Thanks so much!!

TempSensor_project.ino (3.23 KB)

You must have a very good reason to not use the common DS18B20 libraries.
Reading a DS18B20 can be done with just two lines of code.
Leo..

#include <DallasTemperature.h>
#include <OneWire.h>

OneWire oneWire(6); // pin D6
DallasTemperature sensors(&oneWire);
float temp;

void setup() {
  Serial.begin(115200);
  Serial.println("DS18B20 thermometer");
  sensors.begin();
}

void loop() {
  sensors.requestTemperatures();
  temp = sensors.getTempCByIndex(0);
  Serial.print("Temp is ");
  Serial.print(temp, 4); // four decimal places
  Serial.println(" C");
}

Sayuni:
my first post and I would really appreciate your advice

Welcome to the forum. The first bit of advice is to read the "how to use this forum stickies" and post your code in the proper manner.

Next, you are using what looks suspiciously like a 9v PP3 battery. If so, this is a really bad idea and will surely lead to grief, and sooner rather than later. It is not as if Arduinos consume much power, but PP3s have no power at all.

Next, you do not appear to be using the 4k7 pullup resistor required. Don't expect a result without it.

Sensor in the Fritzing seems to be a LM35 or TMP36 temp sensor.
Leo..

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
}

Wawa:
Sensor in the Fritzing seems to be a LM35 or TMP36 temp sensor.

I thought you know better than trying to glean important, useful information from a Fritzing... Code at least seems to be for a DS18B20 sensor.

Sayuni:
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?

[...]

*raw_temp_value |= (unsigned int)(ds18b20_read_byte() << 8); // Read temperature MSB byte and store it on

It would be a good start for you to follow advice given, in this case to actually read the sticky.

Indeed, why not using the DS18B20 library? Much easier. An LCD screen with I2C backpack also makes life a lot easier.

On my site Thesolaruniverse at Wordpress I have posted some time ago an illustrated description and a sketch on how to get the DS18B20 temperature sensor working. This is a very useful and economic sensor for weather stations, room temperature, water temperatures, temperature of electric gear and so forth After writing that article I continued playing with measuring and displaying temperatures with TFT display shields on the Arduino because these offer much more fun that 2x16, 4x20, 128x64 LCD screens or 128x32 OLED screens. Thanks for providing the suggestion to dedicate an article on my site to the most basic of things: reading a DS18B20 sensor and displaying the readings on LCD and OLED displays.

see The DS18B20 temperature sensor – implementation with an Arduino – thesolaruniverse

  • have fun and good luck!

Sayuni:
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?

You already have that in your code.
lcd.setCursor(4, 1);
lcd.print(temp);
So it's not working?

#define DS18B20_PIN 2
LiquidCrystal lcd(8, 7, 5, 4, 3, 2);
So you're using pin2 for the LCD and the sensor?

Again, why the complicated code.
Just two lines can pick up the temp from the sensor.
And two more lines could print it to LCD.
Leo..

@photoncatcher

"Because every DS18B20 has its own unique ID it is necessary to know what that ID is."

Not true (the necessary part).
You can read the sensor(s) by index.
temp1 = sensors.getTempCByIndex(0); // first sensor
temp2 = sensors.getTempCByIndex(1); // second sensor
etc.
Leo..