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)



Introduction:

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 http://wiki.arcadecontrols.com/wiki/Keyboard_Hacks)

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: http://www.obdev.at/products/vusb/hidkeys.html)

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)


Instructions:

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:



1_key_keyboard.jpg


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




1-key-keyboard.jpg
dsc00328.jpg



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;
			break;

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

			break;

		default :

			break;

	}
}
// 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)



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

  1. Flip

    Friday, October 10, 2008 - 11:24:10

    Here are some comments I got on my design from Christian (Objective Developments) which I’d like to share, since they might help to improve the design. I’ll test his suggestions later.

    Christian’s suggestions:

    You don’t need zener diodes. Since the diodes are in forward direction in the supply, ordinary 1N4148 are sufficient. The zeners are required when limiting the voltage on D+ and D-, not the entire supply.

    For a design with short wires between the AVR and the host (as in your case), it should even work without the 68 Ohm resistors. And you can save the 10k pull-up if you enable the AVR’s internal pull-up (which is ~ 30k, I think).

  2. Sharon

    Wednesday, November 12, 2008 - 00:09:53

    Bidirectionality!

    I want to enable a computer-to-board communication. A computer can tell a keyboard to turn on and off the LEDS for caps/scroll/num lock so why can’t this project be bidirectional?

  3. Tester

    Wednesday, November 26, 2008 - 22:41:16

    Using 3 buttons has big adventage:

    first button - to move from “A” to “Z”
    second button - to move from “Z” to “A”
    third button - to choose necessary letter

    it would be possible to emulate whole keybord (with special program - “virtual keyboard”)

  4. Erwin

    Friday, December 5, 2008 - 13:53:22

    Christian is right. The zener diodes just don’t make any sense how they are used here.

  5. Flip

    Friday, December 5, 2008 - 14:56:21

    I updated the project, now without Zener diodes. Thanks for the comments

  6. Flip

    Thursday, April 9, 2009 - 12:28:29

    Another update: the pull-up resistor is no longer necessary, the internal pull-up resistor in the ATTiny has taken over this task now! I edited the post and circuit above.

  7. dvrcapture

    Thursday, April 9, 2009 - 17:08:04

    hi
    i am wondering to know something about ATTiny85 and Atmega 8, so what’s different between them.
    can i use Atmega 8 instead of ATTiny85 in project.?

  8. Flip

    Thursday, April 9, 2009 - 18:31:37

    Hi,

    In principle you could use the Atmega8 for this project, but its internal oscillator can only run at 1, 2, 4 or 8 MHz and AVR-USB supports currently only the internal RC oscillator for CPU clocks of 12.8 MHz or 16.5 MHz. Which basically means that you need to use an external oscillator (or crystal with capacitors to ground) to enable the Atmega8 to communicate with the USB port at the right speed. Obviously the code also needs to be rewritten to use an external oscillator instead of the internal one.

  9. Max

    Saturday, May 2, 2009 - 11:43:14

    Hi,
    I am using Attiny45 with Capslocker, and easylogger. It is working fine if I connect it while the PC is on. after I restart the PC it wouldn’t work and the whole USB port jam, the same if I connect it to the PC while the PC is off then when I turn the PC on it is not working I have to change the USB port in order to get it. any idea of how to solve this problem?
    regards…

  10. Flip

    Saturday, May 2, 2009 - 14:25:52

    Hi Max,

    My first guess is that these projects don’t handle a failed initialization of the USB port properly. It could well be the case that the Attiny is quicker to start-up than the host controller and therefore the initialization is not successful but the firmware on the Attiny still goes into the ‘forever’ loop after the failed initialization.

    if this is the case, a quick & dirty solution could be to add a delay to the start-up of the Attiny. (But beware for issues with the watchdog timer running out)

    I have tested my 1-key-keyboard project on multiple computers and it does work after a restart. Did you test my project as well? Or only the 2 projects you describe in your comment? If my project does work after a reboot, then I’m sure I can help you to debug the code of other 2 projects.

    -Flip

  11. Max

    Saturday, May 2, 2009 - 17:09:31

    Hi Flip,
    I don’t have Attiny85, and I tride your .Hex file on Attiny45 it didn’t work, should it be working properly on Attiny45? or is there some modifications in the code to fit the Attiny45, I would love to try your project.

  12. Flip

    Saturday, May 2, 2009 - 18:58:47

    you can download the complete sourcecode and compile it for your Attiny45, this should definitely work. (link @ top of the post.) I’m surprised that my .hex didn’t work, since the Attiny85 has only more memory but is for the rest exactly the same as your Attiny45. I’ve no Attiny45 around, otherwise I’d have tested it now ;-) let me know if you get it to work!

  13. Max

    Sunday, May 3, 2009 - 12:37:21

    I did exactly what you have suggested but no luck, I will order Attiny85 tomorrow and see if it would work for me, I will get it by tusday and let you know.

  14. Flip

    Sunday, May 3, 2009 - 13:54:21

    I just double-checked and successfully compiled the sourcecode against the Attiny45, so there should really be no need to buy a Attiny85 for this project.

    Can you check is whether you set the fuses correctly. I’ve also a blog post about setting up the fuses and programming the attiny using avr studio.

    If this is all set up ok and it still fails, can you specify more precise where it fails when you try to program it and what errors you get?

  15. Max

    Sunday, May 3, 2009 - 14:26:28

    Hi Flip,
    You are so great, with your support I done it. I got it working, and guess what it works even efter restarting the PC. it didn’t with easylogger and capslocker.
    I am so happy, thank you very much Flip.

  16. Flip

    Sunday, May 3, 2009 - 14:58:12

    Great to hear you got it working and that it’s even working after a restart! Just let me know if you need help with debugging the code of the other 2 projects to get them to work after a restart.

  17. Max

    Sunday, May 3, 2009 - 17:41:03

    Ok, I am enjoying your project at the moment, I will be back to you later. Regards..

  18. Max

    Sunday, May 3, 2009 - 18:26:07

    Do you know the Key code for emulating the Left Shift and Right Shift, I used E1 and E5 but the didn’t fire any event on windows

  19. Flip

    Sunday, May 3, 2009 - 22:57:01

    E1 and E5 are the correct scancodes for the Shift keys. Are you sure it doesn’t work? The way the key-press emulation works in my code is that it sends a key-down event, directly followed by a key-release event. Pressing and releasing a Shift key doesn’t trigger any action in most applications. ;-)

  20. Max

    Monday, May 11, 2009 - 08:55:59

    Hi Flip,

    I am trying to use HID class to read from attiny, have you worked with that before?

  21. Steffen

    Tuesday, June 9, 2009 - 12:23:27

    Hi flip :)
    i’ve got some trouble to set the fuses on an ATTiny45 :(
    The combination (0xFF 0xDF 0xC1) you wrote in your post didn’t work for me :(
    Which value corresponds with wich fuse?
    0xFF => extended?
    0xDF => high?
    0xC1 => low?

    thank you :)

  22. Flip

    Tuesday, June 9, 2009 - 13:22:24

    Hi Steffen,

    Yes, you have the right fuse settings. see my other post called “Programming a .hex file with AVR Studio” for a screenshot of the settings. See:

    http://blog.flipwork.nl/?entry=entry081008-175214

    Could you be a bit more precise in what goes wrong? (maybe you can tell me which error message you get)

    Cheers
    Flip

  23. Michael Gross

    Thursday, June 18, 2009 - 20:23:46

    Hi, I have problems to run this device under windows Vista. Which driver I need?

    Regards Michael

  24. Flip

    Thursday, June 18, 2009 - 23:46:24

    Hi Michael,

    I’m not sure what your problem is. I’ve been using AVR studio on both windows XP and Vista successfully. The only thing I can imagine you having driver problems with is when you are using a usb to serial converter to connect a STK500 programmer. You’d probably need the driver CD which should be with such such a converter.

    -Flip

  25. odeshel

    Sunday, July 5, 2009 - 21:52:12

    Hi Flip do U know maybe what is the code for the Windows key ?
    Odeshel

  26. Sam

    Wednesday, July 8, 2009 - 09:04:03

    Could You please Tell me How Can i install it’s usb Driver in windows xp?(step by step)

  27. Flip

    Thursday, July 9, 2009 - 08:03:27

    Reply to odeshel’s question:
    The scancode you’re probably looking for is E3 this is the ‘Keyboard Left GUI’ key.

    Reply to Sam’s question:
    The great advantage of my 1-key-keyboard project is that you don’t need any special drivers. it simulates a standard keyboard, for which Windows has built-in drivers.

  28. Sam

    Saturday, July 11, 2009 - 09:05:21

    Hi flip.
    I used attiny45 for your project. I programmed micro with your hex file.
    When I Plug the circuit in, windows shows a message that found a new hardware and and runs “found new hardware wizard” and ask me to browse a driver file with “inf” extension. It shows the 1-key-board as an unknown device. Could you tell me what’s the problem? What can I do?

  29. Flip

    Saturday, July 11, 2009 - 10:11:00

    Hi Sam,

    I assume you checked your circuit a couple of times already. The first thing that I’d check is whether you set the fuses on the ATtiny correctly. I have another post on my blog about this, see http://blog.flipwork.nl/?x=entry:entry081008-175214

    and of course there’s an option that your windows installation has some kind of error.. did you check plugging the circuit in a different computer?

  30. Me

    Thursday, August 27, 2009 - 14:27:23

    I don’t find a Makefile do build your project. Can anyone give me an example avr-gcc-call to compile with GNU/Linux?

  31. William

    Monday, September 21, 2009 - 20:45:05

    Have you considered modifying this for use with a rotary encoder? CW, CCW, and BUTTON should only need three of your four available lines.

  32. Schoolproject

    Wednesday, February 10, 2010 - 10:01:03

    Dear Flip,

    I have build your project on an Attiny45, but I have no succes =(
    The fuse settings are right and I already compiled the code for the Attiny45.
    When I plug the USB cable in my laptop said that it doesn’t recognize the USB device.

    Have you any idea of what the problem could be?

    Thanks in advice!

  33. Schoolproject

    Wednesday, February 10, 2010 - 10:20:06

    *update

    We are newbies ^^’

    The problem was that we hadn’t programmed the correct hex file.
    My project mate, took the old hex file and programmed it over and over again…

    So it works… we are really happy that it works:)

  34. Flip

    Wednesday, February 17, 2010 - 10:46:09

    I am currently working on developing a new version of the firmware. This one has support for 4 keys is stead of only 1 key.. without adding any components to the circuit apart from 3 extra switches. (by making use of the 3 free IO ports in the old design)

    So expect me to put the extended project online in the near future!

    people who are in a rush to get 4-key support can always contact me for a beta-version ;-)

  35. Iggy

    Monday, March 1, 2010 - 12:51:37

    Dear Flip,

    I am very curious about your extended version of the one key keyboard.
    Could you please sent me a working beta version?

    Cheers,

    Iggy

  36. Flip

    Monday, March 1, 2010 - 13:30:01

    Hi Iggy,

    I emailed you the beta source code of the 4-key supporting firmware. Please report back on your experiences!

  37. rogger

    Wednesday, March 17, 2010 - 17:53:25

    HI Flip,

    Thank you very much for your great work!

    Based on your soft & hardware i built my own “1-Key-Keyboard”
    I used an Attiny2313 with a 12MHz crystal. For the software I took your source and changed it slightly because I had trouble with the timer. :D

    Now when I press the Button on my small perfboard this is written:
    “hi hi hi hi hi hi hi hi hi hi hi hi hi hi hi hi hi hi hi hi hi hi hi hi hi hi hi hi hi hi hi hi hi hi hi hi hi hi hi hi hi hi hi hi hi hi hi hi hi hi hi hi hi hi hi hi hi hi hi hi hi hi hi hi hi hi hi hi hi hi hi hi hi hi hi hi hi hi hi hi hi hi hi hi hi hi”
    as long as I press the button

    I was havily suprised as i, after soldering the board, put my attiny in, plugged usb in and it was recognized as HID on the first try! (Normaly I have to fix many things before my projects work :D)

    So again many thanks to you!
    rogger

  38. Dan

    Thursday, June 24, 2010 - 04:24:40

    Hi Flip,
    I am using an ATtiny85, and I programmed the fuses accordingly (e 0xFF, h 0xDF, l 0xC1). I downloaded ur .hex file and uploaded it as is to the board.

    When I connect my circuit to my computer, it start installing the driver for a USB (and it should recognize it as an HID) however, it fails to install the device properly and my project is not working. THe exact status is “Unknown device”. I run Windows 7.

    Doo you have any ideas on what i shld check?
    Thanks in advance!

  39. Zeynab

    Wednesday, August 18, 2010 - 11:43:28

    Hi Mr Flip.
    actually I’ve done your project with a mixture of another project and it’s working fine. Thenks for your grate project.
    I have a question if you ddon’t mind or if any body tha can help.
    I need to change the name of the device. I mean when we connect the usb , in the device manager of the computer there is a name.
    Is there any way that we could change this name?
    Again thank you very much for your project and for your attention.
    Regards.
    “Zeynab”

  40. Flip

    Monday, August 23, 2010 - 15:18:43

    Hi Zeynab

    you can change the name in the “usbconfig.h” file.

    the values you have to change are: USB_CFG_DEVICE_NAME and USB_CFG_DEVICE_NAME_LEN (the latter is the amount of characters you use for the name)

  41. unzapped

    Friday, August 27, 2010 - 21:01:39

    I am a noob, and I just ordered all the parts listed here. I want to emulate the pressing of the spacebar. I am not sure what the term “set the fuses” is.

  42. unzapped

    Wednesday, September 1, 2010 - 08:59:31

    Holy Crap I did it. Pretty Scary…. All I need is the hex code that will emulate a spacebar. I push the button and the computer thinks the space bar is pressed. Any help with this would be wonderful! Thanks.

  43. Flip

    Wednesday, September 1, 2010 - 09:38:31

    hi unzapped, try the following:

    - replace line 84 in main.c ( key = 0×2F; // key = [ ) by ( key = 0×2C; // key = space bar )

    - and delete line 82 ( key = 0×30; // key = ] )

    Then just recompile and upload the new .hex file!

  44. Mimisoffice

    Tuesday, September 28, 2010 - 21:25:50

    Hi!
    I am a newbie in programming AVRs! So I want to ask if I use PonyProg is there anything I must do before writing the hex file to the chip? What is the “fuse thing”? (blush) (wastme)

    Thaks!

  45. Mimisoffice

    Thursday, September 30, 2010 - 11:18:46

    Has anybody programmed the ATTINY85 for this project with Ponyprog? I need to know how to program the chip to get it work! :((( tHANX

  46. Mimisoffice

    Thursday, September 30, 2010 - 14:22:19

    Hi all! I have found a solution to the Ponyprog fuses with this script :

    http://home.ict.nl/~fredkrom/pe0fko/SR-V9-Si570/pe0fko.e2s

    Good luck!

  47. santhosh

    Thursday, December 2, 2010 - 06:57:24

    sir iam a student of final year ece i want to do my project on one-key keyboard .so pls send me full details (cost,circuitdiagram,working procedure)

  48. Hun electric

    Friday, April 8, 2011 - 17:35:33

    Hi,

    The capacitor (100nF), i see elekrtolit,but the Volt(V?) parameter how much i need. If now metter how much, sorry my question…

    Best regards,

  49. Fabio

    Wednesday, April 13, 2011 - 19:31:56

    Am i able to use LED’s instead of Diodes?

  50. Phil

    Wednesday, May 11, 2011 - 16:59:02

    i would really like to use this project to create a standalone mute toggle button. i’ve read about how i can map a toggle shortcut to hot keys, which is one way to do it, but i was wondering if there was a “mute button code,” as some keyboards have mute toggle buttons built in.

    also, could you direct us to a list of keypress codes?

  51. Flip

    Thursday, May 12, 2011 - 11:09:22

    Hi Phil,

    a list of USB scancodes can be found here: http://www.win.tue.nl/~aeb/linux/kbd/scancodes-14.html (all are in decimal) - just note that due to different keyboard layouts around the world, the keyboard mapping can be different - this site has the US layout, which is most common around the world

    as you can see there is also a scancode for muting the volume (decimal 127), but as the code is now, it only supports 104 keys (it uses a standard 104 keyboard report descriptor) - so it’s not as simple as just changing the scancode..

    Here is a discussion which should lead you in the cirection of implementing a mute button: http://forums.obdev.at/viewtopic.php?f=8&t=3295

    Good luck!
    -Flip

  52. Sahra

    Tuesday, December 13, 2011 - 17:28:22

    Hi Flip!
    I want send 2 keys togehter ex. LCtrl+1
    don’t know how I write the command line.

    Thanks, Sahra

  53. Flip

    Wednesday, December 14, 2011 - 10:05:30

    Hi Sahra,

    If you have a look at the source-code of the 4-key-keyboard code at: http://blog.flipwork.nl/?x=entry:entry100224-003937 there you will find an example in the main.c code of a CTRL+ALT+Delete key-press and also more explanation on how to deal with modifier-keys like CTRL.. I hope this gives you a push in the right direction!

    The 4-key-keyboard code is fully hardware compatible with the 1-key-keyboard, so you can easily upgrade to this project, without having to change any of your hardware!

    Best regards,
    Flip

  54. martin

    Thursday, December 20, 2012 - 23:13:02

    Hi, I need F4 key keybord. How can i find hex key code for F4 key?
    I found this pdf http://bit.ly/gvwUJ7
    If I understand it, so F4 has code “0×3D”?

    thx for reply