Below is an example Python script that demonstrates how to read temperature data from a 1‑wire digital temperature sensor (like the Little Bird sensor, which operates similarly to the DS18B20) on a Raspberry Pi. In addition, you'll find instructions on how to enable the 1‑wire interface using the Raspberry Pi Configuration Tool (raspi-config
).
-
Open Terminal:
Open a terminal on your Raspberry Pi. -
Run Raspberry Pi Configuration Tool:
sudo raspi-config
-
Navigate to Interfacing Options:
- Select Interfacing Options.
- Then choose 1-Wire.
- When prompted, select Yes to enable the 1‑wire interface.
-
Finish and Reboot:
Exitraspi-config
and reboot your Raspberry Pi for the changes to take effect:sudo reboot
If you prefer, you can manually enable the interface by adding the following line to your /boot/config.txt
file:
dtoverlay=w1-gpio
After editing, reboot your Raspberry Pi.
Below is an example Python script that reads temperature data from your sensor. The script loads the required kernel modules (w1-gpio
and w1-therm
), locates the sensor device directory, and parses the temperature value from the sensor’s data file.
#!/usr/bin/env python3
import os
import glob
import time
# Load the necessary kernel modules (optional if already loaded at boot)
os.system('modprobe w1-gpio')
os.system('modprobe w1-therm')
# Base directory where 1-wire devices are listed
base_dir = '/sys/bus/w1/devices/'
# Find the device folder for our sensor (typically starts with '28-')
device_folders = glob.glob(base_dir + '28*')
if not device_folders:
raise RuntimeError("Could not find a 1-wire device. Ensure your sensor is connected and 1-wire is enabled.")
device_folder = device_folders[0]
device_file = device_folder + '/w1_slave'
def read_raw_data():
"""Read the raw data from the sensor file."""
with open(device_file, 'r') as f:
lines = f.readlines()
return lines
def read_temperature():
"""Parse the raw sensor data to extract temperature in Celsius and Fahrenheit."""
lines = read_raw_data()
# Wait until the sensor output is valid (first line ends with 'YES')
while lines[0].strip()[-3:] != 'YES':
time.sleep(0.2)
lines = read_raw_data()
# Extract the temperature value from the second line after the 't=' marker
equals_pos = lines[1].find('t=')
if equals_pos != -1:
temp_string = lines[1][equals_pos + 2:]
temp_c = float(temp_string) / 1000.0 # Convert to Celsius
temp_f = temp_c * 9.0 / 5.0 + 32.0 # Convert to Fahrenheit
return temp_c, temp_f
else:
raise RuntimeError("Couldn't read temperature value.")
if __name__ == '__main__':
try:
while True:
temp_c, temp_f = read_temperature()
print("Temperature: {:.3f}°C / {:.3f}°F".format(temp_c, temp_f))
time.sleep(1) # Wait 1 second before the next reading
except KeyboardInterrupt:
print("Exiting...")
-
Enabling 1‑Wire:
- The instructions above guide you through enabling the 1‑wire interface via
raspi-config
(or by editing/boot/config.txt
), ensuring the necessary kernel modules are available.
- The instructions above guide you through enabling the 1‑wire interface via
-
Loading Kernel Modules:
- The script uses
os.system()
to loadw1-gpio
andw1-therm
. If these modules are already loaded via the boot configuration, these commands are optional.
- The script uses
-
Locating the Sensor:
- The sensor is recognized as a device folder in
/sys/bus/w1/devices/
with an identifier starting with28-
. The script uses theglob
module to find this directory.
- The sensor is recognized as a device folder in
-
Reading and Parsing Sensor Data:
- The sensor outputs data to a file called
w1_slave
. The script reads this file, verifies that the first line ends withYES
(indicating a valid reading), and extracts the temperature (provided in thousandths of a degree Celsius), converting it to both Celsius and Fahrenheit.
- The sensor outputs data to a file called
-
Continuous Readings:
- The script runs in an infinite loop, printing the temperature every second until you stop the program (e.g., by pressing Ctrl+C).
This example should help you get started with using the Little Bird 1‑Wire Digital Temperature Sensor on your Raspberry Pi for environmental sensing, data logging, and IoT applications.