I wanted to build an USB device using AVR microcontrollers since I found out that it was possible. However, both the USBtiny project and the more extensive V-USB library lacked an easy-to-approach tutorial. So I decided to make one.
This first part covers the basics for making USB-powered devices, and serves as introduction for second part, which goes through simple example for using V-USB library to implement USB communication to and from ATtiny2313. Additional parts might be published later if I have the time and there’s interest.
But let’s get started. Here is what you need for this first part:
- USB cable and pin header
- Small breadboard and a few jump wires
- LED and 330 ohm resistor
- Low voltage drop 3.3V regulator, such as LD1086V33 or LE33CZ
The first thing we need to do is cut the USB cable so the end that goes into computer remains, strip the other end and solder the four wires into a pin header so it’s easy to plug the cable into a breadboard. USB contains four wires which you should solder in the following order (note: not all cables conform to this so check with a multimeter!):
Here you see the end result. When stripping the wire, be careful not to damage the wires and make sure the wires will not touch each other so your cable won’t short circuit your computer or USB hub!
If you want to know more about USB connectors and electrical characteristics, I warmly recommend USB in a NutShell by Beyond Logic, and of course the USB 2.0 specification. For now, it’s enough to understand that USB bus can provide small amounts of current (couple hundred milliamperes at most) at roughly 5V.
Simple breadboard test
Now let’s try if we succeeded in our soldering project. I recommend you first connect your cable to USB hub and use a multimeter to measure if you actually have 5V between VCC (red) and GND (black). I myself got 5.18V. Then plug the pin header into breadboard and use jumper wires to transfer VCC and GND to power rails, and connect a LED in series with the resistor to see if you get it lit!
Congratulations! If all you want is power from USB, you can now proceed to build any 5V circuit as long as the current draw remains rather small. In case the LED stays dark, check that you did not make a mistake with the wires, soldering or insert the LED the wrong way. :)
Preparing for USB communication – getting to 3.3V
While USB power is 5V, the data lines require 3.3. Some computers tolerate 5V logic but not all. To play by the book, you have three options:
- Limit the voltage provided by USB to 3.3V
- Power the circuit externally from 3.3V
- Use resistors, diodes or zener diodes to convert 5V logic to 3.3V
We will choose the first option here. The second can be achieved using your favorite method, such as 9V battery and a regulator, cell phone charger with suitable voltage setting, or 3 AAs and one or two protection diodes that drop the voltage. For third one, you can find many articles from the web by googling “zener diode usb” (Update: You can also take a look at “Part 6” of my tutorial which covers this). V-USB wiki has a good overview of the options:
In this tutorial, I’m using LD1086V33. From the datasheet, we can see that pin 1 of regulator is ground, pin 2 output and pin 3 input. Furthermore, 10 uF capacitors are specified between ground and output, as well as between ground and input.
Here I have connected the regulator’s ground and and input pins to power rail supplying 5V and the output pin into LED and resistor.
While this circuit will likely work, any small glitches in power draw or supply require the regulator to compensate, and this can lead to a continuously oscillating voltage. So we add the 10 uF capacitors between 5V and ground (added here to power rail) and 3.3V and ground (added in front of the regulator). Be sure to connect electrolytic capacitors the right way (minus side is marked). And voila!
Now we are ready to power our AVR circuit or whatever project using a nice 3.3V voltage. Use a multimeter to check that voltage between ground and regulator input is still indeed about 5V, and the regulator output and ground is 3.3 (I got a nice 3.30V myself).