1-Wire Overview
1-Wire is a master-slave serial communication protocol developed by Dallas Semiconductor (USA), primarily designed to enable communication between microcontrollers and 1-Wire devices. Unlike SPI and I2C serial data communication methods, 1-Wire uses a single signal line to achieve bidirectional data transmission. This design greatly simplifies hardware connections and reduces system cost and complexity.
Introduction to 1-Wire Communication Timing
The basic time slots of 1-Wire are divided into three phases: initialization, write 0 & 1, and read 0 & 1(this article uses the time slots of the DS18B20 temperature sensor as an example).
Initialization
All communication with the DS18B20 starts with an initialization sequence, which consists of a reset pulse issued by the host, followed by a presence pulse sent by the DS18B20. When the DS18B20 responds to the reset and sends a presence pulse, it signals to the host that it is connected to the bus and ready for operation.
Specific Process: The bus master transmits (TX) a reset pulse by pulling the 1-Wire bus low for a minimum of 480μs. The bus master then releases the bus and enters receive mode (RX). After the bus is released, the pull-up resistor pulls the 1-Wire bus high. When the DS18B20 detects this rising edge, it waits 15μs to 60μs, then sends a presence pulse by pulling the 1-Wire bus low for 60μs to 240μs.
Write 0 & 1
All write time slots must have a minimum duration of 60μs, with a minimum recovery time of 1μs between each write slot. Both types of write slots are initiated by the host pulling the 1-Wire bus low.
Write 0: To generate a "write 0" slot, after pulling the 1-Wire bus low, the bus master must continue to hold the bus low for the entire duration of the slot (a minimum of 60μs).
Write 1: To generate a "write 1" slot, after pulling the 1-Wire bus low, the bus master must release the 1-Wire bus within 15μs. After the bus is released, the pull-up resistor pulls the bus high.
Specific Process: The DS18B20 samples the 1-Wire bus within a 15μs to 60μs window after the host initiates the write slot. If the bus is high during the sampling window, a 1 is written to the DS18B20. If the bus is low, a 0 is written to the DS18B20.
Read 0 & 1
All read time slots must have a minimum duration of 60μs, with a minimum recovery time of 1μs between slots. A read slot is initiated by the host pulling the 1-Wire bus low for a minimum of 1μs and then releasing the bus. After the host initiates the read slot, the DS18B20 will start transmitting 1 or 0 on the bus. The DS18B20 can only send data to the host when the host issues a read slot.
Read 0: The DS18B20 will release the bus at the end of the read slot, and the bus will be pulled back to the high-level idle state by the pull-up resistor.
Read 1: The DS18B20 keeps the bus at a high level.
Specific Process: The output data from the DS18B20 is valid within 15μs after the falling edge that initiates the read slot. Therefore, the host must release the bus and then sample the bus status within 15μs after the start of the slot.
Functional Overview
This section mainly describes how to use the functions and data types of the 1-Wire driver to establish communication between QuecPython series modules and the DS18B20 sensor. The typical programming workflow is divided into the following parts:
Object Creation
Reset
Data Transmission
Data Reading
Object Creation
When creating a 1-Wire object, you need to set the GPIO connected to the DQ pin.
class machine.OneWire(GPIO)
For parameter details, please refer to the constructor.
Reset
This method is used to reset the bus and detect whether the device responds. This interface must be called first before calling the read and write interfaces.
OneWire.reset()
For API details, please refer to machine.OneWire.reset.
Data Transmission
This method is used for the host to write data to the slave, and the data to be written is of bytes type.
OneWire.write(data)
For API details, please refer to machine.OneWire.write.
Data Reading
The data read by this method is the raw data of the DS18B20, which needs to be converted into the actual temperature according to the data format of the DS18B20.
OneWire.read(len)
For API details, please refer to machine.OneWire.read.
Application Example: DS18B20 Temperature Sensor
The DS18B20 is a widely used digital temperature sensor produced by Dallas Semiconductor. It internally integrates modules including a temperature sensor, temperature conversion circuit, non-volatile memory, configuration register, and 1-Wire interface circuit. Through the 1-Wire interface circuit, the DS18B20 communicates with the external host to realize data transmission and command reception.
Basic Commands:
- CCh: Skip ROM (when only one DS18B20 sensor is connected, 0xCC can be sent to skip ROM matching)
- 44h: Start temperature conversion
- 4Eh: Write scratchpad register
- BEh: Read scratchpad register (a total of 9 registers can be read, the first 2 are temperature values)
The temperature value obtained by the DS18B20 is in the following format and requires conversion:
Complete Communication Flow
- Reset and initialization
- Host sends [0xcc, 0x44] to start DS18B20 temperature conversion
- Reset again
- Host sends [0xcc, 0xbe] to read the temperature value from the scratchpad register
Sample Code for Obtaining DS18B20 Temperature Value
FAQ
- Reset Initialization Failure
This may be caused by a hardware connection issue. Check the hardware connections to ensure that the GPIO used to create the object is consistent with the pin on the development board connected to the DQ pin of the DS18B20, and that the ground (GND) is also correctly connected. The DS18B20 sensor is damaged, resulting in communication failure.
- Normal Transceiving but Erroneous Data
This may be caused by an inconsistent level between the host GPIO and the slave DQ pin. For example, if the host level is 3.3V and the slave level is 1.8V, the high level of the slaveis 1.8V. The host generally recognizes a level higher than 1.65V as high level, which is prone to misjudgment.