Saturday, April 20, 2013

Serial to USB Keyboard

So I finally gave in and got a raspi. I also got a USB Wifi dongle, SD card, and already had a power supply and mouse. However, I did not get a USB keyboard for two reasons. One, I already have a keyboard at home (I am currently in my dorm room). Second, I am in a dorm room! Real estate is at a premium and there is no way I would have room for one on my desk.

So, how do I enter data into my raspi. Well, I've done a single button keyboard before using V-USB, but it would be obnoxious to use only a few buttons for a whole keyboard. Wait! I already have an entire keyboard on my laptop. If only I could use it with my raspi. That is where this project comes in.

In this project I used a python script to detect key press on the laptop and then send them via serial to an ATTINY2313 with V-USB that converts the serial data into key presses on the raspi.

 The hardware is pretty simple. It is just an ATTINY2313 running at 20MHz with connections to a USB to serial converter and hardware to interface to USB.


To detect key presses on my laptop I have a python script running. It uses the msvcrt library, which only works in windows. It also cannot detect the CTRL, ALT, SHIFT, or CAPS keys by themselves. Though if you press SHIFT and a letter it will read it as a capital letter. The same goes for CTRL, though some keys overlap with other buttons (i.e. CTRL-H returns 8 which is that same as Backspace). This data is then sent to the microcontroller with serial. The ATTINY then receives this data and sends the corresponding character using V-USB.




  1. Well done for getting featured on Hack a Day, very impressive - I hope you carry on with this hobby electronics blog.

  2. Why not just use a terminal program, like PuTTY ?

    1. I am hoping to be able to do that in the future, however I do not have a router and cannot connect the raspi to my school's internet (it requires that you login via a browser and run some security software). Hopefully I can just connect it directly to my computer and get it to work from there, but I have read quite a few threads with people having issues connecting directly to Windows 7, so I figured that this would do in the mean time. Plus, being able to configure both sides and see what is happening would be helpful (I'm a linux noob). Not to mention it was a fun challenge and may prove to be useful in other situations.

    2. It is not that hard, use a cross over cable to link it to your PC, or buy a cheap $10 switch from a computer store and plug your PC & RPi into it. If you want to take the easy route, just enable internet sharing in windows, which will start a DHCP server that the RPi will use. If you want to do it the (IMO) better way, configure your Ethernet devices with static IPs (say: & and then you can easily SSH into the device using a SSH tool such as PuTTY.

  3. honestly, you can use the atiny as ttl/usb serial converter to connect to the serial terminal of the raspberry, where you can use a real full terminal.

  4. You can just ssh or vnc into the raspberry pi. Then you won't need an extra mouse/keyboard/display. Your laptop will do the trick. Check here - and