I had some problems with the Wiegand demo code, in particular it wasn’t returning the numbers printed on the tags. This simpler version does return the numbers on the tags and is I think a bit clearer, though it will be a little slower.
The main problem with the original code seems to be that the data pins are mislabelled in the data sheet. The DATA1 line, that is the line that goes low when the output bit is 1, is actually pin 1 on the board (the one closest to the edge), not pin 2. The DATA0 line is pin 2.
Anyway here is the code, hope this helps someone:
[code]//******************************
#ifndef RFID
#define RFID
//==============================
#define DATA0 9 // DATA0 from RFID module (pin 2) connected to this Arduino pin. Note these seem to be mislabelled on the data sheet?
#define DATA1 8 // DATA1 from RFID module (pin 1) connected to this Arduino pin
//==============================
#endif
//******************************
unsigned long tag=0;
unsigned char receive_count=0;
void setup()
{
Serial.begin(115200);
pinMode(DATA0, INPUT);
digitalWrite(DATA0, HIGH); // pullup
pinMode(DATA1, INPUT);
digitalWrite(DATA1, HIGH); // pullup
}
void loop()
{
unsigned char data0=0,data1=0;
// data1 low = bit 1 transmitted, data0 low = bit 0 transmitted
data0 = digitalRead(DATA0);
data1 = digitalRead(DATA1);
//--------------------------------------------
if(data0 != data1) // new bit detected
{
if (receive_count != 0) { // skip the first parity bit
tag <<= 1; // shift previous bits left
if (!data1) { // bit is one if data1 is low
tag |= 1;
}
}
receive_count++;
delayMicroseconds(80); //Data impulse width delay 80us
}
else // no card incoming or finish reading card
{
if(receive_count>= 25) //output card number, skipping the last parity bit so don’t wait for bit 26
{
// Serial output will take longer than 80uS, otherwise you might need a delay here to make sure you skip the final parity bit
Serial.println(tag, DEC);
// start new tag
receive_count = 0;
tag = 0;
}
//----------------------------------------------------
}
} [/code]