Embedded Programming


Before starting to program I had to read the datasheet of the microcontroller again in order to understand better the usage of the pins so that I can program the board. From reading the datasheet, I learnt that there are three different kinds of ports. There are DDRx for the Data Direction Register, PINx for the Pin Input Register, and PORTx for Pin Output Register. Also, every port works differently.


After reading the definitions of the PORTs and pins and understanding how to use them, I used the pinouts of my microcontroller to control an external LED. As the previous week, I only embedded an LED for the indication of the power on my board, I had to add an external LED in order for me to be able to control it. I added the LED by connecting one side of it to the GND and the other end to a 499 Ohm resistor and to a digital pin of the microcontroller.

After adding the external LED, I was ready to program my board. For the programming I used the digital pin PA1 for the LED and PA2 for the button.

I tried the Arduino language and C. For the Arduino language I used the blink code by searching for the corresponding pin of the ATtiny to Arduino. PA1 in Arduino corresponds to the digital pin 1, so that pin was used for the Arduino programming. In C I used both the button and the LED. I programmed it so that every time I would press the button, the LED would turn on in case it was turned off and vice versa.

Below, you can find the program of C:

/* Test code in C language for Fab Academy */

// including pinout library and delay library

#include <avr/io.h>
#include <util/delay.h>

//defining the frequency of the external clock
#define F_CPU 20000000UL

//main program will be executed here
int main(void){
  //set the LED DDR to output
  //PA2 => button pin
  //PA1 => led pin
  // 0 is input whereas 1 is output

  DDRA = 0b00000010;

  // entering on a loop
  /*bit_is_clear tests whether bit bit in IO register
  sfr is clear. This will return non-zero if the bit is clear,
  and 0 if the bit is set.  */
  if (bit_is_clear(PINA, PA2)){

  if (bit_is_clear(PINA, PA2))

 //this function will be called to the main program   
 void switchLED(){
    if(PORTA == 0b00000010){  //if the led is on
      PORTA = 0b00000000;   //switch the led off
    if(PORTA == 0b00000000){  //if the led is off
      PORTA = 0b00000010;     //switch the led on


Here, you can find the Arduino program:

/*Program in Arduino language */

const int buttonPin = 2;     // the pin of the button 
const int ledPin =  1;      // the pin of the LED pin

int buttonState = 0;  // variable for reading the pushbutton status

void setup() {
  // defining LED pin as an output
  pinMode(ledPin, OUTPUT);
  // defining the button as an input
  pinMode(buttonPin, INPUT);

void loop() {
  //reading of the button state
  buttonState = digitalRead(buttonPin);

  //if button pressed then turn led on
  if (buttonState == HIGH) {
    digitalWrite(ledPin, HIGH);
  //if pressed again then turn off
  else {
    digitalWrite(ledPin, LOW);

For the compiling of both programs, I used the Arduino IDE. For the programming of the board, I followed the steps mentioned on the "Electronics Production" week. So, I was using Arduino as a programmer. Again, a mistake I almost did multiple times was to upload the program using the GUI symbol BUT the program should be uploaded using "Upload using Programmer" under the Sketch option.

Here are the results of both programs: