Skip to content

9. Embedded programming | Blinking LED

- Compare the performance and development workflows for different microcontroller families. Document your work (in a group or individually):

My individual part:

When it comes to special applications that need calculations and good speed, it’s good to have a reference to know what controller meets our needs the most.

For this task we have chose 3 microcontrollers: ATtiny1614, Arduino UNO (ATMEGA328P), and Arduino DUE (AT91SAM3X8E) cause they are the most used in the community.

As a theoretical comparison:

Microcontroller Estimated MFlops Flash. Program Memory Size
ATtiny1614 0.1 16 KB
ATMEGA328P 0.08 32 KB
AT91SAM3X8E 0.7 512 KB

Group assignment page: here, which contains also floating point operations per second (FLOPS, flops or flop/s) test for these 3 microcontrollers.

- Read the datasheet for the microcontroller you are programming:

As we have done for the Electronics production assignment, we have read the datasheet of the microcontroller (the ATtiny1614 in this case) and we have learned what components we need for each pin, it’s capabilities and how to program it.

- Program the board you have made to do something, with as many different programming languages and programming environments as possible:

I have done a simple code in Arduino and also in C, consisting of a button and a LED, if the button is pressed, the led is going do a quick blinking with the following sequence: 1sec ON > 0.5secs OFF >0.5secs ON > Turn off completely.

Board used

For this assignment I programmed the board that I made during the Electronics design week. This section is mostly a modified part of that previous documentation.

For further details, check my Electronics design and Electronics production assignments.

Features

Based on the ATtiny1614 and inspired by the HelloD1614 board.

Features

ATtinyX14 pinout

For this practice I used the ATtiny1614 from the ATtinyX14 series, but since they have the same pinout, any of them can be used on this board.

ATtinyPinOut

I added the indication of the pin numbers we are going to be using in the different programming languages.

Coding pins

The following table describes the relationship between the board pinout (including onboard LED and Button) described above and the microcontroller pin number for programming:

Board Pinout Arduino p. pin C programming pin
1_DP 8 PA1
2_DP 3 PA7
3_DAC 2 PA6
4_PWM 1 PA5
5_PWM 0 PA4
Tx 4 PB3
Rx 5 PB2
UPDI 11 PA0
SDA 6 PB1
SCL 7 PB0
LED_Out 10 PA3
Button 9 PA2

Connection

I connected it via UPDI using the FTDI-USB and UPDI-3 boards that I manufactured during the Electronics production week:

Connection

It’s recommended to use an extension cable or an USB hub for connecting to the PC.

Upload a Code

  • To upload a code to the Board, we just need to use the following highlighted settings in the Arduino IDE:
    IDE2

  • After selecting these options and when testing the board by the first time, press Burn Bootloader to setup the Microcontroller; then you can just press the Upload button:
    Upload

Coding

This is just a simple testing code in Arduino consisting of a button and a led, if the button is pressed, the led is going do a quick blinking with the following sequence:
1sec ON > 0.5secs OFF >0.5secs ON > Turn off completely.

I made both of them in the Arduino IDE.

By checking the “Coding pins” table in the section described above, we know what pins correspond to LED_Out and Button.

in Arduino

Complete code

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
/* Made by Jefferson Sandoval during the Embedded programming for the FabAcademy2021
 *  
 * This is just a simple testing code in Arduino consisting of a button and a led, if the 
 * button is pressed, the led is going do a quick blinking with the following sequence: 
 * 1sec ON > 0.5secs OFF >0.5secs ON > Turn off completely.
 * 
 * This code was uploaded to a board with an Attiny1614 microcontroller. Documentation:
 * http://fabacademy.org/2021/labs/kamplintfort/students/jefferson-sandoval/assignments/week09/
 */

const int button = 9; // Set number of the button pin
const int led =  10;  // Set number of the LED pin
byte buttonState = 0;  // Variable for button status

void setup() {
  pinMode(led, OUTPUT);   // Set LED as an output
  pinMode(button, INPUT_PULLUP); // Set button as an input with PullUp resistors configuration
}

void loop() {
  buttonState = digitalRead(button); // Read state of the button value

  if (buttonState == HIGH) // Check if button is pressed
  {
    // If button is pressed, do the blinking sequence
    digitalWrite(led, HIGH); // Turn led on (HIGH)
    delay(1000);             // Wait for a second
    digitalWrite(led, LOW); // Turn led off (LOW)
    delay(500);             // Wait for half a second
    digitalWrite(led, HIGH); // Turn led on (HIGH)
    delay(500);             // Wait for half a second
    digitalWrite(led, LOW); // Turn led off (LOW)
  } else {
    digitalWrite(led, LOW); // If button is not pressed, keep the LED off (LOW) 
  }
}

Code description

Without going trough commands description details:

Before the main parts, we can writ a section to declare variables and include libraries. In this case I just declared the variables for the pins, then it’s easier to make changes later, also initialized a buttonState to read/check the state of the button.

According to our Coding pins table, we know that:
- LED pin = 10
- Button pin = 9

1
2
3
const int button = 9; // Set number of the button pin
const int led =  10;  // Set number of the LED pin
byte buttonState = 0;  // Variable for button status

Setup

The setup() function is called when a sketch starts. Use it to initialize the variables, pin modes, start using libraries, etc. The setup function will only run once, after each power up or reset of the Arduino board.

In this case, I just needed to set up the Led as an output, and the button as an input:

1
2
3
4
void setup() {
  pinMode(led, OUTPUT);   // Set LED as an output
  pinMode(button, INPUT_PULLUP); // Set button as an input with PullUp resistors configuration
}

Loop

After creating a setup() function, which initializes and sets the initial values, the loop() function does precisely what its name suggests, and loops consecutively, allowing your program to change and respond. Use it to actively control the Arduino board.

In this case I read/check first the state of the button, and control the led.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
void loop() {
  buttonState = digitalRead(button); // Read state of the button value

  if (buttonState == HIGH) // Check if button is pressed
  {
    // If button is pressed, do the blinking sequence
    digitalWrite(led, HIGH); // Turn led on (HIGH)
    delay(1000);             // Wait for a second
    digitalWrite(led, LOW); // Turn led off (LOW)
    delay(500);             // Wait for half a second
    digitalWrite(led, HIGH); // Turn led on (HIGH)
    delay(500);             // Wait for half a second
    digitalWrite(led, LOW); // Turn led off (LOW)
  } else {
    digitalWrite(led, LOW); // If button is not pressed, keep the LED off (LOW) 
  }
}

in C

Complete code

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
/* Made by Jefferson Sandoval during the Embedded programming for the FabAcademy2021
 *  
 * This is just a simple testing code in C consisting of a button and a led, if the 
 * button is pressed, the led is going do a quick blinking with the following sequence: 
 * 1sec ON > 0.5secs OFF >0.5secs ON > Turn off completely.
 * 
 * This code was uploaded to a board with an Attiny1614 microcontroller. Documentation:
 * http://fabacademy.org/2021/labs/kamplintfort/students/jefferson-sandoval/assignments/week09/
 */

#define F_CPU 3333333 //equals the frequency (20MHz) divided by 6
#include <avr/io.h>
#include <util/delay.h>

int main(void){

    PORTA.DIR |= PIN3_bm;  // Set LED as an output
    PORTA.DIR &= ~PIN2_bm; // Set button as an input
    PORTA.PIN2CTRL = PORT_PULLUPEN_bm; // Set PullUp resistors configuration

  while (true) {
    if(~PORTA.IN & PIN2_bm) // Check if button is pressed
    { 
      // If button is pressed, do the blinking sequence
      PORTA.OUT |= PIN3_bm;   // Turn led on
      _delay_ms(1000);        // Wait for a second
      PORTA.OUT &= ~PIN3_bm;  // set A1 to 0
      _delay_ms(500);         // Wait for half a second
      PORTA.OUT |= PIN3_bm;   // Turn led on
      _delay_ms(500);         // Wait for half a second
      PORTA.OUT &= ~PIN3_bm;  // Turn led off
    } else { 
      PORTA.OUT = ~PIN3_bm;  // If button is not pressed, keep the LED off
    }     
  } 
    return (0);
} 

Code description

Without going trough commands description details:

Before the main part, I wrote a section to include the libraries:

1
2
3
#define F_CPU 3333333
#include <avr/io.h>
#include <util/delay.h>

Main

Here is the section where the whole code is written. To indicate it, we use the function int main(void)

According to our Coding pins table, we know that:
- LED pin = PA3
- Button pin = PA2
Then I started with that declaration:

1
2
3
PORTA.DIR |= PIN3_bm;  // Set LED as an output
PORTA.DIR &= ~PIN2_bm; // Set button as an input
PORTA.PIN2CTRL = PORT_PULLUPEN_bm; // Set PullUp resistors configuration

To make a loop, I used while (true)

Finally, I read/checked first the state of the button, and according to it, control the Led:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
if(~PORTA.IN & PIN2_bm) // Check if button is pressed
    { 
      // If button is pressed, do the blinking sequence
      PORTA.OUT |= PIN3_bm;   // Turn led on
      _delay_ms(1000);        // Wait for a second
      PORTA.OUT &= ~PIN3_bm;  // set A1 to 0
      _delay_ms(500);         // Wait for half a second
      PORTA.OUT |= PIN3_bm;   // Turn led on
      _delay_ms(500);         // Wait for half a second
      PORTA.OUT &= ~PIN3_bm;  // Turn led off
    } else { 
      PORTA.OUT = ~PIN3_bm;  // If button is not pressed, keep the LED off
    }     
  } 

Performance

Files and references

- ATtiny1614DataSheet.pdf
- ArduinoCode.ino
- CCode.ino

- Getting Started with GPIO


Last update: June 21, 2021