Raspberry Pi - Temperature Sensor - LCD

This tutorial instructs you how to use Raspberry Pi to read the temperature from a DS18B20 one-wire sensor and then display it on an LCD I2C.

Hardware Preparation

1×Raspberry Pi 4 Model B
1×LCD I2C
1×DS18B20 Temperature Sensor (WITH Adapter)
1×DS18B20 Temperature Sensor (WITHOUT Adapter)
1×4.7 kΩ resistor
1×Breadboard
1×Jumper Wires
1×(Optional) Screw Terminal Adapter for Raspberry Pi

Or you can buy the following sensor kits:

1×DIYables Sensor Kit (30 sensors/displays)
1×DIYables Sensor Kit (18 sensors/displays)
Disclosure: Some of the links provided in this section are Amazon affiliate links. We may receive a commission for any purchases made through these links at no additional cost to you. We appreciate your support.

Buy Note: Many DS18B20 sensors available in the market are unreliable. We strongly recommend buying the sensor from the DIYables brand using the link provided above. We tested it, and it worked reliably.

Overview of Temperature Sensor and LCD

If you are unfamiliar with temperature sensor and LCD (pinout, how it works, how to program ...), the following tutorials can help you out:

Wiring Diagram

  • Wiring diagram using a terminal adapter (recommended).
The wiring diagram between Raspberry Pi and DS18B20 Temperature Sensor LCD

This image is created using Fritzing. Click to enlarge image

We recommend buying a DS18B20 sensor along with its accompanying wiring adapter for a seamless setup. This adapter includes an integrated resistor, removing the need for an additional resistor in the wiring.

Raspberry Pi Code

※ NOTE THAT:

The address of the LCD can differ depending on the manufacturer. In our code, we used 0x27 which is specified by DIYables manufacturer.

Detailed Instructions

  • Make sure you have Raspbian or any other Raspberry Pi compatible operating system installed on your Pi.
  • Make sure your Raspberry Pi is connected to the same local network as your PC.
  • Make sure your Raspberry Pi is connected to the internet if you need to install some libraries.
  • If this is the first time you use Raspberry Pi, See how to set up the Raspberry Pi
  • Connect your PC to the Raspberry Pi via SSH using the built-in SSH client on Linux and macOS or PuTTY on Windows. See to how connect your PC to Raspberry Pi via SSH.
  • Make sure you have the RPi.GPIO library installed. If not, install it using the following command:
sudo apt-get update sudo apt-get install python3-rpi.gpio
  • Prior to utilizing the DS18B20 temperature sensor with a Raspberry Pi, we need to enable 1-Wire interface on Raspberry Pi. See How to enable 1-Wire interface on Raspberry Pi
  • Install the library for DS18B20 temperature sensor by running the following command:
pip install w1thermsensor
pip install lcddriver
  • Create a Python script file temperature_lcd.py and add the following code:
# This Raspberry Pi code was developed by newbiely.com # This Raspberry Pi code is made available for public use without any restriction # For comprehensive instructions and wiring diagrams, please visit: # https://newbiely.com/tutorials/raspberry-pi/raspberry-pi-temperature-sensor-lcd import lcddriver import time from w1thermsensor import W1ThermSensor # Initialize the LCD lcd = lcddriver.lcd() # Find the connected DS18B20 sensor def find_ds18b20_sensor(): for sensor in W1ThermSensor.get_available_sensors(): if sensor.type == W1ThermSensor.THERM_SENSOR_DS18B20: return sensor return None # Read temperature from the sensor def read_temperature(sensor): temperature_celsius = sensor.get_temperature() return temperature_celsius # Find DS18B20 sensor ds18b20_sensor = find_ds18b20_sensor() if ds18b20_sensor is not None: print(f"DS18B20 Sensor found: {ds18b20_sensor.id}") try: while True: # Read temperature temperature_c = read_temperature(ds18b20_sensor) # Display temperature on LCD lcd.lcd_display_string(f"Temp: {temperature_c:.2f} C", 1) # Wait for a moment before reading again time.sleep(2) lcd.lcd_clear() except KeyboardInterrupt: pass else: print("DS18B20 Sensor not found.")
  • Save the file and run the Python script by executing the following command in the terminal:
python3 temperature_lcd.py

The script runs in an infinite loop continuously until you press Ctrl + C in the terminal.

  • Put the sensor to both hot and cold water, or hold it in your hand.
  • Check the LCD for the outcome.

If the LCD does not show anything, . consult Troubleshooting on LCD I2C . for assistance.

Code Explanation

Check out the line-by-line explanation contained in the comments of the source code!

Video Tutorial

※ OUR MESSAGES

  • As freelancers, We are AVAILABLE for HIRE. See how to outsource your project to us
  • Please feel free to share the link of this tutorial. However, Please do not use our content on any other websites. We invested a lot of effort and time to create the content, please respect our work!