Everyone has used a magnetic card reader, I believe. I mean, who carries cash these days? They’re not difficult to get your hands on, either, and during a trip to my favorite local electronics shop, I found a bin full of these guys. So….of course, I picked one up and brought it home to see what sort of stuff I could do with it and an AVR.
This instructable will show you how to connect a Magtek magnetic card reader to an AVR or Arduino/clone and read data from the first track of card. Buckle your seats; magnetic card readers have a high bit rate!
Step 1: The Equipment List
Here are a few things you’ll need to get started.
- Magnetic card reader (Mine is a Magetk 90mm dual-head reader. $5.00)
- AVR, Arduino, or clone (ATmega328p ~ $4.30 from Mouser.com
- solderless breadboard
- some wire
- maybe a header if you like that sorta thing.
- something to read your serial port. I use AVR Terminal from BattleDroids.net
That’s all you should need to get started. Depending on the magcard reader you end up getting, you may have to modify these instructions, and most assuredly the code, to work with your specific reader. However, the code I’ve written should get you pretty far, I hope.
Step 2: Self-clocking Magnetic Card Readers
Magnetic card readers are “self-clocking,” meaning that they provide a clock called a strobe, against which the connected microcontroller can sync. This is a boon. It means you don’t have to worry about looking for a clocking signal and timing the signal to center directly on the clock pulse, and no bothersome oscillating into the sweet spot of the clock signal. This make sense when you think about card swipes: everyone swipes at a different pace, some slower, some faster than others. Self-clocking allows even my sweet grandma the ability to use her card without breaking her wrist. Reminds me of having to change the setting for her that determines how much time is valid between clicks to register a double-click….
This card reader’s data is valid 1.0 us before the strobe is put onto the line, so there’s no worry about delaying to get yourself into the “bit time.” For a dual head reader such as the one I’m using, there are two data tracks available to read. In this ‘ible, I’m going to show reading from the primary first track to get you started.
There are five connections you will need to make (four if you don’t mind giving up more fine tuned control for fewer I/O ports being used). Check out the picture below. The red wire goes to +5V while the black wire goes to ground. The green wire is /CARD_PRESENT; the yellow wire is /STROBE, and the white wire is /DATA1. The forward slash ( / ) means that the data is inverted. A low signal (ie 0) is read as a one, or high. The other connectors are brown for /STROBE2 and orange for /DATA2. We won’t be using these.
If you want, you can forget about /CARD_PRESENT. This data line goes low after about 17 head flux rotations to indicate that a card is present (instead of, say, random noise causing your reader to send bogus data) and is used to validate that the data you’re getting is card data and not junk. You can skip this connection if you check for the start sentinel on the data stream. More on that later.
As you can see below, I used a right angle male header connected to a bread board and connected my reader to that. I connected /STROBE to PIND2 (digital pin 2 on an Arduino), /CARD_PRESENT to PIND3 (for illustration purposes), and /DATA1 to PIND4. Make sure you enable pullups on these pins so your pins don’t float. I also traded out my Arduino for a Bare Bones AVR because I like the way it fits into the breadboard.
Step 3: Magnetic Card Basics
The primary functions that you’ll need to do to read a magnetic card are:
1. Detect when the card has been swiped
2. Read the stream of data
3. Detect when the card has gone
4. Process the data
5. Display the data
First, I’ll introduce you to some magnetic card basics that you’ll need to know when you start writing your own code.
For more detail: Turn your Arduino into a Magnetic Card Reader