Arduino + Visual Studio Code


The Arduino IDE is a good tool. It’s simple and it does the job. But as we get more serious with our Arduino projects, I think VS Code can provide a much better coding environment. I use VS Code when working with non-Arduino projects so it’s good to be able to work in the same dev environment and work flow. With VS Code we have access to a rich set of built-in features and well as community-built extensions.

What we will need:
  • Arduino IDE installed
  • Visual Studio Code installed
  • Arduino Uno board
  • Micro USB cable

To work with Arduino we will need to install Visual Studio Code extension for Arduino. This extension is a wrapper for the Arduino IDE. Note that this also has a dependency on the C/C++ extension so that will also be installed.

Open VS Code and press Ctrl + Shift + X to open the Extensions panel and type vscode-arduino. Select the Arduino extension and click install.

Installing Arduino extension

This extension provides several commands in the Command Palette which we can activate using F1 or Ctrl + Shift + P) for working with *.ino files. We can filter the command palette just specific to this extension by typing arduino.

Under our current working directory, execute the Arduino: Initialize command from the command palette. This will allow us to select a name for our new sketch and the Arduino board we want to work with. VS Code will then setup the Arduino environment for us. Two configuration files will be generated which describes our Arduino environment: arduino.json, c_cpp_properties.json.

Arduino: Initialize

Now let’s populate the sketch, it’s a basic countdown timer counting from 10 to 0 and prints it to the serial terminal. If the timer has stopped (either it has reached 0 or interrupted by the user) a ‘.’ will be printed. While the timer has stopped, it will restart again if interrupted by the user.

#define COUNTDOWN_START_VALUE 10U

static uint8_t currentCount = COUNTDOWN_START_VALUE;
static uint16_t secondsSinceBoot = 0;
static uint16_t nextPrintTime = 0;

void setup()
{
   Serial.begin(9600);
   Serial.println("Countdown Demo");
   Serial.println("--------------");
   Serial.println("");
}

void loop()
{
   secondsSinceBoot = millis() / 1000;

   /* -1 if no incoming serial data. */
   if (Serial.read() != -1)
   {
      if (currentCount == 0)
      {
         currentCount = COUNTDOWN_START_VALUE;
         nextPrintTime = secondsSinceBoot + 1;
      }
      else
      {
         currentCount = 0;
         nextPrintTime = secondsSinceBoot;
      }
   }

   if (secondsSinceBoot == nextPrintTime)
   {
      if (currentCount == COUNTDOWN_START_VALUE)
      {
         Serial.println("Countdown Started");
      }
      else if (currentCount == 0)
      {
         Serial.println(".");
         nextPrintTime++;
         return;
      }

      Serial.println(currentCount, DEC);

      /* Countdown to 0 only. */
      if (currentCount != 0)
      {
         currentCount--;
      }

      nextPrintTime++;
   }
}

Once the sketch is ready, select the COM port (if not done so already) and upload the sketch to the Arduino Uno board.

Upload sketch to Arduino Uno board

Finally, let’s see it in action. Below I am using Putty to interact with Arduino board

Timer in action

Categories: Arduino, Internet of things

1 comment

Leave a Reply

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

WordPress.com Logo

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

Google photo

You are commenting using your Google 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 )

Connecting to %s

%d bloggers like this: