Introduction to embedded programming
Embedded programming involves programming microcontrollers, which are small integrated circuits that bring together the essential elements of a computer. Microcontrollers can be found in a variety of everyday objects (robots, computers, cars…).
In this introductory course, you’ll learn how to use an Arduino board and its associated software, in order to assimilate the basics of embedded programming for your future personal projects.
For any comments or questions, feel free to email me at .
Requirements
Software
- A computer with Arduino IDE software.
Hardware
The microcontroller
Components
Components are fairly easy to find on online sales sites. One piece of advice I can give is to buy component kits which already include the components listed below, with a few additional components that you can test on your own.
- Three colored LEDs
- One push button
- Three resistors of 220 ohms
- One resistor of 10 kilo-ohms
- A potentiometer
- An impedance buzzer
- A breadboard
- Some prototyping cables
Missions
For each level, a circuit diagram will be proposed like the one below:
Try to build the circuit as shown. Once done, you can connect your Arduino board to your computer and open the Arduino IDE software. Let’s explore the interface quickly with a code to turn on the red LED:
- The verify button ☑️ allows you to check your code for errors before sending it to the board.
- The upload button ➡️ sends the compiled code to the Arduino board for execution.
We define an integer corresponding to the board pin to which we’ve connected the LED, (here pin 2):
const int ledPin = 2;
As the LED is an output (i.e. we send current from the Arduino board to the LED to turn it on), we configure the ledPin
pin as such with the pinMode()
function:
pinMode(ledPin, OUTPUT);
In the loop()
function, we use digitalWrite()
to set the LED to the HIGH state, thus turning it on:
digitalWrite(ledPin, HIGH);
After compiling and uploading, all the code included in the setup
function will run only once (in this case, the definition of ledPin
as a digital output). As for the code in the loop
function, it will loop indefinitely!
Tip
A few tips to get you started:
- A cheat sheet containing syntax and useful functions is available further down the page.
- Don’t forget the semicolon “;” at the end of each instruction.
- Use syntax highlighting and the verify button to check your code.
- Pay attention to lowercase/UPPERCASE (case sensitivity) in variable names: purple ≠ Purple.
- If you ever get stuck, a solution for each mission is also provided.
- Don’t hit your computer (or anyone else) if it doesn’t work!
Level 1 - Blinking
Level 1 Wiring
Mission
Make the LED blink by changing its state every second.
Level 2 - Traffic Light
Level 2 Wiring
Mission
Change the state of the 3 LEDs successively.
Level 3 - Day, Night, Day…
Level 3 Wiring
Mission
Command the LED to light up using a button.
Tip
- Should the button be configured as an input or an output?
- Maybe there’s a function to read the state of a digital pin… Maybe it’s even in cheat sheet. Handy, isn’t it?
Level 4 - Day, Night, soft version
Level 4 Wiring
Mission
Control the LED brightness using a potentiometer.
Tip
The potentiometer pin is connected to an analog port. Hmm… Analog… analog
…
Level 5 - ♪ Happy Birthday ♪
Level 5 Wiring
Mission
Play “Happy Birthday” when the button is pressed.
Frequencies of notes and (very) simplified “Happy Birthday” music sheet:
Note | Do | Do# | Ré | Ré# | Mi | Fa | Fa# |
---|---|---|---|---|---|---|---|
f(Hz) | 262 | 277 | 294 | 311 | 330 | 349 | 370 |
Note | Sol | Sol# | La | La# | Si | Do2 |
---|---|---|---|---|---|---|
f(Hz) | 391 | 415 | 440 | 466 | 493 | 523 |
Tip
There are 25 notes played in “Happy birthday”. We could use the brute-force method: writing 25 tone()
& noTone()
calls, but I’m sure there’s a smarter way! In the simplified music sheet, one note color corresponds to a different duration — don’t forget to vary delays accordingly.
Going further
Congrats if you’ve completed all the levels! If it was too easy for you, try creating a project using all your components. For example: a variable brightness traffic light that beeps when it turns red to warn pedestrians? Feel free to search the web for fun tutorials.
Arduino is a great platform to learn embedded programming quickly and build small projects. But if you want to go further, you may look into STM32 microcontrollers — less beginner-friendly but widely used in industry and allows you to carry more complex projects.
Cheat Sheet
Variables
Type | Description | Size |
---|---|---|
int | Integer | 4 bytes |
float | Decimal number | 4 bytes |
char | Character | 1 byte |
double | Double-precision decimal | 8 bytes |
Operators
Type | Operators |
---|---|
Arithmetic | + , - , * , / , % |
Assignment | = , += , -= , %= , |= , ^= , <<= , >>= |
Logical | && , || , < , > , <= , >= , == , != |
Conditions
if (x < 3) {
printf("x is less than 3");
} else {
printf("x is 3 or more");
}
Loops
int x = 0;
while(x < 5){
printf("I must not hit my computer.\n");
x++;
}
int sum = 0;
for(int i = 0; i < 100; i++){
sum += i;
}
Preprocessor directives
// Inclusion of a library, in this case servo
#include <servo.h>
// Define constants
#define N 7
#define SIZE 3
#define SIZE2 6
Arrays
int tab[SIZE] = {1, 8, 0, 5};
char tabchar[SIZE2] = {'c', 'h', 'r', 'o', 'm', 'a'};
// Initialize an array to 0
for (int i = 0; i < SIZE; i++) {
tab[i] = 0;
}
Arduino Functions
Function | Description |
---|---|
pinMode(int pin, INPUT) | Set the mode of a pin (INPUT or OUTPUT ) |
digitalWrite(int pin, HIGH) | Set a digital pin state (HIGH = 1, LOW = 0) |
digitalRead(int pin) | Read the state of a digital pin |
delay(1000) | Wait for 1000 ms (1 second) |
analogWrite(int pin, int dutyCycle) | Create a PWM signal with dutyCycle ∈ [0; 255] |
analogRead(int pin) | Read the value from an analog pin |
Serial.begin(int baud) | Start serial communication (commonly baud = 9600) |
Serial.println("Chroma") | Print “Chroma” to the serial monitor |
map(val, Imin, Imax, Omin, Omax) | Rescale a value from range [Imin ; Imax ] to [Omin ; Omax ] |
tone(pin, frequency) | Play a sound of frequency frequency on a buzzer |
noTone(pin) | Stop the sound from the buzzer |
Servo myServo | Create a servo control object |
myServo.attach(pin) | Attach pin pin to the servo object |
myServo.write(90) | Set the servo position (here to 90°) |
Did you know?
The programming language used with Arduino is C++. It’s also used to build mobile apps, video games, desktop software, robotics, and more.
Mission Solutions
These codes are suggested corrections written by me. Don’t treat them as the only solution — there are many valid ways to solve the same problem. Find the one that suits you best!
Level 1 Solution - Blinking
1const int ledPin = 2;
2
3void setup() {
4 pinMode(ledPin, OUTPUT);
5}
6
7void loop() {
8 digitalWrite(ledPin, HIGH);
9 delay(1000);
10
11 digitalWrite(ledPin, LOW);
12 delay(1000);
13}
Level 2 Solution - Traffic Light
1const int redLed = 2;
2const int yellowLed = 3;
3const int greenLed = 4;
4
5void setup() {
6 pinMode(redLed, OUTPUT);
7 pinMode(yellowLed, OUTPUT);
8 pinMode(greenLed, OUTPUT);
9}
10
11void loop() {
12 digitalWrite(redLed, HIGH);
13 digitalWrite(yellowLed, LOW);
14 digitalWrite(greenLed, LOW);
15 delay(1000);
16
17 digitalWrite(redLed, LOW);
18 digitalWrite(yellowLed, HIGH);
19 digitalWrite(greenLed, LOW);
20 delay(1000);
21
22 digitalWrite(redLed, LOW);
23 digitalWrite(yellowLed, LOW);
24 digitalWrite(greenLed, HIGH);
25 delay(1000);
26}
Level 3 Solution - Day, Night, Day…
1const int ledPin = 2;
2const int buttonPin = 4;
3
4void setup() {
5 pinMode(ledPin, OUTPUT);
6 pinMode(buttonPin, INPUT);
7}
8
9void loop() {
10 int buttonState = digitalRead(buttonPin);
11
12 if (buttonState == HIGH){
13 digitalWrite(ledPin, HIGH);
14 }
15 else{
16 digitalWrite(ledPin, LOW);
17 }
18}
Level 4 Solution - Day, Night, soft version
1const int ledPin = 3;
2const int potPin = A0;
3
4void setup() {
5 pinMode(ledPin, OUTPUT);
6}
7
8void loop() {
9 int potValue = analogRead(potPin);
10 int brightness = map(potValue, 0, 1023, 0, 255);
11
12 analogWrite(ledPin, brightness);
13}
Level 5 Solution - ♪ Happy Birthday ♪
1#define DO 262
2#define RE 294
3#define MI 330
4#define FA 349
5#define SOL 392
6#define LA 440
7#define LAd 466
8#define DO2 524
9
10#define SIZE 25
11
12const int buzzerPin = 2;
13const int buttonPin = 4;
14
15int frequences[SIZE] = {
16 DO, DO, RE, DO, FA, MI,
17 DO, DO, RE, DO, SOL, FA,
18 DO, DO, DO2, LA, FA, MI, RE,
19 LAd, LAd, LA, FA, SOL, FA
20
21};
22
23int durations[SIZE] = {
24 400, 200, 500, 500, 500, 900,
25 400, 200, 500, 500, 500, 900,
26 400, 200, 500, 500, 500, 500, 900,
27 400, 200, 500, 500, 500, 900
28};
29
30void setup() {
31 pinMode(buzzerPin, OUTPUT);
32 pinMode(buttonPin, INPUT);
33}
34
35void loop() {
36
37 int buttonState = digitalRead(buttonPin);
38 if (buttonState == HIGH){
39 happyBirthday();
40 }
41}
42
43
44void happyBirthday(){
45 for (int i = 0; i < SIZE; i++) {
46 tone(buzzerPin, frequences[i]);
47 delay(durations[i]);
48 noTone(buzzerPin);
49 }
50}
Credits
- Writer: Ousmane THIONGANE
- Lastest update: May 2025
- Reviewer: Loubna LATRECHE