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
Or you can buy the following sensor kits:
1 | × | DIYables Sensor Kit (30 sensors/displays) | |
1 | × | DIYables Sensor Kit (18 sensors/displays) |
Additionally, some of these links are for products from our own brand, DIYables.
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).
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:
- 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:
- Prior to utilizing the LCD I2C with a Raspberry Pi, we need to enable I2C interface on Raspberry Pi. See How to enable I2C interface on Raspberry Pi
- Install the LCD I2C library by running the following command:
- Create a Python script file temperature_lcd.py and add the following code:
- Save the file and run the Python script by executing the following command in the terminal:
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!