Saturday, April 20, 2013

First Project: Arduino Spede

Here's my take on a reaction time tester entitled Arduino Spede. The device is similar to the one seen in Speden Spelit TV show that ran in Finnish television in the 90s. The idea is to push buttons that lit up in a random sequence. The sequence gets faster and faster until eventually you are too slow or push the wrong button and it's game over. Each LED also plays a different note (C#, D#, F# and G#), so if you are really hardcore you can play with your eyes closed! My musically talented girlfriend Saara gets a score of about 30 with her eyes closed, where I can do only about 4 or 5 if I'm very lucky :)

If you're interested in knowing how it works or even building one yourself, read on!

Schematic


Parts list

  • 1 Arduino UNO R3 (or equivalent)
  • 1 CC56-21SRWA common cathode four digit 7-segment display (or similar)
  • 1 74HC595 shift register chip
  • 4 push buttons
  • 4 LEDs
  • 4 BC548B transistors (almost any NPN transistor with similar specs should work)
  • 1 1uF capacitor
  • 4 1 kohm resistors
  • 4 390 ohm resistors
  • 1 470 ohm resistor
  • 7 560 ohm resistors
  • 1 small speaker
  • 14-pin ribbon cable
  • 2 14-pin ribbon cable connectors (female)
  • 2 14-pin PCB connectors (male)
  • Pin headers and Harwin connectors for LEDs, buttons and speaker
  • Wires, etc.

How does it work?

Buttons: The buttons are connected to digital pins 14-17, which are set up to use the internal pull up resistors inside Arduino. When no buttons are pressed the internal resistors pull up the pins to high state. When a button is pressed the corresponding pin does low. Without the pull up resistors the state of the input pins would be undetermined.

LEDs: The four LEDs are connected to digital pins 3-6 in series with 390 ohm current limiting resistors. Without the resistors the LEDs or digital pins of the Arduino could burn out.

Speaker: The speaker is connected to digital pin 2 via a resistor. In hindsight I should have also used a capacitor in series to filter out the DC and pass only the audible AC frequencies. I'm using the Arduino tone library to generate the tones. I've chosen frequencies 277, 311, 370 and 415 Hz which produce exotic Asian sounding notes C#, D#, F# and G#.

The score display: This is by far the most complicated part of the schematic. I've chosen a 4 digit seven segment display so you shouldn't run out of digits no matter how good you are! The display is a so called common cathode display, meaning that the cathodes (negative terminals of the LEDs) of a digit are tied together. The display has a pin for each segment A-G but the pins are shared for all digits. So, for example, if the anode (positive terminal of a LED) for segments B and C are set high and the common cathode of digit 1 is pulled to ground, number 1 would be displayed in the first digit.

Because a single anode pin is connected to all digits, it's impossible to display different numbers in different digit positions at the same time. Instead, we can turn on a digit one by one very rapidly in succession. So, for example, to display the number 1234, first we would set the anodes to display number 1 and enable only the first digit by pulling the cathode of digit 1 to ground. Then we would turn off the digit, set the anodes to display number 2 and turn on digit 2. And so on for 3 and 4. If this is done fast enough, the eye cannot see the LEDs blinking on and off and it appears as if the digits are all lit.

The job of NPN transistors T1-T4 is precisely to pull the digits to ground. Normally the transistors are off, meaning there is no current flowing through them. Digital pins 10-13 are connected to the base of the transistors, so setting a pin high turns on the corresponding transistor and enables a digit.

In order to cut down the number of Arduino pins I've used a 74HC595 shift register between the Arduino and the display. The shift register is a serial in, parallel out device and also acts as a 8 bit buffer. It has eight outputs which "latch" to a state, meaning that the outputs remember their state until the state changes again. The Arduino sends the state of eight outputs one bit at a time (serial transmission). For seven segments we need only seven bits so the eighth bit is unused. There is a great tutorial about using the 74HC595 chip with Arduino here.

This may sound a little tricky. Check out the datasheets of parts CC56-21SRWA and 74HC595 for more details. You should be able to substitute the display with another part but it has to have common cathodes. The pin ordering is also likely different from the one I used.

Putting it together

The final version consists of three circuit boards which I made myself (I could write about the process in a separate blog post). One of the boards contains only the display and it is connected using a 14 pin ribbon cable to the main board. The main board contains the 74HC595 chip, transistors, resistors and the capacitor.

After building these and making sure everything worked, I realized that I didn't want to allocate my Arduino permanently to the project. So I made a third circuit board, a standalone Arduino board following the instructions here.

You can use normal pushbuttons and LEDs, or you can order some fancy arcade buttons with built-in LEDs from Ebay like I did. I soldered wires to the terminals of buttons and LEDs and connected them using Harwin connectors and headers to the main board.

Source code

Download source code

The code itself should be self explanatory so just a few comments. The game can be in one of three states each of which is implemented in a separate function: startMenu(), playGame() and gameOver().

Start menu flicks between previous score and high score. High score is stored in EPPROM which retains its value even when the device is turned off. When a button is pressed, a new game begins. Holding all four buttons while in start menu causes the high score to be cleared.

Playing the game is handled by playGame() function. Here one of the four LEDs is turned on randomly and a timer starts counting down. If the player presses a wrong button or the timer reaches zero, the game is over. If the player pressed the correct button, the procedure starts again with a new randomly selected LED. This time the timer starts from a little bit smaller value. This repeats until the game is over.

In game over state, the score is blinked a few times and a tone is played. The game enters the menu state automatically after a short delay.

Display and main circuit boards, hand drawn, before dipping into acid bath
The wiring for LEDs. Buttons not connected yet.

Harwin connectors for connecting LEDs and buttons to circuit board
Standalone Arduino board (left) and main board (right)


Testing the outputs

Wednesday, February 6, 2013

Fixing a C64

A couple of weeks ago someone broke into our apartment building's storage cellar. When I was cleaning up the mess the damned thieves had left behind, I found a bag with some electronics junk stuffed into it that I had completely forgotten. Amid the junk was a rare jewel, the motherboard of a Commodore 64 from 1987! I have no idea what had happened to the case and accessories, maybe the thieves got them, those bastards!

The Commodore 64 was a pretty nifty piece of hardware and very popular in Finland in the 80s. It had 64 KB of memory and most importantly thousands of games. The graphics hardware supported up to 8 sprites (with multiplexing many more could be produced) and the SID sound chip is still used by musicians today to get that oldschool sound.

Anyway, the motherboard was in a pretty bad shape and the fusebox of the transformer was missing. That wouldn't do! So I visited Bebek, a famous electronics shop in Helsinki to get some parts. The owner of the shop, Mr. Bebek is a very nice chap who has had the shop for 40 years. Although he is retired he can be spotted in his shop every now and then. I bought a new fusebox and a 160mA fuse, and as an added bonus a male DIN-8 connector, some speaker cable, and a couple of RCA plugs (more about these in a bit). 

Fixing the transformer was a matter of minutes with a soldering iron (the wires were rather thick so I needed to heat them pretty long with my 15W iron). The next problem was to hook up the C64 to a display to see if it still worked. Now, the way C64 were hooked up to a display in the old days was to use a RF adapter which transformed the output signal to a RF signal for a TV. I didn't have a RF adapter so I had earlier googled up "commodore 64 tv out." Luckily the C64 was advanced in its time and it could also produce component video signal through the video port. Among the google hits was a page describing the pin outs of the C64 video port. 

Using the instructions it was pretty easy to make a cable with a DIN-8 connector in one end to plug into the video port and two RCA males in the other end with component video and mono audio. I quickly assembled and attached the cable between the C64 and a monitor and nervously turned on power. Two seconds later to my amazement an old childhood friend of mine, the classic light blue, deep blue welcome screen of the C64 basic appeared on my Dell. 

I have no games or a floppy/tape storage drive for the C64 and the motherboard is in dire need of a case to protect against the elements. I'll probably come back to this project later. My part shipment including the Arduino arrived soon after this, so I had already other projects in my mind... but more about those later!

Ideas for future projects:
  • Wooden case for C64
  • 1541 emulator using Arduino
(oops this post got a lot longer than "the brief intro" that I promised to myself, better fix this for the next post before it becomes a habit!)


Welcome!

This blog is about my endeavors into the world of electronics and microcontrollers. I have always wanted to learn electronics but it wasn't until recently when I got an Arduino microcontroller board that things started to really click. I figured soon that I need to write things down or otherwise I start forgetting what I have learned. Hence this blog.

The blog serves two purposes. It is intended as a notebook for myself so I expect most posts to be quite brief. But if you're an Arduino beginner like me these ramblings might as well be interesting to you too!