USB: The Universal Serial Bus: ATTINY2313

Last Updated: 04 Aug 2014

Featured at: hackadaywww.hackaday.comhackaday

You can now also get the source code from github.com.

On this page, I will show how to make a small USB device, program it, then using the information in my book, you could then communicate with the device.  See Appendix Q for more detailed information.

Since the idea is not to teach you the electrical hardware of the USB, but to teach the software side, this tutorial will create a small, USB device that simply allows you to attach to a bread-board and add components so that you can then create your own devices to run other components, make small robots, etc.

Here is an image of the finished device.  The image on the left is the completed board while the image on the right is the device attached to a bread board.

attiny2313 rev 1.0attiny2313 rev 1.0 Bread Board


The following is a list of parts you will need to create and use this device:

Count Item Description Link1 Each2
1 The Programmer Used to program the ATTiny23133
See here for assembly instructions.
www.adafruit.com $22.00
1 The PCB Our device's board. (blogged here) Available Here 11.50
1 USB Type B Micro MICRO_B Newark or Mouser 1.14
1 Green LED LED1 Newark or Mouser 0.08
1 Six pin Header (2 * 3) HDR Newark or Mouser 0.67
2 5.1v Zener Diode D1, D2 Newark or Mouser 0.07
1 82 Ohm resistor R6 Newark or Mouser 0.29
1 1meg Ohm resistor R2 Newark or Mouser 0.29
1 1.5k Ohm resistor R1 Newark or Mouser 0.29
1 4.7k Ohm resistor R5 Newark or Mouser 0.29
2 68 Ohm resistor R3, R4 Newark or Mouser 0.15
2 10uF capacitor C1, C2 Newark or Mouser 0.35
1 regulator TO-220 (LD1086) Newark or Mouser 0.95
1 12 Mhz Resonator 12 Mhz Newark or Mouser 0.50
1 36 pin header Newark or Mouser 0.83
1 ATTiny2313A 20 pin 2313 Newark or Mouser 1.31
1 20 pin Socket (Optional)   Newark or Mouser 0.69
1Some of the parts may be different between Newark and Mouser, however, I beleive that they are identical in function. Currently, I have only used the Mouser parts, but will soon try the Newark parts. 2Does not include shipping and handling. 3Please note that you only need to purchase "The Programmer" once. You can use it multiple times and on multiple projects.


The PCB
Once you have received your Programmer, PCB, and components, you can get started soldering them to the board.

A tip for soldering:  Get a good solder paste and place a small dab on the part you are soldering.  Place a small amount of solder to the tip of you iron.  Then touch the tip of the iron to the part (pin) you want to solder.  As soon as the pin is warm enough, the solder will flow to the pin.  Hold the iron on the pin for a split second longer, then remove the iron.  Be sure to hold the iron on the pin and the PCB's solder pad at the same time. Holding the iron on just the pin, might not make the PCB's solder pad warm enough to get a good connection. Don't hold the iron on the components pin for very long, you may damage the component.  If the component is not taking the solder, you didn't put enough solder paste on the pin.  Try again.
If you soldered everything correctly, held enough heat to the pins, but not too much, and used plenty of solder paste, your PCB should now be ready to program.


The Programmer:
You can get the programmer from the URL listed above in the parts list.  The from www.adafruit.com will need to be completely assembled.  Be sure to see the instructions listed at the URL above.  If you already have a programmer, then you don't need to purchase another one.

The programmer's USB driver needs to be installed on your host machine.  I use WinXP and the driver for it is here, specifically Windows USBtinyISP driver for WinXP 32-bit.  For now, you don't need any other files on that page (if you are using WinXP as your host.)

Install the driver (before you plug your programmer in), then plug in the programmer.  If all went well, the Windows task bar should show prompts stating so and the programmer's green LED will be lit.

If you have the www.adafruit.com programmer and you follow the MakeIt link, here are a few notes:

Your programmer should now be ready for use.

As a side note, if you have a programmer that uses an ATTiny2313, and that chip some how loses its firmware, you can not reprogram the programmer without another programmer.  Therefore, it is my suggestion that you program another 2313 with the firmware from here and store it away for just this purpose.


The Cross Compiler Toolset:
You will need to download and install the cross compiler WinAVR so that you can compile and "burn" your code to the 2313 chip.  The one I use is the WinAVR for Windows.  If you are using Linux or another host, be sure to find that one.

You will also need the USB library from http://www.obdev.at/products/vusb/download.html.

Have a look at this page for an example of some code you could use.


The Rest of the Story:
Now, using the 8 pins of the controller, you can use your imagination to create your own devices.

You can send more than just single digit control transfers.  Using the buffer space, you can now send multi-byte data to the controller as well.

The sky is the limit.  You can send a total of 256 different bRequests types each with data in the SETUP packet itself, and/or by the buffer pointed to by the SETUP packet.  (note: you will have to add a bit more code to the firmware to retrieve this buffer data).

For example, if you have a stepper motor on pin 4, send a request type of your choosing with a value in the bIndex field of 4 (for pin 4) and a value of 25 in the nLength field.  Your firmware code could then take that as step the motor on pin 4, 25 times.


What's coming up:
If you have any questions, please let me know.