Source: https://mokxi.com/libraries/onewire
Updated: 2026-11-02

Libraries / Communication
Arduino library · Communication

# OneWire: the bus under the DS18B20

OneWire, maintained by Paul Stoffregen, drives Maxim’s 1-Wire bus: one data wire plus ground, a 4.7 k pull-up to the supply, and any number of devices hanging off it, each with a unique 64-bit ROM address burned in at the factory. The DS18B20 thermometer is the device almost everyone meets it through.

Open the example in the editor See the calls

Built in on Mokxi: #include <OneWire.h> and it builds, with nothing to install. Free, and no account needed.

## What OneWire does

The library works at the level of the bus itself. reset() checks whether any device is present, search() walks the bus and hands back one address at a time, select() talks to one address, and read() and write() move bytes. Every address ends in a CRC byte, and OneWire::crc8() lets you check it.

Most temperature sketches never call OneWire directly; they create a OneWire object and pass it to DallasTemperature, which does the commands for them. Reach for OneWire on its own to list the addresses on a bus, to read a device DallasTemperature does not know, or to see exactly what the bytes say.

Library Manager name | OneWire

Author | Paul Stoffregen

Include | #include <OneWire.h>

Boards | every board Mokxi compiles for but ATtiny85

In the simulator | Simulated: it runs against the part the way it does on the bench.

Parts it drives | Temperature sensor, DS18B20

## Install it

It builds on every board Mokxi compiles for but ATtiny85.

In Mokxi it is built in: #include <OneWire.h> and the sketch compiles, with no download, no zip and no Library Manager.

On a real board, install "OneWire" by Paul Stoffregen from the Arduino IDE’s Library Manager (Sketch, Include Library, Manage Libraries), or add OneWire to lib_deps in PlatformIO. Mokxi follows the calls of release 2.3.8, so a sketch written here builds there too.

ESP32-C3 · Arduino Uno · Arduino Nano · Arduino Mega 2560 · Arduino Leonardo · SparkFun Pro Micro · Arduino Micro · ESP32-C6 · Raspberry Pi Pico · Raspberry Pi Pico W · Seeed XIAO SAMD21 · STM32 · STM32 Nucleo-F411RE · STM32 Blue Pill · micro:bit V2 · Arduino Uno R4 · ESP32 DevKit · ESP32-S3-DevKitC-1 · ESP8266 NodeMCU · Arduino Pro Mini 3.3 V · Seeed XIAO ESP32-C3 · ESP32-C3 SuperMini · Seeed XIAO ESP32-C6 · ESP32-DevKitC V4 · LOLIN32 Lite · Wemos D1 R32 · Cheap Yellow Display · TTGO T-Display · Seeed XIAO ESP32-S3 · Arduino Nano ESP32 · Arduino Nano RP2040 Connect · Seeed XIAO RP2040 · Waveshare RP2040-Zero · Arduino Uno R4 WiFi

## The calls you will use

In the order a sketch meets them.

### OneWire ds(2)

Creates a 1-Wire bus on pin 2. The pin needs a 4.7 k pull-up to the supply.

### ds.search(addr)

Finds the next device and copies its 8-byte address into addr. Returns false when there are no more.

### ds.reset_search()

Starts the next search() from the first device again.

### OneWire::crc8(addr, 7)

The CRC of the first seven address bytes, which must equal addr[7] for a good read.

### ds.reset()

Sends a reset pulse. Returns 1 if any device answered with a presence pulse.

### ds.select(addr)

Addresses one device, so the commands that follow go to it alone. skip() addresses every device at once.

### ds.write(0x44, 1)

Sends a byte, here the DS18B20’s "convert temperature" command, keeping the line powered for parasite-powered sensors.

## Example: Thermometers on one wire

On the Arduino Uno R3. It runs in the browser exactly as printed.

sketch.ino · Arduino Uno R3

Open it in the editor, wired and ready to run · Thermometers on one wire: the project page

## Common errors and how to fix them

### search() never finds anything

Almost always the pull-up. Put a 4.7 k resistor between the data pin and the supply (3.3 or 5 V), check the device’s pinout (on a DS18B20 flat face toward you: ground, data, VDD) and make sure the pin number matches.

### CRC is not valid!

The bytes were corrupted on the way in. Shorten the wires, use a stronger pull-up (2.2 k) on a long cable, and keep the data line away from motor and mains wiring.

### Readings of 85 C or -127 C

85 C is the DS18B20’s power-up value, read before a conversion finished; wait 750 ms after the convert command. -127 is DallasTemperature’s code for a sensor that did not answer at all.

## Projects that use OneWire

Built-in projects that include it. Each page has the full sketch and opens in the editor, already wired.

DS18B20 temperature sensor on pin 2 A DS18B20 on pin 2, with a 4.7k pull-up on its data line, measures the temperature over one wire; the Dallas library reads it once a second. Temperature data logger with an SD card A DS18B20, a DS3231 clock and a microSD card: every 5 seconds a dated temperature line goes into TEMPLOG.CSV, ready to open in a spreadsheet.

## Keep going

### Learn

- Arduino DS18B20 Temperature Sensor: Wiring and Code

### Parts

- Temperature sensor, DS18B20

### More communication libraries

- TinyGPS
- TinyGPSPlus
- MFRC522
- SoftwareSerial
- AltSoftSerial
- i2cdetect
- Keyboard
- Mouse

Every built-in library · Open the editor

## Try OneWire without installing anything

Open the example, change the sketch, and keep your version in a free account.

Start building free

Open the editor
