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 cable

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!):

Pin

Color

Function

1

Red

VCC (+5V)

2

White

D-

3

Green

D+

4

Black

Ground (0V)

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).

Awesome stuff, just worked through this; I would love to see more tutorials like this.

FredG:February 11, 2012 at 7:02

Sadly, not all USB cables are built to the standard. Just bought a really cheap cable from the discount store for a USB-AVR project and found that the third-world factory had wired the cable with black and red reversed. Maybe a color-blind worker? Just to keep me on my toes, the white and green wires were blue and white, and they were in the right place (if you reckon blue is the new green)

Pays to check every step…

jokkebk says:February 11, 2012 at 10:51

@FredG
Heh, I was wondering how many cables get it “right” and how many do not when I wrote the part on wire colors. Seems like a word of caution in that part may be in place. :)

Tom:March 14, 2012 at 14:08

Nice tutorial, thanks a lot. Just a question – wouldn’t it be safer to put some resistor in series with the input-side capacitor to limit the current? For a short time after connecting, when the capacitor is fully discharged, there is a short cicuit between USB VCC +5V and GND and thus the current could exceed the USB specs. I beleive it won’t be an issue in most cases, though.

jokkebk:March 14, 2012 at 14:27

@Tom
Yes that would probably be safest. However, I’m not sure but it might actually be that USB spec defines that the port should limit the current (in the case of short circuit etc.) – at least I recall being concerned that I might burn a USB port by drawing too much power, but then reading about it a bit more and realizing that should not happen.

John:March 23, 2012 at 15:22

Would it work with an Atmega48 (it’s able to run at 3.3V)? My local electronics story doesn’t have Attinys and it would be much more expensive to get an Attiny online.

jokkebk:March 23, 2012 at 15:42

@John: I’m quite certain that it would, as long as it’ll run with 12 MHz clock (or any other frequency supported by V-USB), which the ATmega48 seems to support.

David Lee:April 18, 2012 at 4:20

I read it well. I have a question, I tried to control USB Port by using 5V Power, for example, if i put the DPDT Switch for controlling 1 (red line) and 4 (black line) , is it o.k to operate the USB Port ?
Because, USB connected only 2(D-, white),3(D+, green) without 1,4, it will not be reconized from PC.
Is it wrong my guessing ?

jokkebk says:April 18, 2012 at 10:05

Hmm, what do you mean “control USB port by using 5V power”? If you mean that you are driving data lines D+ and D- from a 5V MCU without any level shifting, that will not work on all computers (and might even damage some, although that is quite unlikely), so I’d recommend a zener configuration.

If you mean controlling the USB port while using 5V external power supply, then you’d only need to connect the grounds (black wire) so the logic levels have a common reference point – just make sure that your power supply ground is not significantly on a different level than USB ground (with my switching power supplies, there’s a few millivolts when I measure it with a multimeter, but I don’t think that will cause much current to flow over the ground line, it’ll quickly level once grounds are connected).

If the device is not recognized when VCC (red wire) is not connected to anywhere, you might want to add a 10k-20k resistor between it and ground – the FT232RL datasheet has excellent “reference implementations” for USB circuits using external power, USB power, switchable power etc. so you might want to check that:

“…most hosts and hubs don’t have the ability to detect such an overload of this magnitude and thus if you drain maybe 5mA or even 10mA you should still be fine, bearing in mind that at the end of the day, your device violates the USB specification. “

jokkebk says:May 1, 2012 at 15:54

Yes, the device built here definitely would not pass the USB compliance tests. However, even our MCU will consume more than 500 uA because we’re not handling any “go to standby” signals that might be sent by the USB host. So even discarding the low-drop regulator or using some method to put that to sleep mode, we wouldn’t be any closer to USB compliance – therefore I haven’t bothered too much with that fact. If one would like to build a fully USB compliant device (i.e. get the sticker and everything), the regulator will be simplest of the worries. :)

So I wouldn’t worry about the regulator while our device is so “stupid” it ignores any sleep and standby signals, and probably it doesn’t handle wakeup events any more gracefully. And as you said, it’s unlikely that any USB host would be electronically designed so that going to sleep mode would switch to another current source that could break with the non-standard energy consumption of our device – most likely it’ll work, and in worst case our device would just not receive enough current to do its job.

Mia:September 2, 2012 at 18:56

The USB power in my laptop is 6V. What should I do?help

jokkebk says:September 2, 2012 at 19:11

If you have a low-drop regulator (doesn’t even need to be that low if you have 6V instead of 5V) like the one suggested in this tutorial, it should be no problem.

Powering an ATtiny directly may also work, but there’s a small chance your microcontroller doesn’t like it.

Also, you can decrease the voltage using a simple diode (e.g. 1N2222) – normal ones usually drop the voltage about 0.7V. So instead of just wiring the 6V to your circuit, run it via a diode.

Harpreet Singh:September 19, 2012 at 9:27

Help me out in my project

jokkebk says:September 19, 2012 at 10:09

Could you elaborate somewhat? :)

ali khanjani:September 20, 2012 at 9:46

how can i connect my photo camera to my avr than take photo and at that time transfer my photo to my computer
?

jokkebk says:September 20, 2012 at 10:13

I’m afraid that is completely out of scope of this tutorial. I suggest you try some electronics forums. You’ll need to decide what type of camera (USB webcam, somthing with a interface that MCU can easily talk to, etc.) you’ll use, and then will you do a custom software to transfer the image (in which case you can use USB, RS-232, or many other interfaces), or if you’d like something more plug and play.

Also, if you have only the requirements you mentioned, a webcam connected to PC would be enough, too.

Cool, didn’t notice that. In practice, it does work very well – which isn’t all that surprising if you consider that 10 MHz should work without problems at 2.7V. Maybe I wouldn’t start mass production with those specs, though. :)

Joanna:February 11, 2013 at 17:24

Hmm is anyone else experiencing problems with the pictures on
this blog loading? I’m trying to figure out if its a problem on my end or if it’s the blog.
Any suggestions would be greatly appreciated.

jokkebk says:February 11, 2013 at 17:40

At least from Finland they seem to work fine. If you have specific images missing, report the URLs and I can check that I have not accidentally removed them in WordPress or anything.

kapil:February 15, 2013 at 17:08

Hello, Very simple and descriptive tutorial. Thank you for this nice tutorial. how can I give the command to device like (1,200) where 1 is the ID and 200 is the specific code. Any simple example ?
regard

jokkebk says:April 20, 2013 at 14:35

You have the wValue and wIndex -parameters in usbFunctionSetup, later parts of the tutorial show how those are accessed (e.g. rq->wValue.bytes[0]) and set on the PC (usbtest.c) side.

PS. And yes you probably could simulate the LED example in this part in Proteus if it contains a regulator component (or you build it yourself). Wouldn’t be of much use though (“LED lights” :).

canibalimao:June 25, 2013 at 16:34

I don’t have here with me 10uF capacitors. I can use any other capacitor values, or the value should be arround 10uF?

jokkebk says:June 25, 2013 at 16:48

The regulator might just work without any, but some may start fluctuating and heat up while attempting to keep voltage regular, so capacitors would probably be a safe idea. Anything between 1 uF and 100 uF will probably work OK, even 0.1 uF may help some.

You can think the capacitor as a reservoir – on supply side it provides quick current for regulator in case your voltage supply is momentarily dipping a bit, and on the regulated side it keeps current spikes consumed by microcontroller or any other part of your project out of sight of the regulator. The smaller reservoirs you have, the more directly the regulator “sees” the spikes in demand and supply, and may struggle to compensate for changes (or overcompensate / start to fluctuate). The manufacturer recommended values are usually the best – too small capacitor and you have limited balancing, too big and the capacitor may eat up too much current when loading up.

However, from personal experience things have worked fine in small projects that don’t tax the regulator too much even with smaller caps, or no caps at all (although I haven’t used an oscilloscope to see how bad the regulation becomes).

canibalimao says:June 29, 2013 at 17:50

Thank you very much for the lesson jokkebk :D

I have here at home a bounch of 470uF capacitors that I’d bought to another project and I want to know if I need another one or I could use those.
That way I’ll buy a smaller one when I went to the electronics store.

Krishna:August 17, 2013 at 22:00

Hello,
Is it posssible to use this method on an AT89C51 controller. I know it’s an outdated one and based on 8051 architecture not AVR, but its easy to fetch the old ones from local electronics store.

jokkebk says:August 18, 2013 at 12:10

Highly unlikely – if the instruction set is different, I’m quite sure the V-USB assembly inner loop timings will be different (and probably non-working) and will not results in proper USB signalling. You’ll probably need to order some more modern chips by mail. :-d

student12345:September 17, 2013 at 23:32

Could you just make a voltage divider to get down to 3.3 for that data lines instead of using zener diodes?

Joonas Pihlajamaa says:December 9, 2013 at 23:35

Don’t know if it would draw more power or have some other drawbacks. For some reason zeners seem the most common suggested option, haven’t done the research why that is – might be that because the lines are used also for input, voltage divider doesn’t work as well in that direction – it would lower the 3.3V logic high below AVR treshold of 1.

John says:January 4, 2014 at 23:18

Actually, it wouldn’t lower the voltage at all, assuming the input resistence of the INT1..0 pins is sufficiently higher than the resistor that would be between the port and the data line. Essentially, the divider would be set up like this:
INT1 --vvv--o--- Data
R1 |

|
GND

This would mean that the voltage across R1 and the high input impedance towards GND would be the same as across R2:
3.3V on incoming signal, 3.3V on outgoing.

Or am I missing something?

John says:January 4, 2014 at 23:19

Wow, my ASCII drawing messed up badly. R1 runs from INT1/0 to the data line in question, R2 from the data line to GND.

John says:January 4, 2014 at 23:30

nvm. That probably messes up the early communication about High- vs. LowSpeed usb device.

ben:March 6, 2014 at 3:21

What are your thoughts on the LUFA/MyUSB stack, or the Microchip USB stack (which I think is open source now)?

What challenges do you see porting this code to a design with a ATmega128 instead of an Attiny?

Joonas Pihlajamaa says:March 6, 2014 at 14:29

Haven’t used LUFA or other stacks that rely on hardware USB support, probably they are simpler as there’s less code in firmware side and the device can probably do many things independently without wasting main MCU cycles.