Category: Software

I’m Batman…

Well, I have to do something with my holidays, and considering no one wanted me for summer vacation work experience  (Anyone out there?), I decided to play electronics geek. Don’t look so surprised.

I recently (for Christmas) came into possession of one of these.

A nice Arduino compatible LED Dot Matrix screen. Spent Boxing day wiring up and ATX to give me 5V and setting this baby going.

The concept of using and ATX PSU for a bench supply is in no way a new one. You can, in fact, get some really cool modifications to turn them into variable bench supplies. Considering the only reason I used an ATX is because I am at my parents house for Christmas and hence don’t have my work bench, I didn’t really need a fancy job, just a switch to turn it on and a plug to break out 5 and 12 volts from molex. I had better clarify at this point, the LED Matrix draws a nominal 5V from the Arduino to light up the LEDs, but for increased brightness, you can feed in 5V from another source. Guess what the max. Ampere it reckons it can draw from the extra 5V?

40 amps.


40 of em.

But it does look like a small sun when plugged in.

Back on track.

To turn on an ATX PSU with no Motherboard plugged in, you simply need to short the green wire with the black wire on the 20/24 pin plug. Many people use a paper clip, and that is a pretty good idea, but I seem to have a loathing of paper clips since Microsoft Word 97…


So I threw together a nice reusable plug and switch combo. Pretty no? I have become a convert to heat shrink, so much neater compared to electrical tape. Really all it does is short the green and black wires. A handy tool to keep none the less.To break the voltage out of the molex plug, I found in Dads draw, a molex voltage breaker-outer. So I didn’t even have to make one! It may or may not go back into the draw.To the LED Matrix!It simply plugs into pins 6-13 + GND + AREF of an Arduino, as you can see below. It uses the SPI interface to push data and commands to the screen. It all comes with libraries to easily get the Matrix working with minimal fuss. You simply need to get an extra library from the interwebs to ensure it works, the TimerOne library.

Run the sample code, viola!

Now, what else can I display on this baby?

How about a giant Facebook notification screen? How pointlessly awesome? Right, lets get onto that.


That is all I am going to say about Facebook’s API. So painful. After much research, I decided on using Java and RestFB to grab notification data from currently logged in user of the computer. After finally working out how to generate an Application key, Application secret key and an Auth token, I managed to finally grab notification data from Facebook, and parse the data form JSON form to a class form.

Java Source Code with Notification Class

To generate the app keys, you have to create an app in the Facebook developers site. You can get temp auth tokens from an auth token generator from the dev site as well. The Auth token generator is only really useful for debugging. You really need to set up a way to grab one properly for any deployed apps. Here, I’ll link it for you.

Data! How nice of it to finally work. Here I am only grabbing the Notification_ID from the JSON results, because all I want to do is display total count of unread notifications. If you want more data, modify the file to reflect what data you want to grab from the JSON. Next was pushing the data to the board. Here I hit a snag that I haven’t fixed yet. When the data was transmitted to the board, the Matrix would clear and would display nothing again. I checked the USART and SPI pins, they don’t clash. So must be code somewhere. Well, here was my Arduino sketch I threw together anyway. Keep in mind, it doesn’t work. The serial code for the Java, as you may have noticed, is commented out in the above Java source code.

Arduino Sketch

Well, I then decided to do something silly with the screen.

Batman Logo.

Most annoying bit was plotting the logo to a 16×32 image then translating that to a 2D array. I grabbed a logo image, scaled it down to 16×32 then wrote out by hand where each pixel was then manually entered it into an array. In hind sight, I could have gotten the 16×32 image, then written a nice C code to translate it into an array, but that would have probably taken just as long anyway. Might throw one together anyway for future use.

Well, the fruits of my labours!

And here’s the source codes. The Batman Logo array is in the batmanlogo.h file. Go nuts.

Main Sketch and Batman Logo.

Compressed, grouped files? Who do you think I am? Get them individually. Maybe when I port to a new file server I’ll do that.

Because I’m Batman.


F***en Magnets…


On Friday I bought an old oscilloscope from Uni on the cheap. That and a old Wishmaker, with a signal generator, variable voltage supplies, switches, buttons, multimeter and a breadboard. Getting all this home, I quickly realised that I didn’t actually have enough room on my desk for all this stuff. Sad Panda. So what do you do? Move into the spare room and use the desk sitting in there! (nearly) Instant workspace!

Need a computer for said workspace… For datasheets and coding and what not. I know! I’ll use the broken screen laptop curently on my desk and wire it up onto my workspace instead! But what about powering it on and applications? I’ll, shudder, need a windows boot, I use to use Wake on Lan to turn it on becuase it was just a hassle to open up the screen and press the power button, especially when the monitor was resting on it. Moving it to the spare room meant that I coudn’t run an ethernet cable to it, so Wireless had to be used. WoL doesn’t work on wireless. Well. Atleast not with this old Toshiba laptop. But. I got the bright idea to break out the power button to outside the laptop case. Simple enough really. Tack a new momentary button onto the power button board in the laptop. That was the easy part.

What was the hard part, was the power button wouldn’t work when the lid was closed. Some smart developer decided to disable to power button when the lid was closed. This kinda defeated the whole purpose of the new button, didn’t it? Yeah.
So several hours of hunting around the laptop trying to find the sensor got me REALLY frustrated. Other laptops I have pulled apart had simple switches in the hinge or a basic light sensor. This one, had none of these. No obvious sensors, no hidden switches, nothing.
After pulling, literally, everything apart and unplugging everything, I still couldn’t find the sensor. It had to be in the monitor somehow because nothing else placed over the laptop with the monitor removed would set the sensor off. But it couldnt be in the monitor because nothing was plugged into the monitor. Even after stripping the camera, LCD and mic out of the screen section, it still was sensing some how.
Genius here decides to look on the other side of the bezal that he just put back onto the screen. Low and behold. What was there?
A Frikken magnet. There was obviously a magnetic sensor on the board that detected when the magnet was near by and set laptop to sleep/disabled power button.

It’s kinda hard to see, but it’s the dark silvery bit clipped into the screen. Pop that out.

Bam, it works. That’s it. On the upside, I got rather intimate with a laptops guts, but I am never pulling this one apart again…

Dual boot! This was also far too painful. Because it already had Ubuntu installed on it, installing Windows will destroy the GRUB2 bootloader, rendering the Ubuntu partition useless. But that’s easliy fixed, right? Wrong.
Install Windows, that goes painlessly. Try to repair the GRUB2 bootloader using a live CD, same way I have done it many a time before.
Doesn’t like that.
Why? Because it reakoned that there was no GRUB installed to it in the first place, which is crazy talk. Crazy talk I tells ya!
After a few more hours of hunting and trial an error, I discover I need to chroot (kinda run the Ubuntu commandline within another linux install) into my Ubuntu install from the live CD and Re-install GRUB completly. Sigh.
Ultimatly it was fairly simple from there. Let me walk you through it.
First we needed to find which partition the linux build was on. Punch in:

sudo fdisk -l

Find the partition that says “Linux Partition” and that’s the one we’ll be using. From here on I’ll be using sda1, because that’s the one mine was.
Next, let’s make a folder to mount everything in.

sudo mkdir /mnt/root

Then lets mount our linux install files to allow us to chroot in:

sudo mount /dev/sda1 /mnt/root<br />
sudo mount -t proc none /mnt/root/proc<br />
sudo mount -o bind /dev /mnt/root/dev

Done. Now to chroot in!

sudo chroot /mnt/root /bin/bash

From here you should have a slightly different looking prompt on the terminal. Here, type:

sudo grub-install /dev/sda

Bam. That fixed it! After a little custom work with the grub loader from inside my Ubuntu build, like changing the time out and looking for my Window partition (sudo update-grub), we have a working GRUB2 loader!

Well, here’s the fruits of my labours. My new workspace!

Imperial March on atmega16

I sat down to do one of my micro-controller labs for uni today and noticed we had to play a single tone using PWM. “Easy” I thought. Keep reading. Until I get to the final part of the lab. it was basically “Load this HEX file and play it. It plays a musical piece” .
I was a little let down…
I mean, I was fully expecting it to ask us to write the program ourselves to play a given song, but no. Here it was just giving us the HEX file to load and play.
I wasn’t going to let this deter me. I had it in my head to have this thing play The Imperial March by John Williams from Starwars.
Mate and I got together, and off we went.

It’s a relatively simple program. 2 arrays holding the frequencies and durations of each note, PWM output, and a loop to play each note in sequence, and then start again.


Easy as!
Here’s the source code for anyone interested.

Source Code

This uses about 50% of the atmega16’s memory, becuase of the arrays. So keep that in mind while writing new songs!

Have a go yourself! Port some new songs into chiptunes!
Post your results!

AVR LCD Library

Well here is my library I threw together for interfacing the Jaycar QP-5512 LCD screen to AVR micro controllers. A couple of functions. InitiliseLCD, displayData and writeInstruction.


To use you need to include <util/delay.h> and define F_CPU. Have a gander at the usage of delay.h.
Considering we are passing in the ports and pins as addresses, it’s a little bit of pointer passing. But don’t get phased at all about that.

A slight example of calling the functions.
displayData(&PORTB, &PB7, &PB6, ‘c’);

Do a loop of that function call to print words! Easy!

The code is simple. Have a look for yourself.

For a wiring diagram, look back at this post.

Questions? Comments? Suggestions? Flick us a comment.

Analog RAM and CPU Meters: Update

So I finally found time to fiddle with the CPU and RAM meters again today. Between Uni assignments and The Witcher 2, I haven’t really had much time for hacking, but after finishing The Witcher 2, I had to do something to procrastinate.

So I sat down and wrote a little Python script to grab the current CPU load and RAM usage. To do this, a module needed to be imported into the script. WMI. Windows management instrumentation. Other given modules are also needed. For example, serial and time. So we can actually TALK to the Arduino. You know, I hear that’s handy for what I’m doing…


As you can see, I’m sending the data as a string of characters, with a begin and end byte.

So, I’ve got the data, and I’m pushing it to the Arduino, now all I need to do is get the Arduino to do something with it! Remember those bytes I put in between the data bytes? Yep. Let’s use those to tell the micro controller what data packet is going to which display.
Basically, on Serial.available > 0, we check the byte, if it’s the byte that precedes the CPU byte, we set a flag as 1, if it’s the byte that precedes the RAM byte, we set the flag as 0, if it’s the byte at the end of the stream, do nothing, we don’t need that data. Ultimately I could remove it, but I like having the option for letting the micro controller know that the transmission is finished, but I digress. With any other byte, we check the flag and output the value to the corresponding meter. Simple.

Arduino Source

I originally tried to be all fancy like and send it in 2 data packets with the Most significant nibble of each byte in the first packet, then the least significant byte as the second packet. Basically multiplexing the data. For no real reason but to try and implement something I had learnt in my communications theory class. Well, as you can see, that fell through. The Python script managed to encode and send the data, but for the life of me I couldn’t get the Arduino to decode the data. I’ll probably keep fiddling, but for now, it’s working.


The left meter is RAM and the right is CPU. You can also see where I’m working on the SD card interface.
The CPU output is quite jumpy because the CPU load is changing all the time, I’m working on some action smoothing methods that I can implement on the Arduino, just something else I need to work on.

The final product plan is a mounted set of monitors with LED backlighting and a custom back planes. As you can see, I’m not really measuring mA, so I was planning on scanning in the current back planes and photoshooping some new ones that suit my project, just to give it that finished look.

Yeah I know, I’m avoiding the SD interface. I’m a little daunted by it honestly. But to be fair, I’m avoiding a lot of things, aka, uni work. So on that note, I leave you. Enjoy this quick update!

Bluetooth Media Remote

It is finally finished. My Bluetooth Media Remote!

With each module* working as it should, I set out to build it onto my breadboard. Just to make sure that it works together.

*See previous posts for each module and how they work.

The final Micro-controller I decided to use on the final product was a ATMEGA8. In the first post I used an ATMEGA8515. For several reasons I decided not to use this one in the final product. Firstly, it’s a 40 pin package. I do not need that many I/O lines. It draws more power. And finally, I need that one for my studies at uni. So an ATMEGA8 was used.

Here, I noticed a slight problem…
The buttons double press sometimes. Thus sending the data twice. I think this is a limitation of the tactile switches I am using. I’m not quite sure. Later on I might write some code to only allow the data to transmit after a certain amount of time has passed using a timer interrupt on the ATMEGA8. This is for future work though.

From here, I built it on a set of prototype boards. Modular of course. In hind sight this wasn’t a very good idea, because I still hadn’t decided what I was going to enclose it in, but I was excited to build it. And build it I did.

Ahh. Its working on the prototype boards..

Now, the problem that faces every hacker, what to build it in? It’s gotta be classy, something on hand, and easy to put together.

Thanks to a brilliant idea from my mate, Damo, I thought “Why not in one of my broken Xbox controllers?”

After pulling it apart and trying to jiggle the circuits into it, I decided it just wasn’t feasible.

Back to square one.

Then, like a ray of sunshine, I remember I had a broken N64 controller as well. Thus the decision was made. It’s classy, it’s on hand. Easy of assembly went out the window when I realised how awesome it would be to use nostalgia to control my music.

After rebuilding the circuits onto new prototype boards (about the 5th time I had built the same circuit), I attacked the controller with a jigsaw blade and the file on my pocket knife. Not the best tools, but what I had on hand.
And so the N64 Media Remote was born…

And a video just to prove it works…

For those interested. The source code for the remote. The polling of the buttons is not quite neat, but it works.

I might implement an external interrupt to activate the polling, but at the moment, it’s not needed. Even the interrupt polling double transmits.

And thus the project is complete. Any questions, don’t hesitate to ask!

Later on I might get a full circuit diagram. If someone requests it I’ll make it a priority, but until then it’s on the back burner. Uni work has suffered enough…

Onto my next project! A set of analogue panel meters to monitor my CPU and RAM usage with data logged to an SD card. Stay tuned!

EDIT: Debounced the buttons. Thanks for the idea Matthew Wiebe and BohemianHacks! Here’s the updated source code

If you are a faithful reader, you might remember way back to my first post, my server. Now. That server was based on VB 6.0 and, honestly, a really really bad piece of code. So. I decided to completely re-write it in Python script.
Handy little thing python. Cross platform, easy flow control. The moment I got that server working, I knew that this was the way to do it.
To gain serial port control and the ability to import Media Monkey I had to install a couple of other libraries. Pyserial and pywin32.

Source code.

It basically waits for a connection on a specific port. Once it has gotten it, it will poll the serial port for new data and check if the current playing song has changed. When it receives data, it will act correspondingly.
As you can see I have set an escape case, the character “e”. This allows the server to close the connection and wait for a new connection. This allows us to shut down the remote without having the server still listening on the port. If the remote is turned off and then on again without sending the escape character, the remote will not reconnect to the server because the serial port is already open.

All in all, a handy little script that can be modified for any serial comms and Windows interaction.

Next, the final product! Excited? I am.

On to the next module. I had been thinking for a few weeks, what kind of wireless will I use for my remote. Some options include:

  • Xbee modules with a base station connected to computer.
  • One way transmitter connected to base station and a one way receiver connected to remote.
  • Direct Bluetooth connection.

Now, each has its pros and cons.

The Xbee modules are frikken expensive. Add onto that the price of building a base station with another Micro-controller and USART->serial converters. On the up side, I would have AMAZING range for my remote.

The one way transmitters and receivers are cheap. Really cheap. Again though, I would have to build a base station with a USART->serial converters. Also, I cannot guarantee the range or effectiveness of the cheap wireless modules.

The Bluetooth modules can be found cheaply, easy to use and have no need for a base station. The range though, can be rather small, depending on the module chosen.

Taking all this into account, I chose the Bluetooth solution. I found a nice cheap Bluetooth device on eBay, based on the CSR (Cambridge Silicon Radio) chip. It all came on a breakout board for me too! Nice and easy to incorporate into the micro-controller using a USART connection. This also means I don’t need to modify any of my code from Module #1! How good’s that?

It works!
Now pair it with the computer and create a virtual COM port! For those who get the same device, take note that the PIN for these devices is 1234.

If you noticed, it’s name is “WIDE_HK”. What a boring name. This is the name of the people who made the breakout board. Now, I want  to change this. So we need to do some AT commands. TO do this, we need to connect the Bluetooth to the computer via the serial port. Take note, this is NOT via the Bluetooth wireless protocol, but through a USART->Serial converter and into the data lines on the Bluetooth chip. We also need to drive a line on the board high. This line is Pin 11, some soldering to tack a new line on at that point of the chip is required. Drive that to 5V, power it up and connect to it using putty with a baud rate of 38400. Here, we type in the AT commands. For a full list of AT commands, check the data sheet, but to change the name use this one.


This should return “OK”. And you are set!

Turn off the Bluetooth module, disconnect the line on pin 11. There we go! All ready to use!

Thanks to Hack a Day, specifically this post, I discovered a good way to extend the range of my Bluetooth device! Tack an old router antenna onto the aerial onto it! It’s the same frequency, 2.4GHz, so it is the right length for a proper antenna. Using this I get range of maybe 10m through walls. Without it I was lucky to get 2-3m. Thus the disadvantages of buying cheap modules and a cheap Bluetooth dongle for my computer.

Next module is the button switch bank.

Wireless Remote Module #2: LCD Display

Ahh, it’s been too long since I’ve updated this. As you can guess from the title, module #2 for my wireless media remote is complete. Well, module #3 is ready too, but I’ll put that in another post later on. (Don’t tell anyone, but so is module #4. I’m actually up to building it on prototype boards as opposed to my bread board)

A few weeks ago, I went down to Jaycar and picked up a few things. One of them being a 16×2 LCD character display. Key code QP-5512.
As it turns out, this particular LCD character display from Jaycar, has no datasheet. Which is quite in convenient for anyone, especially someone just learning about micro-controllers. After many hours of research, I found this: Oz electronic forums where several board members figured out its wiring and instruction sets. For the PICAXE.
Now this is wonderful…Except its code is in PICAXE Assembly syntax.

First things first, let’s wire up the circuit. I opted for 460 ohm current limiting resistors because that’s just what I had with me.

D0 -> D3 are the data lines. Because i opted to use the LCD in 4 bit mode, we only need 4 of the data lines for data transmission. EN is the enable line and RS is register select. The 10k trim pot on line 3 is contrast adjustment.

Onto the code. Once again, I would like to point out that this isn’t exactly all my code. The structure and implementation of it in C is, but the enabling and writing to the LCD logic is thanks to SABorn at ozeleforum (See above link).


Compile it, upload it and run it. Works a charm eh?

Note it is displaying the current playing song from my PC. See the serial connection? I’m tying them slowly together… More on this in later posts though.

I have actually found that on the enable pulses, you can decrease the delay to 35 us. This may cause some slight unstable reactions, but I have never had a problem with it. Just don’t go any faster.

Now, I liked this so much, I decided to put my screen on a prototype board.

So that’s pretty much it for now. Chew on that while I get bothered to write up more on the next few modules.

Here is the instruction set for the LCD screen:
DisplayOff = 8
DisplayOn = 12
Home = 2
Clear = 1
Start of line 1 = 128
End of line 1 = 145
Start of line 2 = 192
End of line 2 = 209
Master reset = 0
Scroll screen left to right = 5
Scroll screen right ot left = 7
Screen off = 4
Display top line only = 32
Display both lines = 44
Cursor on = 14
Cursor off = 12
Cursor Blink = 15

Thanks to SABorn @ Oz Electronic Forum.

Nothing amazing here really. This kinda stuff you can do from numerous walk throughs on the interwebs.

The AtMega8515L was the chip used connected to a STK dev board. If you want to use just the chip on your own bread board, you will need a MAX232 chip to convert the TTL logic levels to the +30V required for serial data transmission. Thankfully the STK does this for me while I wait for my MAX232 chips to arrive.

PORTD pins 0 and 1 are the USART data transmission lines that need to be connected to the MAX and then through to the Serial connector.
Next I used PORTA for input into my micro controller. Utilising the onboard switches from the STK. It should be noted that the switches are active-low. Refer to the STK500 user manual for the wiring diagram to set it up switches on a separate breadboard.

Once all this is wired up, it’s time for some maths! 😀

Here, we need to calculate the UBRRL register that corresponds to our “Baud” rate of our serial communications. This is basically the bits per second that the data will be transmitted.


Lets  pick an arbitrary baud rate. Say, 9600 b/s. Good number, commonly used in serial communications. Makes things easier to do when it’s the standard.
Right, using a formula in the AtMega8515L user manual we get:

UBRRL = (f_osc)/(16*baud) – 1

See? It’s simple really. f_osc is the frequency that our chip is running. So in my case, 3.6864 MHz.
We throw all this in together and what do we get? 23!

Oh, if your lazy, there is a table of common baud rates, clock freqs and UBRRL values on page 160… But knowing that formula makes you seem that much smarter…

Right, We’ve done the maths, done the hardware, let’s do the code!
First, let’s get the AtMega8515L sending data.

Fire up AVR Studio and make a AVRGCC project. In this case I decided to steer clear from assembly just because I can do conditional things easier in C.

ok, source code!


The USART initialisation and transmit functions are supplied from the AtMega8515L user manual, I take no credit for either of these two functions.
As you can see I’m setting the USART active with the baud setting of “23” (remember calculating that?) and initialising it. Then in an infinite loop (Sorry, no interrupts yet) I grab input from PORTA and compare it to masks set up. Then transmits an ASCII code to the Serial interface. The “trans” if/else statement is to ensure the data is only sent once, at the initial key stroke. Just makes receiving the data a bit easier.

Ok. Flash the program into your board! Hook it up to your PC using a serial cable, or in my case a serial to USB adapter, and turn it on!

Remember, its not going to do anything yet! Nothing on the computer is waiting for input.
At this point, download and install “hyperterminal“. Just use the free 30 day trial for now, if you use it more and more, why not grab it for real, yo? Or if you want the free open source option (My choice) use “Putty“.

Ok, install, open, create a connection to your comport. Mine was comm3, but yours may be different. The set up is 9600 baud, no parity bit, 8 bytes of data and stop bit is 2. Hit connect.

Now, this is the fun bit, press a button and see if the corresponding ASCII code turns up on the screen. If so, Well done! If not, Get your troubleshooting hat on and go for it. 🙂 I’m not gonna do everything for ya.

Right, The micro controller is talking to the PC, Now, lets make the PC do something with the commands!

For this, I chose to use Visual Basic 6.0. Out dated, yes, but a simple language that easily allows comport data retrieval. Also, I’m using it to control my music. Now I use a slightly obscure media player called MediaMonkey. I highly recommend it, but that’s a different conversation entirely. MediaMonkey has an importable object that allows VB programs to control it and get data from it, handy actually.

Ok, Create a new .EXE. Import the MSCOMM object, add to the form a MSCOMM object and a timer. Set timer interval to, say, 100. Enter in the code given below.


As you can see, I’m using a timer to ask the MSCOMM object if something is new. I tried it on the data receive event, but for some reason all I got was garbage. I’m gonna look into that a lot more in the coming days…

Right, Simple eh?

Works? Music changes? (For Media Monkey Users it should). Add your own commands! Play around.

Next on the cards is a wireless media remote that displays the current playing song via a 2×16 LCD display. This one’s gonna take me a while…
I’ll keep you posted!

Have Fun.

UPDATE: Fixed up some code, added more comments to code.