Skip to content

4. Program PCBs for Embedded Systems

This week was the first installment of the three-part series on electronics design.

This week was about “Embedded programming”, which is about how microcontrollers are programmed with software codes, in order to enable hardwares to perform various functions.

What is an “Embedded system”?
An embedded system is a system designed to perform a specific, dedicated function within a larger mechanical or electrical device. Often operating 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 a datasheet.
- Basics of writing software 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 assignment:
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 the embedded architectures available at FabLab Kamakura.

We selected 4 different processors from FabLab Kamakura’s inventory to try, which all happened to be compatible with the Arduino IDE.

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

Link to Group Assignment documentation page

My learnings from groupwork
- We only managed to try the Arduino IDE, which is said to be the most beginner-friendly, but I also learnt that there exist a variety of other programming languages and Programming environments (IDEs);
Programming languages: C++, Python, Rust, Java, etc
IDE: Arduino, VS Code, STM32CubeIDE, Thonny, etc
- As a beginner in programming I found the Arduino IDE surprisingly straightforward to navigate. I learnt that it not only provides an intuitive programming environment, but it offers a large range of libraries, and facilitates compilation and uploading of the codes (sketches) onto the board, which used to be a much more tricky task.
- When I have a chance, I would like to compare other development environments such as Thonny, CircuitPython, and also to ideally learn to code and compile my own code, as that would enable me to use resources (especially energy) more efficiently.

👉What are the main specifications of Microcontrollers?
[Processing power related specs]
- 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.

[Memory capacity related specs]
- RAM: Data memory (holds variables and data that the microcontroller uses during operation)
- FLASH: Program memory (holds code that the microcontroller executes)

[Other specs]
- 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
Reference: intechhhouse, cui.com

👉What are the major processor families
- PIC
- AVR (ATTiny, ATMega, etc)
- Arm (STM, Raspberry Pi, etc)
- Xtensa /MIPS (ESP)
- RISC-V
...etc

Personal Assignment:

Browse through datasheets to choose a microcontroller

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

Deciding which datasheets to browse

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 on several 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 seems to be the clear winner, and I also like that it’s compatible 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.

Studying 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!

Programming an ESP32-C6

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

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. 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

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.
alt text

4. Debugging

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

Reflections:

This week I learnt about the basic functionalities of micro-controllers, in that they enable embedded systems to perform specific functions. I also learnt to write a simple program for a microcontroller, and simulate its operation using Wokwi.

The assignments also helped me crystallise my final project design in terms of 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, the main 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:

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