What You See
You click Verify (the checkmark) or Upload (the arrow) and instead of "Done compiling," you get red error text at the bottom of the Arduino IDE. The sketch will not compile, so it never gets to the board. The error might say something like:
expected ';' before '}' token'myVariable' was not declared in this scopeexpected ')' before ';' tokenredefinition of 'int x''Serial' was not declared in this scopeexpected unqualified-id before '{' token
These error messages are generated by the C++ compiler, and they are not always beginner-friendly. Sometimes the line number the error points to is not even where the actual mistake is. Let's decode the most common ones.
Important: The Error Line Number Can Be Wrong
The compiler reads your code from top to bottom. When it hits something that does not make sense, it reports an error at that point. But the actual mistake might be on an earlier line. For example, if you forget a semicolon on line 10, the compiler might not notice until line 11, and it reports the error on line 11.
Rule of thumb: When you get an error, look at the reported line first, but also look at the line right above it. That is where the problem usually is.
Most Common Errors and How to Fix Them
1. expected ';' before...
error: expected ';' before '}' token
error: expected ';' before 'int'
error: expected ';' before ')' token
What it means: You forgot a semicolon at the end of a statement. Every statement in C++ must end with a semicolon.
The mistake:
int x = 10 // Missing semicolon here
int y = 20;
The compiler sees int x = 10 int y = 20; and gets confused at the second int because it expected a semicolon after 10.
Fix: Go to the line mentioned in the error (or the line above it) and add the missing semicolon.
Common places people forget semicolons:
- After variable declarations:
int x = 10; - After function calls:
Serial.println("hello"); - After
returnstatements:return 0; - NOT after
if,for,while, or function headers. Those use braces, not semicolons.
2. was not declared in this scope
error: 'myVar' was not declared in this scope
error: 'Serail' was not declared in this scope
What it means: The compiler cannot find a variable or function with that name. Reasons:
- Typo. You typed
Serailinstead ofSerial, ordigitalwriteinstead ofdigitalWrite. C++ is case-sensitive.Setupis not the same assetup.Digitalwriteis not the same asdigitalWrite.
- Variable declared in a different scope. If you declare a variable inside
setup(), it does not exist inloop():
void setup() {
int pin = 10; // Only exists in setup()
}
void loop() {
digitalWrite(pin, HIGH); // Error: 'pin' not declared
}
- Missing #include. If you use a library like
Servobut forget#include <Servo.h>, the compiler does not know whatServomeans.
Fix: Check spelling carefully (especially capitalization). If it is a variable, make sure it is declared in a scope that includes where you are using it. If it is a library function or class, add the #include at the top.
3. expected ')' before...
error: expected ')' before ';' token
error: expected ')' before '}' token
What it means: You have an unmatched parenthesis. You opened a ( but never closed it with ), or you have an extra ).
The mistake:
Serial.println("Hello"; // Missing closing )
if (x > 10 { // Missing closing ) before {
Fix: Count your parentheses. Every ( needs a matching ). Most code editors highlight matching brackets when you click on one.
4. Mismatched Braces { }
error: expected '}' at end of input
error: expected unqualified-id before '{' token
What it means: Your curly braces do not match. You have more { than } or vice versa. This is especially common with nested if statements, for loops, and functions.
The mistake:
void loop() {
if (x > 10) {
Serial.println("big");
if (x > 100) {
Serial.println("very big");
}
// Missing the closing } for the outer if
}
Fix: Use proper indentation. Every time you open a {, indent the code inside. When you close with }, go back to the previous indentation level. This makes mismatches visible:
void loop() { // Level 0
if (x > 10) { // Level 1
Serial.println("big"); // Level 2
if (x > 100) { // Level 2
Serial.println("very big"); // Level 3
} // Closes inner if
} // Closes outer if
} // Closes loop
5. redefinition of...
error: redefinition of 'int x'
What it means: You declared the same variable twice in the same scope.
The mistake:
int ledPin = 10;
int ledPin = 11; // Error: already declared
Or accidentally declaring a variable in a loop:
void loop() {
int count = 0; // This resets to 0 EVERY iteration
count++; // Will always be 1
Serial.println(count); // Always prints 1
}
Fix: Remove the duplicate declaration. If you need a variable that persists across loop() iterations, declare it globally (outside any function) or use static:
void loop() {
static int count = 0; // Only initialized once, persists
count++;
Serial.println(count); // 1, 2, 3, 4...
}
6. void setup() and void loop() Issues
Wrong capitalization:
void Setup() { } // Wrong! Must be lowercase: setup
void Loop() { } // Wrong! Must be lowercase: loop
Missing one of them: Arduino requires both setup() and loop(). Even if you do not need one, include an empty version:
void setup() {
// your code
}
void loop() {
// empty is fine
}
Misspelled:
void setp() { } // Typo! The compiler will not find setup()
7. Strings: Single vs. Double Quotes
In C++, single quotes are for single characters ('A'), and double quotes are for strings ("Hello"). Mixing them up causes confusing errors.
Serial.println('Hello'); // Wrong: single quotes around a string
Serial.println("H"); // Right: double quotes for any text
char c = 'H'; // Right: single quotes for one character
How to Read Arduino Error Messages
The format is usually:
filename.ino:lineNumber:column: error: description
For example:
sketch.ino:15:3: error: expected ';' before 'int'
This means: In file sketch.ino, on line 15, column 3, the compiler expected a semicolon but found the word "int." The actual missing semicolon is probably at the end of line 14.
The Strategy for Fixing Compiler Errors
- Fix the FIRST error only. The compiler often reports many errors, but most of them are caused by the first one. Fix the first error, then re-compile. Often, all the other errors go away.
- Look at the line number, then look one line up. The mistake is often on the line before the one reported.
- Read the error message literally. "expected ';'" means it wanted a semicolon. "was not declared" means it does not recognize a name. Take it at face value.
- Check capitalization. C++ is case-sensitive.
digitalwriteis notdigitalWrite.setupis notSetup.
- Count brackets. For every
(there should be a). For every{there should be a}.
- Use Edit > Auto Format (Ctrl+T) in the Arduino IDE. It re-indents your code properly, which makes mismatched braces much easier to spot.
Test: Minimum Valid Sketch
If you are deep in error hell and cannot figure out what is wrong, start from scratch with the absolute minimum valid sketch and add your code back line by line:
void setup() {
}
void loop() {
}
This compiles. Now add your code back one piece at a time, compiling after each addition. When it breaks, the last thing you added contains the error.
Quick Reference: Syntax Rules
| Rule | Example | |------|---------| | Every statement ends with ; | int x = 10; | | Function calls need () | Serial.begin(9600); | | Blocks use {} | if (x > 5) { ... } | | Strings use double quotes | "Hello" | | Characters use single quotes | 'A' | | Case matters | digitalWrite not digitalwrite | | Both setup() and loop() required | Always, even if empty | | #include has no semicolon | #include <Servo.h> | | Comments: // for single line | // this is a comment | | Comments: /* / for multi-line | / block comment */ |