LIN and 1-Wire
Two cheap single-wire buses: automotive LIN and 1-Wire sensors.
Two cheap single-wire buses
Not every link needs CAN's robustness or SPI's speed. Two buses do useful work with a single signal wire and very cheap hardware: LIN in cars and 1-Wire for small sensors and ID chips.
LIN: the low-cost car network
LIN (Local Interconnect Network, ISO 17987) connects simple automotive nodes (window switches, mirror motors, seat controls, rain and light sensors) to an ECU that bridges them to CAN. It costs much less than CAN because it is just a UART with a single-wire transceiver.
| Property | LIN | CAN (for comparison) |
|---|---|---|
| Wires | 1 (plus ground and supply), 12 V signalling | 2 (differential) |
| Speed | Up to 20 kbit/s (typically 19.2 kbit/s) | 125 kbit/s to 1 Mbit/s (more with FD) |
| Access | One commander schedules everything | Multi-master with arbitration |
| Nodes | Up to 16 | Dozens |
| Hardware | UART + LIN transceiver; responders can use an internal RC clock | CAN controller + transceiver + crystal |
How a LIN frame works
The commander (formerly "master") follows a fixed schedule table and sends a header for each frame slot; whichever node owns that frame sends the response:
header (from the commander): | break (≥13 dominant bits) | sync 0x55 | protected ID |
response (from the owner): | data (1-8 bytes) | checksum |
- The break is longer than any normal byte, so every node recognises the start of a frame.
- The sync byte 0x55 (alternating bits) lets responders measure the commander's bit time and correct their own clock, which is why they can run from an inexpensive RC oscillator.
- The protected identifier is a 6-bit frame ID plus two parity bits.
- The checksum is an 8-bit sum with carry wrap-around, inverted; the "enhanced" checksum (LIN 2.x) also includes the protected ID.
#include <stdio.h>
#include <stdint.h>
/* LIN protected identifier: 6-bit ID plus parity bits P0 and P1. */
static uint8_t lin_pid(uint8_t id) {
id &= 0x3F;
uint8_t b = id;
uint8_t p0 = (uint8_t)(((b >> 0) ^ (b >> 1) ^ (b >> 2) ^ (b >> 4)) & 1);
uint8_t p1 = (uint8_t)(~((b >> 1) ^ (b >> 3) ^ (b >> 4) ^ (b >> 5)) & 1);
return (uint8_t)(id | (p0 << 6) | (p1 << 7));
}
/* Enhanced checksum: sum with carry wrap-around over the PID and data, then inverted. */
static uint8_t lin_checksum(uint8_t pid, const uint8_t *data, int n) {
uint16_t sum = pid;
for (int i = 0; i < n; i++) { sum += data[i]; if (sum > 0xFF) sum -= 0xFF; }
return (uint8_t)~sum;
}
int main(void) {
uint8_t data[2] = { 0x4A, 0x55 };
uint8_t pid = lin_pid(0x10);
printf("ID 0x10 -> protected ID 0x%02X\n", pid);
printf("checksum 0x%02X\n", lin_checksum(pid, data, 2));
return 0;
}
The fixed schedule makes LIN timing completely deterministic, and node configuration is described in an LDF (LIN description file).
1-Wire: power and data on one line
1-Wire (from Maxim, now Analog Devices) runs over a single open-drain data line with a pull-up, plus ground. Devices can even be parasite-powered, charging an internal capacitor from the data line while it is high.
Typical devices: DS18B20 temperature sensors, DS2401 unique serial-number chips, iButton keys, EEPROMs, battery authentication chips.
Timing is everything
1-Wire has no clock line; every bit is a timed slot that begins with the controller pulling the line low:
| Operation | Controller | Device |
|---|---|---|
| Reset and presence | Pulls low for ≥ 480 µs, then releases | Answers with a 60 to 240 µs low pulse: "I am here" |
| Write 1 | Low for 1 to 15 µs, then releases for the rest of a 60 µs slot | — |
| Write 0 | Low for 60 to 120 µs | — |
| Read | Low for ~1 to 2 µs, releases, samples at ~15 µs | Holds low to send 0, leaves it to send 1 |
Bit-banged implementations must disable interrupts during each slot (an interrupt mid-slot stretches the timing), or use a UART trick: running a UART at 115200 baud and sending specific byte values produces exactly the right low pulses, with the received echo giving the bit read back. Some MCUs and bridge chips (DS2482 over I2C) offload it.
Addressing many devices
Every 1-Wire device has a unique 64-bit ROM code: an 8-bit family code, a 48-bit serial number and an 8-bit CRC. The controller can address one device with Match ROM, all at once with Skip ROM (for example to start temperature conversion on every sensor), or discover them with the Search ROM algorithm, which walks the binary tree of IDs bit by bit using the wired-AND line. The CRC uses the polynomial x⁸ + x⁵ + x⁴ + 1 (Dallas/Maxim CRC-8).
#include <stdio.h>
#include <stdint.h>
/* Dallas/Maxim CRC-8 (reflected polynomial 0x8C), used for 1-Wire ROM codes and data. */
static uint8_t crc8_maxim(const uint8_t *p, int n) {
uint8_t crc = 0;
while (n--) {
uint8_t byte = *p++;
for (int i = 0; i < 8; i++) {
uint8_t mix = (uint8_t)((crc ^ byte) & 1);
crc >>= 1;
if (mix) crc ^= 0x8C;
byte >>= 1;
}
}
return crc;
}
int main(void) {
/* A ROM code: family 0x28 (DS18B20), 48-bit serial, then its CRC byte. */
uint8_t rom[8] = { 0x28, 0xFF, 0x4C, 0x6D, 0x81, 0x16, 0x03, 0x00 };
rom[7] = crc8_maxim(rom, 7);
printf("ROM code CRC byte: 0x%02X\n", rom[7]);
printf("check over all 8 bytes: 0x%02X (0 means valid)\n", crc8_maxim(rom, 8));
return 0;
}
Pitfall: In parasite-power mode a DS18B20 needs a strong pull-up (the line driven high by a transistor) during its temperature conversion, up to 750 ms at 12-bit resolution. A weak resistor pull-up alone lets the voltage sag and the conversion returns garbage such as 85 °C, the power-on default.
Interview question: Compare LIN and CAN and explain when a car uses each.
Answer: CAN is a fast (up to 1 Mbit/s, more with CAN FD), multi-master differential bus with arbitration and strong error handling, used for powertrain, chassis and body ECUs that exchange time-critical data. LIN is a slow (up to 20 kbit/s) single-wire, single-commander bus built on a UART and a cheap transceiver, with a fixed schedule and nodes that can run without a crystal, used for simple local functions such as switches, mirrors, seats and sensors. A body ECU typically acts as LIN commander and gateways the information onto CAN.
Interview question: How would you read several DS18B20 sensors on one 1-Wire bus?
Answer: Run the Search ROM algorithm once to discover each sensor's 64-bit ROM code and check its CRC, storing the codes. To measure, send a reset, Skip ROM and Convert T so all sensors convert simultaneously, then wait the conversion time (up to 750 ms at 12-bit, with a strong pull-up if parasite-powered). Then for each sensor send reset, Match ROM with its code and Read Scratchpad, check the scratchpad CRC, and convert the 16-bit value (1/16 °C per bit at 12-bit resolution).
Going deeper
Interview question: How would you read several temperature probes on one wire in a car's climate system?
Answer: Use 1-Wire sensors (such as the DS18B20) on one bus, discover their 64-bit ROM IDs at production and store which is where, start conversions on all at once, then read each by ID with a CRC check. Timing must be precise, so use a UART trick or a peripheral rather than bit-banging with interrupts enabled.
Interview question: Why must interrupts usually be disabled while bit-banging 1-Wire?
Answer: Each bit is defined by the length of a low pulse measured in microseconds: a write-1 pulse must end within 15 µs, and a read must sample about 15 µs after the slot starts. An interrupt arriving mid-slot can stretch the pulse into a write-0 or delay the sample past the device's response window, corrupting the bit. Disabling interrupts for each slot (about 60 to 70 µs) keeps timing exact, or a UART or timer peripheral can generate the slots in hardware.
Interview question: What does the LIN break field achieve that a normal byte cannot?
Answer: A break is a dominant (low) period of at least 13 bit times, longer than any valid UART character including its start and stop bits, so a receiver sees it as a framing error that cannot occur in normal data. That gives every node an unambiguous start-of-frame marker, letting them resynchronise to a new header regardless of what was on the bus before.
Explain it: In your own words, explain how 1-Wire transfers bits without a clock line.
Answer: Every bit is a fixed-length time slot that the controller starts by pulling the shared line low. To write a 1 it releases the line almost immediately; to write a 0 it holds it low for most of the slot, and the device samples the line partway through the slot to tell which. To read, the controller pulls low briefly and releases, and the device either holds the line low to send a 0 or leaves it high to send a 1, while the controller samples about 15 microseconds into the slot. Timing replaces the clock, which is why the timing must be accurate to a few microseconds.