Arduino build error

error: 'ledPin' was not declared in this scope

The compiler reached a name it does not know. It has not been told what ledPin is, so it stops.

  • Build error
  • 4 common causes
  • 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.

Broken
void setup() {
  Serial.begin(9600);
  pinMode(13, OUTPUT);
  digitalwrite(13, HIGH);
  Serial.println(digitalRead(13));
}

void loop() {
}
Fixed
void setup() {
  Serial.begin(9600);
  pinMode(13, OUTPUT);
  digitalWrite(13, HIGH);
  Serial.println(digitalRead(13));
}

void loop() {
}

Mokxi’s build tab says: Arduino spells it digitalWrite, not digitalwrite. Names in C++ must match exactly, capital letters included.

Try this fix in Mokxi

2The variable lives inside a different function

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

Try this fix in Mokxi

3The variable is declared below the line that uses it

The compiler reads from the top of the file down. Unlike a function in a .ino tab, a variable has to be written above its first use.

Broken
void setup() {
  Serial.begin(9600);
}

void loop() {
  Serial.println(limit);
  delay(1000);
}

int limit = 10;
Fixed
int limit = 10;

void setup() {
  Serial.begin(9600);
}

void loop() {
  Serial.println(limit);
  delay(1000);
}

Mokxi’s build tab says: limit is declared on line 10, below this line, and C++ reads from the top down.

Try this fix in Mokxi

4A library object is used without its #include

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.

Broken
void setup() {
  Serial.begin(9600);
  Wire.begin();
  Serial.println("wire ready");
}

void loop() {
}
Fixed
#include <Wire.h>

void setup() {
  Serial.begin(9600);
  Wire.begin();
  Serial.println("wire ready");
}

void loop() {
}

Mokxi’s build tab says: Wire comes from a library the sketch does not include. Add #include <Wire.h> at the top of the sketch.

Try this fix in Mokxi

FAQ

Questions and answers

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.