# Temperature Sensor with LCD

**URL:** <https://forum.arduino.cc/t/temperature-sensor-with-lcd/533096>\
**Category:** Sensors\
**Created:** [June 23, 2018, 10:55pm UTC](https://forum.arduino.cc/t/temperature-sensor-with-lcd/533096 "2018-06-23T22:55:02Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![Sayuni](https://avatars.discourse-cdn.com/v4/letter/s/43a26b/32.png) [@Sayuni](https://forum.arduino.cc/u/Sayuni)\
**Post date:** [June 23, 2018, 10:55pm UTC](https://forum.arduino.cc/t/temperature-sensor-with-lcd/533096/1 "2018-06-23T22:55:02Z")

</div>

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](https://forum.arduino.cc/uploads/short-url/4JO1SdszcFbfmzB4WmInrcmI4Zb.ino) (3.23 KB)

 ![Screen Shot 2018-06-23 at 10.45.15 pm.png](https://europe1.discourse-cdn.com/arduino/original/3X/e/0/e015d336a81e00b5a0a3514b21de4c96a0048865.png)

---

<div class="post-metadata">

**Author:** ![Wawa](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/wawa/32/1211303_2.png) [@Wawa](https://forum.arduino.cc/u/Wawa)\
**Post date:** [June 23, 2018, 11:41pm UTC](https://forum.arduino.cc/t/temperature-sensor-with-lcd/533096/2 "2018-06-23T23:41:30Z")

</div>

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..

```cpp
#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");
}

```

---

<div class="post-metadata">

**Author:** ![Nick\_Pyner](https://avatars.discourse-cdn.com/v4/letter/n/e8c25b/32.png) [@Nick\_Pyner](https://forum.arduino.cc/u/Nick_Pyner)\
**Post date:** [June 24, 2018, 2:39am UTC](https://forum.arduino.cc/t/temperature-sensor-with-lcd/533096/3 "2018-06-24T02:39:55Z")

</div>

> 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.

---

<div class="post-metadata">

**Author:** ![Wawa](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/wawa/32/1211303_2.png) [@Wawa](https://forum.arduino.cc/u/Wawa)\
**Post date:** [June 24, 2018, 3:15am UTC](https://forum.arduino.cc/t/temperature-sensor-with-lcd/533096/4 "2018-06-24T03:15:22Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Sayuni](https://avatars.discourse-cdn.com/v4/letter/s/43a26b/32.png) [@Sayuni](https://forum.arduino.cc/u/Sayuni)\
**Post date:** [June 24, 2018, 12:37pm UTC](https://forum.arduino.cc/t/temperature-sensor-with-lcd/533096/5 "2018-06-24T12:37:54Z")

</div>

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  
}

---

<div class="post-metadata">

**Author:** ![wvmarle](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/wvmarle/32/1013044_2.png) [@wvmarle](https://forum.arduino.cc/u/wvmarle)\
**Post date:** [June 24, 2018, 12:56pm UTC](https://forum.arduino.cc/t/temperature-sensor-with-lcd/533096/6 "2018-06-24T12:56:55Z")

</div>

> 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](http://forum.arduino.cc/index.php/topic,148850.0.html).

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

---

<div class="post-metadata">

**Author:** ![photoncatcher](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/photoncatcher/32/221050_2.png) [@photoncatcher](https://forum.arduino.cc/u/photoncatcher)\
**Post date:** [June 24, 2018, 8:30pm UTC](https://forum.arduino.cc/t/temperature-sensor-with-lcd/533096/7 "2018-06-24T20:30:31Z")

</div>

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](https://thesolaruniverse.wordpress.com/2017/08/17/the-ds18b20-temperature-sensor-implementation-with-an-arduino/)

- have fun and good luck!

---

<div class="post-metadata">

**Author:** ![Wawa](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/wawa/32/1211303_2.png) [@Wawa](https://forum.arduino.cc/u/Wawa)\
**Post date:** [June 24, 2018, 10:29pm UTC](https://forum.arduino.cc/t/temperature-sensor-with-lcd/533096/8 "2018-06-24T22:29:37Z")

</div>

> 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..

---

<div class="post-metadata">

**Author:** ![Wawa](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/wawa/32/1211303_2.png) [@Wawa](https://forum.arduino.cc/u/Wawa)\
**Post date:** [June 24, 2018, 10:33pm UTC](https://forum.arduino.cc/t/temperature-sensor-with-lcd/533096/9 "2018-06-24T22:33:04Z")

</div>

@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..

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [May 6, 2021, 10:22am UTC](https://forum.arduino.cc/t/temperature-sensor-with-lcd/533096/10 "2021-05-06T10:22:01Z")

</div>


