Thursday, September 25, 2008

Magnetic card access the electonic way




Magnetic card lock :- step 1Hardware
Obviously, you first must obtain a magnetic stripe reader. I'm using an Omron V3A-4K that I ordered from digikey. It cost me $20.00 or so. If you can't find one of these, any standard TTL reader will do.

Don't worry about buying one of the fancy harnesses that they sell. There are breakout pads on the circuit board inside of the reader. Once you have received your reader, pop off the side cover, and solder wires to the pads as shown in the picture. Of course, if you have a different reader, the wiring will probably be different. In this case, consult your reader's datasheet to locate the necessary pads.

Next, connect the wires to the Arduino's digital pins as follows:

DATA - 2
CLK - 3
LOAD - 5

Finally, connect the +5v and GND to their respective terminals on the Arduino board.





step 2Software
This step is easy. Simply load the attached sketch on to your Arduino.

Note: I didn't write this code, I found it here. I've just attached it here for convenience.

Arduino_Magstripe_Reader.pde4 KB

Program for the access of the magenatic access card lock

/*
* Magnetic Stripe Reader
* by Stephan King http://www.kingsdesign.com
*
* Reads a magnetic stripe.
*
*/

int cld1Pin = 5; // Card status pin
int rdtPin = 2; // Data pin
int reading = 0; // Reading status
volatile int buffer[400]; // Buffer for data
volatile int i = 0; // Buffer counter
volatile int bit = 0; // global bit
char cardData[40]; // holds card info
int charCount = 0; // counter for info
int DEBUG = 0;

void setup() {
Serial.begin(9600);

// The interrupts are key to reliable
// reading of the clock and data feed
attachInterrupt(0, changeBit, CHANGE);
attachInterrupt(1, writeBit, FALLING);
}

void loop(){

// Active when card present
while(digitalRead(cld1Pin) == LOW){
reading = 1;
}

// Active when read is complete
// Reset the buffer
if(reading == 1) {

if (DEBUG == 1) {
printBuffer();
}

decode();
reading = 0;
i = 0;

int l;
for (l = 0; l < 40; l = l + 1) {
cardData[l] = '\n';
}

charCount = 0;
}
}

// Flips the global bit
void changeBit(){
if (bit == 0) {
bit = 1;
} else {
bit = 0;
}
}

// Writes the bit to the buffer
void writeBit(){
buffer[i] = bit;
i++;
}

// prints the buffer
void printBuffer(){
int j;
for (j = 0; j < 200; j = j + 1) {
Serial.println(buffer[j]);
}
}

int getStartSentinal(){
int j;
int queue[5];
int sentinal = 0;

for (j = 0; j < 400; j = j + 1) {
queue[4] = queue[3];
queue[3] = queue[2];
queue[2] = queue[1];
queue[1] = queue[0];
queue[0] = buffer[j];

if (DEBUG == 1) {
Serial.print(queue[0]);
Serial.print(queue[1]);
Serial.print(queue[2]);
Serial.print(queue[3]);
Serial.println(queue[4]);
}

if (queue[0] == 0 & queue[1] == 1 & queue[2] == 0 & queue[3] == 1 & queue[4] == 1) {
sentinal = j - 4;
break;
}
}

if (DEBUG == 1) {
Serial.print("sentinal:");
Serial.println(sentinal);
Serial.println("");
}

return sentinal;
}

void decode() {
int sentinal = getStartSentinal();
int j;
int i = 0;
int k = 0;
int thisByte[5];

for (j = sentinal; j < 400 - sentinal; j = j + 1) {
thisByte[i] = buffer[j];
i++;
if (i % 5 == 0) {
i = 0;
if (thisByte[0] == 0 & thisByte[1] == 0 & thisByte[2] == 0 & thisByte[3] == 0 & thisByte[4] == 0) {
break;
}
printMyByte(thisByte);
}
}

Serial.print("Stripe_Data:");
for (k = 0; k < charCount; k = k + 1) {
Serial.print(cardData[k]);
}
Serial.println("");

}

void printMyByte(int thisByte[]) {
int i;
for (i = 0; i < 5; i = i + 1) {
if (DEBUG == 1) {
Serial.print(thisByte[i]);
}
}
if (DEBUG == 1) {
Serial.print("\t");
Serial.print(decodeByte(thisByte));
Serial.println("");
}

cardData[charCount] = decodeByte(thisByte);
charCount ++;
}

char decodeByte(int thisByte[]) {
if (thisByte[0] == 0 & thisByte[1] == 0 & thisByte[2] == 0 & thisByte[3] == 0 & thisByte[4] == 1){
return '0';
}
if (thisByte[0] == 1 & thisByte[1] == 0 & thisByte[2] == 0 & thisByte[3] == 0 & thisByte[4] == 0){
return '1';
}

if (thisByte[0] == 0 & thisByte[1] == 1 & thisByte[2] == 0 & thisByte[3] == 0 & thisByte[4] == 0){
return '2';
}

if (thisByte[0] == 1 & thisByte[1] == 1 & thisByte[2] == 0 & thisByte[3] == 0 & thisByte[4] == 1){
return '3';
}

if (thisByte[0] == 0 & thisByte[1] == 0 & thisByte[2] == 1 & thisByte[3] == 0 & thisByte[4] == 0){
return '4';
}

if (thisByte[0] == 1 & thisByte[1] == 0 & thisByte[2] == 1 & thisByte[3] == 0 & thisByte[4] == 1){
return '5';
}

if (thisByte[0] == 0 & thisByte[1] == 1 & thisByte[2] == 1 & thisByte[3] == 0 & thisByte[4] == 1){
return '6';
}

if (thisByte[0] == 1 & thisByte[1] == 1 & thisByte[2] == 1 & thisByte[3] == 0 & thisByte[4] == 0){
return '7';
}

if (thisByte[0] == 0 & thisByte[1] == 0 & thisByte[2] == 0 & thisByte[3] == 1 & thisByte[4] == 0){
return '8';
}

if (thisByte[0] == 1 & thisByte[1] == 0 & thisByte[2] == 0 & thisByte[3] == 1 & thisByte[4] == 1){
return '9';
}

if (thisByte[0] == 0 & thisByte[1] == 1 & thisByte[2] == 0 & thisByte[3] == 1 & thisByte[4] == 1){
return ':';
}

if (thisByte[0] == 1 & thisByte[1] == 1 & thisByte[2] == 0 & thisByte[3] == 1 & thisByte[4] == 0){
return ';';
}

if (thisByte[0] == 0 & thisByte[1] == 0 & thisByte[2] == 1 & thisByte[3] == 1 & thisByte[4] == 1){
return '<';
}

if (thisByte[0] == 1 & thisByte[1] == 0 & thisByte[2] == 1 & thisByte[3] == 1 & thisByte[4] == 0){
return '=';
}

if (thisByte[0] == 0 & thisByte[1] == 1 & thisByte[2] == 1 & thisByte[3] == 1 & thisByte[4] == 0){
return '>';
}

if (thisByte[0] == 1 & thisByte[1] == 1 & thisByte[2] == 1 & thisByte[3] == 1 & thisByte[4] == 1){
return '?';
}
}

step 3Use it!
Finally, simply open the serial connection in the arduino applet, and start swiping cards! The decoded data from the card will appear in the window as soon as you swipe one.

No comments: