The DS18B20 is a precision digital temperature sensor that provides temperature measurements in a digital form using a 1-Wire interface. This sensor is widely used in various applications such as environmental monitoring, HVAC systems, and temperature regulation. The Wokwi compatible version of the DS18B20 is designed to integrate smoothly with the Wokwi simulation platform, allowing for easy development and testing of temperature sensing applications.
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() {
// Start serial communication for debugging
Serial.begin(9600);
// Start the DS18B20 sensor
sensors.begin();
}
void loop() {
// Request temperature measurements from sensors
sensors.requestTemperatures();
// Fetch and print the temperature in Celsius
Serial.print("Temperature: ");
Serial.print(sensors.getTempCByIndex(0));
Serial.println("°C");
// Wait 1 second before next measurement
delay(1000);
}
Temperature Readings Are Inaccurate:
No Communication with Sensor:
ONE_WIRE_BUS
constant.Multiple Sensors Not Working:
sensors.getAddress()
function to verify the address of each connected DS18B20 sensor.sensors.setResolution()
function to adjust the resolution of the temperature readings.Q: Can I connect multiple DS18B20 sensors to the same microcontroller pin? A: Yes, the 1-Wire protocol allows multiple sensors to share the same data line, each with a unique address.
Q: How do I set the resolution of the DS18B20 sensor?
A: Use the sensors.setResolution(sensorAddress, resolution)
function, where resolution
can be 9, 10, 11, or 12 bits.
Q: What is the purpose of the pull-up resistor on the DQ pin? A: The pull-up resistor is necessary for the 1-Wire bus to function correctly. It pulls the data line high when the bus is idle.
Q: How long can the wires be when connecting the DS18B20 sensor? A: The maximum wire length depends on the quality of the wire and the environment, but it is generally recommended to keep the length under 3 meters for reliable communication.