DS18B20 Waterproof Digital Thermometer Sensor Probe Documentation
The DS18B20 Waterproof Digital Thermometer Sensor Probe is a digital temperature sensor that accurately measures temperature in various environments. It is waterproof, making it suitable for applications where humidity and water exposure are a concern. This sensor is commonly used in IoT projects, such as weather stations, aquarium monitoring, and industrial automation.
Temperature range: -55C to 125C (-67F to 257F)
Accuracy: 0.5C (0.9F)
Resolution: 9-bit to 12-bit (configurable)
Supply voltage: 3.0V to 5.5V
Communication protocol: 1-Wire protocol
Waterproof rating: IP67
Connecting the DS18B20 to a Microcontroller
To connect the DS18B20 to a microcontroller, you will need:
DS18B20 Waterproof Digital Thermometer Sensor Probe
Microcontroller (e.g., Arduino, Raspberry Pi, ESP32)
Breadboard and jumper wires
4.7k ohm pull-up resistor
Connect the DS18B20 VCC pin to the microcontroller's 5V pin, GND pin to GND, and the DQ pin to a digital pin on the microcontroller (e.g., D2 on Arduino). Add a 4.7k ohm pull-up resistor between the VCC and DQ pins.
### Example 1: Arduino Sketch to Read Temperature
```cpp
#include <OneWire.h>
#include <DallasTemperature.h>
#define ONE_WIRE_BUS 2 // Pin for DS18B20 communication
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);
void setup() {
Serial.begin(9600);
sensors.begin();
}
void loop() {
sensors.requestTemperatures();
float tempC = sensors.getTempCByIndex(0);
Serial.print("Temperature: ");
Serial.print(tempC);
Serial.println("C");
delay(1000);
}
```
### Example 2: Python Code for Raspberry Pi Using RPi.GPIO Library
```python
import os
import time
import glob
from gpiozero import OutputDevice
# Set up GPIO pin 4 as output for DS18B20 communication
pin = 4
device = OutputDevice(pin)
# Set up 1-Wire communication
base_dir = '/sys/bus/w1/devices/'
device_folder = glob.glob(base_dir + '28')[0]
device_file = device_folder + '/w1_slave'
def read_temp_raw():
f = open(device_file, 'r')
lines = f.readlines()
f.close()
return lines
def read_temp():
lines = read_temp_raw()
while lines[0].strip()[-3:] != 'YES':
time.sleep(0.2)
lines = read_temp_raw()
temp_data = lines[1].find('t=')
if temp_data != -1:
temp_data = lines[1].strip()[temp_data+2:]
return float(temp_data) / 1000.0
while True:
temp = read_temp()
print("Temperature: {:.2f}C".format(temp))
time.sleep(1)
```
Note: These code examples assume you have the necessary libraries installed and configured for your microcontroller or single-board computer. Make sure to consult the relevant documentation for your specific setup.