Home Technology Bare-metal programming the tinyAVR 0 microcontrollers

Bare-metal programming the tinyAVR 0 microcontrollers

All you wish is a text editor, a makefile and a USB-serial cable.


Are you an 8-bit or a 32-bit programmer?

At OMZLO, we were primarily focussing our constructing efforts on newer 32-bit Arm-Cortex chips (STM32 and SAMD), which in most cases offer more RAM, more urge, more peripherals, at a identical or decrease attach-level than older 8-bit MCUs. However 8-bit MCUs are removed from pointless. Microchip has critically released a brand contemporary series of chips, collectively branded because the “tinyAVR 0-series”, which offer more novel peripherals than the older AVR chips at a truly competitive attach level. They seem worship the neatly matched candidate for easy merchandise that don’t favor the total parts and pretty-tuning capabilities of the newer 32-bit MCUs. 8-bit MCUs are also substantially more functional to program, which translates into quicker constructing time.

Ensuing from the success of the Arduino UNO, there are heaps of tutorials online that grunt how it’s likely you’ll presumably presumably program 8-bit Atmega328 microcontrollers and their cousins worship the Attiny85 utilizing tell register salvage admission to, with out the Arduino language or any provider IDE akin to Atmel Studio. Gorgeous google “atmega328 blinky”. All you wish is an AVR C-compiler, a text editor, avrdude, and an AVR programmer. Some sources even show veil how it’s likely you’ll presumably presumably also plan the electronics wanted to salvage a classic atmega328 working on a breadboard. On the different hand, it be no longer easy to search out the the same info for these newer “tinyAVR 0” chips.

Obviously, Microchip affords the total instruments vital to program these newer “TinyAVR” MCUs with their windows-totally IDE. There are also “Arduino cores” for these form of newer “TinyAVR” MCUs that suggest it’s likely you’ll presumably presumably program them with the Arduino IDE. However yet again, at the same time as you worship to jot down code for MCUs in “baremetal” model, alongside with your current text editor, a makefile, and a c-compiler, there are few sources available online.

In this blog post, we can picture how it’s likely you’ll presumably presumably program a blinky firmware on an Attiny406, from the bottom up, utilizing the perfect instruments. Many of the things described here would possibly perhaps perchance also be with out divulge transposed to assorted TinyAVR MCUs. Our arrive is continually guided in direction of macOS or Linux users, but would possibly perhaps perchance presumably honest silent even be acceptable in an MS-Dwelling windows ambiance with just a few minor changes.


We made up our minds to play with the Attiny406, with a gape of utilizing it in due path to interchange the Attiny45 we currently exhaust on the PiWatcher, our Raspberry-Pi watchdog. The Attiny406 has 4K of flash fame, 256 bytes of RAM, and would possibly perhaps perchance presumably trip at 20Mhz with out an exterior clock offer.

Indubitably one of the indispensable ideal differences between the contemporary TinyAVR MCUs and the older classic AVR MCU worship the Attiny85 is that the newer chips exhaust a assorted programming protocol called UPDI, which requires totally 3 pins, as in opposition to the 6-pin ISP on the classic AVRs.

A itsy-bitsy little bit of study presentations that programming TinyAVRs with UPDI would possibly perhaps perchance also be performed with a straightforward USB-to-serial cable and a resistor, because of a python instrument called pyupdi, which implies the following connection map for firmware add:

                        Vcc                     Vcc
                        +-+                     +-+
                         |                       |
 +---------------------+ |                       | +--------------------+
 | Serial port         +-+                       +-+  AVR instrument        |
 |                     |      +----------+         |                    |
 |                  TX +------+   4k7    +---------+ UPDI               |
 |                     |      +----------+    |    |                    |
 |                     |                      |    |                    |
 |                  RX +----------------------+    |                    |
 |                     |                           |                    |
 |                     +--+                     +--+                    |
 +---------------------+  |                     |  +--------------------+
                         +-+                   +-+
                         GND                   GND


We created a minimalistic breakout board for the Attiny406. The board would possibly perhaps perchance also be powered by 5V through USB or a decrease 3.3V through dedicated VCC/GND pins. An LED and a button had been also fitted on the board. For attempting out capabilities, we made up our minds to embed the 4.7K resistor wanted for the UPDI programming straight within the hardware (i.e. resistor R2).
This affords us the following schematic:



The resulting breakout board is exiguous and matches with ease on a little breadboard. The originate recordsdata are shared on aisler.ranking.

Programming the Attiny406 on the board with a USB-serial cable is done by connecting the headers on the board edge:



We set up in pyupdi following the instructions supplied on their webpage.

We connected our USB-Serial cable to the board with the 4 dedicated UPDI pins available on the board. Our USB-Serial converter presentations up because the file /dev/tty.usbserial-FTF5HUAV on a MacOS system.

To study that the programmer acknowledges the Attiny406, it’s likely you’ll presumably presumably divulge a grunt reminiscent of the following, adapting the course for the USB-serial converter to your setup:

pyupdi -d exiguous406 -c /dev/tty.usbserial-FTF5HUAV -i

This would possibly perhaps perchance presumably also honest silent result within the following output if all goes neatly:

Instrument info: {'household': 'tinyAVR', 'nvm': 'P:0', 'ocd': 'D:0', 'osc': '3', 'device_id': '1E9225', 'device_rev': '0.1'}

The C compiler

The humble avr-gcc available on macOS with homebrew did no longer appear to leer the Attiny406 as a compiler plan, so we went off to set up the avr-gcc compiler supplied by Microchip, which is available here. Downloading the compiler requires you to create an story on the Microchip web role, which is a itsy-bitsy stressful.

AVR toolchain link

As soon as downloaded, we extracted the supplied archive in a dedicated directory. The bin directory within the archive have to be added to the PATH variable to kind your existence more straightforward. Assuming the downloaded compiler is kept within the directory $HOME/Src/avr8-gnu-toolchain-darwin_x86_64, the PATH would possibly perhaps perchance also be altered by including the following line to your .bash_profile file:

export PATH=$PATH:$HOME/Src/avr8-gnu-toolchain-darwin_x86_64/bin/

Newer Attiny MCUs have to no longer supported out of the field by the Microchip avc-gcc compiler. That it’s likely you’ll presumably even have to bag a dedicated Attiny Instrument Pack from their web role, as proven below:

AVR toolchain link

The resulting downloaded Instrument Pack is called Atmel.ATtiny_DFP.1.6.326.atpack (or identical reckoning on versioning). Even though the extension is .atpack, the file is truly a zip archive. We modified the extension to .zip and extracted the bundle within the directory $HOME/Src/Atmel.ATtiny_DFP.1.6.326 subsequent to the compiler recordsdata.

C program

We created the following program that blinks the LED on pin PB5 of our Attiny board at a frequency of 1Hz.


int main() {
    _PROTECTED_WRITE(CLKCTRL.MCLKCTRLB, 0); // assign to 20Mhz (assuming fuse 0x02 is determined to 2)

    PORTB.DIRSET = (1<<5);
    for (;;) {
        PORTB.OUTSET = (1<<5);
        PORTB.OUTCLR = (1<<5);

The code looks very reminiscent of what it’s likely you’ll look on a classic AVR “blinky” program. One visible trade is utilizing structures to salvage admission to an total lot of registers of the MCU: e.g as an different of surroundings bits in PORTB, you salvage admission to PORTB.DIRSET.

The a complete lot of visible trade is the clock setup code _PROTECTED_WRITE(CLKCTRL.MCLKCTRLB, 0). Out of the field, at reset, the Attiny406 runs at 3.33Mhz, which corresponds to a immoral frequency of 20Mhz with a 6x clock divider utilized. To enable the fleshy 20Mhz urge, the register CLKCTRL.MCLKCTRLB is cleared. Because this register have to be protected in opposition to accidental changes, the Attiny406 requires a particular programming sequence to change it. Fortunately, here’s natively supplied by the macro _PROTECTED_WRITE. Extra crucial functions are available in within the Attiny406 datasheet.

Compared with an STM32 or a SAMD21, the code is blissfully straightforward.


We design shut the following directory constructing where:

  • Src/Atmel.ATtiny_DFP.1.6.326/ is the role of the Microchip Instrument Pack
  • Src/attiny406-take a look at/ is the directory where the code above is kept in a file called main.c

Compiling the code would possibly perhaps perchance also be done by issuing the following grunt inside attiny406-take a look at/ directory,:

avr-gcc -mmcu=attiny406 -B ../Atmel.ATtiny_DFP.1.6.326/gcc/dev/attiny406/ -O3 -I ../Atmel.ATtiny_DFP.1.6.326/comprise/ -DF_CPU=20000000L -o attiny406-take a look at.elf main.c

An -O optimization flag is required to kind the _delay_ms() characteristic calls work successfully, to boot to defining the variable F_CPU to replicate the anticipated chip clock urge. The reduction of the parameters provide the role of the Attiny406 instrument-boom recordsdata we previously extracted from the Instrument Pack.

Uploading the firmware to the MCU requires a conversion to the intel HEX structure and a name to the pyupdi instrument. To contend with all these steps, we created a straightforward Makefile.

ELF=$(notdir $(CURDIR)).elf  
HEX=$(notdir $(CURDIR)).hex

CFLAGS=-mmcu=attiny406 -B ../Atmel.ATtiny_DFP.1.6.326/gcc/dev/attiny406/ -O3
CFLAGS+=-I ../Atmel.ATtiny_DFP.1.6.326/comprise/ -DF_CPU=$(F_CPU)
LDFLAGS=-mmcu=attiny406 -B ../Atmel.ATtiny_DFP.1.6.326/gcc/dev/attiny406/

all:    $(HEX)  

$(ELF): $(OBJS)
                $(LD) $(LDFLAGS) -o [email protected] $(OBJS) $(LDLIBS)

$(HEX): $(ELF)
                avr-objcopy -O ihex -R .eeprom $< [email protected]

flash:  $(HEX)
                pyupdi -d exiguous406 -c /dev/tty.usbserial-FTF5HUAV -f attiny406-take a look at.hex

                pyupdi -d exiguous406 -c /dev/tty.usbserial-FTF5HUAV -fr

                rm -rf $(OBJS) $(ELF) $(HEX)

To compile the code, we merely kind kind. Uploading is done with kind flash. This Makefile would possibly perhaps perchance also be extra enhanced as wanted.


With the factual instruments, baremetal programming on the contemporary TinyAVR MCUs is as straightforward as on its older AVR cousins.

Whenever you have gotten programming tips for the AVRTiny, please part them with us on on Twitter or within the comments below.



Or no longer it’s gargantuan that someone else is doing things with these MCUs, I’ve dilapidated them in some latest projects and they’re gargantuan, I’m utilizing the attiny1617 in my main projects.

I wrote a script to automate setup of the toolchain here:


Which I judge helps the 406. I haven’t tried it on a Mac, the predominant focal level is Linux.

I’m delighted that I don’t wish to exhaust any love libraries (except for the avr C library and their very minimal runtime / startup code) and it be all straightforward sufficient that I will disassemble my firmware and look precisely what’s going on.

Be conscious Robson, about 1 hour within the past

Leave a observation

Read Extra

Most Popular

Researchers At Large Hadron Collider Are Confident To Make Contact With Parallel Universe In Days

 the astoundingly complex LHC “atom smasher” at the CERN center in Geneva, Switzerland, are fired up to its maximum energy levels ever in an...

Jerry Sloan remembered by fellow coaches Don Nelson, Phil Jackson, Lenny Wilkens

4:34 PM ETJackie MacMullanESPN Senior Writer CloseAward-winning columnist and authorRecipient of Basketball Hall of Fame Curt Gowdy Media AwardJoined ESPNBoston.com in 2010Don Nelson, the winningest coach in NBA history, had just finished a stroll on the beach in his beloved Hawai'i on Friday when a reporter informed him that his coaching contemporary Jerry Sloan had…

Underground nanny economy, racism, exploitation amplified by pandemic

Business Insider talked to 16 nannies and some parents to see how questionable working conditions have changed, in many ways for worse, since March. The nanny industry is an unregulated "gray market" that predominantly employs women of color who aren't citizens. Because there's so little oversight and so few protections, nannies have little recourse in dealing…

The covid-19 pandemic is forcing a rethink in macroeconomics

Editor’s note: Some of our covid-19 coverage is free for readers of The Economist Today, our daily newsletter. For more stories and our pandemic tracker, see our hubIN THE FORM it is known today, macroeconomics began in 1936 with the publication of John Maynard Keynes’s “The General Theory of Employment, Interest and Money”. Its subsequent…