The DS18B20 1-Wire Temperature Sensor is a compact digital sensor that provides temperature measurements in a digital format over a 1-Wire interface. This sensor is widely used in a variety of applications, including HVAC environmental controls, temperature monitoring systems inside buildings, equipment or machinery, and process monitoring and control systems.
Pin Number | Name | Description |
---|---|---|
1 | GND | Ground pin, connected to the system ground |
2 | DQ | Data pin, 1-Wire communication line |
3 | VDD | Power supply pin, 3.0V to 5.5V |
#include <OneWire.h>
#include <DallasTemperature.h>
// Data wire is connected to Arduino digital pin 2
#define ONE_WIRE_BUS 2
// Setup a oneWire instance to communicate with any OneWire device
OneWire oneWire(ONE_WIRE_BUS);
// Pass oneWire reference to DallasTemperature library
DallasTemperature sensors(&oneWire);
void setup() {
Serial.begin(9600);
sensors.begin(); // Start up the library
}
void loop() {
sensors.requestTemperatures(); // Send command to get temperatures
float temperatureC = sensors.getTempCByIndex(0);
Serial.print("Temperature is: ");
Serial.print(temperatureC);
Serial.println(" °C");
delay(1000); // Wait 1 second before next reading
}
search()
function from the OneWire library to confirm the presence and addresses of all DS18B20 sensors on the bus.Q: Can I use the DS18B20 sensor without an external power supply?
A: Yes, the DS18B20 can operate in "parasite power" mode, where it draws power from the data line. This requires only two wires: the DQ and GND pins.
Q: How many DS18B20 sensors can I connect to a single microcontroller pin?
A: You can connect many sensors to a single pin, but the exact number depends on the quality of your wiring and the pull-up resistor. It's important to ensure that each sensor has a unique address.
Q: How do I set the resolution of the DS18B20 sensor?
A: The resolution can be set using the setResolution()
function provided by the DallasTemperature library. The resolution can be set from 9 to 12 bits, with higher resolution providing greater accuracy but slower response times.