Seen 1,904 times in failed builds on Mokxi in the 30 days to November 16, 2026.
The message
What the Arduino IDE prints
Output window
sketch.ino: In function 'void loop()':
sketch.ino:6:3: error: 'ledPin' was not declared in this scope
exit status 1
Compilation error: exit status 1
A name is known only after it is declared, and only inside the curly brackets where it was declared. Read the line number in the message, then check the spelling, the capital letters and where that name was first written.
Mokxi's compiler, clang, words the same mistake as use of undeclared identifier 'ledPin'.
Causes
The 4 usual causes with the fix
1The name is spelled or capitalized differently
C++ treats digitalwrite and digitalWrite as two different names, and the Arduino calls are all written with a capital in the middle. The same goes for your own variables: ledpin is not ledPin.
A variable made between the curly brackets of setup() disappears when setup() ends. loop() cannot see it. Make it above both functions, outside every pair of brackets, and both can use it.
Broken
void setup() {
Serial.begin(9600);
int total = 2 + 3;
}
void loop() {
Serial.println(total);
delay(1000);
}
Fixed
int total = 0;
void setup() {
Serial.begin(9600);
total = 2 + 3;
}
void loop() {
Serial.println(total);
delay(1000);
}
Mokxi’s build tab says: total is declared inside setup() on line 3, so only the lines in there can use it
Wire, Servo and the other library names exist only after the sketch includes the library header. Without the line, the first use of the name is the error.
What does 'was not declared in this scope' mean in Arduino?
The compiler found a name it has no declaration for at that point in the file. The name is misspelled, declared in another function, declared further down, or it comes from a library the sketch never included.
Why does the error point at a line that looks fine?
The line is where the name is used, not where the mistake is. The mistake can be a typo in the declaration or a declaration that sits in the wrong place.
What is the difference between this and 'does not name a type'?
'Was not declared' is about a name used in an expression or a call. 'Does not name a type' is about a name used where a type such as int or Servo belongs.