Skip to content

Week 04 - Group assignment

Introduction

The goal was rather broad as I understood it: we had to compare various MCU / boards, not only in theory but also by programming them.

I focused on the following aspects:

  • The programming workflow
  • The pinout, specifically the PWM outputs
  • The flash and sarm memory

Since I was out of office the days I worked on it, I only used commercial boards I had at my home, plus my ATtiny. I was then able to program and compare :

  • my ATtiny1614 custom circuit (designed by my Instructor)
  • a D1 mini dev board from A-Z circuit with an ESP8266EX
  • a Node MCU 1.0 Amica board, also with an ESP8266EX
  • a Feather Huzzah 32 board from Adafruit, with an ESP-WROOM-32
  • an Arduino UNO with an ATMega328p

I kept only the NodeMCU board and not the D1 mini dev board in my comparision summary as they work similarly. Also I had issues with the D1 mini dev board at the end of my eletronic work hours when I decided to get back to my board comparision, so I just carried on with the NodeMCU.

boards

Testing workflow

boards manager

Finding the correct boards familiy package, installing it, picking the correct board / mcu. The workflow was the same for every board as they are all commercial boards with everythhing necessary build in. They could all directly be plugged to the computer's USB port with a USB cable.

Here are all the URL and boards I picked:

Board / MCU Installing packages url Board package name Board name
ATtiny1614 http://drazzy.com/package_drazzy.com_index.json megaTinyCore ATtiny3224/1624/1614/1604/824/814/804/424/414/404/214/204
Adafruit Feather Huzzah32 https://raw.githubusercontent.com/espressif/arduino-esp32/gh-pages/package_esp32_index.json esp32 Adafruit ESP32 Feather
D1 mini dev board ESP8266 http://arduino.esp8266.com/stable/package_esp8266com_index.json esp8266 LOLIN (WEMOS) D1 R2 & mini
Node MCU Amica ESP8266 http://arduino.esp8266.com/stable/package_esp8266com_index.json esp8266 Node MCU 1.0 (ESP-12E Module)
Arduino UNO none Arduino Arduino UNO

pinout diagrams

I had a couple of LEDs and few resistors at home, I hopefully found a 390 ohm resistor to protect my red led. I was then able to make a simple test circuit.

I wanted to uploaded identical sketches to the various boards and wanted to carry on with the led brigthness pwm controlling, so whichever the board I needed to connect my LED to a pin working as a PWM output.

I thus looked for the various pinout diagrams. Here is an example with the Feather Huzzah32 pinout.

pinout diagram

uploading sketches and comparing the use of the memory

I then uploaded the sketches to the various boards, changing only the LED pin, and wrote down informations about the memory.

You can find the results on this Notion page (to be integrated later).

Sketch used

I mainly had two sketches to test, but did some variations to use a function allowing me to calculate the free SRAM.

1. board_led_pwm_serial

Controls a red LED through Serial Monitor thanks to PWM. Enter a brightness percentage (number between 1 and 100) to light up the LED. PIN number changes according to the board. You must check the pinout diagram to be sure you’re using a PWM pin !

//Arduino Uno
#define LED_R 3  //PWM according to the pinout

int del = 1000;  //delay

void setup() {

  pinMode(LED_R, OUTPUT);
  digitalWrite(LED_R, LOW);
  Serial.begin(9600);

}

void loop() {
 if (Serial.available()) {
    float brightness = Serial.parseInt(); // enter brightness in % (between 1 and 100)
    int state = int(brightness/100.0*255.0);
    if ((state < 256) & (state > 0)) {

      analogWrite(LED_R, state);
      Serial.println("LED R turned at " + String(brightness) + " % brightness");
      delay(del);
      delay(del);
    }
  }

}

2. board_led_pwm_serial_sram

add instruction to evaluate the SRAM left

//Arduino Uno
#define LED_R 3  //PWM according to the pinout

int del = 1000;  //delay

void setup() {

  pinMode(LED_R, OUTPUT);
  digitalWrite(LED_R, LOW);
  Serial.begin(9600);

}

void loop() {
 if (Serial.available()) {
    float brightness = Serial.parseInt(); // enter brightness in % (between 1 and 100)
    int state = int(brightness/100.0*255.0);
    if ((state < 256) & (state > 0)) {

      analogWrite(LED_R, state);
      Serial.println("LED R turned at " + String(brightness) + " % brightness");
      delay(del);
      display_freeram(); // evaluate the freeram
      delay(del);
    }
  }

}

void display_freeram() {
  Serial.print(F("- SRAM left: "));
  Serial.println(freeRam());
}

int freeRam() {
  extern int __heap_start,*__brkval;
  int v;
  return (int)&v - (__brkval == 0  
    ? (int)&__heap_start : (int) __brkval);  
}

3. board_led_random

No serial communication nor float nor calculation, the brightness value is given by a random function (integer between 1 and 255)

//Arduino Uno
#define LED_R 3  //PWM according to the pinout
int del = 1000;  //delay

void setup() {

    pinMode(LED_R, OUTPUT);
    digitalWrite(LED_R, LOW);

}

void loop() {

  int state = random(1,255);
  analogWrite(LED_R, state);
  delay(del);

}

4. board_led_random_sram

add instruction to evaluate the SRAM left

//Arduino Uno
#define LED_R 3  //PWM according to the pinout
int del = 1000;  //delay

void setup() {

    pinMode(LED_R, OUTPUT);
    digitalWrite(LED_R, LOW);
  Serial.begin(9600);

}

void loop() {

  int state = random(1,255);
  analogWrite(LED_R, state);
  display_freeram();
  delay(del);

}

void display_freeram() {
  Serial.print(F("- SRAM left: "));
  Serial.println(freeRam());
}

int freeRam() {
  extern int __heap_start,*__brkval;
  int v;
  return (int)&v - (__brkval == 0  
    ? (int)&__heap_start : (int) __brkval);  
}

Comparisons tables

Sketch 1 : board_led_pwm_serial

Board (Led Pin used) max program storage space use of program storage space maximum size in dynamic memory use of dynamic memory by global variables dynamic memory available for local variables
Uno (PIN 3) 32256 bytes 6362 bytes (19%) 2048 bytes 228 (11%) 1820
ATTiny1614 (10 : PA3) 16384 5956 (34%) 2048 bytes 165 (8%) 1883
ESP32 - Feather Huzzah 32 Adafruit (12) 1310720 bytes 267005 bytes (20%) 327680 bytes 22256 bytes (6%) 305424
ESP8266EX - NodeMCU 1.0 (D2) 1048576 bytes 202128 bytes compressed 80192 bytes ?? 28216 bytes (35%)

example (Uno)

Sketch uses 6362 bytes (19%) of program storage space. Maximum is 32256 bytes.
Global variables use 228 bytes (11%) of dynamic memory, leaving 1820 bytes for local variables. Maximum is 2048 bytes.

Sketch 2 : board_led_pwm_serial_sram

Board (Led Pin used) max program storage space use of program storage space maximum size in dynamic memory use of dynamic memory by global variables dynamic memory available for local variables Free SRAM calculated by display_sram function
Uno (PIN 3) 32256 bytes 6696 bytes (20%) 2048 bytes 228 (11%) 1820 1777
ATTiny 1614 (10) 16834 bytes 6270 bytes (38%) 2048 bytes 165 (8%) 1883 1840

Sketch 3 : board_led_random

Board (Led Pin used) max program storage space use of program storage space maximum size in dynamic memory use of dynamic memory by global variables dynamic memory available for local variables
Uno (PIN 3) 32256 bytes 1636 (5%) 2048 bytes 13 (0%) 2035
ATTiny 1614 (10) 16834 bytes 2567 (15%) 2048 bytes 163 bytes (7%) 1885
ESP32 - Adafruit Feather Huzzah 2 (12) 1310720 bytes 241369 bytes bytes (18%) 327680 bytes 21960 bytes (6%) 305720
ESP8266EX - NodeMCU 1.0 Amica (D2) 1048576 bytes 197835 compressed 80192 bytes 28216 bytes (35%) ?
  • Example of results with the ESP8266EX Node MCU

    . Variables and constants in RAM (global, static), used 28124 / 80192 bytes (35%)
       SEGMENT  BYTES    DESCRIPTION
    ╠══ DATA     1508     initialized variables
    ╠══ RODATA   928      constants       
    ╚══ BSS      25688    zeroed variables
    . Instruction RAM (IRAM_ATTR, ICACHE_RAM_ATTR), used 60583 / 65536 bytes (92%)
       SEGMENT  BYTES    DESCRIPTION
    ╠══ ICACHE   32768    reserved space for flash instruction cache
    ╚══ IRAM     27815    code in IRAM    
    . Code in flash (default, ICACHE_FLASH_ATTR), used 233828 / 1048576 bytes (22%)
       SEGMENT  BYTES    DESCRIPTION
    ╚══ IROM     233828   code in flash   
    esptool.py v3.0
    Serial port /dev/cu.usbserial-0001
    Connecting....
    Chip is ESP8266EX
    Features: WiFi
    Crystal is 26MHz
    MAC: 5c:cf:7f:12:14:85
    Uploading stub...
    Running stub...
    Stub running...
    Configuring flash size...
    Auto-detected Flash size: 4MB
    Compressed 268224 bytes to 197835...
    Writing at 0x00000000... (7 %)
    Writing at 0x00004000... (15 %)
    Writing at 0x00008000... (23 %)
    Writing at 0x0000c000... (30 %)
    Writing at 0x00010000... (38 %)
    Writing at 0x00014000... (46 %)
    Writing at 0x00018000... (53 %)
    Writing at 0x0001c000... (61 %)
    Writing at 0x00020000... (69 %)
    Writing at 0x00024000... (76 %)
    Writing at 0x00028000... (84 %)
    Writing at 0x0002c000... (92 %)
    Writing at 0x00030000... (100 %)
    Wrote 268224 bytes (197835 compressed) at 0x00000000 in 17.5 seconds (effective 122.9 kbit/s)...
    Hash of data verified.
    
    Leaving...
    Hard resetting via RTS pin...
    

Sketch 4 : board_led_random_sram

Board (Led Pin used) max program storage space use of program storage space maximum size in dynamic memory use of dynamic memory by global variables dynamic memory available for local variables Free SRAM calculated by display_sram function
Uno (PIN 3) 32256 bytes 2878 (8%) 2048 bytes 196 bytes (9%) 1852 1813
ATTiny1614 (10) 16834 bytes 2567 (15%) 2048 bytes 163 bytes ( 7%) 1885 1848
  • Example of what display_sram returns in the serial monitor :
- SRAM left: 1813
  • Example of results

    ATTiny1614

    Sketch uses 2567 bytes (15%) of program storage space. Maximum is 16384 bytes.
    Global variables use 163 bytes (7%) of dynamic memory, leaving 1885 bytes for local variables. Maximum is 2048 bytes.
    

Pinouts

ATtiny 1614

http://fabacademy.org/2021/labs/barcelona/students/denise-rey/images/week06/attiny16.jpg

ESP32 - Adafruit Feather Huzzah32

Board : select Adafruit ESP32 Feather

https://makeabilitylab.github.io/physcomp/esp32/assets/images/AdafruitHuzzah32PinDiagram.png

The Huzzah32 has 21 GPIO pins; PWM is possible on every GPIO Pins. However pins 34 (A2), 39 (A3), 36 (A4) are not output-capable.

(source)

ESP8266EX - Node MCU 1.0 Amica

https://www.teachmemicro.com/wp-content/uploads/2018/04/NodeMCUv1.0-pinout.jpg

  • Installing URL

From File > Preferences, add a Boards Manager URL http://arduino.esp8266.com/stable/package_esp8266com_index.json

Arduino UNO

https://www.electronicshub.org/wp-content/uploads/2021/01/High-Res-Arduino-UNO-Pinout.jpg

Remarks

For the ATTiny 1614 you need to upload code with a UPDI programmer. The one my instructor made me also allows me to have some serial communication between my board and my computer, thanks to FTDI.

Flash Memory Measurement

Flash memory on Arduino® boards can be measured with the help of the Arduino IDE. As stated before, Flash memory is where the application code is stored; the Arduino IDE reports Flash memory usage through its compiler output console to let developers know how much Flash memory resources are being used. —> see previously after compiling

SRAM Memory Measurement

Sometimes, there are situations where even when code is compiled and uploaded successfully by the IDE into a board, it suffers from sudden halts. These issues are likely due to memory resource-hogging or insufficient memory to allocate. It is necessary to understand which code sector the memory demand is going beyond the available resources to solve this. The following example code can be used to measure SRAM usage in AVR-based Arduino® boards:

void display_freeram() {
  Serial.print(F("- SRAM left: "));
  Serial.println(freeRam());
}

int freeRam() {
  extern int __heap_start,*__brkval;
  int v;
  return (int)&v - (__brkval == 0  
    ? (int)&__heap_start : (int) __brkval);  
}
source : Arduino Memory Guide