Programming ESP8266 using Visual Studio Code with Arduino Extension

So now I have used the official Arduino IDE for some time to program the ESP8266. It is a convenient and quick-to-get-started method (check here). But recently I plan to write an application that I feel it would be uncomfortable to write codes using the limited features of Arduino IDE. I find particularly two feature-rich IDEs out there, which are free and highly recommended by communities, namely Atom and Visual Studio Code. With Atom, we need to add the PlatformIO extension for programming embedded systems, including the ESP8266. While with VS Code, we need to install either PlatformIO or Arduino extension.

I had difficulties to get both Atom-PlatformIO and VS Code-PlatformIO working. Somehow the PlatformIO installation was not able to complete. I suspected it was caused by my office internet proxy. I spent quite some time to solve it but did not succeed, so I abandoned it and tried the Arduino extension.

Getting the VS Code with Arduino extension working was not that straight forward either. If you search for the Arduino extension, you will find two choices, either developed by Microsoft or Steve Yin. Initially I chose the one developed by Microsoft. Well, Arduino extension is just an extension that allows users to tell the VS Code where the tools for compiling and uploading code reside. So we must have the official Arduino IDE installed because its tools for compiling and uploading will be used by the VS Code. Then we need to specify the Arduino IDE path in the VS Code ‘User Settings’. Btw, my OS was Windows 8, and I installed the official Arduino IDE version 1.8.2. For unknown reasons compiling sketch using VS Code was always fail with a message saying ‘[Error] Exit with code=undefined’. Actually, I also had an Ubuntu machine, and I managed to get the Arduino extension working. I could compile and upload codes to the ESP8266. But for some reasons, I still preferred to work on my Windows 8 machine.

So, then I gave the Steve Yin’s Arduino extension a try on my Win 8. The compile and upload options in User Settings were more complicated than those of Microsoft one.  But happily I succeeded to compile and write bits of codes to the ESP8266 flash memory. The easiest way to specify the compile and upload options is by adapting them from the command list spat out from the Arduino IDE console output. Again, you can follow this instruction to program ESP8266 using Arduino IDE.

arduino ide compile - for vs code arduino ext

Here is my VS Code ‘User Settings’:

"arduino.uploader" : "C:\\Users\\User-pc\\AppData\\Local\\Arduino15\\packages\\esp8266\\tools\\esptool\\0.4.9\\esptool.exe",
"arduino.uploadOptions": "-vv -cd nodemcu -cb 921600 -cp COM12 -ca 0x00000 -cf $TARGET.bin",
"arduino.compileOptions": "-compile -logger=machine -hardware D:/Master/arduino-1.8.2/hardware -hardware C:/Users/User-pc/AppData/Local/Arduino15/packages -tools D:/Master/arduino-1.8.2/tools-builder -tools D:/Master/arduino-1.8.2/hardware/tools/avr -tools C:/Users/User-pc/AppData/Local/Arduino15/packages -built-in-libraries D:/Master/arduino-1.8.2/libraries -libraries C:/Users/User-pc/Documents/Arduino/libraries -fqbn=esp8266:esp8266:nodemcuv2:CpuFrequency=80,UploadSpeed=115200,FlashSize=4M3M -vid-pid=0X2341_0X0043 -ide-version=10802 -warnings=none -prefs=build.warn_data_percentage=75 -verbose",
In the “arduino.compileOptions” I omitted the built-path and built-cache options since they caused compiling to fail. The Arduino extension had its own built-cache which worked fine. I had not looked further for the meaning of the other options. They might be fine to be deleted. However they seems didn’t do any harm, by now I just leave it there.
In the “arduino.uploadOptions” we could set the COM port and upload speed. By default, the upload speed is 115200, which was quite slow. I found it somewhere that says we can increase the speed by a factor of 8, so it becomes 921600. It was indeed much faster. But the reference I read also says that this high speed can sometimes be unstable. So, bear this in mind. I did try other higher baud rates: 1500000, 1843200, 2000000. However the uploading time took longer than that of 921600.
vs code arduino ext compile
Initially I thought installing these tools for programming ESP8266 would be just some clicks away. But in fact I had spent a lot of time to get it done properly. I hope you have a nice and smooth installation. In case you face the same problem, hope my experience would help.
Happy coding!

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s