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 some basic exercises using the LED and button on board, in Arduino and C as General Purpose Input/Output (GPIO). In these codes, the button makes use of the built-in pull-up resistors.

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

Turn LED On

This is a code consisting of a button and a led, the led is going to be On during the time that the button is pressed.

Arduino 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
/* 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, 
 * the led is going to be On during the time that the button is pressed.
 * 
 * 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 = 1; // 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 == LOW) // Check if button is pressed
  { // If button is pressed, Turn on LED
    digitalWrite(led, HIGH); // Turn led on
  } else {
    digitalWrite(led, LOW);} // If button is not pressed, keep the LED off
}

C 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
/* 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, 
 * the led is going to be On during the time that the butotn is pressed.
 * 
 * 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>

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, Turn on LED
      PORTA.OUT |= PIN3_bm;  // Turn led on
    } else {
      PORTA.OUT = ~PIN3_bm;} //If button is not pressed, keep the LED off
  }
  return (0);
}

Performance

Turn LED On and Off

This is a code consisting of a button and a led.

- If led is On, pressing the button will turn it off.
- If led is Off, pressing the button will turn it on.

Arduino 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 led is On, pressing the button will turn it off. 
 * If led is Off, pressing the button will turn it on.  
 * 
 * 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
byte lastButtonState = 0; // previous state of the button
int PressCounter = 0;      // Button press counter

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 != lastButtonState) { // Compare buttonState to lastButtonState
    if (buttonState == HIGH) { // If the current state is HIGH, button was pushed
      PressCounter++;}
    delay(25);}
  lastButtonState = buttonState; // Save the current state as lastButtonState, for next comparison

  if (PressCounter % 2 == 0) {
    digitalWrite(led, HIGH); // If modulo = 0, turns on the LED.
  } else {
    digitalWrite(led, LOW); //If modulo != 0, turns off the LED.
  }
}

Performance

Blinking LED

This is code 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 > 1sec ON > 0.5secs OFF > 1sec ON > turn OFF.

Arduino 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
/* 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 > 1sec ON > 0.5secs OFF > 1sec ON > turn OFF.
 * 
 * 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 = 1; // 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 == LOW) // Check if button is pressed
  { // If button is pressed, do the blinking sequence
    for (int i=0; i<=2; i++){
    delay(500);              // Wait for half a sec
    digitalWrite(led, HIGH); // Turn led on
    delay(1000);             // Wait for a sec
    digitalWrite(led, LOW);} // Turn led off
  } else {
    digitalWrite(led, LOW);} // If button is not pressed, keep the LED off
}

C 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
/* 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 > 1sec ON > 0.5secs OFF > 1sec ON > turn OFF.
 * 
 * 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
      for (int i=0; i<=3; i++){
      _delay_ms(500);         // Wait for half a sec
      PORTA.OUT |= PIN3_bm;   // Turn led on
      _delay_ms(1000);        // Wait for a sec
      PORTA.OUT &= ~PIN3_bm;} // Turn led off
    } else {
      PORTA.OUT = ~PIN3_bm;} //If button is not pressed, keep the LED off
  }
  return (0);
}

Performance

Long and short press

This is code consisting of a button and a led, the led is going to blink 4 times depending on how long the button is pushed.

- If there is a long press (more then 2 secs), the blinking with be 1sec long.
- If there is a short press (less then 2 secs), the blinking with be 0.25secs long.

C 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
38
39
40
41
42
43
44
45
46
47
48
49
/* 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, the led 
 * is going to blink 4 times depending on how long the button is pushed.
 * 
 * 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
  uint8_t counter = 0;

  while (true)
  {
    if(~PORTA.IN & PIN2_bm) // Check if button is pressed
    {
      while(~PORTA.IN & PIN2_bm) { // While button is being pressed...
        _delay_ms(100); // Add 1 to the counter every 0.1 secs
        counter++;}
     }

      if (counter > 0){
        if(counter>=20) {
          for(uint8_t i = 0; i < 4 ; i++)
           {_delay_ms(1000);        // Wait for a sec
            PORTA.OUT |= PIN3_bm;   // Turn led on
            _delay_ms(1000);        // Wait for a sec
            PORTA.OUT &= ~PIN3_bm;} // Turn led off
          }
        else{
          for(uint8_t i = 0; i < 4 ; i++)
           {_delay_ms(250);         // Wait for 0.25 secs
            PORTA.OUT |= PIN3_bm;   // Turn led on
            _delay_ms(250);         // Wait for 0.25 secs
            PORTA.OUT &= ~PIN3_bm;} // Turn led off
          }
      } else {PORTB.OUT &= ~PIN5_bm;}
      counter = 0;
  }
  return (0);
}

Performance

Fading LED

This is a code consisting of a button and a led, if the button is pressed, the led is going fade in and fade out one time.

Arduino 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
38
/* 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 fade in and fade out one time. 
 * 
 * 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 = 1; // 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 == LOW) // Check if button is pressed
  { // If button is pressed, Turn on LED
    delay(500);

    // fade in from min to max in an increments of 5
    for (int i=0; i<=255; i+=5){
    analogWrite(led, i);
    delay(50);}

    // fade out from max to min in a decrement of 5
    for (int i=255; i>=0; i-=5){
    analogWrite(led, i);
    delay(50);}

  } else {
    digitalWrite(led, LOW);} // If button is not pressed, keep the LED off
}

Performance

Files and references

Turn LED On exercise
- ArduinoPushOn.ino
- CPushOn.ino

Turn LED On and Off exercise
- ArduinoPushOnAndOff.ino

Blinking LED exercise
- ArduinoBlinking.ino
- CBlinking.ino

Long and short press exercise
- CLongShort.ino

Fading LED
- ArduinoFading.ino

Support documentation:
- AddOhms. Pull Up Resistor Tutorial
- ATtiny1614 DataSheet.pdf
- Getting Started with GPIO.pdf


Last update: June 27, 2021