Skip to content

4. Program PCBs for Embedded Systems

This week was the first installment of the three-part series on Electronics design⚡🕺

The topic of this week was “Embedded programming”, which is the act of programming microcontrollers with software codes, which when embedded into hardwares can enable them to perform various magical functions.

👉What is an “Embedded system”?
An embedded system is a system designed to perform a specific, dedicated function within a larger device. They often operate autonomously, and are found in a vast range of products, from home appliances like washing machines, to complex systems such as self-driving cars.
Reference: Google AI

Some of my main learnings this week were;
- What are PCBs and microcontrollers, and what are their key components, specifications, peripherals, etc.
- How to select the right microcontroller/MCU for my project, by reading and identifying relevant information on datasheets.
- Introduction to writing codes for microcontrollers.

Assignments for this week (Feb 12-Feb 18):

Group assignment:
1. Demonstrate and compare the toolchains and development workflows for available embedded architectures (processors)

Individual assignments:
1. Browse through the datasheet to choose a microcontroller
2. Write a program for a microcontroller, and simulate its operation to interact and communicate

Group Assignment:

Our goal for this week was to demonstrate and compare the toolchains and development workflows for some of the processors available at FabLab Kamakura.

In the morning Nagano-san laid out all the processors from FabLab Kamakura for us to see, and we eventually picked these four to try;

Versatile, lots of info Cheap and basic Small and basic Display & Wi-fi
Board Name: Arduino Uno Arduino Pro Micro Xiao RP2040 M5StackCore
alt text alt text alt text alt text
Microcontroller: AVR AtMega32U4 ARM RP2040 Espressif ESP32 AVR AtMega328P
CPU: 16MHz Single-Core 16MHz Single-Core 133MHz Dual-Core 240MHz Dual-Core
Memory (SRAM/Flash): 2K / 32K 2.5KB/ 32KB 264KB / 2MB 520KB / 16MB
Size (mm): 68.6 × 53.4 34 x 18 x 13 20.0 x 17.5 x 3.5 54 × 54 x 16
Price (JPY, Approx.): 3,300 ~1,000 1,400 5,500

Link to Group Assignment documentation page

My learnings from groupwork
- We only managed to try the Arduino IDE, because all the 4 chips we picked were compatible with it. But through further reading I learnt that there are a variety of other programming languages and environments (IDEs) out there;
Programming languages: C++, Python, Rust, Java, etc
IDE: Arduino, VS Code, STM32CubeIDE, Thonny, etc
- Although I am a complete newbie in programming, I found the Arduino IDE surprisingly straightforward to use. I learnt that it not only provides a very intuitive and supportive programming environment, but it offers a large range of libraries that enable us to efficiently deploy features onto the chips.
- I also got a little understanding into the progress IDEs such as Arduino had made in making the task of programming chips much easier for us, by streamlining a lot of time-consuming tasks (such as compilation and uploading of the codes/sketches onto the board).
- When I have a chance, I would like to try and compare other development environments such as Thonny, CircuitPython, to get better perspective, and also to ideally learn to code and compile my own code, as apparently that can enable me to use resources (especially energy) more efficiently.

👉What are some of the key components of Microcontrollers?
These are some of the key components we should know, which also happen to be the specs we should focus on when reading the datasheets;

  • Architecture: Harvard (RISC) or Von Neumann (CISC)
  • Clock speed/Operating frequency (MHz): How fast the processor can execute instructions within a given time. This also affects power consumption.
  • Bus width (bits): Governs the microcontroller’s speed and mathematical precision. 8-bit is basic, while 16-bit and 32-bit typically offer more performance.
  • RAM: Capacity of data memory (holds variables and data that the microcontroller uses during operation)
  • FLASH: Program memory (holds code that the microcontroller executes)
  • I/O Ports: Reads data in from pins and sends signals out from pins.
  • Power Requirement (VCC/VDD/AREF/5V,etc): Represents the power supply voltage for the circuit/chip. It can be 3.3V, 5V, or 12V. Using a power supply with a voltage that is significantly higher than what a device is designed for can cause the device to break or malfunction due to “overvoltage”.
  • Power management options: Some microcontrollers offer Deep sleep modes

Personal Assignment:

1. Browse through datasheets to choose a microcontroller

👉What are datasheets?
They are documents published by the device manufacturer, outlining the internal architecture of devices such as microcontrollers, PCBs, etc. They contain critical information for choosing the right device and for making best use of its capabilities.

1.1 Selecting datasheets

Before deciding on which microcontroller’ datasheets to browse through, I gave more thought to the electronic requirements of my final project so that I can narrow down my options.

Electronic requirements of my Final Project:
- Wi-Fi capability
- Microphone (although unsure whether to embed it in my board or make use of a separate external smart speaker)
- AI speech recognition (also unsure how best to achieve this)
- Long battery life / efficient power consumption
- Easy to sync with Google docs (via API?)
- As low cost as possible

Since I felt like I would learn more from starting with comparing a few rather than just one, I selected a few wi-fi compatible chips.
Note: For the RP2040, I looked at the wi-fi enabled SoC offered by Seeed.

Microcontroller RP2040 (Seeed Wio) ESP32 STM32WL
MPU 32 bit ARM Cortex-M0+ 32 bit Xtensa LX6 32 bit Arm Cortex-M
Processing Power 133MHz Dual core 160/240MHz Dual core ~48MHz dual core
Memory (ROM/SRAM/Flash) 512kB/264kB/2MB 448kB/520kB/16MB 640kB/64-256kB Flash
GPIO 28 34 ~72
DAC - 2ch x 8bit 1ch x 6 bit
Touch sensors - 10 -
Wi-Fi 2.4GHz 2.4GHz LoRA WAN (~148dBm), amazon sidewalk, etc
Bluetooth - 4.2 -
Language C/C++, MicroPython, Lua, Arduino, Thonny Arduino, C/C++/C#, Python(MicroPython), JavaScript, Lua, mRuby, Lisp C, Python, STM32CubeIDE
Price (approx,JPY) 1,600 500 1,600

Thoughts:
In terms of performance, ESP32 looked to be the clear winner, and I also liked its compatibility with Arduino. However I also want to be considerate of power consumption / battery life / price, and for that I will need to research a lot more to evaluate how much performance is enough/too much for my project.

1.2 ESP32 Datasheets

For the time being, I decided to look further into different models under the ESP32 family. I also added a Xiao SoC for some context.

Model ESP32 ESP32S3 ESP32C3 ESP32C6 Xiao ESP32C6
Processor 32-bit Xtensa Lx6 dual core 32-bit Xtensa LX7 dual core 32 bit RISC-V single core 32-bit RISC-V single-core Same as left
Clock speed 240MHz 240MHz 160Mhz 160Mhz Same as left
Memory (ROM/SRAM/Flash) 448kB/520kB/16MB 384kB/512kB/8MB 384kB/400kB/external flash 320kB/512kB/external flash 512kB/512kB/4MB
GPIO 34 45 16 or 16 22 or 30 11
Analog input 18 channels 20 6 7 3
Analog output 2 channels 2 - - -
DAC 2ch x 8bit 2ch x 8bit - - unknown
Touch sensors 10 14 - - unknown
Other 4 SPI/2 I2S/2 I2C/3 UART 2 SPI /2 I2S/2 I2C/3 UART 3 SPI/I2S/I2C/2 UART 1 SPI/I2S/I2C/2 UART 1 SPI/I2S/I2C/UART
PWM Motor PWM, LED ~16 channels Motor, LED ~8 channels Motor, LED ~6 channels Motor, LED ~6 channels same
Power cons. in deep sleep 10µA 7µA 5µA 7µA 15µA
Wi-fi 2.4GHz Wi-Fi4 2.4GHz Wi-fi4 2.4GHz Wi-fi4 2.4GHz Wi-fi5 2.4GHz Wi-fi5, and Zigbee,Thread,IEEE 802.15.4
Bluetooth 4.2 5.0 5.0 5.3 5.3
Approx Price (JPY) 1,400 1,400 787

I concluded that the ESP32-C6 seems to have a good balance of low-power consumption and performance.

⚡Further Questions for my Final Project:
The above comparison exercise also helped me come up with further questions to hopefully answer in the coming few weeks;

- What range of CPU/memory is adequate for smooth voice recording and speech keyword recognition?
- How would a low power consumption of 15µA translate to battery life?
- What are the differences between different connectivity options such as Bluetooth 4 and 5, wi-fi 4 and 5?
...etc etc.
Hopefully I will have more clarity in a few weeks!

2. Programming an ESP32-C6

I used Wokwi to simulate the ESP32-C6 microcontroller to execute LED blinking and Serial communication.

2.1 Study the datasheet

First, I looked at the pin-out diagram for my ESP32-C6 board on Wokwi, in order to figure out which pin numbers to use.
alt text

2.2 Create the circuit

I thought of a scenario for the user to select Blue light or Red light (like Neo in Matrix😎), and wired the board.
alt text

2.3 Coding

During the local session, Rico-san gave us a brief overview of coding for microcontrollers. Since I am totally new to coding, this was a helpful introduction.
alt text

For this part of individual assignment, I started by transcribing my instructor Nagano-san’s code. Tsuchiya san calls this “写経 (copying of the sutra)” and recommends not simply copying and pasting, but rewriting the codes letter by letter in order to really understand the content.
I then tried adding an “if else” code based on Wokwi’s example code.

#define led_pin_blue 8 //Declared the pin numbers 
#define led_pin_red 13
#define button_pin_blue 7
#define button_pin_red 15

void setup() {
  Serial.begin(115200); //Use A Serial Monitor
  Serial.println("Hello, ESP32-C6!");
  pinMode(led_pin_blue, OUTPUT); 
  pinMode(led_pin_red, OUTPUT);
  pinMode(button_pin_blue, INPUT_PULLUP);
  pinMode(button_pin_red, INPUT_PULLUP);
}

void loop() {
  // put your main code here, to run repeatedly:
  if(digitalRead(button_pin_blue) == LOW){ //If the button is pressed down
    Serial.println("You picked Blue!"); //Display this message
    digitalWrite(led_pin_blue, HIGH); //And light the LED
    delay(1000); //for 1000 milliseconds
    digitalWrite(led_pin_blue, LOW);
  }
  else if(digitalRead(button_pin_red) == LOW){
    Serial.println("You picked Red!");
    digitalWrite(led_pin_red, HIGH);
    delay(1000);
    digitalWrite(led_pin_red, LOW);}
  delay(10); // this speeds up the simulation
}

2.4 Testing and Debugging

I wasn’t able to test it during the week because of overwhelming demand from FabAcademy, but I was later able to get it to blink without any debugging. (Link here)

Reflections:

This week I learnt about the basic functions of micro-controllers, in that they enable embedded systems to perform specific functions within a device. I learnt how to compare and contrast different micro-controllers by reading datasheets, and started to have a basic understanding of microcontroller’s architectures (or at least the key components that make them up).

I also learnt to write a simple program for a microcontroller, and simulate its operation using Wokwi.

In terms of my Final Project, the assignments helped me crystallise my chip requirements. At this moment, I am thinking of having 2 main interfaces with separate chips, one for operating the rack, and one for interfacing with the user via a Smart speaker.

In the coming weeks, I would like to draw out more specific design requirements for the spice bottle holder and microphone interface, with the most important one being, “What kind of input sensor would enable me to recognise bottle placement???”.
Hopefully I will find the answer during the “input device” week!

Assignment Checklist:

  • Linked to the group assignment page
  • Browsed and documented some information from my microcontroller’s datasheet
  • Programmed my simulated board to interact and communicate
  • Described the programming process(es) I used
  • Included my source code
  • Included ‘hero shot(s)’

  1. Reference for Microcontroler specs: intechhhouse, cui.com