Fix "error: 'x' was not declared in this scope" (Arduino, C++)
error: 'x' was not declared in this scope
- sketch.ino:12:3: error: 'LED_BUILTIN' was not declared in this scope
- error: 'ledPin' was not declared in this scope; did you mean 'ledpin'?
- error: ‘myFunction’ was not declared in this scope
- error: 'WiFi' has not been declared
- error: 'Servo' does not name a type
The compiler meets a name it does not know at that point in the code. It is almost always a typo or wrong case, a variable declared in another function, a missing #include or library, or a wrong board selection that does not define that name (such as LED_BUILTIN).
What this error means
In C++, every name (variable, function, constant, class) must be declared before it is used, and is only visible within its scope: the { } block where it is declared, the file, or what the #include lines bring in. When GCC meets a name it cannot find in any visible scope, it stops with "was not declared in this scope", often followed by a "did you mean" suggestion. The "has not been declared" (before :: or in a declaration) and "does not name a type" (where a type is expected) variants have the same causes. Depending on the system locale, quotes may be typographic: ‘x’.
Common causes
- Typo or wrong case: C++ is case sensitive.
ledpinis notledPin,serialis notSerial,digitalwriteis notdigitalWrite. - Variable declared in another scope: declared in
setup(), inside anifor a loop, then used inloop()where it does not exist. - Missing
#includeor library not installed:Servo,DHT,Adafruit_NeoPixelorWiFiare only known after the right#include, and only if the library is installed (otherwise:fatal error: Servo.h: No such file or directory). - Wrong board selected:
LED_BUILTIN, pin names such asD5,A0,Serial1orWiFidepend on the board and core. ESP32 code compiled for an Uno, or a board variant that does not defineLED_BUILTIN, triggers the error. - Declaration order: in a
.cppfile, a function must be declared before it is called. The Arduino IDE generates prototypes for.inofiles, but not always (default parameters, custom types) and never for.cppfiles. A missing brace or semicolon higher up can also break every declaration after it.
Step-by-step fix
-
Fix the first error first
Scroll up to the very first error message in the compile output: it gives
file:line:column. Later errors often follow from it. A missing}or;just before that line is a classic suspect. -
Check spelling and case
Compare the reported name with its declaration, letter by letter. Read GCC's "did you mean" suggestion: it often gives the right name. A search (Ctrl+F or Cmd+F) for the name quickly shows where it is really declared.
-
Declare the variable in the right scope
A variable used in both
setup()andloop()must be declared outside both functions, at the top of the file.const int ledPin = 13; // global: visible in setup() and loop() void setup() { pinMode(ledPin, OUTPUT); } void loop() { digitalWrite(ledPin, HIGH); delay(500); digitalWrite(ledPin, LOW); delay(500); } -
Add the include and install the library
Add the matching
#includeat the top of the sketch. If the header cannot be found, install the library: Arduino IDE 2 > Sketch > Include Library > Manage Libraries (Ctrl+Shift+I), or witharduino-cli.arduino-cli lib search neopixel arduino-cli lib install "Adafruit NeoPixel" arduino-cli lib install "Servo" arduino-cli lib list -
Declare functions before they are called
Move the function above its first call, or add a prototype at the top of the file. For code split across several
.cppfiles, declare the functions in a.hand include it wherever they are used.// sensors.h #pragma once float readTemperature(int pin); // main.cpp #include <Arduino.h> #include "sensors.h" void setup() { Serial.begin(115200); Serial.println(readTemperature(4)); } void loop() {} -
Select the right board
Check Tools > Board and the installed core (Arduino AVR, esp32 by Espressif...). If your board does not define
LED_BUILTIN, provide a fallback value suited to your board. Witharduino-cli, the FQBN sets the board.#ifndef LED_BUILTIN #define LED_BUILTIN 2 // adjust to your board's LED pin #endif // arduino-cli compile --fqbn arduino:avr:uno MySketch // arduino-cli compile --fqbn esp32:esp32:esp32 MySketch
Did this fix work for you?
Confirm it in one click: it will rise for the next developers stuck on the same error.
Keep it from coming back
- Pick a naming convention (for example
camelCase) and stick to it everywhere: most case errors disappear. - Compile often, after every small change: a single recent error is much easier to find.
- Write the board and required libraries (with their exact Library Manager name) in a comment at the top of the sketch.
Frequently asked questions
Why "LED_BUILTIN was not declared in this scope"?
The selected board does not define that constant, or it is the wrong board. Select the exact model in Tools > Board, or replace LED_BUILTIN with the LED's pin number.
My function exists, why does the error persist?
It is probably defined after its call in a .cpp, in another file without a header, or inside an inactive #ifdef. Add a prototype or a .h, and check that no brace is missing just before.
How is it different from "does not name a type"?
"was not declared" happens when a name is used in an expression, "does not name a type" when it is used as a type in a declaration (Servo myServo;). In both cases, check the include, the library and the spelling.
Useful tools for this error
More Electronics & Arduino errors
- Fix "avrdude: stk500_getsync(): not in sync" on Arduino
- Fix "Failed to connect to ESP32" (Wrong boot mode, No serial data received)
- Fix "Brownout detector was triggered" on ESP32
- Fix "can't open device /dev/ttyUSB0: Permission denied" (Arduino, ESP32)
- Fix "Guru Meditation Error: Core 1 panic'ed (LoadProhibited)" on ESP32
- Fix "Sketch too big" on Arduino and ESP32 (flash memory full)
- Fix "OSError: [Errno 121] Remote I/O error" with I2C on Raspberry Pi