Wiegand 26 protocol AVR example code and schematic
date: 07.03.2011

Connecting RFID card reader to AVR. Wiegand 26 example code for attiny2313.

In Wiegand 26 / Wiegand 34 / Wiegand 36 and all other Wiegand protocols used 2 wires (D0 and D1) to connect reader to MCU.

In standby D0 and D1 connected to VCC(+5V). All data from reader goes in binary format - If reader transmitting 0, it connecting D0 wire to ground for 20-50mks then connecting back to VCC and waits for 2ms before sending next bit. If goes '1' - D1 will be connected to ground fo 20-50mks and so on.
If no change in lines for more than 2ms - transfer complete.


Wiegand protocol logic level

In logic level wiegand26 have 26 bit of data, 8 for facility code, 16 for card number, and 2 for parity control.
Facility code number of building or company where card is used.
Card number card number

Readers with keyboard (for PIN access)

For transfering PIN used 4 bit message with no parity bits - one digit = one 4 bit message

Connecting RFID reader to attiny2313

Simple test schematic:

Attiny2313 + 8 oscillator, 2 capacitors from oscillator to ground, resistor from reset to vcc, pins for ICP, pins for PC-connection (FT232 or MAX232). And 78l05 for 5v power.

One connector for reader (on the left bottom side), Relay for controlling solenoid, and 2 jumpers for mode select (ADD/DEL). - door contact. REX - exit button (opens door).
Pins BEEP, RED and GREEN is for controlling LEDs an Beeper on reader - connect to ground to turn on.

board version 1

board version 2


Testing electronic lock with RFid reader


C source code - wiegand26 for attiny2313

Used CodeVision Avr (CVAVR) compiller

char dat[26]; // 26 bit for data
unsigned char fcode; // for facility code (0..255)
unsigned int code; // for card number (0..65535)


if(PIND.3==0){ // D1 on ground?
	dat[i]=1; //    bit = 1
	while(PIND.3==0){};//waiting for release

if(PIND.4==0){// D0 on ground?
	dat[i]=0; // 	bit = 0
   	while(PIND.4==0){}; //waiting for release

if(i==27) { // i=27, 26 bits collected
	//now parsing
	for(a=9;a>1;a--){ //first 8bit in revers order
		if(dat[a]==1) fcode=fcode+stepen(b); //if bit =1,
    	 							//adding position^2
									//(2^0 , 2^1 , 2^2 and etc)
		b++; // inc position

	b=0; // reset bit counter

	for(a=25;a>9;a--){//same for next 16 bit (card number)
		if(dat[a]==1) code=code+stepen(b);

//If all OK, we have
//facility code  in var fcode
//card number  in var code

//Now searching...
//all numbers in 2 arrays in eeprom
//unsigned char fcode[MAX];  - facility code
//unsigned int code[MAX];  - card number
//MAX  number of "lines" in memory
// for attiny2313 - 40 code numbers

for(i=0;i<MAX;i++){//going thru array
	// cheching only  facility code in this step
	if(fcode == fcodedb[i]){ //if code found
		//checking number in same position
		if(code==codedb[i]){ //number found...
        	 				//...we have exact numbers
			card_found=1; // flag = card found
			break;  //breaking - no need to check next lines

//card found?
	PORTD.5=1;//opening lock
	delay_ms(5000); //5 second to enter
	PORTD.5=0;//locking back
	PORTD.6=1;//turning off LED
} else{ //NO! card not found
	PORTB,0=0;//RED led
	delay_ms(3000); //delay for 3 seconds
	PORTB.0=1;//turn of red led

//now clear all data
fcode=0; // facility code
code=0; // card number
i=0; // bit counter
card_found=0; // flag

//go to first line... and wait for signal on D0 or D1

In this example we don't care about timings - no need, just waiting for all 26bit is collected - works with all readers. And no check for parity.



Board in .lay (sprint layout)

HEX and source code for CVAVR (russian comments)
wiegand26_attiny_avr_example.rar (source CVAVR)

Same code and connection for all wiegand 26 security rfid readers - HID, MIFARE, china noname, long range readers and etc.

Back to projects. zps-electronics.com