Skip to content

9. Embedded programming

In Embedded Programming week I reprogrammed the ATtiny44 version of the echo board I had designed in KiCad for Electronics Design week. After designing and making the board in week 07, I programmed it using an Arduino as ISP programmer. To slightly streamline my setup and finally show a working programmer, I made an ISP programmer this week, too, programmed it, tested it and used it for reprogramming my own board.

Background - Embedded Programming

I started learning about Embedded Programming using an Arduino Duemilanove. As the name suggests, the Duemilanove, meaning 2009 in Italian, was released in 2009, which is when I got one, too. Embedded programming led me to electronics, PCB making, robotics, changing my degree from Biology to Bionics/Biomimetics. I can still remember the epiphany when I realized how simple microcontroller/embedded programming could be and what endless possibilities this opened up for me. I never really got deep into optimization, since the Arduino-ish high-level language was mostly enough for my projects. Ever since the beginning, I did small projects using microcontrollers and/or taught Arduino-style microcontrollers during my times as student assistant and on the job.

Status Quo

When I left my ATtiny 44 echo board, I had programmed it to blink the LED I had added in regular intervals. I had also set up the fuses so it would use the 20 MHz crystal using avrdude and tried burning the bootloader - which did work, but didn’t actually receive new programs over the Serial Port due to the bootloader’s Soft Serial pins (the ATtiny 44 does not have a hardware UART - for full specs see datasheet here or for download in the Reference Files section) not matching the ones from Neil’s board design.

To New Lands

Using the Arduino IDE, I rewrote the firmware for my ATTiny 44 echo board to flash “SOS” in Morse code using the LED. To that end, I implemented simple functions for the dit (the short blip in Morse code) and the dah (the long blip in Morse code). Finally, I also added a function for morsing “SOS” using the proper timing of Morse code (thanks, Wikipedia!):

  • dit is one time unit long
  • dah is three units long
  • there’s a pause of one dit between dits and dahs of a letter
  • there’s a pause of three dit between two letters
  • there’s a pause of seven dit between two words
  • in my code, the time base unit is arbitrary and can be set by a user variable
  • the morse code is also only executed, when the button is not pressed

Here is the Arduino sketch for doing the morse code:

#include <avr/power.h>

int ledPin = 8;
int buttonPin = 7;

int ditMs = 100;
int dahMs = 3 * ditMs;
int delayBetweenCharacters = 2 * ditMs; //theoretically, this is 3 dit, but 1 dit is already included in the dit and dah functions
int delayBetweenSos = 6 * ditMs;  //theoretically, this is 7 dit, but 1 dit is already included in the dit and dah functions

void setup() {
  clock_prescale_set(0);
  pinMode(ledPin, OUTPUT);
  pinMode(buttonPin, INPUT);
  digitalWrite(buttonPin, HIGH);  //Internal pullup on Button Pin
}

void loop() {
  if(digitalRead(buttonPin) != LOW){
    sosBlink(ledPin);
    delay(delayBetweenSos);
  }
}

void dit(int blinkPin){
  digitalWrite(blinkPin, HIGH);
  delay(ditMs);
  digitalWrite(blinkPin, LOW);
  delay(ditMs);
}

void dah(int blinkPin){
  digitalWrite(blinkPin, HIGH);
  delay(dahMs);
  digitalWrite(blinkPin, LOW);
  delay(ditMs);
}

void sosBlink(int blinkPin){
  dit(blinkPin);
  dit(blinkPin);
  dit(blinkPin);
  delay(delayBetweenCharacters);
  dah(blinkPin);
  dah(blinkPin);
  dah(blinkPin);
  delay(delayBetweenCharacters);
  dit(blinkPin);
  dit(blinkPin);
  dit(blinkPin);
}

Since ATtiny boards are not officially supported by the Arduino IDE per se, support for ATTiny boards has to be added from external sources. There are multiple projects for doing so, one of which I already installed in Electronics Design week - the attiny package by David A. Mellis. In trying to burn a functioning bootloader, this week, I added another package for working with ATtiny boards to the Arduino IDE: ATTiny Core by Spence Konde. Both of these can be easily added to the Arduino IDE using the Boards Manager found in the Boards menu. I had following versions installed in the end:

  • attiny (David A. Mellis) 1.0.2
  • ATTiny Core (Spence Konde) 1.5.2

The pins of the ATtiny are given pin numbers in the Arduino system to correspond with the numbering system of the original Arduino. In David A. Mellis’ attiny package, these pin numbers are assigned clockwise, when looking at the datasheet pinout diagram. In the ATTiny Core package, the user can choose between clockwise and counterclockwise pin numbering. Since I based my code on code already written for the attiny package earlier, with CW pin assignment, I chose the same pin assignment when using the ATTiny Core package this time around. Following schematic from the ATTiny Core project shows the pin number assignments. Because of this, the pin number in Arduino terms for the LED is 8, the one for the button is 7.

Uploading the code to the board

To upload the new morse code sketch to the board, I started out by using the same setup as in Electronics Design week. This was using an Arduino as ISP programmer. The Arduino had the ArduinoISP sketch from the examples section loaded onto it to turn it into an ISP programmer. The ISP port of the programmee - my ATtiny44 echo board - was connected to the Arduino pins 10-13, which were set up in the ArduinoISP sketch to their respective MOSI, MISO, SCK and RST functions. The Arduino also provided 5V and GND for power, which I fed to the ATTiny44 echo board’s FTDI port, as the 2x3 pin header of the ISP port was already very crowded.

After connecting the Arduino in this way and selecting the proper port (Arduino programmer’s COM port) and programmer (“Arduino as ISP”) in the Arduino IDE, the new code could be flashed to the ATtiny44 echo board using the “Upload using Programmer” function. The board started blinking “SOS” right away. Success :)

Here’s a small video of the board blinking SOS (granted, this is the same sketch rewritten using AVR-libc for our group assignment, but functionally they are identical, so I did not want to waste the video file space, twice):

Rewriting the Code in AVR C

We later used above code as a basis for our group assignment, where we compared Arduino programming language code with C code using the AVR-libc library. It turned out that the more low-level AVR-libc C code was way more memory efficient, but also way harder to read and understand from the source code. I decided that for the future, unless I ran into major space limitations or specific requirements (e.g. toggling multiple pins at the exact same time), I would stick with the more high-level languages like the Arduino programming language.

Ditching the ArduinoISP

I had not made my own ISP programmer yet, as at this point, it hadn’t been clear yet whether I’d use ISP or not - so in Electronics Manufacturing week I actually made a UPDI programmer board that I never needed so far. Since I had decided to stick with the ATTiny and the ISP-programmable boards at this point, I made a FabTinyISP programmer this week, too, so I could ditch the Arduino as ISP programmer hack and use my very own programmer from here on out. The making and programming of this FabTinyISP board I added to Week 05 - Electronics Production with a disclaimer about this slight break in time-space continuum :)

Reference Files


Last update: July 22, 2022 11:16:10
Back to top