Projects / Pi probe temperature log to a file

Pi probe temperature log to a file

A Raspberry Pi 4 reads a DS18B20 probe on the one-wire bus and writes a timestamped line to a CSV file each minute, ready to open in a spreadsheet.

Intermediate2 hGuided buildReviewed

Build it step by step

0 of 7 done
  1. Pi 4

    Shut the Pi down

    Then unplug power.

  2. Pi 4DS18B2026.4°C

    Wire the probe

    Red to pin 1, black to pin 6, yellow to pin 7.

  3. Pi 4

    Add the pull-up

    4.7 kilo-ohm from pin 7 to pin 1.

  4. Pi 4

    Enable one-wire

    raspi-config, then reboot.

  5. Pi 4DS18B2026.4°C

    Check the probe

    ls /sys/bus/w1/devices shows a 28 folder.

  6. Pi 4

    Run the program

    A line prints each minute.

  7. Pi 4

    Open the CSV

    Chart it in a spreadsheet.

In short

How do you build a pi probe temperature log to a file?

A Raspberry Pi 4 reads a DS18B20 probe on the one-wire bus and writes a timestamped line to a CSV file each minute, ready to open in a spreadsheet. You need Raspberry Pi 4 Model B (4 GB), DS18B20 waterproof probe, Resistor 4.7 kΩ and Breadboard 830 Tie Points. It takes 7 steps and about 120 minutes.

Level
Intermediate
Time
About 120 min
Parts
4

Parts list

Tick what you already own. It saves to My Garage, so “Build with what I have” knows.

Wiring

FromToWhy
DS18B20 redPi pin 1 (3.3 V)Power at 3.3 V
DS18B20 blackPi pin 6 (GND)Ground
DS18B20 yellowPi pin 7 (GPIO4)One-wire data
4.7 kilo-ohm resistorBetween GPIO4 and 3.3 VPull-up for the data line

Code

The Pi has built-in support for the DS18B20 one-wire probe. Once the interface is on, the kernel finds the probe, and each probe shows up as a folder whose name starts with 28 and holds a small text file with the latest reading. The first line ends with YES when the checksum is good, and the second ends with t equals the temperature in thousandths of a degree. The program reads that file, ignores a bad checksum, and appends a line with a timestamp to a CSV file once a minute, which any spreadsheet can open and chart. The 4.7 kilo-ohm resistor pulls the data line up to 3.3 V, since the probe only pulls the line low. Readings are indicative only: a probe in air follows the temperature slowly and its metal tip must touch what you are measuring. Do not use it for food safety or medical decisions.

# Enable the one-wire interface first:  sudo raspi-config nonint do_onewire 0   then reboot
import glob, time, datetime

LOG = "/home/pi/temperature_log.csv"            # change pi to your user name

def read_probe():
    files = glob.glob("/sys/bus/w1/devices/28-*/w1_slave")
    if not files:
        return None
    with open(files[0]) as f:
        lines = f.read().splitlines()
    if len(lines) < 2 or not lines[0].endswith("YES"):   # YES means the checksum is good
        return None
    return int(lines[1].split("t=")[1]) / 1000.0

with open(LOG, "a") as f:
    if f.tell() == 0:
        f.write("time,celsius\n")

while True:
    t = read_probe()
    if t is not None:
        stamp = datetime.datetime.now().isoformat(timespec="seconds")
        with open(LOG, "a") as f:
            f.write("{},{:.2f}\n".format(stamp, t))
        print(stamp, "{:.2f}".format(t))
    time.sleep(60)

Test checklist

What you should see at each check. If a reading is far off, stop and use the troubleshooting list.

CheckExpected
Probe in airRoom temperature
Probe in warm waterValue rises
Checksum errorLine is skipped, no crash
CSV fileHeader and one line per minute
After rebootAppends to the same file

If it does not work

No 28 folder

Check the one-wire interface is on and the pull-up is fitted.

Reads 85 degrees

The probe has just powered up, wait for the next reading.

File not found

Change the LOG path to your home folder.

Permission error

Run as your own user and check the folder exists.

No lines written

Look for the checksum YES in the device file.

Still stuck? Find the fault step by step with your multimeter →

Ask Lark AI

Common questions

What parts do I need for the pi probe temperature log to a file?

Raspberry Pi 4 Model B (4 GB), DS18B20 waterproof probe, Resistor 4.7 kΩ and Breadboard 830 Tie Points.

How long does the pi probe temperature log to a file take to build?

About 120 minutes for a intermediate maker, in 7 steps.

How do I know the pi probe temperature log to a file works?

Probe in air: Room temperature. Probe in warm water: Value rises. Checksum error: Line is skipped, no crash.

No 28 folder: what should I check?

Check the one-wire interface is on and the pull-up is fitted.

Reads 85 degrees: what should I check?

The probe has just powered up, wait for the next reading.

File not found: what should I check?

Change the LOG path to your home folder.

Permission error: what should I check?

Run as your own user and check the folder exists.