Skip to content
FR
  1. LuzineDebug
  2. Electronics & Arduino
  3. error: 'x' was not declared in this scope
Electronics & Arduino Beginner ≈ 5 min to fix

Fix "error: 'x' was not declared in this scope" (Arduino, C++)

Error message
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
In short

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).

Be the first to confirm this fix

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

  1. Typo or wrong case: C++ is case sensitive. ledpin is not ledPin, serial is not Serial, digitalwrite is not digitalWrite.
  2. Variable declared in another scope: declared in setup(), inside an if or a loop, then used in loop() where it does not exist.
  3. Missing #include or library not installed: Servo, DHT, Adafruit_NeoPixel or WiFi are only known after the right #include, and only if the library is installed (otherwise: fatal error: Servo.h: No such file or directory).
  4. Wrong board selected: LED_BUILTIN, pin names such as D5, A0, Serial1 or WiFi depend on the board and core. ESP32 code compiled for an Uno, or a board variant that does not define LED_BUILTIN, triggers the error.
  5. Declaration order: in a .cpp file, a function must be declared before it is called. The Arduino IDE generates prototypes for .ino files, but not always (default parameters, custom types) and never for .cpp files. A missing brace or semicolon higher up can also break every declaration after it.

Step-by-step fix

  1. 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.

  2. 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.

  3. Declare the variable in the right scope

    A variable used in both setup() and loop() must be declared outside both functions, at the top of the file.

    cpp
    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);
    }
  4. Add the include and install the library

    Add the matching #include at 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 with arduino-cli.

    bash
    arduino-cli lib search neopixel
    arduino-cli lib install "Adafruit NeoPixel"
    arduino-cli lib install "Servo"
    arduino-cli lib list
  5. 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 .cpp files, declare the functions in a .h and include it wherever they are used.

    cpp
    // 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() {}
  6. 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. With arduino-cli, the FQBN sets the board.

    cpp
    #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

All Electronics & Arduino errors →