Projects / Manchester code lab

Manchester code lab

One Nano sends bits as Manchester code, where every bit has a change in the middle, and a second Nano reads it and prints the message. You learn why data links use codes that carry their own timing.

Intermediate1.7 hGuided buildReviewed

Build it step by step

0 of 7 done
  1. NANO18.8 mA

    Upload the sender to board one

    The LED flickers faintly.

  2. NANO

    Upload the receiver to board two

    Open the serial monitor at 9600.

  3. NANO

    Join D3 on the sender to D2 on the receiver

    And join the grounds.

  4. NANO

    Read the message

    HELLO LARK repeats every two seconds.

  5. NANO

    Change BIT_US to 1200 on the sender only

    The text turns to question marks.

  6. NANO

    Set both back to 1000

    The message returns.

  7. NANO

    Try 500 on both

    It still works at double the speed.

In short

How do you build a manchester code lab?

One Nano sends bits as Manchester code, where every bit has a change in the middle, and a second Nano reads it and prints the message. You learn why data links use codes that carry their own timing. You need Arduino Nano × 2, LED 5 mm red, Resistor 220 Ω, Breadboard 830 Tie Points and Jumper wires male to male (40 pcs, 20 cm). It takes 7 steps and about 100 minutes.

Level
Intermediate
Time
About 100 min
Parts
5

Parts list

Tick what you already own. It saves to My Garage, so “Build with what I have” knows.

Wiring

FromToWhy
Sender board D3Receiver board D2The data wire
Sender board GNDReceiver board GNDShared ground
Sender board D3LED long leg through a 220 ohm resistor, short leg to GNDLets you see the data flicker

Code

Plain high and low levels are hard to read if the receiver's clock drifts a little, because a long run of ones looks like a flat line. Manchester code solves this by giving every bit a change in the middle. Here a one is low then high and a zero is high then low, so the line always moves, and the receiver can check each half. The file has two sketches, one for each board. The sender sends each letter as a start bit followed by eight data bits, one thousand bits a second, and rests with the line high between letters. The receiver waits for the first falling edge, then samples each bit a quarter and three quarters of the way through, and decodes it only when the two halves differ. Join the grounds and the data wire and watch the message HELLO LARK appear on the receiver's serial monitor. Then change BIT_US on one board only and see the text turn to question marks, which shows why both ends must agree on the speed.

Runs on a virtual Arduino Nano in your browser, with sliders for the sensors. Happy with it? Verify and upload to your real board from the same page (Chrome or Edge on a computer).

// ===== SENDER (board one) =====
const uint8_t TX_PIN = 3;
const unsigned long BIT_US = 1000;      // one bit lasts 1 ms (1000 bits per second)

void half(bool level) {
  digitalWrite(TX_PIN, level);
  delayMicroseconds(BIT_US / 2);
}

void sendBit(bool b) {                   // 1 = low then high, 0 = high then low
  half(b ? LOW : HIGH);
  half(b ? HIGH : LOW);
}

void sendByte(uint8_t c) {
  sendBit(1);                            // start bit
  for (int i = 7; i >= 0; i--) sendBit((c >> i) & 1);
  digitalWrite(TX_PIN, HIGH);            // idle high between bytes
  delayMicroseconds(4 * BIT_US);
}

void setup() {
  pinMode(TX_PIN, OUTPUT);
  digitalWrite(TX_PIN, HIGH);
}

void loop() {
  const char* msg = "HELLO LARK\n";
  for (int i = 0; msg[i]; i++) sendByte(msg[i]);
  delay(2000);
}

/* ===== RECEIVER (board two, a separate sketch) =====
   Copy everything below into a new sketch for the second board. */
/*
const uint8_t RX_PIN = 2;
const unsigned long BIT_US = 1000;       // must match the sender

void setup() {
  pinMode(RX_PIN, INPUT);
  Serial.begin(9600);
  Serial.println("Waiting for Manchester data...");
}

int readHalfPair(unsigned long t0, int k) {   // returns 1, 0 or -1 for a bad bit
  while (micros() - t0 < (unsigned long)k * BIT_US + BIT_US / 4) {}
  bool a = digitalRead(RX_PIN);
  while (micros() - t0 < (unsigned long)k * BIT_US + 3 * BIT_US / 4) {}
  bool b = digitalRead(RX_PIN);
  if (a == LOW && b == HIGH) return 1;
  if (a == HIGH && b == LOW) return 0;
  return -1;
}

void loop() {
  while (digitalRead(RX_PIN) == LOW) {}      // wait for the idle level
  while (digitalRead(RX_PIN) == HIGH) {}     // the falling edge starts the start bit
  unsigned long t0 = micros();
  if (readHalfPair(t0, 0) != 1) { Serial.print('?'); return; }
  uint8_t c = 0;
  for (int k = 1; k <= 8; k++) {
    int bit = readHalfPair(t0, k);
    if (bit < 0) { Serial.print('?'); return; }
    c = (c << 1) | bit;
  }
  Serial.write(c);
}
*/

Test checklist

What you should see at each check. If a reading is far off, stop and use the troubleshooting list.

CheckExpected
Matching speedsHELLO LARK prints cleanly
Speeds differentQuestion marks or wrong letters
Data wire unpluggedNothing prints
LED on the senderFlickers while data is sent
Shorter BIT_US on bothFaster and still correct

If it does not work

Nothing prints

Check the data wire, the shared ground and that both sketches are uploaded.

Only question marks

BIT_US must match exactly on both boards.

Letters are wrong

Check the wires are short and firm.

Garbled after a long time

Reset both boards.

Upload fails

Choose the Old Bootloader option.

Still stuck? Find the fault step by step with your multimeter →

Ask Lark AI

Common questions

What parts do I need for the manchester code lab?

Arduino Nano × 2, LED 5 mm red, Resistor 220 Ω, Breadboard 830 Tie Points and Jumper wires male to male (40 pcs, 20 cm).

How long does the manchester code lab take to build?

About 100 minutes for a intermediate maker, in 7 steps.

How do I know the manchester code lab works?

Matching speeds: HELLO LARK prints cleanly. Speeds different: Question marks or wrong letters. Data wire unplugged: Nothing prints.

Nothing prints: what should I check?

Check the data wire, the shared ground and that both sketches are uploaded.

Only question marks: what should I check?

BIT_US must match exactly on both boards.

Letters are wrong: what should I check?

Check the wires are short and firm.

Garbled after a long time: what should I check?

Reset both boards.