Every now and then you need an electronic component and there just isn’t a good match available…necessity is the mother of invention so why not make your own? The problem was I had a very tight cost budget, very little PCB space, and was behind schedule…I needed a cheap, tiny solution FAST. Fortunately, I found the ATtiny which is an 8-bit AVR micro-controller available in packages as small as SOT23-6 package (about the size of the head of a pin) that costs $0.28 in small quantity and the more powerful ATTiny13A that costs $0.35 and is available in SOIC8 and DIP8.
I needed to generate a 5V PWM output to drive a MOSFET for programmable closed-loop control of the speed of two types of DC motors in a very tiny space. There aren’t that many *really* small micro-controllers in packages that are reasonable to prototype with (I hate prototyping with QFN and BGA packages). I considered a few including the Microchip PIC10F322 (also SOT23-6) and the STM8 series (supports the great STLink debug environment). I eventually counted out the PIC because the development environment was so unfriendly (I’m done with expensive proprietary compilers) and counted out the STM8 because even though I’m quite experienced with the STM32, getting the peripherals programmed properly can be complex and I needed a solution fast, so I chose the ATTiny.
I used avr-gcc as the C compiler which is fast, familiar, and generates tight code (you can’t go wrong with gcc). Microchip has breadboard-friendly DIP-8 versions of the ATtiny13A and the pin-compatible ATTiny25/45/85, so I could rapidly prototype a solution. The documentation is excellent, the peripheral set is rich and easy to use (unlike ST uCs which are incredibly flexible but accordingly complex). It was so easy that I was able to get the code fully functional in just a few hours having never use AVR processors before. I understand you can even use Arduino to program it (I didn’t).
- Tiny AVR Programmer (see tutorial) OR USBasp-H6P (only $6 on amazon)
- avr-gcc pre-built binaries
- 100 ATtiny13 projects (lots of code examples and useful links)
- Product pages for ATtiny13 and ATtiny10
Some notes on flashing tools. The Atmel-ICE is an awesome debugger that uses only the RESET line to do all of its debugging! Unfortunately it doesn’t supply power so it’s a lousy tool for flashing the target. I haven’t found the perfect tool yet, but the TinyAVR and USBasp (see links above) work well. The main down-sides are that they don’t have enclosures and getting them set up can be annoying; you need to install Zadig and reconfigure them to use libusb-win32 (see instructions here)…after that, they work well.
It’s not going to replace the STM32 family for most of my applications, and the STM8 series is arguably better, but for those times I need a really cheap, really small solution, the ATtiny is a nice addition to the toolbox. You can even order them from Digikey with your software pre-installed for an additional ~$0.25/chip making them truly a custom hardware component!
Some useful links:
- AVRdude (command line flash programming utility)
- Soft UART (super useful for debugging)
- Even cleverer soft UART (uses a single-pin for Rx and Tx with a single transistor)
- Making good use of limited I/O (e.g. read lots of switches with one GPIO)
- C NOP implementation (needed e.g. after re-configuring GPIO before reading it)
- ATTiny Arduino Core (not a great idea with such limited resources?)