🔧 Debugging
Arduino Code –
Student Handout
(4th ESO)
C.C. Bilingüe Educrea El Mirador
Department of Technology & Digitalization
Debugging Arduino Code – Student Workbook
Traffic Lights • Sensors • Logic Structures • Systematic Debugging
Course: 4th ESO
Sessions: 2 × 55 minutes
Tools: Arduino UNO, Protoboard, LEDs, LDR, Cables, Chromebook (Google
Classroom + Arduino Cloud)
🕵️♂️
SESSION 1 — Understanding & Detecting Bugs
🎯 Learning Objectives
● Identify syntax, logic, and hardware errors.
● Compare expected vs. real behaviour of an Arduino system.
● Use Serial Monitor to observe internal program states.
● Document bugs using an engineering debugging table.
📚 Quick Theory Recap
Write notes in each box:
Concept Your Notes
🔍 What is Debugging? Debugging is the process of finding and fixing
errors, or "bugs," in computer software or hardware.
This involves identifying the root cause of
Concept Your Notes
problems, which can be syntax, logic, or runtime
errors, and then correcting them so the program
functions as intended. It is an essential part of the
development process that improves software
stability and performance
🧩 Types of Errors Execution errors: The program is syntactically
correct but fails during execution due to
impossible operations (for example, division by
zero). The program stops unexpectedly.
Logical errors: The program runs without
errors, but the result is incorrect due to flaws in
the programmer's logic or algorithm. They are
the most difficult to debug, since they do not
generate error messages.
🔌 Why test modules individually? Testing modules individually (unit testing)
isolates failures, allowing you to identify errors
immediately in a single component, rather than
searching the entire application. This
eliminates ambiguity caused by interconnected
modules, speeds up debugging, and ensures
that changes don't affect existing functionality,
resulting in more robust and maintainable
code.
🚦 Activity 1 — Traffic Light (Part 1): Detect the Errors
Take your box and prepare the circuit. You can use as reference (to reproduce) the
circuit done in tinkerCAD.
Your Arduino sketch contains intentional bugs. Run it and observe.
// INTENTIONALLY BROKEN TRAFFIC LIGHT CODE
// 4th ESO – Debugging Practice (Activity 1)
int red = 8;
int yellow = 9;
int green = 9;
int redTime = 500
int greenTime = 50;
int yellowTime = 3000;
void setup() {
pinMode(red, OUTPUT);
pinMode(yellow, OUTPUT);
}
void loop() {
// STATE 1 — RED
digitalWrite(red, HIGH);
digitalWrite(yellow, HIGH);
delay(redTime);
// STATE 2 — RED + YELLOW
digitalWrite(red, HIGH);
delay(100);
// STATE 3 — GREEN
digitalWrite(red, LOW);
digitalWrite(green, HIGH);
dely(greenTime);
// STATE 4 — YELLOW
digitalWrite(green, HIGH);
digitalWrite(yellow, HIGH);
delay(50);
}
Expected Traffic Light Sequence:
Red → Red+Yellow → Green → Yellow → Red3
Debugging Table
🐞 Bug
Observed
✔ Expected
Behaviour
❓ Possible
Cause
📎 Evidence (Serial /
Observation)
(; miss) The led would int
be fine because greenTime=50
it had the
semicolon on the
wrong side
Now it will work int green=9
We had to put because it was
int green=8 set to the wrong
pin
We had to put Now it will work int red=8
int red=12 because it was
set to the wrong
pin
🐞 Bug
Observed
✔ Expected
Behaviour
❓ Possible
Cause
📎 Evidence (Serial /
Observation)
We had to put Now it will work int yellow=9
int yellow=10 because it was
set to the wrong
pin
We had to add It wasn’t be a yellow+redTime
a yellowredTime time and we
added
We had to put it All the digital All digital writes
well, putting the write were ON wrong
LOW, HIGH
words in each
place so it could
work
We had to The times were Wrong Time
change the time wrong because
because it was the times were
so short small
We had to instead of delay wrong
change delay putting delay it written
instead of dely put dely
We had to the cables were In the circuit,
situate each in the wrong cables wrong
cable in the place situated
right place so it
could work
We had to put The time was Time problem
less time wrong in the circuit
CODE
int red = 12;
int yellow = 10;
int green = 8;
int redTime = 10000
;int greenTime = 10000;
int yellowTime = 2000;
int yellowredTime = 2000;
void setup() {
pinMode(red, OUTPUT);
pinMode(yellow, OUTPUT);
void loop() {
// STATE 1 — RED
digitalWrite(red, HIGH);
digitalWrite(yellow, LOW);
digitalWrite(green, LOW);
delay(redTime);
// STATE 2 — RED + YELLOW
digitalWrite(red, HIGH);
digitalWrite(yellow, HIGH);
digitalWrite(green, LOW);
delay(yellowredTime);
// STATE 3 — GREEN
digitalWrite(red, LOW);
digitalWrite(yellow, LOW);
digitalWrite(green, HIGH);
delay(greenTime);
// STATE 4 — YELLOW
digitalWrite(green, LOW);
digitalWrite(yellow, HIGH);
digitalWrite(red, LOW);
delay(yellowTime);
BEFORE YOU MOVE ON CHECK WITH THE TEACHER THE CORRECT
BEHAVIOUR OF THE CIRCUIT
💡 Activity 2 — LDR Sensor Readings
Build circuit in the picture:
Upload this minimal reading sketch:
void setup() {
[Link](9600);
}
void loop() {
int value = analogRead(A0);
[Link](value);
delay(200);
}
Record your measurements:
Condition Reading Notes
🌑 Dark 0 We did it without light
🌤 Normal 284 We did it with normal
light
☀ Bright 620 We did it with the
maximum light in the
window
➡ Propose a useful threshold: ___0-620___
Change the resistor from 1KOhm to 6,8KOhm and record your measurements again:
Condition Reading Notes
🌑 Dark 15 We did it without light
🌤 Normal 712 We did it with normal
light
☀ Bright 921 We did it with the
maximum light in the
window
➡ Propose a useful threshold: ___15-921____
BEFORE YOU MOVE ON CHECK WITH THE TEACHER THE CORRECT
BEHAVIOUR OF THE CIRCUIT.
📝 Exit Ticket (Session 1)
● One bug I understand clearly: Is that I have to pay attention in all the code_
● Something I want help with: I don’t know
The leds will light up in 4 states in the first the red and yellow light up, in the second
the red ones light up, in the third the green light up and in the third green and yellow
light up