Skip to content

Intro to Embedded Programming | Cheatsheet

As this topic is entirely new to me, I’ve absorbed a lot of information this week. So, I’ve dedicated this extra page solely for my learning notes on embedded programming. If you wish to check my work for the week, please go here to the the group assignment or the individual assignment. However, if you’re new to programming and electronics like me, feel free to explore this page; it might offer some helpful insights!

Key Concept

What is Embedded Programming?

From what I understand, embedded programming is the process of composing (writing, designing) programs for embedded systems. An embedded system is a specialized computing device that designed to be able to perform specific functions within a larger system. Example of embedded systems are the microcontrollers in home appliances like fan, mixer, thermometer, etc. These systems, the microcontrollers, typically have limited resources, including processing power, memory, and/or power consumption constraints.

Programming vs Coding

As a noob, I always thought programming = coding. That is true, to some extent:

  • Coding, is an act of writing the actual lines of code. The focus is more on the syntax and structure of the code, necessary to translate the solution into a form that a computer can understand and execute. Even though it is fundamental, it’s just one part of the broader programming process

  • Programming, obviously, involves writing codes. But doing programming does not always mean that you have to write all your own code from scratch. Instead, an act of programming refers to the broader process of ‘designing’ a set of instructions (pseudocode) or algorithms that tells a computer on how to perform a certain task or solve a problem. So, it focuses more on the understanding of the problem domain(problem-solving), planning, designing the algorithms or logics, assembling the codes, testing, and debugging.

🧠 analogy

So, imagine you want to make a poster design. Coding is like when you have to build the assets, such as icons, styles, or other graphical elements, from scratch. Meanwhile, programming is when you assemble all the pre-existing assets and elements into a cohesive poster design. So, you can leverage icons or styles that have already been developed by others and then customize them to meet your design objectives.

🤔As a beginner, I think it’s more important for me to master ‘how to program’, not ‘how to code’ (yet). Especially, in terms of understanding how to create an effective and efficient program. However, if you aim to dive deeper into the world of programming, understanding the intricacies of ‘coding’ might be essential.

Programming for Embedded Systems

So, what makes embedded programming different? Here are some notable highlights from what I’ve understood so far:

  • Hardware implementation, real-time characteristic

    The resulting code of our program will be implemented directly to control/interact with hardware components, typically for managing inputs and outputs and/or coordinating the overall behavior of the device. Due to this close interaction with hardware, embedded programming is often characterized by its ‘precision timing’ requirements (often requires real-time responsiveness to external events.)

  • Resource limitations constraints –> effective programming

    Microcontroller tend to operate under strict resource limitations, for example like limited memory, processing power, and energy consumption. I learned that the way in which we craft and structure our code, down to each line, affects storage capacity and power consumption. Therefore, it’s important to continuously evaluating and optimizing our program, to make it effective and efficient due to the MCU operating limitations. This is something that I’m looking forward to learn as well this week.

  • Challenge in testing and debugging

    Unlike other types of programming, here, there are many parameters that need to be taken into account aside from the program itself, such as the circuit/board design, how we put together the electronic components, soldering quality, and etc. Limited visibility into the system and the difficulty of simulating hardware interactions can make testing and debugging more challenging. Therefore, it’s important to build your program ‘incrementally’ and simulate/test it right away.

  • Programming Language

    For embedded programming, the common ones used are C and C++, which considered as a high-level language (more user-friendly). But sometimes low-level language (closer to the machine code and hardware) like Assembly is better for performance and resource efficiency.

In the case of embedded programming, therefore, we must have a deep understanding of both hardware and software concepts. We need to (1) understand the architecture of the embedded system that we are working with, including the specific microcontroller or processor, as well as any peripherals or sensors connected to it. We also need to (2) understand how to write efficient and optimized code to make the most of the limited resources available due to our microcontroller capabilities.

Programming Basics

The computer will read our program from top to bottom, so it’s important to understand how we should best organize/structure our code.

Programming Structure

  • Libraries

    • collections of pre-written code that provide additional functionality. Imagine a drawer consists of various tools specialized for a hair-cutting purpose.
    • only accessed when needed.
  • Variables

    • containers used to declare and to store data of different types
    • basically to introduce, to the computer, (1) the various components and/or states that will be used throughout the program, and (2) their values or where to derive them –> just like ‘setting variables’ in parametric design (?)
  • Custom function (optional)

  • Setup (function):

    • runs once when the program starts
    • to setup the tasks or environment /to initialize hardware peripherals, set pin modes, or perform any other setup tasks necessary for the program to run properly
  • Loop (function)

    • runs continuously after the setup function completes
    • contains the main program logic
    • the code inside it executes repeatedly until we stopped it (the board is powered off or reset)

Programming Vocabs

  • Datatype(s)

    • int: whole numbers value
    • const int: whole number value that cannot be changed during program execution
    • float: fractional / decimal number value
    • bool / boolean: represent true/false
    • string: characters
  • Functions: Blocks of code that perform specific tasks

  • Commands: instructions that tell what to do, such as reading, writing, printing, etc. can be used in variables, setup, or loop section. Here are some commands that I found handy.

    • Variables Section

      • #define: Declares constant values for easy reference, does not take memory space

        ex: #define LED_PIN 13

    • Setup

      • pinMode(pin, mode): Sets the mode of a digital pin (input or output).

        ex: pinMode(BUTTON_PIN, INPUT_PULLUP);

      • Serial.begin(baudrate): Initializes serial communication with a specified baud rate

        ex: Serial.begin(9600);

      Example of how a setup function looks like:

      void setup() {
      // Initialize serial communication
      Serial.begin(9600);
      
      // Set pin modes
      pinMode(LED_BUILTIN, OUTPUT);
      }
      
    • Loop

      • digitalRead(pin): reads the state (HIGH or LOW) of a digital pin.

        ex: digitalRead(BUTTON_PIN);

      • digitalWrite(pin, value): writes a digital value (HIGH or LOW) to a digital pin.

        ex: ` digitalWrite(LED_PIN, HIGH);

      Example of a loop function looks like:

      void loop() {
      // Read sensor data
      int sensorValue = analogRead(A0);
      
      // Convert sensor value to voltage
      float voltage = sensorValue * (5.0 / 1023.0);
      
      // Print voltage to serial monitor
      Serial.print("Voltage: ");
      Serial.println(voltage);
      
      // Blink LED
      digitalWrite(LED_BUILTIN, HIGH);
      delay(1000);
      digitalWrite(LED_BUILTIN, LOW);
      delay(1000);
      }