Embedded Programming

This Week I worked on Embedded Programming

Assignment Brief:

  1. Understanding formats: Browse through the data sheet for your microcontroller to interact (with local input &/or output devices)
  2. Compare the performance and development workflows for other architectures.
  3. Write a program microcontroller development board, to interact with local input &/or output devices and communicate with remote wired or wireless devices.
  4. Extra credit: use different languages &/or development environments
  5. Extra credit: connect external components to the board

Embedded Programming

Embedded programming is writing software that runs on specialized devices rather than general-purpose computers. These devices, like washing machines, smartwatches, or car engines, have tiny computers (microcontrollers or microprocessors) inside them that control how they work. Unlike regular software, embedded programs are designed to be efficient, reliable, and work with limited memory and power. They are written in languages like C or Python and directly interact with the hardware to perform specific tasks, like turning on a motor or reading sensor data.

Microcontrollers and microprocessors

Microcontrollers and microprocessors are both types of computing chips, but they serve different.

Microcontroller (MCU): A small, self-contained chip with a processor, memory (RAM & ROM), and input/output (I/O) ports all of this built into one unit is a microcontroller. It is designed for specific tasks in embedded systems, like controlling appliances, cars, or IoT devices. Since everything is integrated, it consumes less power and is cost-effective.

Microprocessor (MPU): A more powerful chip that mainly consists of a processor (CPU) without built-in memory or I/O ports. It relies on external components to function, making it more suitable for complex tasks like running an operating system on a computer or smartphone. It is faster and more flexible but requires more power and additional components. triangle cut

In-build Parts

Processor: You can think of the processor (CPU - Central Processing Unit) as the brain of a computer or device.

Memory (RAM & ROM):To learn more about Memory in microcontroller refer to attatched Link.
RAM (Random Access Memory) is a temporary memory that stores data while a device is running. It is fast but loses all data when power is off. ROM (Read-Only Memory) is a permanent memory that stores essential data, like the system’s startup instructions (firmware). It retains data even when power is off. Different Types of Memory in Microcontroller

  • Flash Memory: Non-volatile memory used to store the program (firmware) in a microcontroller. Retains data even when power is off.
  • SRAM (Static Random Access Memory): Volatile memory used for temporary data storage while the microcontroller is running. Faster but loses data when power is off.
  • EEPROM (Electrically Erasable Programmable Read-Only Memory): Non-volatile memory used to store small amounts of data that need to be preserved even after power loss, like user settings or calibration data. Also it can be rewritten multiple times.
  • (First Image) Microcontroller Memory Classification. Microcontroller memory is categorized into Flash Memory (Non-Volatile, used as ROM), SRAM (Volatile, used as RAM), and EEPROM (Non-Volatile). Flash Memory stores the program (ROM). SRAM is used as data memory (RAM) for temporary storage. EEPROM is another non-volatile storage used for retaining small amounts of data.

    (Second Image) Types of Program Memory in Microcontrollers. Program memory includes different types of ROM: ROM (Read-Only Memory) – Pre-programmed, non-modifiable. PROM (Programmable ROM) – Can be programmed once. EPROM (Erasable and Programmable ROM) – Erased using UV light and reprogrammed. EEPROM (Electrically Erasable and Programmable ROM) – Erased and rewritten electrically.

    Relevance of EEPROM in Today's Microcontrollers: EEPROM is crucial in modern microcontrollers for storing non-volatile data, such as calibration settings, configuration data, and user preferences, without requiring an external storage device. Unlike Flash memory, EEPROM allows byte-wise erasing and writing, making it suitable for applications that require frequent updates without wearing out the entire memory block.


    Input/output (I/O) ports: I/O refers to how a device interacts with the outside world. Input: Data or signals received by a device to process Output: Data or signals sent by a device after processing. Modulation is the process of altering a signal to transmit data efficiently. Modulation is the process of modifying a signal (message) so it can be efficiently transmitted over a medium, like radio waves or cables. In microprocessors, modulation plays a key role in controlling outputs and communicating with other devices.To learn more about Why Modulation is Required ? Types of Modulation Explained. refer to attatched Link.

    Key Uses of modulation in microcontrollers:
  • PWM (Pulse Width Modulation) – Output Control: Used to control devices like motors, LEDs, and audio signals by varying pulse width. Helps adjust brightness (LEDs) or speed (motors) without needing extra power. Common in microcontrollers for efficient power management.
  • Serial Communication (Modulated Signals) – Data Transfer: Microprocessors use UART (Universal Asynchronous Receiver-Transmitter), I2C (Inter-Integrated Circuit), SPI (Serial Peripheral Interface) - Serial communications to exchange data with sensors, displays, and memory chips.

      Serial Communication

      ram rom

      I2C Communication

      triangle cut

      Serial Peripheral Interface

      triangle cut
      triangle cut
    To learn more about communication Protocols refer to the attatched Link.
  • To give a brief of communication Protocols: Imagine you are talking to your friends,
  • UART (Universal Asynchronous Receiver-Transmitter) – Like texting on a phone. Uses TX (Transmit) to send and RX (Receive) to get messages. No clock signal (both sides must agree on the speed beforehand). Simple but not the fastest.
  • I2C (Inter-Integrated Circuit) – Like a classroom discussion. Uses only 2 wires: SDA (Data Line) → Carries the message. SCL (Clock Line) → Keeps everyone in sync. Can connect many devices using the same 2 wires. Slower than SPI but saves wiring.
  • SPI (Serial Peripheral Interface) – Like a private hotline. Uses 4 wires for communication: MOSI (Master Out, Slave In) → Sends data. MISO (Master In, Slave Out) → Receives data. SCLK (Clock) → Keeps timing in sync. CS (Chip Select) → Chooses which device to talk to. Fastest communication but needs more wires.

  • What is a Clock?
    Think of it like a conductor in an orchestra, making sure all devices send and receive data at the right time and simultaneously support the cycles. triangle cut
  • I2C & SPI use a clock, while UART does not (which is why UART devices must agree on speed before talking).
  • Multiple Wires? Some protocols (like I2C) use fewer wires to save space. Others (like SPI) use more wires for faster data transfer.
  • Group Assignment

    Comparison of Toolchains and Development Workflows for ATmega and RP2040 ATmega (used in AVR microcontrollers) and RP2040 (used in Raspberry Pi Pico) are two popular embedded architectures. Below is a comparative demonstration of their toolchains and development workflows.

    ATmega328 Toolchain

    The ATmega328 is an 8-bit microcontroller commonly used in Arduino boards (like the Arduino Uno). To develop software for this microcontroller.

    Editor (To write the code):
    You can use Arduino IDE, PlatformIO, or a general editor like VS Code.
    Programming is usually done in C or C++.
    Compiler (To convert human-readable code into machine code)

    Compiler(To convert human-readable code into machine code)
    The AVR-GCC (GNU Compiler Collection for AVR) is used to compile the code.

    AVR stands for Advanced Virtual RISC. It is developed by Atmel (now part of Microchip Technology). These microcontrollers are designed with a simple and efficient RISC (Reduced Instruction Set Computing) architecture, allowing them to execute commands quickly and consume less power.

    Linker (To connect different parts of the program)
    This arranges the compiled code properly for execution.

    Flasher/Programmer (To upload the code to the microcontroller):
    The AVRDUDE (AVR Downloader/Uploader) is used to transfer the compiled code (.hex file) into the ATmega328 chip using an ISP (In-System Programmer) or USB-to-serial converter (like an Arduino bootloader).

    Debugger (To check and fix errors in the program)
    The SimAVR simulator or hardware debuggers like Atmel ICE can be used for debugging.

    ATmega Development Workflow

    🔹 Step 1: Setup Environment
    - Install Arduino IDE or AVR toolchain (AVR-GCC, AVRDUDE)
    - Choose programming method: USB (Arduino) or ISP (Bare ATmega328)
    - Connect hardware (Arduino board or ATmega328 with ISP programmer)
    🔹 Step 2: Write Code
    - Write program in C/C++ (Arduino IDE or raw AVR)
    - Define pin configurations, logic, and functions
    🔹 Step 3: Compile Code
    - If using Arduino IDE: Click Upload (compiles automatically)
    - If using AVR-GCC: Compile manually to generate .hex file
    🔹 Step 4: Upload Code
    - If using Arduino IDE: Upload via USB
    - If using ISP Programmer: Use AVRDUDE to flash the .hex file
    🔹 Step 5: Debug & Test
    - Use Serial Monitor (UART) for debugging
    - Check outputs using LEDs or external debugging tools
    🔹 Step 6: Optimize & Finalize
    - Optimize power consumption and memory usage
    - Burn bootloader (if required)
    - Lock fuses to prevent accidental overwriting
    END

    RP2040 Toolchain

    The RP2040 is a dual-core ARM Cortex-M0+ microcontroller developed by Raspberry Pi, commonly used in Raspberry Pi Pico boards. To develop software for this microcontroller, you need the appropriate toolchain.

    Editor (To write the code):
    You can use Thonny (for MicroPython), VS Code, or a command-line toolchain.
    Programming is usually done in C, C++, or MicroPython.

    Compiler (To convert human-readable code into machine code):
    For C/C++, the RP2040 uses GCC (GNU Compiler Collection) and CMake to compile the code.

    RP2040 is designed by Raspberry Pi and features a flexible I/O system, dual-core processing, and built-in USB support. It is optimized for embedded applications requiring low power and high performance.

    Linker (To connect different parts of the program):
    This arranges the compiled code properly for execution and generates the necessary firmware files (.uf2 or .bin).

    Flasher/Programmer (To upload the code to the microcontroller):
    The RP2040 microcontroller supports drag-and-drop programming. Holding the BOOTSEL button while connecting via USB mounts the device as a storage drive. The compiled .uf2 file can be copied directly onto it.

    Debugger (To check and fix errors in the program):
    Debugging can be done using OpenOCD with a hardware debugger like the Raspberry Pi Debug Probe, or by using print statements and serial debugging via UART.

    RP2040 Development Workflow

    🔹 Step 1: Setup Environment
    - Install Raspberry Pi Pico SDK or use Arduino IDE/MicroPython
    - Choose programming language: C/C++, MicroPython, CircuitPython
    - Set up development tools: Thonny, VS Code, or command-line toolchain
    🔹 Step 2: Write Code
    - Write program using CMake (for C/C++) or Python
    - Define GPIO, peripherals, and logic
    🔹 Step 3: Compile Code
    - If using MicroPython: No compilation needed (upload .py script)
    - If using C/C++ SDK: Compile using CMake to generate .uf2 file
    🔹 Step 4: Upload Code
    - Hold BOOTSEL button on RP2040 and connect to PC via USB
    - Drag and drop the .uf2 file into the RP2040 storage
    - If using MicroPython: Upload script via Thonny or rshell
    🔹 Step 5: Debug & Test
    - Use Serial Monitor (UART) for debugging
    - Check outputs using LEDs, oscilloscope, or print statements
    🔹 Step 6: Optimize & Finalize
    - Optimize power consumption (sleep modes, low-power optimizations)
    - Test performance with different clock speeds and peripherals
    - Deploy final firmware and protect memory (if needed)
    END

    Comparison of Microcontrollers

    Feature RP2040 (Raspberry Pi Pico) ATmega328 (Arduino Uno) ESP32
    Bit Width 32-bit 8-bit 32-bit
    Clock Speed Up to 133 MHz Up to 20 MHz Up to 240 MHz
    Power Usage Low Very Low Medium
    Ease of Use Moderate Easy Moderate
    Programming Tools ARM GCC, CMake, UF2 AVR-GCC, Arduino IDE ESP-IDF, Arduino


    Datasheet of RP 2040 microcontroller

    The RP2040 is Raspberry Pi’s first microcontroller, offering high performance, low cost, and easy use. It has two powerful cores, lots of built-in memory, and versatile connectivity options. Its unique Programmable I/O (PIO) allows flexible control of external devices. It’s great for both professionals and beginners, thanks to detailed guides, MicroPython support, and an easy-to-use bootloader.

    Boot loading is the process of starting up a microcontroller by loading essential software into memory. The RP2040 has a built-in UF2 bootloader, making it easy to upload new code—just drag and drop files like a USB drive, no extra tools needed

    To Learn more about the RP 2040 microcontroller to the link RP2040 Datasheet.

    Seeed Arduino Xiao 2040

    The Seeed Arduino XIAO 2040 is a compact development board powered by the RP2040 microcontroller from Raspberry Pi. Designed for embedded electronics projects, its small form factor makes it ideal for space-constrained applications. The board comes with various built-in peripherals, including GPIO pins, analog inputs, PWM outputs, UART, SPI, I2C, and USB connectivity. It supports multiple development environments like the Arduino IDE and CircuitPython, making it beginner-friendly while also catering to experienced developers for prototyping projects with sensors, actuators, and displays.

    interrface

    To Learn more about the RP 2040 microcontroller to the link, Seeed Studio XIAO RP2040. Find the extended datasheet link here. I also reffered to Andrian Torres Fab Lab Page to learn an in detailed description of Seeed Arduino Xiao 2040.

    The Seeed Studio XIAO RP2040 is fully compatible with the Raspberry Pi RP2040 ecosystem, as both share the same RP2040 chip. It supports multiple programming languages, including C, MicroPython, and CircuitPython, making it a great choice for beginners and developers alike. Key feature:

    Quentorres Board

    The Quentorres board is a development board designed to program the new AVR Series 1 and 2 microcontrollers. It was first created during the 2024 Instructors Bootcamp in León by Quentin Bolsée and later redesigned by Adrián Torres. The board includes a button and an LED, making it ideal for learning and practicing programming in C, Rust, Go, MicroPython, and more. Additionally, it features breakout pins, allowing users to connect external components like sensors and motors for expanded functionality. This makes the Quentorres a versatile and beginner-friendly tool for experimenting with AVR microcontrollers.

    Programming the microcontroller development board

    To program the microcontroller development board, I explored Seeed Studio, a well-known platform offering various development boards, modules, and sensors for embedded systems and IoT projects. Seeed Studio provides extensive documentation, libraries, and resources to assist in programming microcontrollers like ATtiny85, Arduino, ESP32, and Raspberry Pi.

    I referred to the Seeed Studio website to understand the programming process, including setting up the development environment, selecting the correct board, and uploading code using platforms like Arduino IDE or PlatformIO. The website also offers pre-built libraries and sample codes to help streamline development.


    Connecting XIAO-RP2040 and other components to the Quentorres Board.

    I soldered the components onto the Quentorres PCB, which includes connecting the XIAO RP2040 microcontroller.This is the Quentorres Board designed by Adrian Torres. Here’s a detailed step-by-step guide:


    Write a program for a microcontroller, and simulate its operation

    Wokwi is an online simulator for microcontrollers like Arduino, ESP32, and Raspberry Pi Pico, allowing users to build and test circuits virtually before implementing them on hardware. It supports various components, including the HC-05 Bluetooth module, ATtiny85, sensors, and displays, while enabling coding in C++, MicroPython, and CircuitPython. Wokwi also provides debugging tools to troubleshoot errors in connections and code efficiently. I used Wokwi to build my connection for an ATtiny85 blink code, allowing me to simulate and refine the setup before working with the physical components. Click here to explore the website.


    I explored some featured projects on Wokwi and chose an Arduino-based servo motor project to understand motor control and further experiment with microcontroller applications to learn the basic code language and simulator techniques.


    From Simulation to Reality

    Building an ATtiny85 LED Circuit. With a solid understanding of the simulations, we moved on to building a practical circuit using the ATtiny85 microcontroller. Our objective was to implement a functional LED blinking system, translating our virtual simulations into a real-world working model.

    Step 1: The components required to build this ATtiny85 LED circuit are:

    Step 2. The ATtiny85 requires a power connection to function

    Typically 3V–5V Power, which can be supplied using batteries or an external power source. To program the ATtiny85, an Arduino board is used as an In-System Programmer (ISP).

    ISP (In-System Programming) is a method of programming microcontrollers while they are installed in a circuit. It allows firmware updates and code uploads without removing the chip, using a programmer (e.g., an Arduino as ISP) via SPI communication.

    Connecting the ATtiny85 to the Arduino via SPI (MISO, MOSI, SCK) along with power (VCC, GND) and a reset pin. For which I used ChatGPT to understand the Pin configuration.ChatGPT command: Generate a code for blinking a led add a push button using attiny85 also give a schematic an dpin confirmation. ram rom

    Building this ATtiny85 LED circuit by using Aurdino as the ISP and power supplier. ram rom

    Step 3: Bootloading and Programming ATtiny85 Using Arduino as ISP.

    Bootloading is the process of installing a small program (bootloader) onto a microcontroller, allowing it to be programmed via an external interface like USB or ISP. It initializes the hardware, sets up clock configurations, and enables future code uploads without needing a dedicated programmer.

    Setting Up ATtiny85 Board Manager:

  • Open Arduino IDE and go to File > Preferences.
  • Under Additional Board Manager URLs, add the GitHub link that provides the ATtiny85 board definitions.
  • Navigate to Tools > Board > Boards Manager, search for ATtiny, and install the ATtiny board library.
  • ram rom ram rom

    Setting Up Arduino as ISP: Connect your Arduino Uno to your computer and open Arduino IDE. Go to File > Examples > 11. ArduinoISP > ArduinoISP and upload this code to the Arduino Uno. This converts the Arduino into an ISP programmer.

    ram rom ram rom ram rom

    Burn Bootloader:

  • In Arduino IDE, go to Tools and set:
  • Board: ATtiny85
  • Processor: ATtiny85
  • Clock: 8 MHz (Internal)
  • Programmer: Arduino as ISP
  • Click Burn Bootloader to erase the memory and set the correct clock speed.
  • Step 4: Uploading Code to ATtiny85

  • Now, write or load the desired program (e.g., LED blinking, switch-controlled LED).
  • Change the board settings back to ATtiny85.
  • >Select Arduino as ISP as the programmer.
  • Click Upload to transfer the code to ATtiny85.
  • Step 5: Testing functionality