← All modulesBeginner
Day 3

Embedded Programming

Boards, the IDE, and your first working code.

Day 3

Introduction to Embedded Programming

From blank board to blinking LED

Arduino / ESP32 / STM32 boardsThe IDE and sketch structuresetup(), loop(), pinMode()Serial monitor for debuggingLED control & PWM fadingPotentiometer & analog inputData types, variables, operators
ElectroLab · Embedded Programming1 / 19

Navigate with ← → or Space · F to present fullscreen · N for the script

Lesson notes

Pick a board, install the IDE, and learn the anatomy of a sketch: setup(), loop(), pins, the serial monitor, blinking and fading LEDs, reading a potentiometer, plus data types and operators.

Day 3

Introduction to Embedded Programming

From blank board to blinking LED

  • Arduino / ESP32 / STM32 boards
  • The IDE and sketch structure
  • setup(), loop(), pinMode()
  • Serial monitor for debugging
  • LED control & PWM fading
  • Potentiometer & analog input
  • Data types, variables, operators
Topic 1

Choosing a Board

Arduino vs ESP32 vs STM32

Arduino UnoESP32STM32
CoreATmega328 · 8-bitDual 32-bitARM Cortex-M
Speed16 MHz240 MHz72–480 MHz
WirelessNoneWi-Fi + BluetoothNone (varies)
Best forLearning basicsIoT & connectivityPerformance & control

For this bootcamp we mostly use ESP32 — it has Wi-Fi/BT and is cheap, but Arduino code runs on all of them.

The boards you will program

Different brains, same idea — pins on the edges, USB to upload code.

  • Arduino Uno — classic 8-bit, beginner-friendly
  • ESP32 — fast, Wi-Fi + Bluetooth
Topic 2

The IDE & Sketch Structure

Two functions run every sketch

setup()

  • Runs ONCE at power-on/reset
  • Configure pins, start Serial
  • Initialize sensors and libraries

loop()

  • Runs FOREVER, over and over
  • Your main program logic
  • Read sensors → decide → act

Walk through: install IDE, select board, select port, click Upload. The classic flow.

pinMode + the skeleton

  • pinMode() tells a pin to be an INPUT or OUTPUT
  • Serial.begin() opens the USB link for debugging
void setup() {
  pinMode(13, OUTPUT);   // set pin 13 as an output
  Serial.begin(115200);  // start serial at 115200 baud
}

void loop() {
  // your code repeats here, forever
}
Topic 3

Serial Monitor — your debugger

Print values to see what is happening

  • Match the baud rate in the monitor to Serial.begin()
  • print() stays on the line; println() moves to the next
void setup() {
  Serial.begin(115200);
}

void loop() {
  int reading = analogRead(34);
  Serial.print("Sensor: ");
  Serial.println(reading);   // println adds a new line
  delay(500);
}

Teach "printf debugging" — when in doubt, print the variable. It is the #1 beginner debugging skill.

Topic 4

LED Control & PWM

Blink, then fade

  • digitalWrite = fully ON or OFF
  • analogWrite uses PWM — rapid on/off pulses fake an in-between brightness
// Blink
digitalWrite(13, HIGH);  delay(500);
digitalWrite(13, LOW);   delay(500);

// Fade with PWM (analogWrite: 0–255)
for (int b = 0; b <= 255; b++) {
  analogWrite(ledPin, b);   // brightness
  delay(5);
}

PWM = Pulse Width Modulation

The pin switches on/off thousands of times a second. The fraction of time it stays ON — the "duty cycle" — sets the average power.

  • 25% duty = dim · 75% = bright
  • Same trick sets motor speed
  • analogWrite(pin, 0–255)

The bar shows average power tracking the duty cycle — the LED brightness follows it.

Topic 5

Analog Input — the potentiometer

Read a knob, control brightness

  • analogRead returns a number across the ADC range
  • map() rescales one range to another — incredibly useful
int raw = analogRead(34);          // 0–4095 on ESP32
int duty = map(raw, 0, 4095, 0, 255);
analogWrite(ledPin, duty);          // knob sets brightness

Digital vs Analog pins

Digital

  • Only HIGH or LOW (1 or 0)
  • digitalRead / digitalWrite
  • Buttons, LEDs, logic signals

Analog

  • A range of values (a voltage)
  • analogRead in, PWM out
  • Sensors, knobs, dimming
Topic 6

Data Types & Operators

Common data types

TypeHoldsExample
intwhole numbers-5, 0, 42
floatdecimals3.14, 0.5
booltrue / falsetrue
charone character'A'
Stringtext"hello"
byte0–255 (8 bits)0xFF

Variables, constants & operators

Storing values

  • int ledPin = 13; — a variable
  • #define PIN 13 — text replaced before compile
  • const int PIN = 13; — typed, safer constant

Operator families

  • Arithmetic: + − * / %
  • Relational: == != < > <= >=
  • Logical: && || !
  • Bitwise: & | ^ ~ << >>

Prefer const over #define in modern code — it is type-checked. Bitwise ops matter for registers and flags.

Day 4 — Key Takeaways

  • setup() runs once; loop() runs forever
  • pinMode sets direction; digitalWrite/Read for on-off
  • Serial.println is your debugger
  • PWM (analogWrite) fakes analog output for fading & speed
  • analogRead + map() turns a knob into any range
  • Choose the right data type; know your operator families