====== lamaPLC: DS18B20 1-Wire Digital Thermometer ====== {{ :sensor:ds18b20_5.png?200|DS18B20 1-Wire Digital Thermometer}} The DS18B20 digital thermometer offers 9-bit to 12-bit Celsius temperature readings and features an alarm function with nonvolatile, user-programmable upper and lower trigger points. It communicates via a [[com:basic_1wire|1-Wire]] bus that requires only one data line (and ground) to connect with a central microprocessor. Additionally, the DS18B20 can draw power directly from the data line, known as [[com:basic_1wire|"parasite power"]], eliminating the need for an external power supply. Each DS18B20 has a unique 64-bit serial code, enabling multiple units to operate on the same 1-Wire bus. This makes it easy to control multiple DS18B20s from a single microcontroller over a large area. This feature benefits applications such as HVAC environmental controls, temperature monitoring systems inside buildings, equipment or machinery, and process monitoring and control systems. ==== Features ==== * Unique [[com:basic_1wire|1-Wire]] interface requires only one port pin for communication. * Multidrop capability simplifies distributed temperature sensing applications. * It requires no external components and can be powered from a data line. * The power supply range is **3.0V to 5.5V** DC. * Zero standby power is required. * It measures temperatures from **-55°C to +125°C**. The Fahrenheit equivalent is **-67°F to +257°F**. * It offers ±0.5°C accuracy from -10°C to +85°C. * Thermometer resolution is programmable from 9 to 12 bits. * Converts 12-bit temperature to a digital word in 750 ms (max.). * User-definable, nonvolatile temperature alarm settings are available. * The alarm search command identifies and addresses devices whose temperature is outside the programmed limits (temperature alarm condition). * Applications include thermostatic controls, industrial systems, consumer products, thermometers, or any thermally sensitive system. The BME/BMP sensors can be integrated with the [[https://tasmota.github.io/docs/About/|Tasmota]] system. \\ For more details, see here: https://tasmota.github.io/docs/DS18x20/ {{page>:tarhal}} ==== Wiring ==== |< 100%>| |**Red wire:** Power supply (3.0V - 5.5V DC) \\ **Black wire:** GND \\ **Yellow wire:** Signal (1-wire bus) \\ |{{ :sensor:ds18b20_7.png?300|DS18B20 wiring}}|{{ :sensor:ds18b20_8.png?300|DS18B20 wiring}}| ===== Parasitic/normal mode DS18B20 / 1-wire bus ===== |< 100% 50% 50% >| ^Normal mode^Parasitic mode| |{{:com:ds18b20_normal_mode.png|Normal mode by 1-wire bus}}|{{:com:ds18b20_parasite_mode.png|Parasitic mode by 1-wire bus}}| |With an external supply, three wires are needed: the bus wire, ground, and power. The **4.7k pull-up resistor** remains necessary on the bus wire. Since the bus is available for data transfer, the microcontroller can continually check the device's status during conversion. This allows a conversion to complete as soon as the device reports it is done, without waiting for the conversion time (which depends on device function and resolution) in //"parasite"// power mode.|When operating in parasite power mode, it requires only two wires: one data wire and one ground wire. The power line must be connected to ground in this mode, as specified in the datasheet. A 4.7k pull-up resistor should be connected to the 1-wire bus at the controller. When the line is in a //"high"// state, the device pulls current to charge an internal component capacitor.| ==== Addressing a 1-Wire device ==== Each 1-Wire device has a unique 64-bit 'ROM' address, which includes an 8-bit family code, a 48-bit serial number, and an 8-bit CRC. The CRC helps verify data integrity. For example, the sample code below checks if the device being addressed is a DS18S20 temperature sensor by looking for its family code, 0x10. To use the sample code with the newer DS18B20 sensor, you would look for a family code of 0x28. For the DS1822, you would check for 0x22. ==== Arduino ==== The DS18B20 is a popular 1-Wire digital temperature sensor known for its simplicity and the ability to connect multiple sensors to a single Arduino pin. **Wiring & Pull-up Resistor** The most critical component of the setup is the 4.7 kΩ pull-up resistor. Without it, the 1-Wire bus cannot return to a "high" state, and you will not get any readings. * **VCC:** 3.3V or 5V * **GND:** Ground * **Data (DQ):** Any digital pin (e.g., Pin 2) * **Resistor:** Place the 4.7kΩ resistor between VCC and Data. **Required Libraries** To run the code below, install these two libraries via the Arduino Library Manager (Sketch > Include Library > Manage Libraries): * **OneWire** by Paul Stoffregen * **DallasTemperature** by Miles Burton **Arduino Sketch** This script initializes the sensor and prints the temperature in both Celsius and Fahrenheit every second. #include #include // Data wire is plugged into port 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. DallasTemperature sensors(&oneWire); void setup(void) { Serial.begin(9600); Serial.println("DS18B20 Single Sensor Read"); // Start up the library sensors.begin(); } void loop(void) { // Send the command to get temperatures Serial.print("Requesting temperatures..."); sensors.requestTemperatures(); Serial.println("DONE"); // Use index 0 to refer to the first (and only) sensor on the wire float tempC = sensors.getTempCByIndex(0); // Check if reading was successful if(tempC != DEVICE_DISCONNECTED_C) { Serial.print("Temperature: "); Serial.print(tempC); Serial.print("°C | "); Serial.print(DallasTemperature::toFahrenheit(tempC)); Serial.println("°F"); } else { Serial.println("Error: Could not read temperature data"); } delay(1000); } ==== 1-wire topics on lamaPLC ==== {{topic>1-wire}} \\ \\ {{tag>DS18B20 sensor 1-wire communication Arduino Thermometer Parasitic_mode}} \\ This page has been accessed for: Today: {{counter|today}}, Until now: {{counter|total}}