Many people have work laptops and don't really feel like spending money on their own personal laptop for home use. Often work laptops come with security restrictions around what you can do with them, which is of course not a bad thing in and of itself. My wife would get annoyed when the screensaver would come on in the middle of streaming something on her laptop. She could do the old thing of moving the mouse every couple of minutes, but that's pretty annoying, and watching streaming videos is supposed to be relaxing.
She asked me if I could make a Raspberry Pi thing to keep the screensaver from coming on, but as this task wouldn't require a full-blown Linux operating system I aimed my sights at something simpler and cheaper. You can emulate USB devices with some Arduino variants like the Leonardo, but I went even simpler and cheaper.
There is a C library for AVR chips (the chips the original Arduino is based on), V-USB, which makes it possible for you to create a USB device with an AVR chip - including one of my favorites, the humble-but-capable (and really inexpensive at around $1) ATTiny85. Armed with a programmer, my mint-tin AVR prototyper/programmer, and an extra USB mouse, I had all I needed.
Step 1: Materials and Tools
You will need:
- 8 Pin Chip Socket
- 1 1500 Ohm resistor
- 2 68 Ohm resistor
- 0.1 uF Capacitor
- SPST Push button
- LD33 Regulator to drop 5V coming from the USB port to 3.3V to power the chip.
- Some USB device you don’t really need, so you can cut off the USB connector. Possible sources: your closet, your local makerspace (shout-out to Bloominglabs), Goodwill or similar second-hand store
- A Mint Tin
- Double-sided tape
- Hot glue (you almost always need this)
- AVR programmer. I used the usbtiny programmer from Sparkfun plus my Mint Tin ATTiny Programmer. You can use an Arduino to program ATTinys, too.
- Wire Cutters
- Wire Strippers
- Dremel, bandsaw, or anything else that can trim your PCB down to the right size.
- Soldering Iron
- Hot Glue Gun (you almost always need this)
Step 2: Inspiration and Preparation
This project was very heavily inspired by the 1-Key-Keyboard. It uses the V-USB library to simulate a USB device which sends a character (or string of characters, think password or ‘randomly selected synonyms for ‘cool’’, or such) to the computer when you press a button.
I wanted to simulate pressing the 'up' button every 7 minutes to keep the computer awake. I still included the button in my circuit. Why? Because then all I have to do is push that button to verify the device is ‘alive’ in the event I need to do some troubleshooting. Also, who knows? Maybe you want to program it to send a char or string of chars in addition to its periodic pinging.
Take your USB device and cut the cable. Leave a couple of inches so you can strip the wires and run the cable from the board through a hole in the tin. Probably best not to leave the cable too long.
Strip the wires making up the USB interface. There will be a red, black, green, and white wire.
Drill a hole in the center on one side of the tin that's wide enough that your cable can pass through it.
Cut the PCB down to size: (about 2" x 2 3/4"). You can do this with a bandsaw or a dremel, whatever you have handy.
Heat up your soldering iron.
Step 3: Assembly
My circuit is a simplified version of the one on the V-USB page (see the circuit diagram above). I didn't have luck getting two diodes to drop the voltage from 5 to 3.3, so I swapped those out for an LD33 Voltage regulator. Additionally, thanks to some things I learned from the '1 Key USB Keyboard' page, fuse settings plus things you do in the code make the crystal and 2 associated capacitors unnecessary. More about that in the next step.
The circuit I ended up with is illustrated in the colorful Fritzing breadboard diagram above. You can use it and the image of the finished project to guide you in soldering it together on the PCB. A sort-of tricky bit is soldering the leads from the cable to the board after running the cable through the hole in the tin, so do this last.
Refrain from attaching the board to the tin with double-sided tape until you've had a chance to ensure everything works fine. You will need to put some insulator like paper in the tin to prevent the board from shorting out connections when it comes in contact with the tin.
I like using hot glue liberally whenever I have the opportunity. In this case it secured the cable (and the somewhat fragile wires from the USB cable) and I spread it over exposed solder and what not.
Step 4: Code and Programming
Here again I need to give big thanks and props to the 1-Key USB Keyboard for guidance and inspiration. I didn't have a crystal around but it turned out you don't need to.
I suppose you can skim this, but I found it really interesting, and it's certainly not something I would have known to do if I hadn't run across this trick.
As you may know, AVR chips have an internal oscillator, for example the one in the ATTiny85 will allow you to clock it at a max of 8MHz. Often people choose 1MHz because it's fast enough to do the job, but means there's less power use, which you have to love if you're running off batteries.
However, from page 24 of the 234 page ATTiny85 (et al) datasheet:
6.1.5 Internal PLL for Fast Peripheral Clock Generation - clkPCK
The internal PLL in ATtiny25/45/85 generates a clock frequency that is 8x multiplied from a source input. By default, the PLL uses the output of the internal, 8.0 MHz RC oscillator as source.
So, you can get up to 64MHz out-of-the box with this setting.
Now if you read the V-USB notes, you will learn that 'Only the 16.5 MHz and 12.8 MHz variants allow a deviation of up to 1%. They are therefore suitable for RC oscillators. 16.5 MHz is suitable for devices which can derive a 16 MHz clock from an RC oscillator (e.g. ATTiny25/45/85 and ATTiny26).'
The 1-Key-Keyboard uses this PLL (Phase-locked-loop) fuse setting plus a function called 'calibrateOscillator' (see main.c) to 'tune' the OSCCAL ('Oscillator Calibration Register') value to get things running at 16.5 MHz. If you'd really like to go deep with this, check out the datasheet and search for OSCCAL.
Fuse settings can be found in the Makefile.
This is one of the things that's fun about Microcontroller programming - it doesn't really take to much digging and research to find out how to do many things, but you can dive in as deep as you want. You can even check out the assembly code to measure USB frame length in the V-USB library if you like.
OK, if you just want to program the thing, plug it in and type these commands:
Step 5: Future Directions
My wife has been using this for several months and it appears to be working well. After putting together version 1 on the breadboard, and version 2 in a mint tin as described in this Instructable, I decided why not design a board and have it printed and shipped to me? I used Fritzing to do that, which is not going to get me in the cool EE club anytime soon, but it was fun to get my boards in a package. It was less fun to realize I'd made a couple mistakes, but with a circuit this simple it was easy to work around them. Should I tidy that up a bit and design a compact case I can print on the 3D Printer at Bloominglabs (Bloomington, Indiana's Makerspace), I will share the details of that effort here on Instructables.
All in all, it was a fun and functional project, and I learned a lot about ATTiny programming from it.