How to use raspberry pi 2 model B with DS18B20 probe to read temperature
- User
How to use raspberry pi 2 model B with DS18B20 probe to read temperature
- Assistant
How to use raspberry pi 2 model B with DS18B20 probe to read temperature
To use a DS18B20 temperature sensor with your Raspberry Pi 2 Model B, you'll need to connect the sensor to the Raspberry Pi, enable the 1-Wire interface, and then read the temperature data using the terminal or a Python script. Here's a step-by-step guide to help you through the process.
The DS18B20 is a digital temperature sensor that communicates over a 1-Wire bus. It requires only one data line (and ground) to communicate with the Raspberry Pi, making it simple to connect.
DS18B20 Pinout:
Power Connections:
Data Line Connection:
Pin Mapping Reference:
| DS18B20 Pin | Raspberry Pi Pin | Description |
|---|---|---|
| VDD | 1 (3.3V) | Power Supply |
| GND | 6 (GND) | Ground |
| DQ | 7 (GPIO4) | 1-Wire Data Communication |
Note: Be careful with the wiring to avoid short circuits. Double-check connections before powering up.
Access the Raspberry Pi Configuration Tool:
Open a terminal on your Raspberry Pi and enter:
sudo raspi-config
Navigate the Configuration Menu:
Select Interfacing Options.
Select 1-Wire.
When prompted to enable the 1-Wire interface, select Yes.
Exit the configuration tool and reboot your Raspberry Pi:
sudo reboot
Alternatively, you can enable 1-Wire by adding the following line to the /boot/config.txt file:
dtoverlay=w1-gpio
After rebooting, load the necessary kernel modules:
sudo modprobe w1-gpio
sudo modprobe w1-therm
To make sure these modules load on every boot, add them to /etc/modules:
sudo nano /etc/modules
Add the following lines at the end of the file:
w1-gpio
w1-therm
Save and exit (Ctrl+X, then Y, then Enter).
Once the modules are loaded, you can access the sensor data through the device file system.
Navigate to the directory where 1-Wire devices are listed:
cd /sys/bus/w1/devices/
ls
You should see a directory that starts with 28- followed by a unique serial number (e.g., 28-0000065af6ff). This is your DS18B20 sensor.
Navigate into the sensor's directory and read the temperature:
cd 28-xxxxxxxxxxxx # Replace with your sensor's directory
cat w1_slave
You'll get an output similar to:
ff 00 4b 46 ff ff 0c 10 2c : crc=2c YES
ff 00 4b 46 ff ff 0c 10 2c t=23125
The temperature is given after t= in thousandths of degrees Celsius. In this example, t=23125 means the temperature is 23.125°C.
For convenience, you can write a Python script to read and display the temperature more elegantly.
Create a Python Script:
sudo nano /home/pi/temperature.py
Add the Following Code:
#!/usr/bin/python
import os
import glob
import time
os.system('modprobe w1-gpio')
os.system('modprobe w1-therm')
base_dir = '/sys/bus/w1/devices/'
device_folder = glob.glob(base_dir + '28*')[0]
device_file = device_folder + '/w1_slave'
def read_temp_raw():
with open(device_file, 'r') as f:
lines = f.readlines()
return lines
def read_temp():
lines = read_temp_raw()
while lines[0].strip()[-3:] != 'YES':
time.sleep(0.2)
lines = read_temp_raw()
equals_pos = lines[1].find('t=')
if equals_pos != -1:
temp_string = lines[1][equals_pos+2:]
temp_c = float(temp_string) / 1000.0
temp_f = temp_c * 9.0 / 5.0 + 32.0
return temp_c, temp_f
while True:
temp_c, temp_f = read_temp()
print('Temperature: {:.2f}°C / {:.2f}°F'.format(temp_c, temp_f))
time.sleep(1)
Save and Exit:
Press Ctrl+X, then Y, then Enter to save the script.
Make the Script Executable:
chmod +x /home/pi/temperature.py
Run the Script:
python /home/pi/temperature.py
You should see the temperature readings displayed in both Celsius and Fahrenheit.
No Sensor Detected:
CRC Errors or Unstable Readings:
Multiple Sensors:
/sys/bus/w1/devices/.You've successfully connected a DS18B20 temperature sensor to your Raspberry Pi and read temperature data. You can integrate the readings into larger projects, log the data over time, or even create a web server to monitor the temperature remotely.
Note: Always ensure your Raspberry Pi is turned off while connecting or disconnecting hardware to prevent damage.
Not yet included in a published release.