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.

Hardware Preparation

1×Raspberry Pi 4 Model B
1×SSD1306 I2C OLED Display 128x64
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
1×(Optional) Power Adapter for Raspberry Pi 4B
1×(Optional) Plastic Case for Raspberry Pi 4B

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.
Additionally, some of these links are for products from our own brand, DIYables.

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:

Wiring Diagram

The wiring diagram between Raspberry Pi and DS18B20 Temperature Sensor OLED

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

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 Adafruit-SSD1306
  • Create a Python script file DS18B20_OLED.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-oled """ import time from w1thermsensor import W1ThermSensor import Adafruit_SSD1306 from PIL import Image, ImageDraw, ImageFont # Set up the DS18B20 sensor DS18B20 = W1ThermSensor() # Set up the SSD1306 OLED display RST = None OLED = Adafruit_SSD1306.SSD1306_128_64(rst=RST) OLED.begin() OLED.clear() OLED.display() # Set up fonts and drawing width = OLED.width height = OLED.height image = Image.new('1', (width, height)) draw = ImageDraw.Draw(image) font = ImageFont.load_default() while True: # Read temperature from DS18B20 sensor temperature = DS18B20.get_temperature() # Clear the OLED display draw.rectangle((0, 0, width, height), outline=0, fill=0) # Display temperature on OLED draw.text((5, 5), f'Temperature: {temperature:.2f} C', font=font, fill=255) # Update the OLED display OLED.image(image) OLED.display() # Wait for a moment before reading again time.sleep(2) except KeyboardInterrupt: # Clean up code before exiting the script OLED.clear() OLED.display() print("\nExiting the script.")
  • Save the file and run the Python script by executing the following command in the terminal:
python3 DS18B20_OLED.py

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.

※ 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!