Lesson 4: Conditional Statements with Arduino
Teach your program how to make decisions with `if`, `else`, and comparisons
32-Lesson Course • Semester 1
You already know variables, calculations, and Serial output. Now it is time to make your programs choose between different actions. Conditional statements let Arduino decide what to do based on a value or situation.
Learning Objectives
By the end of this Lesson, you will:
- Understand what `if` and `else` do in Arduino code
- Use comparison operators like `==`, `!=`, `<`, `>`, `<=`, and `>=`
- Write simple decision-making code with the Serial Monitor
- Control an LED differently depending on a button state or a value
Skills Developed:
- Tracing program flow step by step
- Testing different outcomes using values and inputs
- Reading conditions carefully
- Preparing for loops, functions, and hardware control later
Section 1: What Is a Conditional Statement?
A conditional statement tells the program to check whether something is true. If it is true, one block of code runs. If it is false, a different block can run instead.
Everyday idea
If it is raining, bring an umbrella. Else, leave the umbrella at home. Arduino code works the same way: it checks a condition and then decides which action to take.
if (condition) {
// code to run if the condition is true
} else {
// code to run if the condition is false
} Section 2: Comparison Operators
A condition usually compares two values. These comparison operators help Arduino decide if something is true or false.
Useful operators
==means equal to!=means not equal to<means less than>means greater than<=means less than or equal to>=means greater than or equal to
Important detail
= assigns a value. == compares two values. Beginners often mix these up, so read conditions carefully.
Section 3: A First `if` Statement
Start with a simple value in a variable. Then let the program decide what message to print.
void setup() {
Serial.begin(9600);
int score = 85;
if (score >= 70) {
Serial.println("You passed.");
}
}
void loop() {
} What happens?
- The program stores `85` in `score`
- It checks `score >= 70`
- That condition is true, so the message prints
Section 4: Using `if` and `else`
An `if` statement can also include an `else` block. That gives the program a different action when the condition is false.
void setup() {
Serial.begin(9600);
int temperature = 62;
if (temperature > 75) {
Serial.println("It is warm.");
} else {
Serial.println("It is not warm.");
}
}
void loop() {
} Only one of these blocks runs each time the program checks the condition.
Section 5: More Than Two Choices with `else if`
Sometimes a program needs more than just yes or no. `else if` lets you check another condition if the first one is false.
void setup() {
Serial.begin(9600);
int score = 92;
if (score >= 90) {
Serial.println("Grade: A");
} else if (score >= 80) {
Serial.println("Grade: B");
} else if (score >= 70) {
Serial.println("Grade: C");
} else {
Serial.println("Keep practicing.");
}
}
void loop() {
} Order matters
Arduino checks from top to bottom. As soon as one condition is true, it runs that block and skips the rest.
Section 6: Using Conditionals with Hardware
Decision-making becomes even more useful when the program reacts to a real input. A button can tell the Arduino when to turn an LED on or off.
Hardware setup: wire the button and LED first
LED wiring
- Connect a jumper wire from Arduino pin
8to a220Ωresistor - Connect the resistor to the long leg of the LED
- Connect the short leg of the LED to
GND
Button wiring
- Place the pushbutton across the center gap of the breadboard
- Connect one side of the button to Arduino pin
2 - Connect the other side of the button to
GND - Because the code uses
INPUT_PULLUP, you do not need an extra resistor for the button
2 reads HIGH. When the button is pressed, it connects to GND, so pin 2 reads LOW.
const int ledPin = 8;
const int buttonPin = 2;
void setup() {
pinMode(ledPin, OUTPUT);
pinMode(buttonPin, INPUT_PULLUP);
}
void loop() {
int buttonState = digitalRead(buttonPin);
if (buttonState == LOW) {
digitalWrite(ledPin, HIGH);
} else {
digitalWrite(ledPin, LOW);
}
} This is a real Arduino decision: if the button is pressed, the LED turns on. Else, it stays off.
Section 7: Common Beginner Mistakes
Mistake 1
Using = instead of == in a condition.
Mistake 2
Forgetting that `INPUT_PULLUP` usually reads `LOW` when pressed.
Mistake 3
Expecting every `if` block to run even after a true condition was already found in an `else if` chain.
Mistake 4
Not checking the actual value in the Serial Monitor while debugging.
Hands-On Activity: Pass, Retry, or Excellent
Write a program that stores a score in a variable and prints one of three messages:
- If the score is `90` or higher, print `Excellent work!`
- Else if the score is `70` or higher, print `You passed.`
- Else, print `Try again.`
void setup() {
Serial.begin(9600);
int score = 76;
if (score >= 90) {
Serial.println("Excellent work!");
} else if (score >= 70) {
Serial.println("You passed.");
} else {
Serial.println("Try again.");
}
}
void loop() {
} Extension: change the value of `score` and predict what will print before you run the program.
Assessment and Homework
Lesson 4 quiz topics
- What `if`, `else`, and `else if` do
- Comparison operators
- How program flow changes based on a condition
- Simple button and LED decision-making
- Common conditional mistakes
Homework ideas
- Write two small `if`/`else` programs using different numbers
- Change conditions and predict what will happen before running the code
- Practice reading button logic with `INPUT_PULLUP`
- Be ready to learn counted repetition and loops in Lesson 5