How to connect 2 ds18b20 temperature probe to raspberry pi 2 model b
- User
How to connect 2 ds18b20 temperature probe to raspberry pi 2 model b
- Assistant
How to connect 2 ds18b20 temperature probe to raspberry pi 2 model b
To connect two DS18B20 temperature probes to your Raspberry Pi 2 Model B, you'll need to wire them correctly and configure your Raspberry Pi to read their data. The DS18B20 sensors use the 1-Wire protocol, which allows multiple devices to be connected to the same data line. Below are detailed steps to help you set up and read temperature data from both sensors.
Materials Needed:
Step 1: Understanding the DS18B20 Sensor
Each DS18B20 sensor has three pins:
Note: Some DS18B20 sensors can operate in "parasite power" mode using only two wires (DQ and GND). However, it's recommended to use the three-wire method for reliability.
Step 2: Wiring the Sensors
Connect VDD Pins:
Connect GND Pins:
Connect DQ Pins:
Add the Pull-up Resistor:
Wiring Diagram:
Raspberry Pi Pin Layout (Physical Pins):
3.3V (Pin 1) ------
|
+------------+------------+
| | |
VDD VDD Resistor (4.7kΩ)
(Sensor 1) (Sensor 2) |
|
DQ (Sensor 1) ---+-----------+--- GPIO4 (Pin 7)
|
DQ (Sensor 2) ---+
|
GND (Pins 6, 9, etc.)
GND (Sensor 1)---+
|
GND (Sensor 2)----+
Step 3: Enable the 1-Wire Interface on the Raspberry Pi
Edit the Config File:
Open the Raspberry Pi configuration file with your preferred text editor:
sudo nano /boot/config.txt
Add the 1-Wire Overlay:
Add the following line at the end of the file to enable the 1-Wire interface on GPIO4:
dtoverlay=w1-gpio
Note: By default, w1-gpio uses GPIO4. If you wish to use a different GPIO pin, specify it like so:
dtoverlay=w1-gpio,gpiopin=4
Save and Exit:
Press Ctrl + X, then Y, and Enter to save and exit.
Reboot the Raspberry Pi:
sudo reboot
Step 4: Load the 1-Wire Kernel Modules
After rebooting, the necessary kernel modules should load automatically. If not, you can load them manually:
sudo modprobe w1-gpio
sudo modprobe w1-therm
To ensure these modules load on every boot, you can add them to the /etc/modules file:
Edit the Modules File:
sudo nano /etc/modules
Add the Modules:
Add the following lines:
w1-gpio
w1-therm
Save and Exit:
Press Ctrl + X, then Y, and Enter to save and exit.
Step 5: Verify the Sensors are Detected
Navigate to the Devices Directory:
cd /sys/bus/w1/devices/
List the Devices:
ls
You should see directories named after your sensors, starting with 28-. For example:
28-000005e2fdc3
28-0000065aadcd
w1_bus_master1
Each 28-xxxxxxxxxxxx represents a detected DS18B20 sensor.
Step 6: Read Temperature Data from the Sensors
Read Raw Data:
Replace 28-xxxxxxxxxxxx with your actual sensor IDs.
cat /sys/bus/w1/devices/28-000005e2fdc3/w1_slave
Sample Output:
3b 01 4b 46 7f ff 0c 10 ae : crc=ae YES
3b 01 4b 46 7f ff 0c 10 ae t=19062
Interpret the Data:
YES).t=19062 means 19.062°C.Repeat for the Other Sensor:
cat /sys/bus/w1/devices/28-0000065aadcd/w1_slave
Step 7: Create a Python Script to Read Temperatures (Optional)
For ease of use, you can create a Python script to read and display the temperatures from both sensors.
Install Python (if not already installed):
sudo apt-get update
sudo apt-get install python3
Create the Script:
nano read_temps.py
Add the Following Code:
import os
import glob
import time
os.system('modprobe w1-gpio')
os.system('modprobe w1-therm')
base_dir = '/sys/bus/w1/devices/'
device_folders = glob.glob(base_dir + '28*')
def read_temp_raw(device_file):
f = open(device_file, 'r')
lines = f.readlines()
f.close()
return lines
def read_temp(device_file):
lines = read_temp_raw(device_file)
while lines[0].strip()[-3:] != 'YES':
time.sleep(0.2)
lines = read_temp_raw(device_file)
equals_pos = lines[1].find('t=')
if equals_pos != -1:
temp_string = lines[1][equals_pos+2:]
temp_c = float(temp_string) / 1000.0
return temp_c
for device_folder in device_folders:
device_file = device_folder + '/w1_slave'
temperature = read_temp(device_file)
print('Sensor {}: {}°C'.format(device_folder, temperature))
Save and Exit:
Press Ctrl + X, then Y, and Enter to save and exit.
Run the Script:
python3 read_temps.py
Sample Output:
Sensor /sys/bus/w1/devices/28-000005e2fdc3: 19.06°C
Sensor /sys/bus/w1/devices/28-0000065aadcd: 18.75°C
Troubleshooting Tips:
28-xxxxxxxxxxxx directories, the sensors aren't being detected.w1-gpio and w1-therm modules are loaded.Conclusion:
By following these steps, you should be able to connect two DS18B20 temperature probes to your Raspberry Pi 2 Model B and read temperature data from both. The 1-Wire protocol simplifies the wiring by allowing multiple devices to share the same data line, and the unique addresses of each DS18B20 sensor enable the Raspberry Pi to differentiate between them.
Not yet included in a published release.