Increase data reliability using checksum check

In this article, I want to give you an example of how it can be easy to implement a simple checksum check when transfering data from Arduino 101 from and to any other device.

First of all, let's think about the importance of checksum check. When data is transfered from a source to a destination device, the data might be slightly corrupted. This might happen due to noise or many other reasons. If the data is corrupted, and the receiver uses this data, some failure might happen. Therefore, it is important that the receiver understands if the data received is reliable or not. This can be done using a checksum check. Simply speaking, additional information (checksum value) are added to the payload (useful data). By comparing the data and the checksum value, the receiver is able to understand if, during the transfer, any corruption happened.

A simple checksum check algorithm is visible above. The checksum is composed by two bytes. The first one (CK_A) is the simple sum of every bytes composing the payload. The second one (CK_B) is the sum of CK_A values calculated at each step. These 2 bytes can be appended to the payload (array), and the complete packet (payload + checksum bytes) can be sent to the receiver. The receiver then implements the same calculation, obtaining the checksum values of the payload received. If the checksum calculated (2 bytes) and the checksum received (2 bytes) are exactly the same, the data can be considered reliable. If there is any difference, the data is considered not reliable. In this second case, the receiver might request to re-send the data.

In my opinion, this algorithm is very simple, therefore I really recommend you to implement it every time you think that, due to some reason, your data might become corrupted. Here below, I give some examples:

  • Data sent, via any communication protocol (for example: Serial) from sender to receiver. Especially in the case of serial communication, in case you don't use any parity bit check, the checksum is very useful. And it is even better than the usual parity check. In fact, when using parity check, if 2 bits are flipped, the data will be still considered to be OK. But when using the above checksum algorithm on the complete payload, this does not happen.
  • Data stored on Arduino SD card, or EEPROM. Arduino lets you store data on SD cards, or EEPROM memory. There might be any corruption when writing to EEPROM memory, due to power supply noise for example. Also, in case of SD card, the data transfer is performed using SPI communication protocol. During the transfer, some corruption might happen. A possible way to solve this issue is using both checksum check, and data mirroring (having multiple copies): in case data block #1 is corrupted, data block #2 is used (if not corrupted), and the content of data block #2 is copied to data block #1.

The following is an example of Arduino sketch which uses the checksum check. Download here: IMU_send_20160625. The program, which runs on Arduino 101, acquires the raw data from gyroscope and acceleration sensors unit. This is performed once every millisecond, by using an interrupt call. The, the PC can poll this raw data by sending a command "s" via Serial communication. The complete packet size includes a header (packet number, 2 bytes), 12 bytes of data (3-axis acceleration, 3-axis gyro, 2 bytes each), plus 2 bytes of checksum. The payload are the first 14 bytes (header plus sensors raw data).

Author: Davide Cavaliere

I am an Italian Electrical Engineer graduated at Politecnico di Milano. My interests are motorcycles and cars, electronics, programming, Internet of Things, and Japanese culture.

2 thoughts on “Increase data reliability using checksum check”

    1. Thank you. Especially for critical data, I always use checksum, because it is easy to implement, it does not require much calculation power (especially if the byte size is small).

Leave a Reply

Your email address will not be published. Required fields are marked *