0
0
AutocadHow-ToBeginner · 3 min read

How to Use OneWire Library for DS18B20 in Arduino

To use the OneWire library with a DS18B20 sensor on Arduino, first include the library and create a OneWire object on the data pin. Then, send commands to the sensor to start temperature conversion and read the temperature data from it.
📐

Syntax

The OneWire library allows communication with devices like the DS18B20 sensor using a single data wire. You create a OneWire object by specifying the Arduino pin connected to the sensor's data line. Then you use methods like reset(), write(), and read() to talk to the sensor.

Key parts:

  • OneWire oneWire(pin); - creates the OneWire bus on a specific pin.
  • oneWire.reset(); - resets the bus to start communication.
  • oneWire.write(byte); - sends a command byte to the sensor.
  • oneWire.read(); - reads a byte from the sensor.
arduino
#include <OneWire.h>

const int pin = 2; // Define the pin connected to the sensor
OneWire oneWire(pin);  // Create OneWire object on chosen pin

// Example commands
oneWire.reset();      // Reset bus
oneWire.write(0x44);  // Start temperature conversion
// Wait and then read bytes
byte data = oneWire.read();
💻

Example

This example shows how to read temperature from a DS18B20 sensor using the OneWire library. It starts a temperature conversion, waits for it to finish, then reads the temperature data and prints it in Celsius.

arduino
#include <OneWire.h>

const int oneWireBus = 2;     // DS18B20 data pin connected to Arduino pin 2
OneWire oneWire(oneWireBus);

void setup() {
  Serial.begin(9600);
}

void loop() {
  byte data[9];

  oneWire.reset();
  oneWire.write(0x44); // Start temperature conversion
  delay(750);          // Wait for conversion to complete

  oneWire.reset();
  oneWire.write(0xBE); // Read scratchpad

  for (int i = 0; i < 9; i++) {
    data[i] = oneWire.read();
  }

  int16_t rawTemperature = (data[1] << 8) | data[0];
  float celsius = rawTemperature / 16.0;

  Serial.print("Temperature: ");
  Serial.print(celsius);
  Serial.println(" °C");

  delay(1000); // Wait 1 second before next reading
}
Output
Temperature: 23.75 °C
⚠️

Common Pitfalls

  • Not using a pull-up resistor: The DS18B20 data line needs a 4.7kΩ pull-up resistor to 5V for proper communication.
  • Skipping the delay after starting conversion: The sensor needs about 750ms to finish temperature conversion; reading too early gives wrong data.
  • Not resetting the bus before commands: Always call oneWire.reset() before sending commands to avoid communication errors.
  • Mixing up byte order: Temperature data is two bytes; combine them correctly with bit shifting.
arduino
/* Wrong way: Missing reset and delay */
oneWire.write(0x44); // Start conversion
// Immediately read without delay or reset
byte temp = oneWire.read(); // Wrong data

/* Right way: */
oneWire.reset();
oneWire.write(0x44);
delay(750);
oneWire.reset();
oneWire.write(0xBE);
// Then read data bytes
📊

Quick Reference

Summary tips for using the OneWire library with DS18B20:

  • Connect DS18B20 data pin to Arduino with a 4.7kΩ pull-up resistor to 5V.
  • Use oneWire.reset() before sending commands.
  • Send 0x44 command to start temperature conversion.
  • Wait at least 750ms for conversion to finish.
  • Send 0xBE command to read temperature data.
  • Combine first two bytes to get raw temperature and divide by 16 for Celsius.

Key Takeaways

Always use a 4.7kΩ pull-up resistor on the DS18B20 data line for reliable communication.
Call oneWire.reset() before sending commands to the sensor to start communication cleanly.
Wait at least 750ms after starting temperature conversion before reading data.
Combine the first two bytes read from the sensor correctly to calculate temperature in Celsius.
Use the OneWire library methods write() and read() to send commands and receive data from DS18B20.