How to easily measure temperature with a Raspberry Pi

For a long time I wanted to do something simple - measure room temperature affordably using a Raspberry Pi or something like that. But for some reason, I could never find the right solution. I’m not an electronics person, and I’ve long made a policy decision that I’m not going to solder anything. However, I could apparently not reasonably spend myself out of this problem by buying some kind of USB thermometer.

Full shopping list: any Raspberry Pi with headers (pins), a DS18B20, three female-female DuPont wires.

So here’s the very easy solution I found, and with this page I hope to make it easy for you as well. The instructions here are up to date for late 2026, unlike some other pages you might find.

I settled on the DS18B20 digital thermometer, which is very well established and available in many forms. You can get a transistor-like looking variant, but there are also probes, many of which can also be submerged in water.

You can buy these for next to nothing money (‘1.5 euro’), and despite this there are counterfeit versions around, and I hear these can suck. So be careful.

The DS18B20 is a “1-Wire” device, yet still requires three wires, but let’s not get too upset about that, since it is all rather wonderful. It claims ±0.5C accuracy, and while I have not done any real tests of that, it measures the same temperatures as the professional Honeywell thermostat it has replaced.


The TO-92 form factor - from the data sheet

The three wires/pins are called GND, DQ and Vdd. The manual states that you should mount a 5kΩ resistor between Vdd and DQ. But, it turns out the Raspberry Pi can be configured to provide a 50kΩ resistor function, and this also works for my use case (more about this later).

On your Raspberry, run the amazingly useful pinout command:


Output as shown on a Raspberry Pi Zero 2

Compare this to what your actual Raspberry Pi looks like, paying special attention to spotting where pin 1 is. If you mess this up you could fry your devices! Hint, check the colors, pin 1 is cyan in the picture above, and pins 2 and 4 are red.

Next up, hook up the GND pin of your DS18B20 to any of the ground pins (which you can recognize in the pinout picture above by having no label). Connect Vdd to any of the 3.3V pins. And connect DQ to GPIO14 (which is pin 8).

You then end up with this:


Note that we are facing the DS18B20’s arched side, so we are seeing the pins in Vdd, DQ, GND order. Vdd is connected to pin 1 on the Raspberry Pi

Next up, we need to tell the Raspberry that we hooked up a 1-Wire device to GPIO14. There are multiple ways to do this, but this is nicely persistent. Add the following to the existing /boot/firmware/config.txt:

dtoverlay=w1-gpio,gpiopin=14

Next up, reboot the Raspberry, and then run this command to enable the 50kΩ “pull-up” (pu) resistor:

pinctrl set 14 pu

And you should be in business! All 1-Wire devices show up in /sys/bus/w1/devices, and the DS18B20 ones all have a serial number that starts with 28:

$ cat /sys/bus/w1/devices/28-*/temperature 
19625

This is 19.625C. Touch the sensor for a bit and repeat the command and you should see a higher temperature.

Note that the ‘pinctrl’ command is not persistent and needs to be repeated for every power-up! Also note, it appears there is no special reason to specifically use GPIO14.

Linux Details

Behind the scenes, the kernel is running the 1W protocol for you, and you should see the following modules active:

$ lsmod | grep w1
w1_therm               24576  0
w1_gpio                12288  0
wire                   49152  2 w1_gpio,w1_therm

In the /sys/bus/w1/devices/28* directory you can find lower level access to 1-Wire messages.

The 1-Wire protocol is rather neat, and it also supports much longer cables, and also many devices sharing a single bus. When doing this, you’ll likely need the recommended 5kΩ resistor. Helpfully, there are many adapters on sale with that resistor built in, and which also make it easier to use DS18B20 variants that give you three wires instead of three pins:

Good luck!

Do let me know if you find mistakes on this page, or if you manage to get this to work in ‘parasitic mode’ with only two wires!