Skip to content

Programming the ATTiny

jbaumann edited this page Jun 20, 2019 · 17 revisions

The Environment

I assume that you have some familiarity with the Arduino IDE, because we will use it to program the ATTiny. You might ask, why not a real development IDE, but an underlying decision was to make building this thing as simple as possible, and this means the Arduino IDE.

If you have never programmed a bare-bones micro-controller before then the following will be a new experience, but the process is as simple as programming an Arduino Nano, even though there are a few more steps.

IDE Installation

First download and install the Arduino IDE.

Then install the additional board definition by https://github.com/damellis/attiny (there should be enough tutorials if you do not yet know how to do it).

Finally we need an I2C implementation for the USI of the ATTiny. This can be found here: https://github.com/puuu/USIWire

Versions

Most probably the compiled software will work with newer version of the libraries and Arduino IDE as well, but I worked with the following version:

  • The Arduino IDE version used is 1.8.9
  • The ATTiny Board definition provided by https://github.com/damellis/attiny hasn‘t been changed since 2016. No release exists, but I do not assume that it will change anytime soon.
  • The USIWire-implementation used is provided by https://github.com/puuu/USIWire, and I use the release 0.1.0.

Interestingly, when I tried the ATTinyCore by https://github.com/SpenceKonde/ATTinyCore, it generated a lot of problems when reading over the I2C. I will follow up on that later, but currently you have to install a board variant and a library instead of the integrated solution. As an interesting side note, the produced firmware was larger and needed more SRAM. Since the current version already is at about 90% of the available flash it might be more prudent to stay with the above solution even if it would be possible to get a running version with the ATTinyCore.

Compiling the Code

Download all the files in the firmware subdirectory and open the corresponding directory in your Arduino IDE. Now change the target to „ATTiny 25/45/85“, change the processor to „ATTiny45“, and choose „8MHz(internal)“ as the processor frequency. Hit the compile button and the result should be a firmware that needs around 89-90% of the available flash and 36% of the SRAM (we need some space here because we use a lot of function calls and local variables).

In the main file ATTiny_Daemon there are a number of variables that are pre-initialized with sensible values. These can be changed, but I would advise against it, because they are safe for the first start and can later be changed without problems from the Raspberry. Most of these will be written to the EEPROM on the first start to survive resets of the ATTiny.

Uploading the code to the ATTiny

The last step is to send the code to the chip. I recommend to use an USBASP in-circuit programmer which is amazingly simple to use. But if you have e.g. an Atmel AVRISP mk.II then that works equally well. Simply choose the correct programmer in the IDE and press the Upload button.

First Test (aka smoke test)

If the upload was successful, remove the chip and place it on your board. This could be either a breadboard if you want to experiment, could be a proto board you soldered or the PCB that is provided in the hardware folder.

Now, without connecting your board to the Raspberry, simply provide 3.3-5V to using the GND and Battery pins. The LED should start to flash in 1 second intervals. If that is the case, everything should work and you can plug it into the Raspberry Pi.

Clone this wiki locally