Using Arduino if else Statements for Conditional Logic
Using Arduino if else Statements for Conditional Logic
When you need Arduino to make decisions based on situations, such as turning on a fan when temperature exceeds 30°C, understanding if else statements is essential before writing conditional code.
How if Works
The if statement takes a condition and evaluates it. If the condition is true, the code inside the curly braces { } executes. If false, it skips over that block.
int temperature = 32;
if (temperature > 30) {
// This code runs if temperature is greater than 30
Serial.println("Temperature high, turning on fan");
}
In this example, temperature = 32 which is greater than 30, so the condition is true and Arduino sends the message through Serial Monitor immediately.
Adding else for the Alternative Case
If you want a backup action when the condition is false, add else to handle that scenario.
int temperature = 25;
if (temperature > 30) {
Serial.println("Temperature high, turning on fan");
} else {
// This code runs when temperature is 30 or less
Serial.println("Temperature normal");
}
When temperature = 25, the condition temperature > 30 is false, so Arduino executes the else block instead of the if block.
Common Comparison Operators
| Operator | Meaning | Example |
|---|---|---|
| == | Equal to | i == 1 |
| != | Not equal to | x != 0 |
| < | Less than | value < 100 |
| > | Greater than | temp > 30 |
| <= | Less than or equal to | count <= 10 |
| >= | Greater than or equal to | level >= 5 |
A critical mistake to avoid is using = instead of == in conditions. The single = assigns a value rather than comparing values, causing your code to behave incorrectly without any error message.
Using && and || to Combine Conditions
When dealing with more complex conditions, logical operators help combine multiple checks.
int temperature = 28;
int humidity = 80;
// && requires both conditions to be true
if (temperature > 25 && humidity > 70) {
Serial.println("Hot and humid, turning on fan");
}
// || executes if either condition is true
if (temperature > 35 || humidity > 90) {
Serial.println("Extreme weather conditions");
}
Testing with Serial Monitor
For those without sensor hardware yet, you can simulate input through Serial Monitor to practice if else logic first.
char key;
void setup() {
Serial.begin(9600);
}
void loop() {
if (Serial.available() > 0) {
key = Serial.read();
if (key == '1') {
Serial.println("You pressed 1, status ON");
} else if (key == '2') {
Serial.println("You pressed 2, status OFF");
} else {
Serial.println("Unknown command");
}
}
}
[image: Serial Monitor displaying results when sending values 1 and 2 to Arduino with labels showing which condition matches each message]
How to test:
- Upload the code above to your Arduino board
- Open Serial Monitor and set Baud Rate to 9600
- Type number 1 or 2 and click Send
- Observe the output on Serial Monitor
Pressing 1 shows “You pressed 1, status ON”. Pressing other numbers triggers the else block and displays “Unknown command”.
Summary
The if statement checks a condition and executes code in braces when true. else provides an alternative path when the condition is false. Comparison operators (==, !=, <, >, <=, >=) and logical operators (&&, ||) enable complex condition building. Try applying this knowledge to control LEDs, motors, or various sensors in your own projects.
อยากทำโปรเจคแบบนี้?
รับทำโปรเจค Arduino / IoT จบงานไว ส่งงานครบ พร้อมสอน
If you need Arduino project service or urgent IoT development, see full service details on the home page
จ้างทำโปรเจคเลย