science tinker

Thermometer on an OLED display: Raspberry Pi Pico project with wiring and code

Breadboard wiring for Thermometer on an OLED display, as drawn in the simulator.

Show the temperature from a DS18B20 sensor on a 128 × 64 OLED display. The OLED is on the I²C pins GP4 (SDA) and GP5 (SCL). The data leg of the DS18B20 is on GP22, with a 4.7 kΩ resistor to 3.3 V. The top line of the display says "Temperature". Under it show the reading with one decimal place and the letter C, for example "22.0 C". Read the sensor again and again, so that the display follows the temperature.

  • Raspberry Pi Pico
  • Advanced level
  • 4 parts
  • Free, in your browser

You will learn: Two MicroPython drivers working together: ssd1306 for the display (fill, text, show) and onewire with ds18x20 for the sensor (scan, convert_temp, read_temp). Why a DS18B20 needs 750 ms for a measurement, and why the display is cleared before each new frame.

Project 83 of 85 in the project gallery. Lesson 7 of 7 in the course Raspberry Pi Pico with MicroPython.

The idea behind this page: how a microcontroller project works

A microcontroller project has three sides that must all be right: the parts, the wiring and the program. A microcontroller is a small computer on one chip. Its pins can read inputs, such as a button or a sensor, and switch outputs, such as an LED, a buzzer or a motor. The program, called a sketch on an Arduino, is the list of instructions that says what the outputs should do when the inputs change. Almost every project follows the same pattern: read an input, decide, then set an output, and repeat this many times a second.

A guided project gives you a goal, the parts, the wiring and starter code with gaps for you to complete. The simulator then checks your work by running it, the way a teacher would test a real circuit. The solution is not shown on this page, because working it out is how the ideas are learned.

This page is about one guided project: Thermometer on an OLED display. Read it with these ideas in mind.

How to study this page

  1. Read the goal at the top, then the parts table to see what each part is for.
  2. Study the wiring table and the picture: which leg of each part goes to which pin.
  3. Read the starter code line by line before you change it. Find the place where your own lines must go.
  4. Open the project in the simulator, complete the code and press Run. Read any message the check gives you: it says what is wrong and what to write.
  5. When it passes, change one thing (a pin, a delay, a limit) and predict what will happen before you run it again.

Mistakes students make

  • Using a different pin in the code from the one in the wiring.The pin number in the sketch must match the pin the wire really goes to.
  • Leaving out pinMode() in setup().A pin must be set as an input or an output before it is used, or it will not behave as expected.
  • Changing many things at once.Change one thing, run, and see what happens. Then you know which change caused the result.
  • Ignoring the check message.The message names the fault. Read it fully before trying again.

Key words

Microcontroller
A small, complete computer on one chip, with a processor, memory and input and output pins.
Sketch
The Arduino name for a program.
Pin (of a microcontroller)
A metal contact on a chip or board through which it reads an input or drives an output.
Input
A signal going into the microcontroller, for example from a button or a sensor.
Output
A signal coming out of the microcontroller, for example to an LED or a motor.
Breadboard
A board with rows of joined holes for building a circuit without soldering.

See every key word in the glossary

What to learn first and next

Learn this first
Learn this next

Parts you need

PartHow manyWhat it does in this project
Raspberry Pi Pico1Runs your Python program and powers the circuit.
OLED display (SSD1306)1The output you read. It is on the I²C pins GP4 (SDA) and GP5 (SCL) and shows the picture each time show() is called.
Digital temperature sensor (DS18B20)1The sensor. Its data leg is on GP22 with a 4.7 kΩ resistor to 3.3 V, and one measurement takes 750 ms.
4.7 kΩ resistor1Sets a voltage level for the pin to read.
Breadboard and jumper wires1Hold the parts and join them without soldering.

How to wire it

Put the parts on the breadboard, join the board's 5V and GND pins to the breadboard's power rails, then make these connections:

PartConnection
Digital temperature sensorDQ → GP22
OLED displayI²C address 0x3C: SDA → GP4, SCL → GP5

In the Studio, the 💡 guide rings show where each wire goes, and the ✔ Check button tells you which connection is missing.

Starter code

Start from this Python program and write the missing lines. The comments say what each part has to do.

from machine import Pin, I2C
import ssd1306, onewire, ds18x20
import time

i2c = I2C(0, sda=Pin(4), scl=Pin(5))
oled = ssd1306.SSD1306_I2C(128, 64, i2c)           # width, height, bus

ds = ds18x20.DS18X20(onewire.OneWire(Pin(22)))     # DS18B20 data leg on GP22
roms = ds.scan()                                   # the addresses of the sensors found

while True:
    ds.convert_temp()                 # ask the sensor to measure
    # 1. wait for the measurement:  time.sleep_ms(750)
    celsius = ds.read_temp(roms[0])
    # 2. clear the picture:  oled.fill(0)
    # 3. write the title at the top left:  oled.text("Temperature", 0, 0)
    # 4. write the reading lower down:  oled.text(str(round(celsius, 1)) + " C", 0, 24)
    # 5. send the picture to the display:  oled.show()

Build it step by step

  1. Open the project. Press Build it in the Studio. The parts are already in the tray.
  2. Place and wire the parts. Follow the wiring table above, or switch on the guide rings.
  3. Write the code. Complete the starter program in the Code panel. It is Python, written as text.
  4. Run it. Press ▶ Run and use the part's slider, button or switch to test it. The terminal panel shows what your program prints, and Stop works like Ctrl+C.
  5. Check it. Press ✔ Check: the simulator tests the circuit and the code and tells you exactly what to fix.

Questions students ask

What does the Thermometer on an OLED display project do?

Show the temperature from a DS18B20 sensor on a 128 × 64 OLED display. The OLED is on the I²C pins GP4 (SDA) and GP5 (SCL). The data leg of the DS18B20 is on GP22, with a 4.7 kΩ resistor to 3.3 V. The top line of the display says "Temperature". Under it show the reading with one decimal place and the letter C, for example "22.0 C". Read the sensor again and again, so that the display follows the temperature.

What parts do I need for the Thermometer on an OLED display project?

An Raspberry Pi Pico, OLED display, digital temperature sensor, 1 × 4.7 kΩ resistor, a breadboard and jumper wires.

How do I wire the Thermometer on an OLED display project?

Digital temperature sensor: DQ → GP22; OLED display: I²C address 0x3C: SDA → GP4, SCL → GP5. Power comes from the board's 5V (or 3.3V) and GND pins along the breadboard rails.

What will I learn from the Thermometer on an OLED display project?

Two MicroPython drivers working together: ssd1306 for the display (fill, text, show) and onewire with ds18x20 for the sensor (scan, convert_temp, read_temp). Why a DS18B20 needs 750 ms for a measurement, and why the display is cleared before each new frame.

Can I build this without real hardware?

Yes. The project runs in the free Science Tinker microcontroller simulator in your browser: place the parts, wire them, write the sketch and press Run. The ✔ Check button tests your circuit and code and tells you what to fix. Nothing to install and no account needed.

Where is the finished code?

The page gives the starter sketch so that you write the important lines yourself. If you are stuck, the 💡 Solution button in the Studio shows a complete, working sketch that you can run and study.

More advanced projects

Components in this project

Know someone who would use this?

It’s free for every learner. Send it to your class, team or club.

Updated · Free, no sign-up ·