USB: The Universal Serial Bus: ATTINY2313

Last Updated: 23 Sept 2012

New Item Appendix Q is now available New Item My Blog New Item
Featured at: hackadaywww.hackaday.comhackaday
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 Link Each
1 The Programmer1 Used to program the ATTiny2313A. (Either one of the following two -->) "commercially available"
See here for assembly instructions.
www.adafruit.com $22.00
or
$15.652
Smaller, simple, less parts, easier to assemble. Available Here
1 The PCB Our device's board. Available Here 7.50
1 USB Type B Micro MICRO_B Mouser 1.14
1 Green LED LED1 Mouser 0.08
1 Six pin Header (2 * 3) HDR Mouser 0.67
2 5.1v Zener Diode D1, D2 Mouser 0.07
1 82 Ohm resistor R6 Mouser 0.29
1 1meg Ohm resistor R2 Mouser 0.29
1 1.5k Ohm resistor R1 Mouser 0.29
1 4.7k Ohm resistor R5 Mouser 0.29
2 68 Ohm resistor R3, R4 Mouser 0.15
2 10uF capacitor C1, C2 Mouser 0.35
1 regulator TO-220 (LD1086) Mouser 0.95
1 12 Mhz Resonator 12 Mhz Mouser 0.50
1 36 pin header Mouser 0.83
1 ATTiny2313A 20 pin 2313 Mouser 1.31
1 20 pin Socket (Optional)   Mouser 0.69
Total Cost:3 Adafruit Programmer: $37.97
fysnet.net Programmer: $31.62
1Please note that you only need to purchase "The Programmer" once. You can use it multiple times and on multiple projects. 2Includes the cost of the 6-pin cable connector, available here, not included in the kit from fysnet.net. 3Cost in US Dollars at time this list was created. Does not include any shipping and handling that may apply to any order.


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 one of the URL's listed above in the parts list.  If you get the kit from www.adafruit.com, you will need to solder all of the components onto it as well.  Be sure to see the instructions listed at the URL above.  If you get the kit from www.fysnet.net, you can request it be unassembled or assembled at no additional cost.  If you already have a programmer, then you don't need to purchase another one.

The programmer needs to be installed on your host machine.  I use WinXP and the driver for it found 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 program and 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.