CRC16 Error Checking Help


I'm just a newbie but I've been put on a project that goes past what limited coding experience I have.

I need to setup a "simple" crc-16 error checking program. Data will come in via serial with this format : AA 1000 25; header, crc. Via an 8 byte package.

I am trying to run the crc16.h library to perform this but I don't even know where to start. My assumption is the Serial.Event () will be the way to read the data?

You should start by learning how to read and analyze the contents of one packet. When that is working, come back, post your code and ask about CRC16 error checking.

If your are really a newbie, then this exercise you've been given is probably intended to teach you to extract bytes and bits from data streams, and manipulate them. So focus at the moment on the bitwise operators, and extracting the high and low bytes from words. Once you've mastered that, the project should be not so bad.

Good luck,

John Doner

Does the library come with any example sketches? Most do.

A look at the subject in Wikipedia tells me there isn’t any one CRC16 method but many.

Perhaps the most direct way to get it right is to tear into that library, assuming that you have the source.

So with some help from coworker here I was able to get the CRC error checking code started. The next step for this is to receive incoming data from a Digi X4 gateway via serial and route it appropriately. Data with the correct CRC checksum needs to be pushed out via a PWM port with a high frequency signal that has a simple RC filter on it, (that at least is done and stable) this signal will be pushed to the plunger controller input, the remaining data from some other devices needs to be pushed out another serial port to a different port on the controller.

#include <PWM.h>
#include <util/crc16.h>

int32_t frequency = 50000; //in HZ

void setup() {


bool success = SetPinFrequency(9, frequency);
if(success) {


void loop() {

int sensorValue = analogRead(A0);
pwmWrite(9, sensorValue / 4);

// read from port 0, send to port 1:
if (Serial1.available()) {
int inByte =;
uint16_t crc = 0xFFFF;
uint8_t data = {0xAA, 0x12, 0x34};
uint8_t i;

for (i = 0; i < sizeof data / sizeof data[0]; i++)
crc = _crc16_update(crc, data*);*

  • Serial.println(crc, HEX); *
  • Serial1.println(crc, HEX); *