The simplest magnetic stripe writer
After I successfully finished my tests of the
simplest magnetic stripe reader, I started the next natural project: a
writer as simple as the reader. While I had read contradictory informations, it
was my believe that it is possible to write tracks using a magnetic head from
an old music cassette player. This was further confirmed when I accidentally
erased a couple of cards using my reader without a capacitor coupled with the
microphone input (see the details).
I did some tests and finally I was able to write low density tracks, that
later I could read successfully. However I found some problems with high
density tracks. While I was able to write them, I couldn't get any successful
read out of them. My research showed that there seemed to be a problem handling
the higher frequencies associated with high density tracks when compared to low
density tracks. I started to study whether a pass-band filter in the writer
might help, but then I got busy with other subjects and the work was never
Therefore all the work has been kept unpublished for many years. However
every now and then some people e-mailed me asking for this kind of information.
This has triggered me to gather up the information and now that I have found
the time, I have quickly written it all up. But the work is still uncompleted,
I will simply describe my setup and provide the experimental, yet unfinished,
My design for the writer is complementary of that for the reader. I use the same magnetic head to which I have
connected a switch. In one position of the switch (read position) one pin of
the head is connected to a capacitor (which is optional, see discussion above)
and this one to the jack plug for the microphone input. In the other position
of the switch (write position) this pin of the head is connected to a DB-9
socket connector (female) for the serial port (it is also possible to use a
DB-25 socket if so is the size of the serial port connector of your PC). The
other pin of the head is always connected to ground of both, the jack plug and
the serial port connector.
Therefore I use the serial port as the source of current to feed the
magnetic head and to write the track. I chose the serial port because according
to the specifications and to my experiments, this is the most powerful DC
current source readily available in usual PCs that is register controllable
(on/off switchable by software), and with the advantage of having double
polarity (+/-12V). Within the serial port there are several pins you can use as
source. I chose the DTR pin. It is simply a convention, you might use any other
as well. With the serial port and a good magnetic head you can write low
coercivity as well as high coercivity magnetic media.
One might be tempted to use the sound output from the sound card as source
for the magnetic head, but this is usually not advisable. There are two main
reasons for this, one is that it is more simple to control the serial port than
the sound card, and you don't need to generate a complex waveform, a square
wave is perfect. The second reason is that the sound output from the sound card
might not provide enough current to write tracks without an external amplifier.
It is important to note that not all magnetic heads are well suited to be
used for writing using the serial port of the PC as source. Some heads do need
more current than it can be provided by the serial port to write a track. One
way to select a good head is to measure the ohmic resistance between its two
pins. The higher the resistance, the better. A higher resistance implies a
longer coil, which in turn means a higher magnetic field for the same current
flowing. If you are unable to write tracks using the serial port as source
(either because of the head or the serial port, or because the combination of
both), you can always use an external power supply. To control it you can use a
simple circuit like the one outlined next or a variation of it.
You can connect the positive terminal of the power supply to the collector
of a general purpose NPN silicon transistor. The emitter of the transistor
should be connected to one terminal of a variable resistor, and the other
terminal of the resistor to ground (the negative terminal of the power supply
together with the ground of the serial port). The emitter should also be
connected to the positive terminal of a electrolytic capacitor and the other
terminal of the capacitor to one end of the magnetic head. The other end of the
magnetic head should be connected to ground. The base of the transistor should
be connected to the DTR pin of the serial port. This
way switching DTR on and off you will switch the transistor thus allowing to
oscillate the current in the magnetic head (RLC resonator). You should tune the
variable resistor to the value in which it has maximum resistance while being
able to write tracks easily.
There is an additional hardware requirement for a writer. You need a system
to synchronize the flow of data to be written with the swipe of the card. One
way to do that is to place a sensor that is activated shortly before the
magnetic track gets in contact with the magnetic head. Actually it is better to
place two sensors so that you can get an estimate of the speed of the swipe.
This way you can automatically adapt the speed of the flow of data, and thus
the record is finally written at the correct nominal track density.
I use a very simple approach for the sensor. I attach a piece of wire to
the magnetic head which I swipe over the magnetic track (I read/write cards
with a procedure which is opposed to the usual method, swiping cards over the
head). I also place two wires fixed on the board where the card to be written
is placed, in a way so that when the card is swiped the wire in the magnetic
head briefly touches both wires on the board successively, just before reaching
the magnetic stripe. The wire in the magnetic head is connected to the RTS pin
of the serial port and the wires on the board are
connected to the DSR pin of the serial port.
This way the PC knows when the magnetic head is about to get in contact
with the magnetic media, and at what speed. Of course there are other possible
choices for the sensors. A quite natural approach would be to use a LED and a
photo-diode or photo-transistor for each sensor (you can get these
photo-devices from an old PC ball mouse). This has the advantage of getting rid
of mechanical parts which are always subject to easily deteriorate, but it has
the disadvantage of having to design and to attach additional electronic
components to polarize the photo-devices and to amplify signals. But you still
can use the same pins of the serial port and therefore use my software without
Last, but not least, you will need a magnet to erase the card prior to
being written. Normally you should erase the whole magnetic stripe, i.e. all
usual three tracks at the same time, by rubbing the magnetic stripe from one
end to the other with the magnet. Usually one pass should be sufficient, but
you can do several of them just to be sure. This procedure is valid for low and
for high coercivity magnetic media. Then the card is ready to be written, track
by track, for example starting with track 1 and ending with track 3.
Using the software
I have developed an experimental C program for
linux to write cards. Instructions to compile are included inside. Of
course you can develop your own program to write data using Windows, for
example, but please don't ask me to do it. If you do not have and do not want
to install Linux (you don't know what you are missing ;-) you still have the
choice to run Linux on CD and use my programs, see my page running Linux without installing it.
There are a few things you should configure before compiling the program,
they all are at the beginning of the code. One is the data regarding the port,
by default it uses pins RTS, DTR and DSR of COM1 serial
port. There are other things you can configure (see source for details, it
is self-explanatory) for which you can leave the default values, but you must
type the exact value of the distance between your two sensors (DIST macro).
Otherwise the estimate of the speed would surely be wrong, consequently the
record density will not be adequate and the software would not be able to
detect the error.
Both sensors can not be too close one to each other. The distance should be
such that it can be easily measured with millimetric precision, and that it
allows a reliable measure of the speed, having into account the time resolution
of the PC. Above half a centimetre should be ok. One of the sensors (the last
one) and the magnetic track (or the magnetic head, depending on your swiping
style), should be placed side by side. This way when the last sensor is
activated the program would start sending the leading clocking bits, and they
will arrive to the magnetic head on time to get written in the magnetic stripe.
Tuning the parameter of the number of leading clocking bits will help to write
the data at the right position in the magnetic stripe.
Once you have compiled the C program, connect the magnetic head and the
sensors to the serial port, then you just have to run the program and swipe the
card. The program must be run with root user to gain access to the serial port
and to run with real time priority. It is required to run with the highest
priority to guarantee the correct timing of the data signals, so you may notice
that the computer freezes when you run the program; it is normal, it will come
to a normal state when the program exits. When you run the program it waits for
activity in the sensors, that is, a card swipe. If no activity is detected
within the time-out period the program exits without doing anything. If a card
swipe is detected the program starts sending the data to be written at a rate
according to the type of track to be written and to the speed of the detected
swipe. As mentioned above you will need some trial and error to tune some
parameters of the program, mainly the number of leading clocking bits, so that
the data to be written is perfectly located within the magnetic stripe, and at
the proper nominal density.
The program accepts the following options:
- -t <track> : Type of track to write, valid values are 1 to 3,
corresponding to the three standard tracks. Default value is track 2. This
option selects several parameters appropriated for each track.
- -c <clk_bits> : Number of leading clocking bits, default value depends
on the selected track. This is the number of initial 0s (or 1s if -i option is
given, see below) that are sent to be written before actual data. Tuning this
parameter helps to synchronize the data within the magnetic stripe.
- -f <file> : Name (including optional path) of the file with the data to
be written, default value is "writetrack.dat". The format should be a single
line with a string of bits (actually bytes 0 and 1) or bytes (0 to 9, A to Z,
and the rest of characters from the ANSI/ISO ALPHA charset). The program will
autodetect the format. You should include the start and the stop sentinels if
they are necessary. The LRC character can be included or not. You have the
option to add it automatically (see below).
- -l : Calculate and add the LRC character to the data string. To be used
only if you did not include it in the input data.
- -1 : Bits 1 are just one flux reversal. This parameter takes place when
writing the input data string, so if you provide the data string in bit format
you must always use just one 1 byte to represent a logic 1. The program would
write two short flux reversals by default to represent a 1 unless you use this
option, in which case only one short flux reversal will be used.
- -i : Inverse clocking bits, they will be 1s instead of 0s. I found this non
standard behavior in at least one type of cards, Madrid Underground tickets.
After the program finishes writing the track, it displays some information
which can be used to infer the quality of the swipe. Actually the program warns
if the measured speed of the swipe was out of the range that is considered good
according to my experiments.
As mentioned above I was able to write, erase and rewrite high coercivity
cards with the setup, tools and procedures given in this document. I was able
to write low density as well as high density tracks, however I could not read
successfully the high density tracks that I wrote. Nevertheless I was able to
read successfully the low density tracks that I wrote, thus proving that the
general procedure is right.
To investigate the problem with the high density tracks I read them and
watched the signal graphically. The profile looked quite acceptable and it
might be decoded by eye. This means that maybe a program to read cards more
sophisticated than mine might successfully decode these high density tracks. In
any case the profile suggested that some distortion was present, maybe as if
high frequency signals were damped, or if some high frequency components were
artificially added, maybe due to glitches introduced in the switching of the
serial port line.
To test this hypothesis I started to play with simple RC low-pass,
high-pass and band-pass filters, but I got distracted with other subjects
before I could come to any conclusion. So it is up to you. Maybe adding some
kind of filter fixes the problem, or maybe there is no hardware problem at all,
maybe it is just a matter of tuning the software and practising with hand made
swiping. I must admit that I did not dedicate too much time testing the writing
of the high density tracks and the subsequent read out test. Maybe I was not
patient enough! :-)
Good luck if you give it a try and please keep me informed of your
E-mail: padilla at
domain "gae ucm es" (my PGP/GPG public key)
First version: 11-Dec-2009, last update: 10-Apr-2010
This link: http://www.gae.ucm.es/~padilla/extrawork/magwrite.html
Go to the parent page: Magnetic stripe reader/writer.