Skip to content

4. Embeded programming

week 4

   group assignment:
      • demonstrate and compare the toolchains and development workflows
         for available embedded architectures
   individual assignment:
      • browse through the data sheet for a microcontroller
      • write and test a program for an embedded system using a microcontroller
         to interact (with input &/or output devices)
         and communicate (with wired or wireless connections)
      • extra credit: assemble the system
      • extra credit: try different languages &/or development environments

My working plan

1 compare ”masterboard” and “Dfrduino” with same task

2 try to do something with dfrdunio intermediate kit and study esp32 file

Group Assignment: Embedded Programming – Comparative Analysis of AVR (ATmega328P) vs ARM (ESP32)

I plan to focus on the basic interactive scenario of “LED Blinking + Button Control”, and compare the development workflow, performance, and programming differences between these two MCUs of different architectures.

docs/images/week4/1.jpg

First I prepare the hardware for both MCU:

Item DFRduino UNO 3.0 (ATmega328P) DFRobot Control Board (ESP32)
Main Control Board DFRduino UNO 3.0 DFRobot Control Board (ESP32)
USB Cable USB Type-A to USB-B cable USB Type-C cable
Input Module Digital button module Onboard key A
Output Module LED module / Onboard LED (D13) Onboard RGB LED
Connection Wires Dupont wires Not required (onboard peripherals)
Power Supply Powered via USB Powered via USB

alt text alt text

Software Environment Setup

1.Download Arduino IDE (2.3.7)

Visit this website: http://arduino.cc/en/Main/Software

alt text alt text

2.Configure Board Support for DFRobot Control Board (ESP32):

  1. Open the Arduino IDE → Click 「File」→「Preferences」→「Additional Board Manager URLs」;
  2. Paste the ESP32 board URL: https://dl.espressif.com/dl/package_esp32_index.json;
  3. Click 「Tools」→「Board」→「Board Manager」, search for “ESP32”, and install “ESP32 by Espressif Systems” (select the latest stable version);

alt text

3 Program Writing and Flashing for DFRduino UNO 3.0 (AVR)

3.1 Hardware Wiring (UNO Board and extension board)

Button Module: Connect VCC to UNO’s 5V pin, GND to UNO’s GND pin, and DO (Digital Output, signal pin) to UNO’s D2 pin;

LED Module: Connect VCC to UNO’s 5V pin, GND to UNO’s GND pin, and DI (Digital Input, signal pin) to UNO’s D13 pin

alt text

3.2 Code Writing (Copy directly to Arduino IDE)

/*
FabAcademy Group Assignment - AVR (ATmega328P) Test Program
Function: Button-controlled LED (solid on when pressed, blinking when released)
Hardware: DFRduino UNO 3.0
*/
// Define pins
const int btnPin = 2;    // Button connected to D2
const int ledPin = 13;   // LED connected to D13 (onboard LED of UNO)
int btnState = 0;        // Store button state

void setup() {
  // Initialize pin modes
  pinMode(btnPin, INPUT_PULLUP);  // UNO uses internal pull-up resistor (avoids external pull-up)
  pinMode(ledPin, OUTPUT);
  // Initialize serial port (for debugging, required by FabAcademy to record debug info)
  Serial.begin(9600);
  Serial.println("UNO 3.0 (ATmega328P) Program Started");
}

void loop() {
  // Read button state (in INPUT_PULLUP mode, LOW = pressed, HIGH = released)
  btnState = digitalRead(btnPin);

  if (btnState == HIGH) {  // Button pressed
    digitalWrite(ledPin, HIGH);  // LED stays on
    Serial.println("Button Pressed: LED Solid On");
    delay(10);  // Debounce
  } else {  // Button released
    // LED blinks (on/off every 500ms)
    digitalWrite(ledPin, HIGH);
    delay(500);
    digitalWrite(ledPin, LOW);
    delay(500);
    Serial.println("Button Released: LED Blinking");
  }
}

3.3 Flashing Steps (UNO)

Connect the UNO to the computer with a USB cable, then in the Arduino IDE:

Select 「Tools」→「Board」→「Arduino AVR Boards」→「Arduino Uno」;

Select 「Tools」→「Port」→ the serial port corresponding to the UNO (e.g., COM3);

Click 「Verify」 (√) in the top-left corner of the IDE to confirm no code errors;

Click 「Upload」 (→) and wait for flashing to complete ;

alt text

4 Program Writing and Flashing for DFRobot Control Board(ESP32)

4.1 Hardware Connection Verification

Take out DFRobot Control Board, and connect it to a USB port on your computer using the original Type‑C USB data cable

Observe the indicator lights on the Control Board:✅

4.2 Driver Installation ( for Windows)

The serial chip on the Control Board is CP2102. The corresponding driver must be installed for the computer to recognize the board. For my computer driver installed automatically.

Close and reopen Arduino IDE. Go to

File → Preferences

Additional Board Manager URLs, and keep only the official ESP32 URL:

https://dl.espressif.com/package_esp32_index.json

✅ Checkpoint: No extra URLs (multiple URLs can be separated by commas, but beginners are advised to keep only this one).

Go to

Tools → Board → Board Manager

.Search for

ESP32 in the search box, and find ESP32 by Espressif Systems

Select the latest stable version (I pick 2.0.17; avoid the newest beta versions), then click Install

Keep the Control Board connected to the computer. In Arduino IDE:

Go to Tools → Board → ESP32 Arduino→ Select ESP32 Dev Module

(Critical! Many upload failures come from selecting the wrong board).

Set these board parameters (essential):

  • Flash Size: 4MB (32Mb)
  • Upload Speed: 115200
  • Core Debug Level: None
  • Port: Select the serial port corresponding to the Control Board
    • Windows: COM3

4.3 Write Code for the DFRobot Control Board (Adapted to Onboard Buttons / LED)

The Control Board has built-in buttons (Button A = GPIO 39, Button B = GPIO 38) and an RGB LED (Red = 13, Green = 12, Blue = 11).We write code with the

same logic as the UNO

/*
FabAcademy Group Assignment - ESP32 (DFRobot Control Board) Test Program
Function: Button A controls red LED (solid on when pressed, blinking when released)
Hardware: DFRobot Control Board (ESP32)
*/

// Define onboard pins of the Control Board
const int btnAPin = 39;   // Button A pin on Control Board
const int redLedPin = 13; // Red channel of onboard RGB LED
int btnState = 0;         // Store button state

void setup() {
  // Initialize pin modes (internal pull-up resistor)
  pinMode(btnAPin, INPUT_PULLUP);
  pinMode(redLedPin, OUTPUT);

  // Initialize serial port (default baud rate for ESP32: 115200)
  Serial.begin(115200);
  Serial.println("DFRobot Control Board (ESP32) Program Started");
}

void loop() {
  // Read Button A state (INPUT_PULLUP mode: pressed = LOW, released = HIGH)
  btnState = digitalRead(btnAPin);

  if (btnState == LOW) {  // Button A pressed
    digitalWrite(redLedPin, HIGH); // Red LED stays on
    Serial.println("Button A Pressed: Red LED Solid On");
    delay(10); // Button debouncing
  } else {  // Button A released
    // Red LED blinks (500ms on / off)
    digitalWrite(redLedPin, HIGH);
    delay(500);
    digitalWrite(redLedPin, LOW);
    delay(500);
    Serial.println("Button A Released: Red LED Blinking");
  }
}

4.4 Upload the Program to the Control Board (Core Step! Fix Upload Failures)

The ESP32 requires entering download mode to upload code — this is the most common pain point for beginners. Follow these steps:

Click Upload in the top-left corner of Arduino IDE. The status bar will show:Connecting...

text text

I failed with the DF control board since the serial monitor shows something wrong. I try to change pin numbers but it does not work. I decide to finish the group work and use different tools to program the DF control board.

Comparative Testing and Data Recording

I use “Doubao” AI to help me with this table by the prompt”compare DF control board and DFRduino Uno3.0”

Comparison Item DFRduino UNO 3.0 (ATmega328P) DFRobot Control Board (ESP32) Reason for Difference (Combined with Architecture)
Architecture Type 8-bit AVR (CISC, Harvard Architecture) 32-bit ARM Cortex-M4F (RISC, Von Neumann Architecture) AVR is a simplified 8-bit architecture, while ESP32 is a high-performance 32-bit RISC. Harvard Architecture separates program/data storage; Von Neumann shares the bus.
Development Environment Setup Difficulty Simple (native support, no extra configuration required) Medium (ESP32 board URL must be added) UNO is an official Arduino board, while ESP32 is a third-party board.
Code Size 28 lines (logic code) 28 lines (logic code) The code size is the same for basic functions; complex functions (e.g., WiFi) require extra code on ESP32.
Upload Speed Fast (upload completes in ~2 seconds) Relatively slow (~9 seconds, Flash erasure needed) ESP32 has a much larger Flash (4MB vs. 32KB on UNO), so erasing and writing take longer.
Running Efficiency (Blinking Precision) 500ms delay with ~5ms deviation 500ms delay with <1ms deviation ESP32 has a much higher clock frequency (240MHz vs. 16MHz), and its timer precision is far higher.
Peripheral Convenience Easy-to-use basic peripherals (GPIO/UART), no wireless support Basic peripherals + WiFi/BLE/ADC/PWM, wireless functions ready to use ESP32 integrates far more peripherals; AVR only supports basic interaction.
Debugging Experience Serial debugging only, no online debugging Serial debugging + JTAG online debugging supported ARM architecture supports more complete debugging protocols; AVR has limited debugging functions.
Resource Limits Flash 32KB, SRAM 2KB (prone to overflow for complex programs) Flash 4MB, SRAM 520KB (no resource pressure) Hardware storage specifications differ; ESP32 is suitable for complex projects.

  • UNO is simpler since it needs no extra drivers and fast uploading speed. Esp32 can do more since it has a lot of I/Os and wifi.

Individual assignment: write and test a program for an embedded system using a microcontroller to interact and communicate (with DFRobot Control Board (ESP32))

1 With mind+ environment

alt text alt text

alt text

mind+ is a programming environment for DF boards. First I try Hello word with scratch mode

alt text

I try to use button A to control the board so I program with scratch a short program

alt text alt text

But when I press bot B it does not work so I guess the bottom is broken.

2 study esp32 data file

Datasheet Reference

https://documentation.espressif.com/esp32_datasheet_cn.pdf

Target Hardware

DFRobot Control Board (ESP32-D0WDQ6 core)

Objective

This study aims to deeply understand the hardware characteristics, functional specifications, and low-level working principles of the ESP32 chip, and to guide the practical development of embedded programming assignments (peripheral interaction, multi-environment programming, and debugging) for FabAcademy.


1. Chip Overview & Core Architecture

1.1 Extracted Key Information

  • Processor Core: Xtensa® 32-bit LX6 dual-core microprocessor, configurable asdual-core operation;
  • Clock Frequency: Up to 240 MHz (default 160 MHz for DFRobot Control Board);
  • Architecture Type: Modified Harvard Architecture (separates instruction and data buses for high throughput, while supporting shared memory access for flexibility);
  • Instruction Set: RISC (Reduced Instruction Set Computer) – simplified instruction set with fixed-length instructions, high execution efficiency for embedded scenarios.

1.2 Technical Interpretation

The dual-core design enables parallel execution of tasks (e.g., one core handling wireless communication, the other controlling peripheral sensors), which is critical for complex interactions (e.g., Bluetooth-controlled motor + real-time temperature sampling in the intermediate kit). The 240 MHz clock frequency delivers ~15x higher instruction execution speed than ATmega328P (16 MHz), directly improving the precision of time-sensitive operations (e.g., servo motor PWM control with <1ms delay deviation).


2. Pinout & Functional Allocation

alt text

2.1 Extracted Key Information

Pin Category ESP32 Pin Numbers (DFRobot Control Board) Key Features (Relevant to Intermediate Kit)
GPIO GPIO0-GPIO39 (partial pins exposed) Support input/output, pull-up/down resistors, interrupts (e.g., GPIO39 for onboard Button A, GPIO13 for RGB LED);
ADC (Analog-to-Digital Converter) ADC1 (GPIO32-GPIO39), ADC2 (GPIO0-GPIO21) 12-bit resolution (4096 levels), 0-3.3V input range (matches intermediate kit’s photosensitive/thermistor sensors);
PWM (LEDC) GPIO2/GPIO4/GPIO12-GPIO19/GPIO21-GPIO23 16 independent channels, 1 Hz-40 MHz frequency (ideal for SG90 servo motor control – requires 50 Hz PWM signal);
UART UART0 (GPIO1/GPIO3), UART1 (GPIO9/GPIO10), UART2 (GPIO16/GPIO17) Supports baud rates up to 5 Mbps (compatible with HC-05 Bluetooth module in the intermediate kit);
Power Pins VDD_3V3 (3.3V), GND All peripherals (sensors/motors) must match 3.3V logic (5V peripherals require level conversion).

2.2 Practical Relevance

  • The ADC pins (e.g., GPIO34) are used to connect the intermediate kit’s thermistor/photosensitive sensor (avoid ADC2 pins if WiFi is enabled – ADC2 is shared with WiFi and cannot be used simultaneously);
  • PWM channel GPIO19 is optimal for SG90 servo control (matches the 50 Hz PWM frequency required by servos);
  • UART2 (GPIO16/GPIO17) is assigned to the HC-05 Bluetooth module to avoid conflicting with the USB serial port (UART0).

3. Clock & Timing System

alt text

3.1 Extracted Key Information

  • Clock Sources:
    • Internal 8 MHz RC oscillator (low precision, for standby mode);
    • External 40 MHz crystal oscillator (high precision, default for active mode);
    • 32 kHz RTC oscillator (for low-power timing);
  • Timers: 4 × 64-bit general-purpose timers, 2 × watchdog timers (WDT), 1 × RTC timer;
  • PWM (LEDC): 16 channels with 1-16 bit duty cycle resolution (configurable for servo/motor speed control).

3.2 Technical Interpretation

The external 40 MHz crystal oscillator ensures stable PWM signal generation (critical for servo motor angle accuracy – jitter-free operation). The 64-bit timers support long-duration timing (e.g., 1-hour interval sampling for temperature sensors) without overflow issues, unlike ATmega328P’s 16-bit timers.


4. Peripheral Interfaces (Relevant to Intermediate Kit)

4.1 Wireless Connectivity (ESP32 Core Advantage)

  • WiFi: 802.11b/g/n (2.4 GHz), supports station/soft AP mode (enables remote control of kit peripherals via WiFi);
  • Bluetooth: Bluetooth 4.2 Classic + BLE (Bluetooth Low Energy) – compatible with HC-05 (Classic) and BLE modules in the intermediate kit;
  • Key Constraint: WiFi/BT operation consumes ~80mA current (higher than AVR), requiring stable 3.3V power supply (avoid powering from USB hubs for complex peripheral setups).

4.2 Other Critical Peripherals

  • I2C: Two I2C controllers (GPIO21/SDA, GPIO22/SCL) – for 1602 LCD display in the intermediate kit;
  • SPI: Four SPI controllers – for high-speed data transfer (e.g., external Flash/SD card modules);
  • Touch Sensors: 10 capacitive touch pins (e.g., GPIO4/GPIO12) – can replace physical buttons in the kit for contactless control.

5. Memory Configuration

5.1 Extracted Key Information

Memory Type Capacity (ESP32-D0WDQ6) Use Case (Intermediate Kit)
Internal SRAM 520 KB (including 16 KB RTC SRAM) Stores runtime data (e.g., sensor readings, motor control parameters);
Flash (External) 4 MB (default for DFRobot Control Board) Stores program code, configuration files (e.g., Bluetooth pairing data);
ROM 448 KB Preloaded with bootloader and ESP-IDF system libraries.

5.2 Practical Relevance

The 520 KB SRAM eliminates memory overflow issues even with complex code (e.g., simultaneous WiFi communication + servo control + LCD display), unlike ATmega328P (2 KB SRAM) which requires memory optimization for multi-peripheral projects. The 4 MB Flash supports storing large firmware (e.g., MicroPython/CircuitPython interpreters + custom scripts).


Last update: February 18, 2026