Thursday, October 9, 2008

the 1-Key-Keyboard project

Update: Please have a look at my 4-key-keyboard project as well!

Click here to download the complete sourcecode of this project (including .hex and Flash demo program)


The idea for a 1-key keyboard comes from the need as an interaction designer to make “cheap, quick and dirty prototypes.” When creating a quick screen-based demo in e.g. Adobe Flash, a common way of quickly prototyping physical actions is to assign actions to certain key presses on a keyboard. In more advanced prototypes, physical hacks to the circuit board from a keyboard are also often used to interface custom buttons and switches with a computer. (see

What this project does is to create a keyboard hack that is smaller and cheaper and better reproducible than what one would get when hacking a keyboard circuit; a perfect solution for when you only want to use one switch in your prototype. (Note: 3 IO ports from the microprocessor are still unused, so it’s very easy to add 3 more switches. If you want to interface more than 4 switches, I suggest having a look here:

To maximize the functionality of the 1-key keyboard, I implemented having a different key being sent to the computer for a key-down and a key-up event. This can be useful when you create for example a prototype that has to detect whether a physical object is pushing down a switch or not.

This project implements V-USB (previously called AVR-USB) from Objective Development to emulate a standard USB keyboard using an Atmel AVR microcontroller. This project has been heavily inspired by the EasyLogger Reference project.

What does it do?

This 1-Key-Keyboard can be plugged into any computer and will be recognised as a standard USB keyboard.
When closing the switch, the keystroke ‘]’ will be sent.
When opening the switch, the keystoke ‘[’ will be sent.

That’s all.

What do we need:

1x Atmel ATTiny45-20PU or ATTiny85-20PU microcontroller (I used a ATTiny85, which has more memory than the ATTiny45 but the program will fit the ATTiny45 too)
2x Diodes (0.5W) (ordinary 1N4148 are sufficient)
2x 68 ohm resistors
1x 1.5K resistor
1x 0.1 uF capacitor
1x switch
a piece of prototype-board (striped 2.54mm single-sided)

a programmer for the Atmel AVR
the .hex file from the 1-Key-Keyboard project (Download it here)


First program the microcontroller with the .hex file (don’t forget to set the fuses first to 0xFF 0xDF 0xC1)

Link: Instructions for how to set up the Atmel AVR STK500 programmer to program an 8-pin ATTiny

Link: Instructions for setting up AVR studio for using the Atmel STK500 programmer

Link: Instructions for programming a .hex file with AVR Studio

With the programmed ATTiny, build the following circuit:


To keep the costs low, you can make a usb-plug from strip board (veroboard) see the pictures below


Now it’s time to test the 1-Key-Keyboard! you can test it by opening a text editor and press and release the switch a couple of times. If everything went well, you should see a series like this being typed: ][][][][][

Creating a demo-program in Adobe Flash

Here’s the actionscript 3.0 code for a demo-program which detects whether the switch is pressed or not. If the switch is pressed, the movie-clip ‘onSwitch’ is visible. When released, the movie-clip ‘offSwitch’ is visible.

//actionscript 3.0 source file 
stage.addEventListener(KeyboardEvent.KEY_DOWN, reportKeyDown);
offSwitch.visible = true;
onSwitch.visible = false;
function reportKeyDown(event:KeyboardEvent):void {
	switch (String.fromCharCode(event.charCode)) {

		case ']' :
			offSwitch.visible = false;
			onSwitch.visible = true;

		case '[' :
			offSwitch.visible = true;
			onSwitch.visible = false;


		default :


// end of source file

Try it out by clicking here and press/release the switch on your 1-Key-Keyboard.

or click here to download a .zip file with the .fla and .swf files

Click here to download the complete sourcecode of this project (including .hex and Flash demo program)

Please feel free to leave comments on my 1-key-keyboard project!

Wednesday, October 8, 2008

Programming a .hex file with AVR Studio

If you just want to write an existing program to your Atmel AVR chip, without changing any actual code, you need a precompiled .hex file (a hex file is a compiled version of a program, to work on a specific microcontroller)

If you have a ATTiny45 or ATTiny85 you can Download here a precompiled .hex file for my 1-key-keyboard project. For other microcontrollers you’ll need a .hex file which is compiled for it.

Connect with AVR Studio to your device by pressing the connect button connect.jpg and selecting your programmer from the list and connect.

Before programming the actual firmware, the fuses need to be set on the AVR.
Read more about fuses here

If you’re using my 1-key-keyboard .hex file, the easiest way to get the correct setting is to change the bottom 3 values to:

LOW: 0xC1

Then press “Program” and the fuses are set! (this has to be done only once for every chip, unless you want to change these fuse settings off course)

See the picture below for the correct settings


Now you’re ready to program the actual .hex file on the AVR.

Go to the Program tab and under “Flash” select the path of the capslocker.hex file and press “Program”


If everything goes correct, the ATTiny is programmed and you get the following report (only last 5 lines visible without scrolling)

Getting isp parameter.. SD=0×0a .. OKOK
Reading FLASH input file.. OK
Entering programming mode.. OK!
Erasing device.. OK!
Programming FLASH .. OK!
Reading FLASH .. OK!
FLASH contents is equal to file.. OK
Leaving programming mode.. OK!

Now you have successfully uploaded the code to the AVR!

Setting up AVR studio for ATTiny85 (or ATTiny45 / ATTiny25) using the Atmel STK500 programmer

Software used: AVR studio

(Click here to get AVR Studio)

After starting up AVR studio, press the connect button. connect.jpg

Select the “STK500” option and “Auto” port selection. Then press connect


Then In the “Main” tab, select the ATTiny85 as Device.
Set Programming mode to “ISP mode” and press “Read signature”

If everything is set up OK, then you should see the following output:

Entering programming mode.. OK!
Reading signature .. 0×1E, 0×93, 0×0B .. OK!
Leaving programming mode.. OK!


Now you’re ready to start programming the ATTiny using the STK500

How to set up the Atmel AVR STK500 programmer for use with ATTiny85-20PU (or ATTiny45 / ATTiny25)


There are 2 ways of programming an ATTiny85 microcontroller using the Atmel AVR STK500 programmer. The first one is In-System Programming (ISP); The most common way of programming AVR’s.
The other way of programming the ATTiny85 is High Voltage Serial Programming (HVSP). This is the only way of programming that can be used after the ‘reset disable’ fuse is set on the ATTiny85.

In-System Programming:

Before being able to program the ATTiny85-20PU with the STK500 you need to add 2 jumper wires on the STK500:

• One wire to connect the PB3 pin (pin 4) on the PORTB header to the XT1 pin (pin 7) on the PORTE / AUX header. This is to connect the clock system to the AVR device.

• The second wire to connect the PB5 pin (pin 6) on the PORTB header to the RST pin (pin 4) on the PORTE / AUX header. This is to connect the reset system to the AVR device.

• Place the 6-pin programmer cable so that it connects ISP6PIN to SPROG1

Jumper settings:

VTAGET: Enabled
AREF: Enabled
RESET: Enabled
XTAL1: Enabled
OSCSEL: pin 1-2 Enabled
BSEL2: Enabled
All other jumpers: Disabled

Place the ATTiny85-20PU in the SCKT3400D1 socket

Connect the (12V) power supply and a (usb-to-)serial cable to connect the programmer to your computer to the RS232 CTRL port and power on the STK500

See the complete setup for ISP in the picture below


High Voltage Serial Programming:

source: STK500 user guide

1. Switch power off.
2. Place the ATTiny85 in the SCKT3400D1 socket and make sure all other sockets are empty!
3. Mount jumper OSCSEL on pins 1 and 2 to select software-controlled clock.
4. Mount jumper XTAL1 to route the oscillator signal to the device.
5. Mount jumpers VTARGET and RESET.
6. Use one 2-wire cable to connect the PB3 pin (pin 4) on the PORTB header to the XT1 pin (pin 7) on the PORTE/AUX header. This will connect the clock system to the AVR device.
7. Use another 2-wire cable to connect the PB5 pin (pin 6) on the PORTB header to the RST pin (pin 4) on the PORTE/AUX header. This will connect the reset system to the AVR device.
8. Use a third 2-wire cable to connect the PB0 and PB2 pins (pins 4 and 3) on the SPROG1 header to the DATA0 and DATA2 pins (pins 1 and 3) on the PROG DATA header.
9. Use the last 2-wire cable to connect the PB1 pin (pin 1) on the SPROG1 header to the DATA1 pin (pin 2) on the PROG DATA header.
10. Switch power on and you are ready to program.

See the complete setup for HVSP in the picture below