AVR ATtiny USB Tutorial Part 1

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!

Cable with soldered pin header

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!

LED lit using USB power

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:

  1. Limit the voltage provided by USB to 3.3V
  2. Power the circuit externally from 3.3V
  3. 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:

http://vusb.wikidot.com/hardware

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.

Pin ordering and example circuit

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

Proceed to the next part of this tutorial

Published by

Joonas Pihlajamaa

Coding since 1990 in Basic, C/C++, Perl, Java, PHP, Ruby and Python, to name a few. Also interested in math, movies, anime, and the occasional slashdot now and then. Oh, and I also have a real life, but lets not talk about it!

70 thoughts on “AVR ATtiny USB Tutorial Part 1”

  1. I know this post is ooooooold, but a bunch of sites link to it as it’s very clearly written and explains things well, so it might be pertinent to mention, that Objective Development warns about using general-purpose LDOs, and specifically mentions the LE33CZ recommended in this post, as their power-usage is often (relatively) high, which in combination with the pull-up on the bus is viable to cause issues with USBs quiescent current draw limit of 500 micro-amps during suspend mode. In practice, this is unlikely to cause issues, but it’s worth noting, since behavior in out of spec situations like that is unpredictable.
    The LE33CZ has, at zero output current, a typical quiescent current draw of 0.5mA or 500µA. Together with the 200µA from the D- lines pull-up puts it way past the limit before *any* draw from the MCU.
    More expensive low-power LDOs, like the ones in TI’s TPS715 line, OTOH have typical current consumptions of 3-5µA, leaving much more breathing room for the MCU from the 500µA limit, even with the pull-up.
    This is one reason why a lot of people opt instead to go the route of using two rectifier diodes in series on the MCUs supply line, because even though the voltage will be unregulated, and the MCU might be overclocked slightly out of spec, in practice (if you don’t need to do analog shenanigans which would need voltage regulation) AVR chips will champion on slightly overclocked and everything will be (mostly) in spec on the USB side.

Leave a Reply

Your email address will not be published. Required fields are marked *

Time limit is exhausted. Please reload the CAPTCHA.

This site uses Akismet to reduce spam. Learn how your comment data is processed.