My VS Code have a problem:identifier "Serial" is undefined
but I upload program to my arduino nano OK.
this is my arduino file:
void setup(){
pinMode(LED_BUILTIN, OUTPUT);
Serial.begin(9600);
}
void loop(){
digitalWrite(LED_BUILTIN, HIGH);
Serial.println("hello arduino 2333333333333");
delay(1000);
digitalWrite(LED_BUILTIN, LOW);
delay(1000);
}
this is arduino.json
{
"board": "arduino:avr:nano",
"configuration": "cpu=atmega328old",
"programmer": "AVRISP mkII",
"sketch": "serialPort.ino",
"port": "COM9"
}
this is c_cpp_properties.json
{
"configurations": [
{
"name": "Win32",
"includePath": [
"C:\\\\Arduino\\\\tools\\\\**",
"C:\\\\Arduino\\\\hardware\\\\arduino\\\\avr\\\\**",
"C:\\\\Arduino\\\\hardware\\\\arduino\\\\avr\\\\cores\\\\arduino\\\\**",
"C:\\\\Arduino\\\\hardware\\\\tools\\\\avr\\\\avr\\\\include\\\\avr\\\\**",
"C:\\\\Arduino\\\\hardware\\\tools\\\\**",
"C:\\\\Arduino\\\\hardware\\\tools\\\\avr\\\\avr\\\\include\\\\**"
],
"forcedInclude": [
"C:\\\\Arduino\\\\hardware\\\\arduino\\\\avr\\\\cores\\\\arduino\\\\Arduino.h"
],
"intelliSenseMode": "gcc-x64",
"compilerPath": "C:/mingw64/bin/gcc.exe",
"cStandard": "c11",
"cppStandard": "c++17"
}
],
"version": 4
}
Hi, could you please provide more information of your situation? Where does the message identifier "Serial" is undefined occur? Is it an intelliSense prompt message(like a red wavy line suggesting error) or an output message when you try to compile the code?
Since you say that you upload the code to arduino nano device and the code can work well, you mean that the code can compile smoothly without error and can work well. But the vscode intelliSense just gives out the undefined message. Did I misunderstand what you mean?
i have same error like this.
i try to include hardwareserial but give errors when writing include files too


I also have the same error.

commands like delay, pinMode, digitalWrite, digitalRead, etc. works fine with intellisense.
but when using Serial, it shows:
identifier "Serial" is undefined
trying to add #include<HardwareSerial.h> it gives me:
#include errors detected. Please update your includePath. Squiggles are disabled for this translation unit (F:\project\arduino\sketches\gto\app.ino).

and then after clicking on the lightbulb, then "add to includePath..." i get :
PCH warning: header stop cannot be in a linkage block. An IntelliSense PCH file was not generated.

and this is my c_cpp_properties.json:
{
"configurations": [
{
"name": "Win32",
"includePath": [
"C:/Users/gemul/AppData/Local/Arduino15/packages/arduino/tools/**",
"C:/Users/gemul/AppData/Local/Arduino15/packages/arduino/hardware/avr/1.6.23/**",
"C:/Users/gemul/AppData/Local/Arduino15/packages/arduino/hardware/avr/1.6.23/cores/arduino"
],
"forcedInclude": [
"C:/Users/gemul/AppData/Local/Arduino15/packages/arduino/hardware/avr/1.6.23/cores/arduino/Arduino.h"
],
"intelliSenseMode": "msvc-x64",
"compilerPath": "C:/Program Files (x86)/Microsoft Visual Studio/2017/Community/VC/Tools/MSVC/14.16.27023/bin/Hostx64/x64/cl.exe",
"cStandard": "c11",
"cppStandard": "c++17"
}
],
"version": 4
}
the line:
C:/Users/gemul/AppData/Local/Arduino15/packages/arduino/hardware/avr/1.6.23/cores/arduino
is added when i clicked on lightbulb then "add to includePath...".
By the way, using SoftwareSerial works fine.

it added
"C:/Users/gemul/AppData/Local/Arduino15/packages/arduino/hardware/avr/1.6.23/libraries/SoftwareSerial/src"
in includePath.
It seems i have solved my own issue. And here is what i have tried.
First, i can get rid of the intellisense errors by simply using tag parser instead of default for my cpp intellisense engine. by changing C_Cpp:Intelli Sense Engine in File>preference>settings to "Tag Parser".
That indeed get rid of the errors, and put intellisense autocompletion and suggestion to works again. But it get rid of error checking (which is very important for me).
So if you don't mind using tag parser, then go on.
Second, after googling for a while, i found out that in order for Serial to be able to works with intellisense, you need to add "defines": [ "USBCON" ] in your c_cpp_properties.json. And i did, and now the intellisense is working (i changed back the cpp intellisense engine to Default).
So you might try this one first, perhaps it will solve your problem. If not, use the tag parser one.
my c_cpp_properties.json. now looks like this:

It seems i have solved my own issue. And here is what i have tried.
First, i can get rid of the intellisense errors by simply using tag parser instead of default for my cpp intellisense engine. by changing C_Cpp:Intelli Sense Engine in File>preference>settings to "Tag Parser".
That indeed get rid of the errors, and put intellisense autocompletion and suggestion to works again. But it get rid of error checking (which is very important for me).
So if you don't mind using tag parser, then go on.Second, after googling for a while, i found out that in order for Serial to be able to works with intellisense, you need to add
"defines": [ "USBCON" ]in your c_cpp_properties.json. And i did, and now the intellisense is working (i changed back the cpp intellisense engine to Default).
So you might try this one first, perhaps it will solve your problem. If not, use the tag parser one.
my c_cpp_properties.json. now looks like this:
Thanks for this answer man, now my IntelliSense is working very well, code completion and the libraries :) (I leave the Intelli Sense engine on Default, cause code completion and syntax highlighting wasn't working for me in "Tag Parser" mode.
Here's my config files:
c_cpp_properties.json
```{
"configurations": [
{
"name": "Win32",
"includePath": [
"${workspaceRoot}",
"C:\\\\Program Files (x86)\\\\Arduino\\\\hardware\\\\arduino\\\\avr\\\\**",
"C:\\\\Program Files (x86)\\\\Arduino\\\\tools\\\\**",
"C:\\\\Program Files (x86)\\\\Arduino\\\\hardware\\\\tools\\\\**",
"C:\\\\Program Files (x86)\\\\Arduino\\\\libraries\\\\**",
"C:\\\\Users\\\\WILSONCAZARRESOUSA\\\\Documents\\\\Arduino\\\\libraries\\\\**",
"C:\\Program Files (x86)\\Arduino\\hardware\\tools\\avr\\lib/gcc\\avr\\5.4.0\\include",
"C:\\Program Files (x86)\\Arduino\\hardware\\arduino\\avr\\cores\\arduino",
"C:\\Program Files (x86)\\Arduino/hardware\\arduino\\avr\\variants\\standard",
"C:\\Program Files (x86)\\Arduino"
],
"forcedInclude": [
"C:\\\\Program Files (x86)\\\\Arduino\\\\hardware\\\\arduino\\\\avr\\\\cores\\\\arduino\\\\Arduino.h"
],
"browse": {"limitSymbolsToIncludedHeaders": true,
"path": ["C:\\\\Program Files (x86)\\\\Arduino\\\\hardware\\\\arduino\\\\avr\\\\cores\\\\arduino\\\\Arduino.h"]},
"intelliSenseMode": "gcc-x64",
"cStandard": "c11",
"cppStandard": "c++17",
"defines": ["USBCON"]
}
],
"version": 4
}
**settings.json**
{
"arduino.path": "C:\Program Files (x86)\Arduino",
"arduino.commandPath": "arduino_debug.exe",
"arduino.logLevel": "info",
"arduino.enableUSBDetection": true,
"arduino.disableTestingOpen": false,
"arduino.skipHeaderProvider": false,
"arduino.additionalUrls": [
"https://raw.githubusercontent.com/VSChina/azureiotdevkit_tools/master/package_azureboard_index.json",
"http://arduino.esp8266.com/stable/package_esp8266com_index.json"
],
"arduino.defaultBaudRate": 9600,
"workbench.colorTheme": "Darcula",
"C_Cpp.intelliSenseEngine": "Default",
"terminal.integrated.shell.windows": "C:\Program Files\Git\bin\bash.exe",
"editor.quickSuggestions": {
"other": true,
"comments": false,
"strings": false
},
"editor.suggestOnTriggerCharacters": true,
}
```
It seems i have solved my own issue. And here is what i have tried.
First, i can get rid of the intellisense errors by simply using tag parser instead of default for my cpp intellisense engine. by changing C_Cpp:Intelli Sense Engine in File>preference>settings to "Tag Parser".
That indeed get rid of the errors, and put intellisense autocompletion and suggestion to works again. But it get rid of error checking (which is very important for me).
So if you don't mind using tag parser, then go on.Second, after googling for a while, i found out that in order for Serial to be able to works with intellisense, you need to add
"defines": [ "USBCON" ]in your c_cpp_properties.json. And i did, and now the intellisense is working (i changed back the cpp intellisense engine to Default).
So you might try this one first, perhaps it will solve your problem. If not, use the tag parser one.
my c_cpp_properties.json. now looks like this:
Extremely appreciative. Thank you.
@gemul Dude, THANK YOU!!!!!! I spent entirely too long tracking this down, and finally someone solved it! I just confirmed this problem exists on Linux, as well, and your solution fixed it! Finally got my Linter back!!!!
@dilin-MS this bug still persists in my version of the extension. The workaround described above fixes it, but should we Close this Issue before the bug itself is fixed?
@dilin-MS this bug still persists in my version of the extension. The workaround described above fixes it, but should we Close this Issue before the bug itself is fixed?
Agreed. If it were my software, I wouldn't be closing the bug report unless we'd already fixed it, and were prepping to roll it out in the next update, and even then, I'd probably wait a few days until after the update gets pushed out to make sure we don't get any more complaints about it before calling it closed.
Hi people.
I have a similar problem with Serial2 and Serial3. They remain undefined. Any suggestions to fix this?
I got the same issue.
My comprehension is that the vscode arduino extension is not able to get definition defined in the platform.txt/boards.txt. So they are not added to C/C++ properties automatically. So, they have to be manually added to the .vscode/c_cpp_properties.json
Example for this core for an STM32F1 based board (BluePillF103C8):
https://github.com/stm32duino/Arduino_Core_STM32
"defines": [
"ARDUINO=10809",
"HAL_UART_MODULE_ENABLED",
"STM32F1xx"
]
HAL_UART_MODULE_ENABLED is managed thanks the Arduino menu and is required to get Serial instance defined for C/C++ code browsing.
So, you have to find which define(s) is(are) required.
In the same way, the generated includePath in the .vscode/c_cpp_properties.json is generic and include all top level folders:
"includePath": [
"${workspaceRoot}",
"c:\\\\STM32\\\\arduino\\\\arduino-1.8.9-windows\\\\arduino-1.8.9\\\\portable\\\\packages\\\\STM32\\\\hardware\\\\stm32\\\\1.6.0\\\\**",
"c:\\\\STM32\\\\arduino\\\\arduino-1.8.9-windows\\\\arduino-1.8.9\\\\portable\\\\packages\\\\STM32\\\\tools\\\\**",
"c:\\\\STM32\\\\arduino\\\\arduino-1.8.9-windows\\\\arduino-1.8.9\\\\hardware\\\\tools\\\\**",
"c:\\\\STM32\\\\arduino\\\\arduino-1.8.9-windows\\\\arduino-1.8.9\\\\libraries\\\\**",
"C:\\\\Users\\\\fpistm\\\\Documents\\\\Arduino\\\\hardware\\\\tools\\\\**",
"C:\\\\Users\\\\fpistm\\\\Documents\\\\Arduino\\\\libraries\\\\**"
],
But all headers should not be parsed and could depend of definitions/options,
In my case, some path are added to include path thanks the platform.txt
compiler.stm.extra_include="-I{build.source.path}" "-I{build.core.path}/avr" "-I{build.core.path}/stm32" "-I{build.core.path}/stm32/LL" "-I{build.core.path}/stm32/usb" "-I{build.core.path}/stm32/usb/hid" "-I{build.core.path}/stm32/usb/cdc" "-I{build.system.path}/Drivers/{build.series}_HAL_Driver/Inc/" "-I{build.system.path}/Drivers/{build.series}_HAL_Driver/Src/" "-I{build.system.path}/{build.series}/" "-I{build.system.path}/Middlewares/ST/STM32_USB_Device_Library/Core/Inc" "-I{build.system.path}/Middlewares/ST/STM32_USB_Device_Library/Core/Src"
Here, {build.series} is defined thanks the boards.txt depending of the board selected.
So, I have to update includePath property to get only the right include and avoid multiple definitions.
"includePath": [
"${workspaceRoot}",
"c:\\\\STM32\\\\arduino\\\\arduino-1.8.9-windows\\\\arduino-1.8.9\\\\portable\\\\packages\\\\STM32\\\\hardware\\\\stm32\\\\1.6.0\\\\cores\\\\**",
"c:\\\\STM32\\\\arduino\\\\arduino-1.8.9-windows\\\\arduino-1.8.9\\\\portable\\\\packages\\\\STM32\\\\hardware\\\\stm32\\\\1.6.0\\\\system\\\\STM32F1xx\\\\**",
"c:\\\\STM32\\\\arduino\\\\arduino-1.8.9-windows\\\\arduino-1.8.9\\\\portable\\\\packages\\\\STM32\\\\hardware\\\\stm32\\\\1.6.0\\\\system\\\\Drivers\\\\STM32F1xx_HAL_Driver\\\\**",
"c:\\\\STM32\\\\arduino\\\\arduino-1.8.9-windows\\\\arduino-1.8.9\\\\portable\\\\packages\\\\STM32\\\\hardware\\\\stm32\\\\1.6.0\\\\system\\\\Drivers\\\\CMSIS\\\\Device\\\\ST\\\\STM32F1xx\\\\**",
"c:\\\\STM32\\\\arduino\\\\arduino-1.8.9-windows\\\\arduino-1.8.9\\\\portable\\\\packages\\\\STM32\\\\hardware\\\\stm32\\\\1.6.0\\\\variants\\\\BLUEPILL_F103XX\\\\**",
"c:\\\\STM32\\\\arduino\\\\arduino-1.8.9-windows\\\\arduino-1.8.9\\\\portable\\\\packages\\\\STM32\\\\tools\\\\**",
"c:\\\\STM32\\\\arduino\\\\arduino-1.8.9-windows\\\\arduino-1.8.9\\\\hardware\\\\tools\\\\**",
"c:\\\\STM32\\\\arduino\\\\arduino-1.8.9-windows\\\\arduino-1.8.9\\\\libraries\\\\**"
],
With those updates, there is no more error reported and code browsing is well performed else I've multiple definition.
Note that those errors only rely on code browsing, Arduino build is well performed with correct paths and definitions.
Hope this help and clear.
It seems having this defined should also be part of #438
@gemul nice find, this was driving me crazy
It seems i have solved my own issue. And here is what i have tried.
First, i can get rid of the intellisense errors by simply using tag parser instead of default for my cpp intellisense engine. by changing C_Cpp:Intelli Sense Engine in File>preference>settings to "Tag Parser".
That indeed get rid of the errors, and put intellisense autocompletion and suggestion to works again. But it get rid of error checking (which is very important for me).
So if you don't mind using tag parser, then go on.Second, after googling for a while, i found out that in order for Serial to be able to works with intellisense, you need to add
"defines": [ "USBCON" ]in your c_cpp_properties.json. And i did, and now the intellisense is working (i changed back the cpp intellisense engine to Default).
So you might try this one first, perhaps it will solve your problem. If not, use the tag parser one.
my c_cpp_properties.json. now looks like this:
This is an amazing find and I now know how to use the define field in the c_cpp_properties.json file. Worked like a champ! I am greatful!
It seems i have solved my own issue. And here is what i have tried.
First, i can get rid of the intellisense errors by simply using tag parser instead of default for my cpp intellisense engine. by changing C_Cpp:Intelli Sense Engine in File>preference>settings to "Tag Parser".
That indeed get rid of the errors, and put intellisense autocompletion and suggestion to works again. But it get rid of error checking (which is very important for me).
So if you don't mind using tag parser, then go on.Second, after googling for a while, i found out that in order for Serial to be able to works with intellisense, you need to add
"defines": [ "USBCON" ]in your c_cpp_properties.json. And i did, and now the intellisense is working (i changed back the cpp intellisense engine to Default).
So you might try this one first, perhaps it will solve your problem. If not, use the tag parser one.
my c_cpp_properties.json. now looks like this:
This is very helpful. I started Arduino coding in VSCode, and encountered this issue almost immediately. Thank you very much.
It seems i have solved my own issue. And here is what i have tried.
First, i can get rid of the intellisense errors by simply using tag parser instead of default for my cpp intellisense engine. by changing C_Cpp:Intelli Sense Engine in File>preference>settings to "Tag Parser".
That indeed get rid of the errors, and put intellisense autocompletion and suggestion to works again. But it get rid of error checking (which is very important for me).
So if you don't mind using tag parser, then go on.Second, after googling for a while, i found out that in order for Serial to be able to works with intellisense, you need to add
"defines": [ "USBCON" ]in your c_cpp_properties.json. And i did, and now the intellisense is working (i changed back the cpp intellisense engine to Default).
So you might try this one first, perhaps it will solve your problem. If not, use the tag parser one.
my c_cpp_properties.json. now looks like this:
Had a similar issue and this solved my problem too. Thank you @gemul.
Hi to everyone who is suffering from this!
I was so annoyed, that I began to work on an auto-configuration mechanism, which parses the build output from Arduino and creates a c_cpp_properties.json from it. The goal is that no one wants likely ever edit this file manually again - except for very weird setups for which I will add a configuration flag/option that will keep the extension from touching the file.
I coded some proof of concept already but I haven't committed anything yet. But I began documenting the project here.
Anyone who wants to keep me motivated can chip in some :beer:-money - the donation button can be found in the link above.
Other support is appreciated and comprises
The progress can be followed within the same file. The repo is set to this feature branch by default.
Thank you for sharing. I had same issue.
As my conf file had a previous defines entry I just added a ne item to the matrix.

I had the same issue, so I tried the @gemul 's solution but didn't work for me. However, I've added #include <Arduino.h> and worked well.
Should just #include<Arduino.h> be the solution?
@kopiro I keep doing this, so I restart the VS Code and it works without error messages.
It seems i have solved my own issue. And here is what i have tried.
First, i can get rid of the intellisense errors by simply using tag parser instead of default for my cpp intellisense engine. by changing C_Cpp:Intelli Sense Engine in File>preference>settings to "Tag Parser".
That indeed get rid of the errors, and put intellisense autocompletion and suggestion to works again. But it get rid of error checking (which is very important for me).
So if you don't mind using tag parser, then go on.Second, after googling for a while, i found out that in order for Serial to be able to works with intellisense, you need to add
"defines": [ "USBCON" ]in your c_cpp_properties.json. And i did, and now the intellisense is working (i changed back the cpp intellisense engine to Default).
So you might try this one first, perhaps it will solve your problem. If not, use the tag parser one.
my c_cpp_properties.json. now looks like this:
this have solved my problem. thank you!
I found I needed more than just USBCON. When you compile your project, look for a line like this:
"C:\\Program Files (x86)\\Arduino\\hardware\\tools\\avr/bin/avr-g++" -c -g -Os -w -std=gnu++11 -fpermissive -fno-exceptions -ffunction-sections -fdata-sections -fno-threadsafe-statics -Wno-error=narrowing -flto -w -x c++ -E -CC -mmcu=atmega328p -DF_CPU=8000000L -DARDUINO=10812 -DARDUINO_AVR_PRO -DARDUINO_ARCH_AVR "-IC:\\Program Files (x86)\\Arduino\\hardware\\arduino\\avr\\cores\\arduino" "-IC:\\Program Files (x86)\\Arduino\\hardware\\arduino\\avr\\variants\\eightanaloginputs" "path\\to\\your\\sketch\\sketch.ino.cpp" -o nul
Look for the arguments that start with -D... Each one of those is a #define that is set by the arduino build process. In my above example they are -DF_CPU=8000000L -DARDUINO=10812 -DARDUINO_AVR_PRO -DARDUINO_ARCH_AVR. Take these (remove the -D) and put then into your "defines" section, like this:
"defines": [
"USBCON",
"F_CPU=8000000L",
"ARDUINO=10812",
"ARDUINO_AVR_PRO",
"ARDUINO_ARCH_AVR"
]
After using elektronikworkshop/vscode-arduino fork with the autogeneration feature, I can attest that this does indeed solve this problem perfectly.
This is fixed in v0.3.4
Most helpful comment
It seems i have solved my own issue. And here is what i have tried.
First, i can get rid of the intellisense errors by simply using tag parser instead of default for my cpp intellisense engine. by changing C_Cpp:Intelli Sense Engine in File>preference>settings to "Tag Parser".
That indeed get rid of the errors, and put intellisense autocompletion and suggestion to works again. But it get rid of error checking (which is very important for me).
So if you don't mind using tag parser, then go on.
Second, after googling for a while, i found out that in order for Serial to be able to works with intellisense, you need to add

"defines": [ "USBCON" ]in your c_cpp_properties.json. And i did, and now the intellisense is working (i changed back the cpp intellisense engine to Default).So you might try this one first, perhaps it will solve your problem. If not, use the tag parser one.
my c_cpp_properties.json. now looks like this: