This post describes the making of a USB programmable microcontroller board using the atmega16u4 microcontroller. This is the board I’m currently using to control the BrontoBot.
- Easy to program
- Can run by battery
- Contains some new technology (for me to learn)
I go through all of this because I like to do it. It would of course be easier to control the robot with an Arduino, and almost all of the code I write for this project will be easy to port to an Arduino or other AVR based microcontroller boards. But if I were to use an Arduino, it would affect point 4 badly!
- Four servo connectors
- Front legs
- Back legs
- BEC plug for servos
- The servo current will come through a DC/DC switch transformer. Servos have a tendency to reset microcontrollers when they drain too much current.
- I use the cheap HobbyKing UBEC 3A
- On/off switch for servo supply
- During programming, it is often useful to be able to stop the servos and keep the microcontroller alive
- This could be any switch, I use this pack from dealextreme
- Power on/off switch
- This is a lot more practical than pulling in and out wires.
- I used the same type of switch as for the servo supply
- Reset/program button
- To restart the program when it crashes, and enable programming mode when connected to USB
- I already had some, but I’m quite sure these will do
- Piezo buzzer
- The robot will come alive by making beeping sounds.
- Power indicator LED
- Needed to see if the board has power
- Status LED
- In the very beginning of the firmware programming it is useful to have a LED for debugging.
- 5v Voltage regulator
- This one converts the voltage when running from battery, to drive the microcontroller
- AtMega16U4 in TQFP package
- Can be programmed by USB
- Passive components for microcontroller and USB
- UCAP capacitor 1uF
- 16MHz external oscillator
- 2x 20pF capacitors for the oscillator
- 2x 22Ohm resistors for USB signal line
- IR Module 38kHz
- This component makes it possible to use a TV remote to control the robot
- I used Vishay TSOP24 because it was cheap at farnell
- IR LED and IR photodiode
- Together, these form an obstacle detector
- The IR LED can also be used to replicate a TV remote
- Lots of bypass capacitors
- Especially with CNC-milled PCBs I’ve seen that thin copper wires make all kind of weird noise problems.
- The bypass capacitors will be distributed evenly around the PCB.
- I use 100nF SMD capacitors in 1206 packets for this
AtMega16U4 pins research
TDI – JTAG Test Data In
TDO – JTAG Test Data Out
TMS – JTAG Test Mode Select
TCK – JTAG Test Clock
T0 – Timer0 Source
T1 – Timer1 Source
PDO – Programming Data Output (MISO)
PDI – Programming Data Input (MOSI)
HWB – Execute bootloader during reset
D- – USB Minus
D+ – USB Plus
UCAP – This pin must be connected through an 1uF capacitor to ground
UVCC – This is the power to the USB core, and can be given between 3.4v and 5.5v. This can be from the USB power line or from an external power supply. In my circuit, I connect it to VCC (+5v).
RESET – When set low, resets the MCU or starts programming mode if USB is connected
If you choose to use these schematics for your own project, be aware of some errors currently in this version:
- I forgot that the IR transistor works within a voltage divider, not just a resistor in series.
- For the IR Module, I googled the most common pinout and assumed mine was the same… it wasn’t.
Make sure every single pin on the MCU has been soldered properly. Remember that when they are pushed down to the pad by a voltmeter probe, you won’t see if there is no connection. I recommend probing the MCU lightly and sideways to avoid this.
I had no plug to connect the UBEC to the battery, so I had to solder this fix (top right). The next revision of the board will have this plug on board.
The servo plugs were supposed to go inwards on the board, with a 90 degree connector. It turned out it collided with the reset button, so that didn’t work out.
And finally, the switch to the left is placed too close to the servo connector, so I barely managed to squeeze it on there.
Flip – The application used to program the USB microcontroller
DFU – The name of the bootloader pre-installed on the microcontroller to make this possible
WinAVR – The compiler used to make the final .hex files for the mcu
AVR Studio 4 – The IDE used to write and simulate code
Jungo – A driver package used for AVR USB devices and programmers
Compiling the HID example in AVR Studio 4
Had to make some changes in power_drv.h, following this. I changed:
#define Clear_prescaler() (clock_prescale_set(0))
#define Clear_prescaler() (Set_cpu_prescaler(0))
#define Clear_prescaler() clock_prescale_set(0)
#define Clear_prescaler() Set_cpu_prescaler(0)
and it compiles without error. Since I’m using an atmage16u4 and not an atmega32u4, I had to write atmega16u4 in config.mk. The oscillator frequency is also different, so I changed from 8000 to 16000 in config.h.
As the makefile is specified now, and not auto-generated by AVR Studio, all new c-files needs to be manually added in the config.mk file.
- I’m planning to connect a bluetooth modem to communicate with an Android device
- I forgot to add a connector for the buzzer
- There would be a lot to win by using smaller connectors
- I could make a smaller two layer version, then order it from Olimex