Back

OneWire

Loading views...

1-Wire Protocol

Overview

1-Wire is a single-wire serial protocol developed by Dallas Semiconductor (now Maxim Integrated) that uses one wire for both data and power (parasitic power mode). It is primarily used for temperature sensors (DS18B20) and iButton devices.


1. Theory & Fundamentals

  • Solves: Low-pin-count communication with powered or parasitic devices
  • Physical layer: Single open-drain wire + GND
  • Voltage: 3.3V or 5V
  • Parasitic power: Device powered from data line capacitance
  • Speed: Standard (16.3kbps) and Overdrive (142kbps)
  • Pull-up: 4.7kΩ required on data line
  • Multiple devices share one wire using 64-bit ROM addresses

2. Frame / Packet Structure

All communication uses time slots:

  • Reset pulse: Master holds LOW ≥ 480µs, releases; slave responds with presence pulse (60–240µs LOW)
  • Write 1: Master pulls LOW for 1–15µs, releases; line recovers
  • Write 0: Master pulls LOW for 60–120µs
  • Read: Master pulls LOW for 1–15µs; slave drives data during 15–45µs window
  • 64-bit ROM: 8-bit family code | 48-bit serial | 8-bit CRC

3. Protocol Mechanics

  • Master-slave: Master initiates all communication
  • ROM commands: Search ROM (0xF0), Read ROM (0x33), Match ROM (0x55), Skip ROM (0xCC), Alarm Search (0xEC)
  • Skip ROM: Address all devices simultaneously (only safe for 1 device)
  • CRC-8: Dallas/Maxim polynomial (0x31) for data integrity
  • Parasitic power: Strong pull-up required during conversion

4. Hardware Implementation

  • One GPIO configured as open-drain output + input
  • Pull-up resistor: 4.7kΩ (standard) or 2.2kΩ (short bus)
  • For parasitic power: Use MOSFET strong pull-up during temperature conversion
  • Common devices: DS18B20 (temp), DS2431 (EEPROM), DS2408 (I/O)
  • Bus length: Up to 300m with proper pull-ups and network topology

5. Register-Level / Configuration

#define OW_PIN  GPIO_PIN_0
#define OW_PORT GPIOA

void OW_Low(void)  { GPIOA->MODER |= (1<<0); GPIOA->BSRR = GPIO_BSRR_BR0; }
void OW_Release(void) { GPIOA->MODER &= ~(1<<0); } // Set to input
uint8_t OW_Read(void) { return (GPIOA->IDR & 1); }

uint8_t OW_Reset(void) {
    OW_Low(); delay_us(480);
    OW_Release(); delay_us(70);
    uint8_t presence = !OW_Read();
    delay_us(410);
    return presence; // 1 = device present
}

void OW_WriteBit(uint8_t bit) {
    OW_Low();
    delay_us(bit ? 6 : 60);
    OW_Release();
    delay_us(bit ? 64 : 10);
}

uint8_t OW_ReadBit(void) {
    OW_Low(); delay_us(6);
    OW_Release(); delay_us(9);
    uint8_t bit = OW_Read();
    delay_us(55);
    return bit;
}

6. Driver / Software Development

// DS18B20 temperature read
float DS18B20_Read(void) {
    OW_Reset();
    OW_WriteByte(0xCC); // Skip ROM
    OW_WriteByte(0x44); // Convert T
    // Strong pull-up for 750ms during conversion
    delay_ms(750);
    
    OW_Reset();
    OW_WriteByte(0xCC);
    OW_WriteByte(0xBE); // Read scratchpad
    
    uint8_t lsb = OW_ReadByte();
    uint8_t msb = OW_ReadByte();
    int16_t raw = (msb << 8) | lsb;
    return raw / 16.0f; // 12-bit resolution: LSB = 0.0625°C
}

7. Debugging & Testing

  • Logic analyzer: Measure pulse widths for reset/presence and bit slots
  • Common bugs: Timing too fast → missed presence pulse; no pull-up → bus stuck LOW
  • CRC mismatch → noise on long bus; add 100nF filter cap
  • Parasitic power issue: Use MOSFET pull-up for conversion phase
  • Multi-device: Use ROM search algorithm to enumerate all devices

8. Real-World Applications

  1. DS18B20 temperature monitoring: Read multiple sensors on one wire
  2. Cold chain logging: Track temperature with parasitic-powered sensors
  3. iButton access control: Read 64-bit ID from DS1990A token
  4. DS2431 EEPROM: Store calibration data in sensor housing
  5. Pool temperature: Waterproof DS18B20 in pool monitoring system

9. Advanced Topics & Edge Cases

  • ROM search algorithm: Enumerate all 64-bit IDs on multi-device bus (tree walk)
  • Overdrive mode: 10x speed; requires 1µs timing precision
  • Parasitic strong pull-up: MOSFET switch for conversion power
  • Long bus: Use active pull-up or bus driver for >10m
  • CRC verification: Always verify 8-bit CRC on ROM reads and scratchpad

10. Standards & Variants

Aspect Value
Standard Maxim/Dallas 1-Wire protocol
ROM size 64-bit (8 family + 48 serial + 8 CRC)
Speed 16.3kbps standard, 142kbps overdrive
Devices Theoretically unlimited
Distance Up to 300m (star topology)
Power External or parasitic (3.0–5.5V)

💡 Practical Examples

Example 1: Single DS18B20 read

float temp = DS18B20_Read(); // Returns degrees Celsius
printf("Temp: %.2f C\n", temp);

Example 2: Search ROM for all devices

uint8_t rom[8];
uint8_t found = 1;
while (found) {
    found = OW_SearchNext(rom); // Walk ROM tree
    printf("Device: %02X-%02X-%02X...\n", rom[0], rom[1], rom[2]);
}

Example 3: Read multiple sensors

// Address each sensor by its unique 64-bit ROM
OW_Reset(); OW_WriteByte(0x55); // Match ROM
OW_WriteBytes(sensor1_rom, 8);
OW_WriteByte(0xBE); // Read scratchpad

🧪 Practice Questions

Beginner

  1. How many wires does 1-Wire use for data and power?
  2. What is the purpose of the reset/presence pulse?
  3. What is the family code of DS18B20?
  4. What is parasitic power mode?
  5. Why does 1-Wire need a pull-up resistor?

Intermediate

  1. Explain the timing for write-0 and write-1 bit slots.
  2. How do you address one specific sensor among many on the same bus?
  3. Calculate temperature from DS18B20 raw scratchpad bytes 0xD0 0x01.
  4. What is the ROM search algorithm and why is it needed?
  5. How does CRC protect 1-Wire communication?

Advanced

  1. Implement a complete ROM search tree walk in C to discover all devices.
  2. Design a 10-sensor logging system on a single GPIO using interrupts.
  3. How do you handle timing precision for 1-Wire on a preemptive RTOS?
  4. Implement overdrive mode for faster communication.
  5. Design a parasitic power supply circuit for DS18B20 in a 100m cable installation.

Hands-on Projects

  1. Temperature Array: Read 8 DS18B20 sensors on one wire, average and log data.
  2. iButton Access System: Scan DS1990A tokens and grant/deny access.
  3. Hot/Cold Alarm: Alert via buzzer if any sensor exceeds threshold.

Checklist

  • [ ] Implement reset/presence pulse with correct timing
  • [ ] Write bit-bang write-0, write-1, and read bit functions
  • [ ] Read temperature from single DS18B20
  • [ ] Verify data with CRC-8
  • [ ] Implement ROM search for multiple devices
  • [ ] Use Skip ROM for single-device bus
  • [ ] Handle parasitic power strong pull-up
  • [ ] Debug timing with logic analyzer
  • [ ] Read DS18B20 in all 4 resolution modes
  • [ ] Build multi-sensor system with ROM matching