Industrial manufacturing
Industrial Internet of Things | Industrial materials | Equipment Maintenance and Repair | Industrial programming |
home  MfgRobots >> Industrial manufacturing >  >> Manufacturing Technology >> Manufacturing process

How to Read Temperature with a DS18B20 on Raspberry Pi 2

Welcome! This guide shows you how to connect a DS18B20 digital temperature sensor to a Raspberry Pi 2 and read the room temperature accurately.

What You’ll Need

Step 1 – Wiring the Sensor

The DS18B20 uses a 1‑Wire interface. Connect it as follows:

Place the 4.7 kΩ resistor between the 3.3 V line and the DQ line to act as a pull‑up. The resistor’s “3.3 V” end should touch the red (VDD) side.

Step 2 – Enable the 1‑Wire Interface

1. Edit the boot configuration file:

sudo nano /boot/config.txt

2. Add or uncomment the line:

dtoverlay=w1-gpio

3. Save and exit, then reboot:

sudo reboot

Step 3 – Load Kernel Modules

After the Pi restarts, load the 1‑Wire drivers:

sudo modprobe w1-gpio
sudo modprobe w1_therm

Step 4 – Verify the Sensor is Detected

Check the device tree for the sensor ID:

ls -l /sys/bus/w1/devices/

You should see a folder beginning with 28-, e.g., 28-000006dfa76c.

Step 5 – Read the Temperature

Each sensor exposes its data through w1_slave. View it with:

cat /sys/bus/w1/devices/28-000006dfa76c/w1_slave

The output contains two lines; the second line ends with t=XXXXX, where XXXXX is the temperature in thousandths of a degree Celsius. For example, t=23450 equals 23.450 °C.

Optional – Parse the Value with a Script

Here’s a quick Python snippet to convert the raw reading into °C:

#!/usr/bin/env python3
import time, glob
sensor = glob.glob('/sys/bus/w1/devices/28-*/w1_slave')[0]
while True:
    with open(sensor, 'r') as f:
        lines = f.readlines()
    if lines[0].strip().endswith('YES'):
        temp_raw = lines[1].split('t=')[1]
        temp_c = float(temp_raw) / 1000.0
        print(f"Temperature: {temp_c:.3f} °C")
    else:
        print("Reading error")
    time.sleep(2)

Save this as temp_read.py, make it executable, and run it to get live temperature updates.

By following these steps, you’ll have a reliable temperature monitor running on your Raspberry Pi 2.

For deeper insights, see the Adafruit DS18B20 guide.

Manufacturing process

  1. DS18B20 Temperature Sensor – Precise 1‑Wire Digital Thermometer for Industrial & Consumer Use
  2. Build a Raspberry Pi Temperature Logger with a $5 I2C Sensor
  3. Connect Multiple DS18B20 1‑Wire Sensors to a Raspberry Pi for Accurate Temperature Monitoring
  4. Professional Raspberry Pi Temperature Monitoring with DS18B20
  5. Measuring Temperature on Raspberry Pi with Maxim 1‑Wire Sensors and DS2482 I2C Bridge
  6. Accurate Temperature Monitoring in a Server Closet with Raspberry Pi
  7. How to Connect a DS18B20 One‑Wire Digital Thermometer to a Raspberry Pi – A Step‑by‑Step Guide
  8. Build a ThingSpeak Temperature Monitor with Raspberry Pi & BrickPi
  9. Build a Raspberry Pi Home Temperature Monitor with MCP9808, InfluxDB & Grafana
  10. Read Temperature & Humidity with DHT11 on Blynk – Step‑by‑Step Arduino Tutorial