Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Complete example for reading the 125Khz RFID module RDM630
#include <SoftwareSerial.h>
// Pin definitions
const int rfid_irq = 0;
const int rfid_tx_pin = 6;
const int rfid_rx_pin = 7;
// For communication with RFID module
SoftwareSerial rfid(rfid_tx_pin, rfid_rx_pin);
// Indicates that a reading is now ready for processing
volatile bool ready = false;
// Buffer to contain the reading from the module
uint8_t buffer[14];
uint8_t* buffer_at;
uint8_t* buffer_end = buffer + sizeof(buffer);
void rfid_read(void);
uint8_t rfid_get_next(void);
void setup(void)
// Open serial connection to host PC to view output
// Open software serial connection to RFID module
// Listen for interrupt from RFID module
void loop(void)
if ( ready )
// Convert the buffer into a 32-bit value
uint32_t result = 0;
// Skip the preamble
// Accumulate the checksum, starting with the first value
uint8_t checksum = rfid_get_next();
// We are looking for 4 more values
int i = 4;
// Grab the next value
uint8_t value = rfid_get_next();
// Add it into the result
result <<= 8;
result |= value;
// Xor it into the checksum
checksum ^= value;
// Pull out the checksum from the data
uint8_t data_checksum = rfid_get_next();
// Print the result
Serial.print("Reading: ");
if ( checksum == data_checksum )
Serial.println(" OK");
Serial.println(" CHECKSUM FAILED");
// We're done processing, so there is no current value
ready = false;
// Convert the next two chars in the stream into a byte and
// return that
uint8_t rfid_get_next(void)
// sscanf needs a 2-byte space to put the result but we
// only need one byte.
uint16_t result;
// Working space to assemble each byte
static char byte_chars[3];
// Pull out one byte from this position in the stream
buffer_at += 2;
return static_cast<uint8_t>(result);
void rfid_read(void)
// Only read in values if there is not already a value waiting to be
// processed
if ( ! ready )
// Read characters into the buffer until it is full
buffer_at = buffer;
while ( buffer_at < buffer_end )
*buffer_at++ =;
// Reset buffer pointer so it's easy to read out
buffer_at = buffer;
// Signal that the buffer has data ready
ready = true;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment