Transmit and receive X10 commands with an Arduino (2026 edition)
One sketch, no library: an Arduino Uno sends X10 commands typed into the Serial Monitor and prints every X10 code it hears on the mains, through a TW523 or PSC05. Written from X10's own technical note and tested against a simulation of the interface.
This is the code half of X10 home automation with Arduino in 2026. That article covers what X10 is, what you can still buy and how the interface is wired. This one is a single sketch that does both directions:
- type
A1 ONin the Serial Monitor and lamp A1 comes on; - press a button on any X10 remote or controller in the house and the sketch prints
RX A1, thenRX A ON.
It uses no library. Everything it does comes from X10’s technical note for the PL513 and TW523 interfaces, and the comments point back to it.
What you need
- An Arduino with 5 V pins: Uno, Nano, Mega or Leonardo. The TW523’s transmit input needs at least 4 V, so a 3.3 V board needs a transistor in between.
- A TW523 or PSC05 two-way powerline interface. They are no longer made, so this means a used one.
- An RJ-11 cable with four wires, cut at one end.
- Something to switch: an LM465 lamp module or an AM466 appliance module, set to house A, unit 1.
Wiring
| TW523 pin | Wire | Signal | Arduino |
|---|---|---|---|
| 1 | black | zero-crossing output | D2 |
| 2 | red | common | GND |
| 3 | green | received data output | D3 |
| 4 | yellow | transmit envelope input | D4 |
The sketch turns on the internal pull-ups for D2 and D3, because both TW523 outputs are open collector. Check the cable’s colours with a meter: phone cables are often wired reversed.
The sketch
x10_bridge.ino
// x10_bridge.ino: an Arduino sends and receives X10 through a TW523 or PSC05 two-way powerline interface.
// Type a command in the Serial Monitor (115200 baud, line ending "Newline"):
// A1 ON address house A unit 1, then switch it on
// A1 address only
// A OFF a function only, for the units last addressed on house A
// Everything the interface hears on the power line is printed back: "RX A1", "RX A ON" and so on.
// Pins and timing follow X10's PL513/TW523 technical note. geekcowboy.net, 2026. MIT licence.
const uint8_t ZC_PIN = 2; // TW523 pin 1 (black): zero-crossing square wave, open collector
const uint8_t RX_PIN = 3; // TW523 pin 3 (green): received envelope, open collector, low = a "1"
const uint8_t TX_PIN = 4; // TW523 pin 4 (yellow): transmit envelope, high = 120 kHz on (needs 4 V or more)
// TW523 pin 2 (red): common, to the Arduino's GND
const unsigned int HALF_CYCLE_US = 8333; // 60 Hz mains; 10000 where the mains is 50 Hz
const unsigned int BURST_US = 1000; // a "1" is 1 ms of 120 kHz
const unsigned int SAMPLE_US = 600; // read the receive line 500-700 us after the crossing
// The technical note's code table, in transmission order (H1 H2 H4 H8). Unit n uses the same four bits as
// the n-th house letter, then D16 = 0.
const uint8_t CODE[16] = {
0b0110, 0b1110, 0b0010, 0b1010, 0b0001, 0b1001, 0b0101, 0b1101, // A-H, units 1-8
0b0111, 0b1111, 0b0011, 0b1011, 0b0000, 0b1000, 0b0100, 0b1100 // I-P, units 9-16
};
struct Function { const char *name; uint8_t code; }; // D1 D2 D4 D8, then D16 = 1 marks a function
const Function FUNCTIONS[] = {
{"ALLUNITSOFF", 0b0000}, {"ALLLIGHTSON", 0b0001}, {"ON", 0b0010}, {"OFF", 0b0011},
{"DIM", 0b0100}, {"BRIGHT", 0b0101}, {"ALLLIGHTSOFF", 0b0110}, {"HAILREQ", 0b1000},
{"HAILACK", 0b1001}, {"STATUSON", 0b1101}, {"STATUSOFF", 0b1110}, {"STATUSREQ", 0b1111},
};
const uint8_t FUNCTION_COUNT = sizeof(FUNCTIONS) / sizeof(FUNCTIONS[0]);
int zcLevel; // the zero-crossing line's last level: every change is a crossing
bool warned; // the "no zero crossing" warning was printed
uint32_t rxBits; // received half cycles, newest in bit 0
int8_t rxNeeded = -1; // half cycles still to read after a start code; -1 while waiting for one
char line[24]; // the command being typed
uint8_t lineLen;
bool lineTooLong;
int8_t indexOf(uint8_t code) {
for (uint8_t i = 0; i < 16; i++) if (CODE[i] == code) return i;
return -1;
}
// Waits for the next zero crossing. False after 50 ms without one (interface unplugged or without power).
bool waitZeroCross() {
unsigned long start = micros();
while (digitalRead(ZC_PIN) == zcLevel) {
if (micros() - start > 50000UL) return false;
}
zcLevel = digitalRead(ZC_PIN);
return true;
}
// One half cycle: for a "1", three 1 ms bursts, one for each phase of a three-phase supply; nothing for a "0".
bool sendHalfCycle(bool one) {
if (!waitZeroCross()) return false;
if (!one) return true;
for (uint8_t phase = 0; phase < 3; phase++) {
if (phase) delayMicroseconds(HALF_CYCLE_US / 3 - BURST_US);
digitalWrite(TX_PIN, HIGH);
delayMicroseconds(BURST_US);
digitalWrite(TX_PIN, LOW);
}
return true;
}
// A frame is 22 half cycles: the start code 1110, then each house and key bit followed by its complement.
uint32_t frame(uint8_t house, uint8_t key) {
uint32_t bits = 0b1110;
uint16_t data = (uint16_t)house << 5 | key; // H1 H2 H4 H8 D1 D2 D4 D8 D16
for (int8_t i = 8; i >= 0; i--) bits = bits << 2 | ((data >> i & 1) ? 0b10 : 0b01);
return bits; // bit 21 goes out first
}
// Every frame goes out twice, then the line stays quiet for 3 cycles before the next pair.
bool sendPair(uint8_t house, uint8_t key) {
uint32_t bits = frame(house, key);
for (uint8_t copy = 0; copy < 2; copy++)
for (int8_t i = 21; i >= 0; i--)
if (!sendHalfCycle(bits >> i & 1)) return false;
for (uint8_t i = 0; i < 6; i++)
if (!waitZeroCross()) return false;
return true;
}
void printFrame(uint16_t data) {
uint8_t key = data & 0b11111;
Serial.print(F("RX "));
Serial.print((char)('A' + indexOf(data >> 5)));
if (!(key & 1)) {
Serial.println(indexOf(key >> 1) + 1);
return;
}
Serial.print(' ');
for (uint8_t i = 0; i < FUNCTION_COUNT; i++) {
if (FUNCTIONS[i].code == key >> 1) {
Serial.println(FUNCTIONS[i].name);
return;
}
}
Serial.print(F("FUNCTION ")); // extended code, extended data or preset dim
Serial.println(key >> 1, BIN);
}
// Called right after each zero crossing. The TW523 passes on the second frame of every pair it hears.
void receiveHalfCycle() {
delayMicroseconds(SAMPLE_US);
rxBits = rxBits << 1 | (digitalRead(RX_PIN) == LOW);
if (rxNeeded < 0) {
if ((rxBits & 0b1111) == 0b1110) rxNeeded = 18;
return;
}
if (--rxNeeded > 0) return;
rxNeeded = -1;
uint16_t data = 0;
for (int8_t i = 8; i >= 0; i--) {
uint8_t pair = rxBits >> (2 * i) & 0b11;
if (pair != 0b10 && pair != 0b01) return; // not a bit and its complement: noise, drop the frame
data = data << 1 | (pair == 0b10);
}
printFrame(data);
}
// "A1 ON", "A1" or "A ON". Replies OK once the codes are on the line, or ERR and why.
void runCommand(char *cmd) {
char *houseWord = strtok(cmd, " ");
char *fnWord = strtok(NULL, " ");
if (!houseWord || strtok(NULL, " ") || houseWord[0] < 'A' || houseWord[0] > 'P') {
Serial.println(F("ERR try A1 ON: a house A-P, a unit 1-16, a function"));
return;
}
uint8_t house = CODE[houseWord[0] - 'A'];
long unit = 0;
if (houseWord[1]) {
char *end;
unit = strtol(houseWord + 1, &end, 10);
if (*end || unit < 1 || unit > 16) {
Serial.println(F("ERR the unit is 1-16"));
return;
}
}
int8_t fn = -1;
for (uint8_t i = 0; fnWord && i < FUNCTION_COUNT; i++)
if (strcmp(fnWord, FUNCTIONS[i].name) == 0) fn = i;
if ((fnWord && fn < 0) || (!unit && fn < 0)) {
Serial.println(F("ERR functions: ON OFF DIM BRIGHT ALLUNITSOFF ALLLIGHTSON ALLLIGHTSOFF STATUSREQ ..."));
return;
}
bool ok = true;
if (unit) ok = sendPair(house, CODE[unit - 1] << 1);
if (ok && fn >= 0) ok = sendPair(house, FUNCTIONS[fn].code << 1 | 1);
rxBits = 0; // what the interface heard meanwhile was our own transmission
rxNeeded = -1;
if (ok) Serial.println(F("OK"));
else Serial.println(F("ERR no zero crossing: is the interface plugged in?"));
}
void readSerial() {
while (Serial.available()) {
char c = Serial.read();
if (c == '\n' || c == '\r') {
line[lineLen] = '\0';
if (lineTooLong) Serial.println(F("ERR the line is too long"));
else if (lineLen) runCommand(line);
lineLen = 0;
lineTooLong = false;
} else if (lineLen < sizeof(line) - 1) {
line[lineLen++] = toupper(c);
} else {
lineTooLong = true;
}
}
}
void setup() {
pinMode(ZC_PIN, INPUT_PULLUP); // enough on a short cable; X10's reference circuit uses 5k to 5 V
pinMode(RX_PIN, INPUT_PULLUP);
pinMode(TX_PIN, OUTPUT);
digitalWrite(TX_PIN, LOW);
Serial.begin(115200);
zcLevel = digitalRead(ZC_PIN);
Serial.println(F("x10_bridge ready: try A1 ON"));
}
void loop() {
if (waitZeroCross()) {
warned = false;
receiveHalfCycle();
} else if (!warned) {
Serial.println(F("No zero crossing: check the cable and that the interface is plugged into the mains"));
warned = true;
}
readSerial();
}
Open the Serial Monitor at 115200 baud with the line ending set to “Newline”, and you should see:
x10_bridge ready: try A1 ON
A1 ON
OK
RX A OFF
The last line is someone pressing “Off” for house A on a remote elsewhere in the house. If the interface is unplugged, or the cable is wrong, the sketch says No zero crossing instead of hanging.
How sending works
X10 sends one bit per half cycle of the mains, starting at the zero crossing. The TW523 gives you that moment on pin 1 as a 60 Hz square wave. Every change of level, up or down, is a crossing, and the note promises it is within 100 µs of the real one.
waitZeroCross() simply polls that pin until it changes. On an Uno a poll takes a few microseconds, comfortably inside the 50 µs the note allows between the crossing and the start of your envelope.
For a 1, the sketch holds the transmit pin high for 1 ms. The TW523 turns that envelope into 120 kHz on the mains. It then does it twice more, a third and two thirds of a half cycle later. Those extra bursts land on the zero crossings of the other two phases of a three-phase supply, as the note asks; in a normal house they are harmless. For a 0 it does nothing.
frame() builds the 22 half cycles of one frame: the start code 1110, then the four house bits and the five key bits, each followed by its complement. sendPair() sends the frame twice and then waits three full cycles of silence, which X10 receivers need between pairs. “A1 ON” is two pairs, about 0.8 seconds on 60 Hz mains, and the sketch blocks while it sends. For a serial bridge that is fine.
The code table is the one in the note, in the order the bits go out. Unit numbers reuse the house letter patterns: unit 1 is the same four bits as house A, unit 2 as house B, and so on. A fifth bit, D16, says whether the key is a unit (0) or a function (1).
How receiving works
The TW523 does the hard part. A custom chip inside listens to the mains, checks every frame, and only passes valid codes to pin 3. Three things about that output shape the code:
- It is inverted: a received 1 is a 1.1 ms low pulse starting about 100 µs after the crossing. The sketch samples it 600 µs after each crossing, in the middle of the 500 to 700 µs window the note recommends.
- It only shows the second frame of each pair, so you get one
RXline per command, not two. - It only reports the first of the three phase bursts, so one sample per half cycle is enough.
receiveHalfCycle() shifts each sample into rxBits and waits for the start code. After a start code it collects 18 more half cycles, checks that every pair really is a bit and its complement, and prints the house and the unit or function. A pair like 11 or 00 means noise, and the frame is dropped.
The TW523 also hears its own transmissions. While the sketch is sending, it is not listening, and it clears its receive state afterwards, so you do not see your own commands echoed.
Limits you will meet
- Dim and Bright are sent back to back with no gap. The TW523 only reports the first one and then every third, so you cannot count dim steps from the received side.
- Extended codes cannot be received at all: the TW523 only passes standard pairs. Preset dim is a standard code, and shows up as a
FUNCTIONline with its raw bits. - Split-phase homes. North American houses have two 120 V legs, and X10 often does not cross from one to the other. If a module only works from some outlets, a passive phase coupler at the breaker panel is the usual fix.
- Noise. Switching power supplies, chargers and some LED drivers soak up or swamp 120 kHz. A plug-in filter on the worst offender helps more than any code change.
- 50 Hz mains. The protocol is the same; set
HALF_CYCLE_USto10000. You will need a 230 V interface, and its pinout may differ from the TW523’s: check before wiring.
How this was tested
No TW523 was on the bench for this article, so the sketch was checked in two other ways:
- It compiles for an Arduino Uno with arduino-cli, using flash for the strings so it leaves plenty of RAM.
- A small C++ program runs the unchanged sketch on a computer, against a simulated TW523: a 60 Hz square wave on pin 2, a scripted receive line on pin 3 with the 1.1 ms low pulses the note describes, and a recorder on pin 4. It checks that:
- the frame for “A, unit 2” matches the example in the technical note bit for bit;
A1 ONproduces exactly the right half cycles: two copies of each frame, three quiet cycles after each pair, three 1 ms bursts per “1”, each starting within 50 µs of its target;- received frames for B12 and B OFF are printed as
RX B12andRX B OFF; - a frame with a broken complement pair prints nothing;
- bad commands never touch the transmit pin, and an unplugged interface gives a warning and
ERR, not a hang.
To make sure the test can fail, five deliberate bugs were planted in copies of the sketch: a wrong start code, sampling outside the window, too short a gap between pairs, wrong phase timing, and inverted bit decoding. The test caught every one.
If you run it on real hardware and something does not behave as described, the About page says where to write.