Raspberry Pi - Temperature Sensor - OLED
This tutorial instructs you how to use Raspberry Pi to acquire the temperature from a DS18B20 one-wire sensor and then display it on an OLED.
|Raspberry Pi 4 Model B
|SSD1306 I2C OLED Display 128x64
|DS18B20 Temperature Sensor (WITH Adapter)
|DS18B20 Temperature Sensor (WITHOUT Adapter)
|4.7 kΩ resistor
|(Optional) Screw Terminal Adapter for Raspberry Pi
Or you can buy the following sensor kit:
|DIYables Sensor Kit 30 types, 69 units
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 OLED and DS18B20 Temperature Sensor
If you are not familiar with OLED and DS18B20 Temperature Sensor (including pinout, functioning, programming, etc.), the following tutorials can help you:
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 - Temperature from DS18B20 Temperature Sensor and display it on OLED
- 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 OLED display with a Raspberry Pi, we need to enable I2C interface on Raspberry Pi. See How to enable I2C interface on Raspberry Pi
- Install the OLED library by running the following command:
- Create a Python script file DS18B20_OLED.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.
- Position the sensor over hot and cold water, or hold it in your hand.
- Check out the outcome on the OLED display.