Design a serial protocol with checksums
Two boards exchange framed packets with start bytes, length and CRC, and recover from noise.
Build it step by step
0 of 7 doneDefine the frame
Start, length, type, data, CRC.
Write the CRC
Test it on a known string.
Write the sender
Send a counter and a sensor reading.
Write the receiver state machine
Print good packets.
Add ACK and NAK
Let the receiver answer.
Inject errors
Hold the button and watch the resend.
Measure throughput
Count good packets per minute.
How do you build a design a serial protocol with checksums?
Two boards exchange framed packets with start bytes, length and CRC, and recover from noise. You need Arduino Nano × 2, Jumper wires male to male (40 pcs, 20 cm), Breadboard 830 Tie Points and Tactile push button 6 mm. It takes 7 steps and about 120 minutes.
- Level
- Intermediate
- Time
- About 120 min
- Parts
- 4
Parts list
- Development BoardsBuyArduino Nano × 2
- PrototypingBuy
- PrototypingBuy
- SwitchesBuy
Tick what you already own. It saves to My Garage, so “Build with what I have” knows.
Wiring
| From | To | Why |
|---|---|---|
| Nano A pin 11 (TX) | Nano B pin 10 (RX) | Data from A to B |
| Nano B pin 11 (TX) | Nano A pin 10 (RX) | Replies from B to A |
| Nano A GND | Nano B GND | Common ground |
| Button | Nano A pin 2 and GND | Injects an error into the next packet |
| Both Nanos | USB cables (to a computer or adapters) | Power and serial monitor |
Code
A frame is a start byte, a length, a type, the data, and a CRC-8 checksum computed over the length, type and data. The receiver is a small state machine that discards anything until it sees the start byte, refuses lengths above 32 so noise cannot make it wait forever, and checks the CRC before trusting the data. On a good packet it answers ACK; on a bad one NAK, which makes the sender resend. If neither arrives the sender times out and tries up to three times. Hold the button on board A to corrupt the next packet and watch the NAK and resend appear. A CRC catches nearly every accidental error but is not security: anyone can compute it, so it protects against noise and not against an attacker. For real links add a frame counter so repeated packets can be spotted. The sketch uses SoftwareSerial at 9600 baud, which is reliable at that speed. Arduino IDE, board "Arduino Nano" (try the Old Bootloader option if the upload fails).
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).
// Packet protocol with CRC, shared by both boards. Set SENDER to 1 for Nano A and 0 for Nano B.
// Board: Arduino Nano. Library: SoftwareSerial (built in). Link on pins 10 (RX) and 11 (TX) at 9600 baud.
#include <SoftwareSerial.h>
#define SENDER 1
SoftwareSerial link(10, 11);
const byte START = 0xAA, ACK = 0x06, NAK = 0x15;
byte crc8(const byte* d, byte n) { // CRC-8, polynomial 0x07
byte c = 0;
for (byte i = 0; i < n; i++) {
c ^= d[i];
for (byte b = 0; b < 8; b++) c = (c & 0x80) ? (c << 1) ^ 0x07 : (c << 1);
}
return c;
}
// ---- Frame: START, LEN, TYPE, DATA[LEN], CRC over LEN, TYPE and DATA ----
void sendFrame(byte type, const byte* data, byte len, bool corrupt) {
byte body[34]; body[0] = len; body[1] = type;
for (byte i = 0; i < len; i++) body[2 + i] = data[i];
byte c = crc8(body, len + 2);
if (corrupt) c ^= 0x55; // deliberately wrong checksum
link.write(START); link.write(body, len + 2); link.write(c);
}
#if SENDER
byte counter = 0; unsigned long lastSend = 0; byte pending[4]; bool waitingAck = false; unsigned long sentAt = 0; byte retries = 0;
unsigned long goodAcks = 0, resends = 0;
void setup() { pinMode(2, INPUT_PULLUP); Serial.begin(115200); link.begin(9600); }
void loop() {
if (!waitingAck && millis() - lastSend > 500) { // send a new packet every half second
counter++; pending[0] = counter; pending[1] = analogRead(A0) >> 2; pending[2] = 0x12; pending[3] = 0x34;
sendFrame(1, pending, 4, digitalRead(2) == LOW); // button held = corrupt this packet
waitingAck = true; sentAt = millis(); lastSend = millis(); retries = 0;
}
if (link.available()) {
byte r = link.read();
if (r == ACK) { waitingAck = false; goodAcks++; Serial.print("ACK, total "); Serial.println(goodAcks); }
else if (r == NAK) { sendFrame(1, pending, 4, false); resends++; Serial.println("NAK, resent"); }
}
if (waitingAck && millis() - sentAt > 300) { // no reply: resend, give up after 3 tries
if (++retries > 3) { waitingAck = false; Serial.println("Gave up"); }
else { sendFrame(1, pending, 4, false); sentAt = millis(); resends++; }
}
}
#else
enum State { WAIT_START, GET_LEN, GET_TYPE, GET_DATA, GET_CRC };
State st = WAIT_START; byte len, type, buf[34], idx; unsigned long good = 0, bad = 0;
void setup() { Serial.begin(115200); link.begin(9600); }
void loop() {
while (link.available()) {
byte b = link.read();
switch (st) {
case WAIT_START: if (b == START) st = GET_LEN; break;
case GET_LEN: if (b <= 32) { len = b; buf[0] = b; st = GET_TYPE; } else st = WAIT_START; break; // refuse silly lengths
case GET_TYPE: type = b; buf[1] = b; idx = 0; st = (len ? GET_DATA : GET_CRC); break;
case GET_DATA: buf[2 + idx++] = b; if (idx == len) st = GET_CRC; break;
case GET_CRC:
if (b == crc8(buf, len + 2)) { good++; link.write(ACK); Serial.print("Good packet, counter "); Serial.println(buf[2]); }
else { bad++; link.write(NAK); Serial.print("Bad CRC, total bad "); Serial.println(bad); }
st = WAIT_START; break;
}
}
}
#endifTest checklist
What you should see at each check. If a reading is far off, stop and use the troubleshooting list.
| Check | Expected |
|---|---|
| Normal running | Good packets and ACKs twice a second |
| Button held | Bad CRC then NAK, then a resend that succeeds |
| Unplug the wire | Sender gives up after three tries |
| Replug | Link recovers |
If it does not work
Nothing received
Cross TX to RX and join the grounds.
Every packet bad
Check both boards use the same CRC polynomial and baud rate.
Garbage on the monitor
Match the serial monitor to 115200.
Stalls after noise
Make sure the state machine returns to WAIT_START.
Still stuck? Find the fault step by step with your multimeter →
Common questions
What parts do I need for the design a serial protocol with checksums?
Arduino Nano × 2, Jumper wires male to male (40 pcs, 20 cm), Breadboard 830 Tie Points and Tactile push button 6 mm.
How long does the design a serial protocol with checksums take to build?
About 120 minutes for a intermediate maker, in 7 steps.
How do I know the design a serial protocol with checksums works?
Normal running: Good packets and ACKs twice a second. Button held: Bad CRC then NAK, then a resend that succeeds. Unplug the wire: Sender gives up after three tries.
Nothing received: what should I check?
Cross TX to RX and join the grounds.
Every packet bad: what should I check?
Check both boards use the same CRC polynomial and baud rate.
Garbage on the monitor: what should I check?
Match the serial monitor to 115200.
Stalls after noise: what should I check?
Make sure the state machine returns to WAIT_START.
