Arduino

How to Fix “not declared in this scope” Error in Arduino

When programming the Arduino learners come across various types of errors and sometimes it becomes difficult to troubleshoot the error code.  The error codes mostly come when the Arduino program is compiled, and these errors are displayed in the output menu of the Arduino IDE which is at the bottom of the software. The Arduino IDE also indicates on which line of the code the error exists and it also suggests suitable actions for rectifying the error. The most common error beginners face is the “not declared in this scope” and we have discussed why such type of error arises and how we can fix this error.

What is “not declared in this scope” error

As from the name we can understand that when the compiler of Arduino IDE is unable to recognize any variable or is unable to process any loop or any instruction having any undeclared variable so it gives the error “not declared in this scope”, which means that code is unable to understand the instruction given in the Arduino code.

The Arduino IDE has a feature that it automatically indicates the line of the program which contains errors simply by underlining them with red color even before the code is compiled.

Situations when the compiler gives the error “not declared in this scope”

To illustrate when the error of not declaring in this scope arises in the Arduino program we have given an example code in which this type of error was encountered.

For simplicity we have compiled an Arduino code which performs an addition operation and the image of the code for the simple addition having the error is posted below:

When we compiled this code we got the error “b is not declared in this scope“ as it can be seen in the image posted below.

As you can see that the compiler also identified the line which has the error and also it has indicated that the variable “b” is out of the scope in the program.

We have posted an image below in which you can clearly see that the Arduino IDE has highlighted the variable that it is unable to understand.

How to fix “not declared in the scope” error

To fix the error we simply just declare that variable so that the compiler can understand the variable used in the code. So always remember to declare the variable you want to use in the Arduino program. We have posted the correct code after removing this error:

We have successfully compiled the code and it can be seen from the image of the output menu of the Arduino IDE:

And the output of the code shown in the serial monitor is:

Conclusion

In Arduino programming while compiling the code one can encounter a whole bunch of error codes but these errors can be fixed by avoiding the mistakes in the first place. However, there are some errors which are due to the incorrect syntax used for programming and some are the logical errors. Although the chances of logical errors are minimal. The most common errors that are encountered by the users are related to syntax or declaration errors. So, we have described what are the declaration errors and how they arise and how we can remove them with the help of simple Arduino example code.

About the author

Aaliyan Javaid

I am an electrical engineer and a technical blogger. My keen interest in embedded systems has led me to write and share my knowledge about them.