5.Embedded programming¶
Summary¶

What is Embedded Programming?¶
One kind of programming language used to create system code based on a microcontroller or microprocessor is called embedded programming. There are certain hardware requirements for this programming language. A common form of Internet of Things and electronic consumer applications are embedded systems, which are utilized inside industrial machinery, bicycle charging systems, and residential appliances.
Where is Embedded Programming Used?¶
Embedded programming is widely used across various industries. Industrial Automation: Used for production monitoring and machine control. Consumer Electronics: Found in devices like digital cameras, washing machines, and smart TVs. Internet of Things (IoT): Powers smart home appliances and connected devices. Automotive Systems: Plays a crucial role in airbags, ABS brakes, and engine control. Medical Equipment: Used in devices such as ventilators, insulin pumps, and heart rate monitors.
Group assignment¶
About Microcontroller¶
A circuit called a microcontroller is made to regulate particular operations in an embedded system. On a single chip, it combines a CPU, memory (RAM, ROM, and Flash), and input/output peripherals. Because of their low power consumption and real-time processing capabilities, microcontrollers are widely utilized in consumer electronics, automation, robotics, and the Internet of Things.
Microcontroller Comparison¶

-
AVR Microcontrollers AVR microcontrollers are commonly used in small embedded systems and are based on the RISC (Reduced Instruction Set Computer) architecture. They are widely popular among beginners and hobbyists, often found in Arduino boards. Known for their low power consumption, simplicity, and ease of programming, AVR chips are a preferred choice for basic embedded applications.
-
ARM Microcontrollers ARM microcontrollers are used in a wide range of high-performance applications. They are known for their scalability and power efficiency, making them ideal for industrial automation, automotive control, and mobile devices. The ARM Cortex-M family is widely utilized in various embedded systems.
-
ESP Microcontrollers The ESP8266 and ESP32 are popular microcontrollers designed for Internet of Things (IoT) applications. These microcontrollers are ideal for cloud-based applications, wireless communications, and smart home automation due to their built-in Wi-Fi and Bluetooth capabilities. Among IoT projects, the ESP32 is widely used for its advanced features and versatility.
-
STM32 Microcontroller STMicroelectronics' STM32 microcontrollers are based on the ARM Cortex-M core. They are widely used in automotive, medical, and industrial automation applications. These microcontrollers offer real-time processing, multiple peripherals, and high computational power, making them suitable for a variety of advanced embedded systems.
Reference by Search Engine
Toolchains and Development Workflows¶
The key instruments and procedures required for embedded system development are the subject of research on toolchains and development workflows. A compiler, assembler, linker, debugger, and integrated development environment (IDE) comprise a toolchain that facilitates the efficient writing, compilation, and debugging of code. Different toolchains are used by different microcontrollers, including AVR, ARM, ESP32, and STM32, to guarantee the seamless creation and operation of embedded applications. A clear workflow increases productivity, lowers errors, and boosts system performance as a whole.
Toolchain Components¶
Compiler: Transforms high-level code (C, C++) into machine code. Examples include GCC, Clang, IAR, and Keil. Assembler: Converts assembly language into binary instructions. Common assemblers include GNU Assembler and NASM. Linker: Combines compiled code and libraries to construct an executable. Examples include GNU LD and Keil Linker. Debugger: Enables step-by-step execution for troubleshooting. Popular debuggers include GDB, OpenOCD, and J-Link Debugger. Integrated Development Environment (IDE): Provides a complete development environment. Examples include VS Code, Eclipse, Keil, STM32CubeIDE, and Arduino IDE.
Development Workflow¶
Requirement Analysis: Select the appropriate hardware and define system objectives. Code Development: Write embedded code using C, C++, or Assembly. Compilation & Linking: Utilize the toolchain to convert source code into machine code. Flashing to Target: Use a programmer or debugger to upload the compiled application onto the microcontroller. Testing & Debugging: Employ tools like Serial Monitor, JTAG, or SWD for real-time debugging. Optimization: Enhance power efficiency, minimize memory usage, and improve overall performance. Maintenance & Deployment: Finalize and install the firmware on operational devices.
Example Toolchain for Microcontrollers¶

Which Tools Were Easier to Use?¶
Arduino UNO & Nucleo F103RB¶
Arduino UNO¶

Arduino Uno is a small open-source microcontroller board used for building electronics projects. It’s based on the ATmega328P chip and has digital and analog pins for connecting sensors, LEDs, and motors. You can program it through the Arduino IDE using C/C++, making it ideal for beginners and prototyping.
Arduino Uno – specifications (short):¶
Microcontroller: ATmega328P
Operating Voltage: 5 V
Input Voltage (recommended): 7 – 12 V
Digital I/O Pins: 14 (6 can output PWM)
Analog Input Pins: 6
DC Current per I/O Pin: 20 mA
Flash Memory: 32 KB (0.5 KB used by bootloader)
SRAM: 2 KB
EEPROM: 1 KB
Clock Speed: 16 MHz
USB Connection: Type-B
Communication: UART, SPI, I²C
Programming: via Arduino IDE in C/C++

We assembled a small circuit prototype using a breadboard, three LEDs, 100 Ω resistors, male-to-male jumper wires, and an Arduino Uno. I created a mini version of a running light effect using the three LEDs and wrote the code in PlatformIO using C++. The result turned out quite good.
Here is code¶
#include <Arduino.h>
void setup() {
for (size_t i = 8; i < 12; i++)
{
pinMode(i , OUTPUT);
}
}
void loop() {
for (size_t i = 8; i < 12; i++)
{
digitalWrite(i , 1);
delay(200);
digitalWrite(i , 0);
delay(100);
}
for (size_t i = 12; i > 8; i--)
{
digitalWrite(i , 1);
delay(200);
digitalWrite(i , 0);
delay(100);
}
}
Nucleo F103RB¶

Nucleo F103RB is a development board by STMicroelectronics based on the STM32F103RB microcontroller (ARM Cortex-M3). It includes:
USB interface for programming and power,
Arduino-compatible pin headers,
ST-Link debugger built in.
It’s used for learning, prototyping, and developing embedded systems using STM32CubeIDE or Arduino IDE.
STM32 CudeMX¶

STM32CubeMX is an official software tool from STMicroelectronics that helps developers configure STM32 microcontrollers before programming them.
In short:
It is a graphical interface (GUI) where you can select a microcontroller, configure its pins, clock settings, and peripheral interfaces such as UART, SPI, and I2C.
It automatically generates C code for various IDEs, including STM32CubeIDE, Keil, and IAR.
It simplifies the configuration process and saves time during project setup.
In simple terms, STM32CubeMX is a configuration tool that allows you to easily set up an STM32 microcontroller and generate the necessary code for development.
CLion¶

CLion is a professional Integrated Development Environment (IDE) created by JetBrains for programming in C and C++. It offers powerful features such as:
intelligent code completion,
advanced debugging tools,
cross-platform project building (using CMake, Makefile, or Gradle),
code analysis and refactoring,
integration with Git, Docker, and other development tools.
CLion makes C/C++ development more convenient and efficient, especially when working on large-scale projects or microcontroller-based systems.
STM32 CudeProgrammer¶

STM32CubeProgrammer is an official software tool developed by STMicroelectronics for programming and managing the memory of STM32 microcontrollers.
With STM32CubeProgrammer, you can:
Flash firmware to STM32 devices via USB, UART, SWD, JTAG, or DFU;
Read and erase memory;
Modify Option Bytes and security settings;
Work with both internal and external flash memory;
Update firmware using either a graphical interface (GUI) or a command-line interface (CLI).
In simple terms, STM32CubeProgrammer is a universal tool for flashing, configuring, and maintaining STM32 microcontrollers.
These are the software tools I use when working with microcontrollers from STMicroelectronics.¶
We created a running light effect using LEDs on the Nucleo-F103RB board.¶
We first created a new project in STM32CubeMX, configured the pins in output mode to connect the LEDs, and selected CMake as the toolchain.


This is the implementation of the running light effect based on the STM32 microcontroller — here is the result.
You can download the code here.¶
Individual assignment¶
Servo Motor Simulation in Tinkercad¶
I created a circuit in Tinkercad to control a servo motor and an LED using a potentiometer. The components I used include a servo motor, a resistor, an LED, and another servo motor.

Here is code¶
#include <Servo.h>
const int potPin = A0;
const int ledPin = 3;
const int servoPin = 9;
Servo sv;
void setup() {
sv.attach(servoPin);
pinMode(ledPin, OUTPUT);
}
void loop() {
int v = analogRead(potPin);
int angle = map(v, 0, 1023, 0, 180);
int bright = map(v, 0, 1023, 0, 255);
sv.write(angle);
analogWrite(ledPin, bright);
delay(10);
}
Another experiment with the Nucleo-F103RB board.¶
I used the same circuit but modified the code. The board has a User Button, and I used it so that the LEDs light up when the button is pressed.
You can download the code here.