The DS18B20 is a digital temperature sensor that provides temperature readings in degrees Celsius with an accuracy of ±0.5°C. It communicates with microcontrollers and other digital devices using the 1-Wire protocol, which allows multiple sensors to be connected to a single data wire. This sensor is widely used in a variety of applications, including environmental monitoring, home automation, and industrial control systems.
Pin Number | Name | Description |
---|---|---|
1 | GND | Ground pin, connected to the system ground |
2 | DQ | Data pin, used for 1-Wire communication |
3 | VDD | Power supply pin, 3.0V to 5.5V |
#include <OneWire.h>
#include <DallasTemperature.h>
// Data wire is connected to pin 2 on the Arduino
#define ONE_WIRE_BUS 2
// Setup a oneWire instance to communicate with any OneWire devices
OneWire oneWire(ONE_WIRE_BUS);
// Pass our oneWire reference to Dallas Temperature sensor
DallasTemperature sensors(&oneWire);
void setup(void)
{
// Start serial communication for debugging
Serial.begin(9600);
// Start up the library
sensors.begin();
}
void loop(void)
{
// Request temperature measurements from the sensor
sensors.requestTemperatures();
// Fetch and print the temperature in Celsius
Serial.print("Temperature is: ");
Serial.print(sensors.getTempCByIndex(0));
Serial.println("°C");
// Wait 1 second before requesting new data
delay(1000);
}
sensors.getAddress()
function to ensure each sensor has a unique address.sensors.search()
function to find all devices on the bus.Q: Can I use the DS18B20 sensor without an external power supply?
A: Yes, the DS18B20 supports "parasite power" mode, where it draws power from the data line. This requires only two wires: the data line and ground.
Q: How many DS18B20 sensors can I connect to a single microcontroller pin?
A: You can connect many sensors to a single pin, limited by the bus capacitance and the quality of your pull-up resistor. In practice, around 10-20 sensors are often manageable.
Q: How do I waterproof the DS18B20 sensor for outdoor use?
A: You can purchase the DS18B20 in a waterproof probe form, or you can waterproof a standard sensor using appropriate sealing techniques and materials like heat shrink tubing and silicone sealant.