Projects / CRC error detection lab

CRC error detection lab

Intermediate1.5 hGuided buildReviewed

Build it step by step

0 of 7 done
  1. NANO

    Upload the sender to board one

    Wire its two buttons to D4 and D5.

  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

    Watch the output

    HELLO LARK CRC OK sum OK each second.

  5. NANO

    Hold button A

    One letter changes and both checks fail.

  6. NANO

    Hold button B

    The letters swap and only the CRC fails.

  7. NANO

    Release the buttons

    Both go back to OK.

In short

How do you build a CRC error detection lab?

One Nano sends short messages with a CRC check number and a simple sum check, a second Nano checks them, and two buttons on the sender deliberately damage the message. You see how data links notice mistakes, and why a CRC beats a plain sum. You need Arduino Nano × 2, Tactile push button 6 mm × 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 90 minutes.

Level
Intermediate
Time
About 90 min
Parts
6

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 D2Serial data wire
Sender board GNDReceiver board GNDShared ground
Sender button A leg 1Sender D4Hold to flip one bit
Sender button A leg 2Sender GNDPressing joins D4 to ground
Sender button B leg 1Sender D5Hold to swap two letters
Sender button B leg 2Sender GNDPressing joins D5 to ground
Receiver LED long legReceiver D8 through a 220 ohm resistor, short leg to GNDLights when the CRC fails

Code

A check number is calculated from the message and sent along with it, and the receiver calculates it again. If the numbers differ, something changed on the way. The file has two sketches, one for each board. The sender repeats HELLO LARK once a second with two check numbers, a simple sum of the bytes and a CRC-8, which is a more clever calculation that treats the message as a long binary number and divides it. Hold button A and one bit of one letter flips, which both checks catch. Hold button B and two letters swap places. The sum stays the same, because adding in a different order gives the same total, so the sum check says OK, but the CRC changes and still catches the error. The LED on the receiver lights when the CRC fails. This is why real links such as Ethernet and USB use CRCs. Note that SoftwareSerial is reliable at 9600 baud, and that both boards must share a ground.

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) =====
#include <SoftwareSerial.h>
SoftwareSerial link(10, 3);               // RX pin 10 unused, TX pin 3

uint8_t crc8(const uint8_t* data, int len) {   // CRC-8, polynomial 0x07
  uint8_t crc = 0;
  for (int i = 0; i < len; i++) {
    crc ^= data[i];
    for (int b = 0; b < 8; b++) crc = (crc & 0x80) ? (crc << 1) ^ 0x07 : (crc << 1);
  }
  return crc;
}

uint8_t sum8(const uint8_t* data, int len) {   // a plain sum check
  uint8_t s = 0;
  for (int i = 0; i < len; i++) s += data[i];
  return s;
}

void setup() {
  link.begin(9600);
  pinMode(4, INPUT_PULLUP);
  pinMode(5, INPUT_PULLUP);
}

void loop() {
  uint8_t msg[10] = {'H','E','L','L','O',' ','L','A','R','K'};
  uint8_t crc = crc8(msg, 10);              // both checks are made on the good message
  uint8_t sum = sum8(msg, 10);
  if (digitalRead(4) == LOW) msg[2] ^= 0x08;                    // damage 1: flip one bit
  if (digitalRead(5) == LOW) { uint8_t t = msg[5]; msg[5] = msg[6]; msg[6] = t; }   // damage 2: swap two bytes
  for (int i = 0; i < 10; i++) link.write(msg[i]);
  link.write(crc);
  link.write(sum);
  delay(1000);
}

/* ===== RECEIVER (board two, a separate sketch) =====
   Copy everything below into a new sketch for the second board. */
/*
#include <SoftwareSerial.h>
SoftwareSerial link(2, 11);               // RX pin 2, TX pin 11 unused
const uint8_t LED_PIN = 8;

uint8_t crc8(const uint8_t* data, int len) {
  uint8_t crc = 0;
  for (int i = 0; i < len; i++) {
    crc ^= data[i];
    for (int b = 0; b < 8; b++) crc = (crc & 0x80) ? (crc << 1) ^ 0x07 : (crc << 1);
  }
  return crc;
}

uint8_t sum8(const uint8_t* data, int len) {
  uint8_t s = 0;
  for (int i = 0; i < len; i++) s += data[i];
  return s;
}

void setup() {
  pinMode(LED_PIN, OUTPUT);
  link.begin(9600);
  Serial.begin(9600);
  Serial.println("Waiting for frames...");
}

void loop() {
  uint8_t buf[12];
  int n = 0;
  unsigned long start = millis();
  while (n < 12 && millis() - start < 1500) {
    if (link.available()) buf[n++] = link.read();
  }
  if (n < 12) return;                       // not a whole frame
  bool crcOk = (crc8(buf, 10) == buf[10]);
  bool sumOk = (sum8(buf, 10) == buf[11]);
  digitalWrite(LED_PIN, crcOk ? LOW : HIGH);
  for (int i = 0; i < 10; i++) Serial.write(buf[i]);
  Serial.print("   CRC ");
  Serial.print(crcOk ? "OK  " : "FAIL");
  Serial.print("   sum ");
  Serial.println(sumOk ? "OK" : "FAIL");
}
*/

Test checklist

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

CheckExpected
No buttonCRC OK, sum OK
Button A heldA changed letter, CRC FAIL, sum FAIL
Button B heldTwo letters swapped, CRC FAIL, sum OK
Data wire unpluggedNothing prints
LEDLights while the CRC fails

If it does not work

Nothing prints

Check the data wire, shared ground and both uploads.

Always FAIL

Check both sketches use the same CRC code.

Garbled letters

SoftwareSerial is best at 9600, check the wires are short.

Button has no effect

Check the buttons go to D4 and D5 and GND.

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 CRC error detection lab?

Arduino Nano × 2, Tactile push button 6 mm × 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 CRC error detection lab take to build?

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

How do I know the CRC error detection lab works?

No button: CRC OK, sum OK. Button A held: A changed letter, CRC FAIL, sum FAIL. Button B held: Two letters swapped, CRC FAIL, sum OK.

Nothing prints: what should I check?

Check the data wire, shared ground and both uploads.

Always FAIL: what should I check?

Check both sketches use the same CRC code.

Garbled letters: what should I check?

SoftwareSerial is best at 9600, check the wires are short.

Button has no effect: what should I check?

Check the buttons go to D4 and D5 and GND.