header banner
Default

Utilize GitHub Copilot AI to expedite your Arduino projects


Things used in this project

VIDEO: GitHub Copilot in 7 Minutes 👨‍💻🤖🚀
Developers Digest

Story

VIDEO: Coding with an AI pair programmer: Getting started with GitHub Copilot
GitHub

Leverage AI from the makers of ChatGPT to increase your coding speed, learn more about Arduino libraries, and explore Arduino hardware -- all from a single editor!

Estimated Time:Part 1: 10 min; Part 2: 10 min; Part 3: 40 min

Estimated Cost: $ of Arduino Nano RP2040 Connect or device of your choice.

Introduction

This tutorial shows you how to leverage GitHub Copilot to accelerate your learning and Arduino project development. The resulting code will be a simple accelerometer program. The Arduino Nano RP2040 Connect is used for this example, but the approach can be extended to any Arduino supported board.

This is part 1 of the Arduino with GitHub Copilot tutorial series. Please view part 2 for troubleshooting assistance and advanced topics!

More information about the AI tools:

GitHub Copilot: An AI code assistant built on OpenAI Codex.

OpenAI Codex: An “AI system that translates natural language to code.”

OpenAI: An AI research and deployment company and creators of ChatGPT. Their mission is to "ensure that artificial general intelligence benefits all of humanity."

Prerequisites

This tutorial uses VS Code with Microsoft’s Arduino extension and the GitHub Copilot extension. New to VS Code or the Arduino extension? No problem! Check out the resources below.

UPDATE: Support for the legacy Arduino IDE will be removed in a future version of Microsoft's Arduino extension for VS Code. At that time, please use the Arduino CLI that comes bundled with the extension. See Microsoft’s Arduino extension for more details.
IMPORTANT: For Arduino IDE 1.8.X, you MUST use the standard Windows download option. The Arduino extension will NOT work with the Windows app store download.

The following was run on Windows 11 and the Arduino Nano RP2040 Connect.

Part 1: Setup a new Arduino project in VS Code

In this section you will learn how to use the Arduino extension for VS Code to setup and interact with a new Arduino project.

Estimated Time: 10 min

Step 1. Create a new sketch.

NOTE: The Arduino extension for VS Code works best with existing sketches. To create a new sketch, it may be easiest to use the Arduino IDE.
  • Open the Arduino IDE.
  • Navigate to File > New.
  • Navigate to File > Save.
  • Type 'ArduinoCopilot' for the sketch name. Select 'Save'.
  • Open VS Code.
  • Navigate to 'File > Open Folder...'.
  • Navigate to Documents/Arduino/ArduinoCopilot. Select 'Select Folder'.
  • In the VS Code Explorer tab, select ArduinoCopilot.ino.

Step2. In VS Code, setup and connect the device.

  • In the bottom toolbar, select '<Select Board Type>'.
  • In the Arduino Board Configuration window, search for 'Nano RP2040'.
  • Select 'Arduino Nano RP2040 Connect (Arduino Mbed OS Nano Boards)'.
NOTE: If you cannot find your board, confirm its board package is installed: Navigate to View > Command Palette… > Arduino: Board Manager.
  • Plug in your device to your PC.
  • In the bottom toolbar, select '<Select Serial Port>'.
  • In the drop-down bar, select the '<Port associated with device>'.

Step3. Run the sketch to validate the setup.

  • Confirm the ArduinoCopilot.ino window is selected.
  • In the top right corner of the window, select the 'Verify' icon. An OUTPUT log window will appear. Confirm there are no errors.
NOTE: If there are red squiggles, hover the cursor over the squiggles. In the pop-up, click on 'Quick Fix...'. Select 'Disable error squiggles'.
  • Once complete, select the 'Upload' icon. Confirm there are no errors.

Part 2: Learn Copilot suggestion hotkeys

In this section you will learn keyboard shortcuts to interact with Copilot suggestions. The shortcuts presented are for a Windows or Linux environment. For macOS, please view the six documented pre-defined hotkeys.

Estimated Time: 10 min

Step1. In setup(), on a new line type //. A Copilot suggestion will appear.

Suggestion: "Initialize the serial port"

IMPORTANT: The suggestions in the tutorial images may be different from what you see. This is expected.

Step2. Try the following hotkeys:

  • <Tab>: Accept the inline suggestion.
  • <Esc>: Clear or dismiss the inline suggestion.
  • Alt + \: Trigger an inline suggestion.

Suggestion: "Initialize the serial port"

NOTE: This is useful if the suggestion has disappeared.
  • Alt + ]: Show next inline suggestion.

Suggestion: "Initialize serial and wait for port to open:"

NOTE: There may not be any other available suggestions.
  • Alt + [: Show previous inline suggestion.

Suggestion: "initialize serial communication at 9600 bits per second:"

NOTE: There may not be any other available suggestions.
  • Ctrl + Enter: Open GitHub Copilot window to see more, expanded suggestions.

To accept a suggestion, select 'Accept Solution' above the suggestion. The suggestion will be implemented in your file.

Part 3: Use Copilot with the Nano RP2040's accelerometer

In this section, you will use Copilot to help you write a program using the Arduino Nano RP2040 Connect's accelerometer. The finished code is a simple application that turns the LED on when the device is tilted and off when the device is level.

Estimated Time: 40 min

IMPORTANT: Your Copilot suggestions will most likely differ from what is shown below. This is expected and okay!

Add library

Use Copilot to discover and add the accelerometer library.

Step1. At the top of the file, type a comment to include the accelerometer library.

// include the accelerometer library for the Arduino Nano RP2040 Connect

Step2. Press <Enter> to continue. You will see a suggestion for what library to include.

Suggestion: "#include <Arduino_LSM6DSOX.h"

Step3. Press <Tab> to accept.

TIP: The more specific your comment is, the more likely Copilot will understand what you are trying to do.

Initialize hardware

Use Copilot, logging, and manual code edits to initialize the serial port, accelerometer, and LED.

Accelerometer

Step1. In setup(), on a new line type //. A new comment suggestion will appear.

Suggestion: "initialize the accelerometer"

Step2. Press <Tab> to accept.

Step3. Press <Enter> to continue. A code suggestion will appear.

Suggestion: "if (!IMU.begin()) { Serial.println("..."); while(1); }"

Step4. Press <Tab> to accept.

Step5. Press <Enter> to continue.

Step6. As appropriate, continue accepting Copilot suggestions.

Suggestion: "set the accelerometer full scale to 2G"

TIP: You do not need to accept every suggestion.
TIP: Alt + ] and Alt + [ may show you other suggestions.

Step 7. Press <Esc> to decline the comment "set the accelerometer full scale to 2G".

Serial and LED

Step1. In setup(), type a new initialization comment.

Suggestion: "initialize the serial port"

TIP: You do not need to complete a full comment for a suggestion to appear.

Step2. Press <Tab> to accept.

Step3. Press <Enter> to continue.

Step4. Continue accepting, declining, and manually initiating hardware initialization Copilot suggestions until your code is complete.

// include the accelerometer library for the Arduino Nano RP2040 Connect
#include <Arduino_LSM6DSOX.h>

void setup() {
// put your setup code here, to run once:

// initialize the accelerometer
if (!IMU.begin()) {
Serial.println("Failed to initialize IMU!");
while (1);
}

// set the accelerometer range to 2G
IMU.setAccelRange(LSM6DSOX_ACCEL_RANGE_2G);

// set the accelerometer data rate to 104 Hz
IMU.setAccelDataRate(LSM6DSOX_ACCEL_ODR_104_HZ);

// initialize the serial port
Serial.begin(115200);

// wait for the serial port to open
while (!Serial);

// initialize the LED
pinMode(LED_BUILTIN, OUTPUT);
}

Verify code

Step1. In the top right corner of the window, select the 'Verify' icon.

NOTE: If the library Arduino_LSM6DSOX.h is not found, navigate to View > Command Palette… > Arduino: LibraryManager to install it.

Step2. The OUTPUT log window reveals unrecognized functions and macros:

Made up macros and member functions for LSM6DSOXClass, e.g., setAccelRange and setAccelDataRate

TIP: Copilot is not always correct in its technical suggestions. This is expected.

Step3. Remove the unrecognized code.

// include the accelerometer library for the Arduino Nano RP2040 Connect
#include <Arduino_LSM6DSOX.h>

void setup() {
// put your setup code here, to run once:

// initialize the accelerometer
if (!IMU.begin()) {
Serial.println("Failed to initialize IMU!");
while (1);
}

// initialize the serial port
Serial.begin(115200);

// wait for the serial port to open
while (!Serial);

// initialize the LED
pinMode(LED_BUILTIN, OUTPUT);
}

Copilot suggestions have the serial port initiated after the accelerometer logs output. We need to fix this manually.

TIP: Copilot is not always correct in its suggestion order. This is expected.

Step4. Move serial initialization logic prior to accelerometer initialization.

// include the accelerometer library for the Arduino Nano RP2040 Connect
#include <Arduino_LSM6DSOX.h>

void setup() {
// put your setup code here, to run once:

// initialize the serial port
Serial.begin(115200);

// wait for the serial port to open
while (!Serial);

// initialize the accelerometer
if (!IMU.begin()) {
Serial.println("Failed to initialize IMU!");
while (1);
}

// initialize the LED
pinMode(LED_BUILTIN, OUTPUT);
}

Step5. Select 'Verify' to confirm all errors are fixed.

Application logic

Use Copilot, IntelliSense, and manual code edits to correctly detect and log when the device is tilted.

Copilot suggestions and IntelliSense

Step1. In loop(), on a new line type //. A new comment suggestion will appear.

Suggestion: "read the accelerometer"

Step2. Press <Tab> to accept.

Step3. Press <Enter> to continue. A code suggestion will appear.

Step4. Press <Esc> to decline the suggestion of IMU.read();.

Step5. Type IMU. and confirm the IntelliSense pop-up appears.

Step6. Select the readAcceleration function.

Step7. Hover over the function to view its signature.

IntelliSense correct function signature and Copilot incorrect suggestion: "();"

Step8. Press <Esc> to decline Copilot's suggestion of ();.

Step9. Manually type the correct signature and add x, y, and z variables of type float.

void loop() {
// put your main code here, to run repeatedly:
float x, y, z;

// read the accelerometer
IMU.readAcceleration(x, y, z);
}

Step10. On a new line, begin to type a comment. A new comment suggestion appears.

Suggestion: "print the accelerometer values"

Step11. Press <Tab> to accept.

Step12. Press <Enter> to continue.

Step13. Continue until all three variables have logging code.

...
// print the accelerometer values
Serial.print("x: ");
Serial.print(x);
Serial.print(" y: ");
Serial.print(y);
Serial.print(" z: ");
Serial.prinln(z);

Step14. Press <Enter> to continue.

Step15. Begin to type a comment. Copilot correctly guesses to check the accelerometer tilt.

Suggestion: "turn on the LED if the accelerometer is tilted"

Step16. Press <Tab> to accept. Complete the comment with a request to log the device state.

...
// turn on the LED if the accelerometer is tilted. Log the state of the device.

Step17. Press <Enter> to continue. A suggestion will appear. However, only x is considered and the logging is for the LED, not the tilt.

Suggestion: "if (x > 0.5) { digitalWrite(LED_BUILTIN, HIGH); ..."

Step18. Rewrite the comment with more context.

...    
// turn on the LED if the accelerometer is tilted and log the tilt state.
// turn off the LED if the accelerometer is not tilted and log the tilt state.

Step19. Press <Enter> to continue. A more promising suggestion appears.

Suggestion: "if (x > 0.5 || y > 0.5 || z > 0.5) { digitalWrite(LED_BUILTIN, HIGH); ..."

Step20. Press <Tab> to accept.

void loop() {
// put your main code here, to run repeatedly:
float x, y, z;

// read the accelerometer
IMU.readAcceleration(x, y, z);

// print the accelerometer values
Serial.print("x: ");
Serial.print(x);
Serial.print(" y: ");
Serial.print(y);
Serial.print(" z: ");
Serial.prinln(z);

// turn on the LED if the accelerometer is tilted and log the tilt state.
// turn off the LED if the accelerometer is not tilted and log the tilt state.
if (x > 0.5 || y > 0.5 || z > 0.5) {
digitalWrite(LED_BUILTIN, HIGH);
Serial.println("Tilted");
} else {
digitalWrite(LED_BUILTIN, LOW);
Serial.println("Not Tilted");
}
}

Upload, test, and edit code

Step1. Select 'Verify' to confirm there are no errors.

Step2. Once complete, select the 'Upload' icon. Again, confirm there are no errors.

Step3. In the bottom toolbar, select the 'Serial Monitor' icon.

Step4. Observe the device and the serial monitor. The LED is on when the device is level and face up, and the LED is off at certain tilt angles. This is not the desired behavior.

The code only checks for positive values along the accelerometer's three axes. Negative values also need to be considered.

  • Update the logic with abs() to account for both positive and negative values.
...
// turn on the LED if the accelerometer is tilted and log the tilt state.
// turn off the LED if the accelerometer is not tilted and log the tilt state.
if (abs(x) > 0.5 || abs(y) > 0.5 || abs(z) > 0.5) {
digitalWrite(LED_BUILTIN, HIGH);
Serial.println("Tilted");
} else {
digitalWrite(LED_BUILTIN, LOW);
Serial.println("Not Tilted");
}
  • Select the 'Upload' icon.
  • Once complete, select the 'Serial Monitor' icon.

Step5. Observe the device and the serial monitor. The LED is always on. Again, this is not the desired behavior.

At a level resting position, the accelerometer's z-axis is non-zero. This is due to gravity! The value will be close to 1 or -1 depending on the orientation of the device. As such, the z axis should not be considered for tilt.

  • Remove the z variable from the logic to check tilt.
...
// turn on the LED if the accelerometer is tilted and log the tilt state.
// turn off the LED if the accelerometer is not tilted and log the tilt state.
if (abs(x) > 0.5 || abs(y) > 0.5) {
digitalWrite(LED_BUILTIN, HIGH);
Serial.println("Tilted");
} else {
digitalWrite(LED_BUILTIN, LOW);
Serial.println("Not Tilted");
}
  • Select the 'Upload' icon.
  • Once complete, select the 'Serial Monitor' icon.

Step6. Observe the device and the serial monitor. At a level resting position, the device LED is off and the serial monitor logs "Not Tilted". At any tilted or vertical position the LED is on and the serial monitor logs "Tilted." This is the expected behavior!

Example Output:

x: -0.00 y: 0.02 z: 1.00
Not Tilted
x: -0.01 y: 0.02 z: 0.99
Not Tilted
...
x: -0.50 y: -0.05 z: 0.83
Not Tilted
x: -0.51 y: -0.05 z: 0.84
Tilted
...
x: 0.54 y: -0.67 z: 0.53
Tilted
x: 0.51 y: -0.72 z: 0.49
Tilted
...
x: -0.27 y: -0.58 z: -0.79
Tilted
x: -0.28 y: -0.55 z: -0.83
Tilted
...
x: -0.03 y: -0.03 z: -1.01
Not Tilted
x: -0.01 y: -0.01 z: -0.99
Not Tilted
x: 0.01 y: 0.02 z: -0.98
Not Tilted
NOTE: At a true level resting position, the x-axis and y-axis values will be close to 0. As such, tilt detection can be made more precise by changing the x-axis and y-axis control values to be closer to 0. For example, change 0.5 to 0.1 and observe what happens!

Summary

Copilot is a great tool to help you learn a new code space, language, and brainstorm ideas on how to approach a problem. However, it should not be overly relied upon because it can be incorrect. Remember to reference official documentation and test your code!

Troubleshooting

Did you run into any issues during this tutorial? For example, IntelliSense didn't seem to show up or you would like more tips and tricks? Please see our tutorial for further assistance on these topics.

Appendix: How to install VS Code extensions

Microsoft Arduino extension

  • Open VS Code.
  • In the left column, select the 'Extensions' icon.
  • Search for 'arduino'.
  • Install 'Arduino' by Microsoft.
  • Close and restart VS Code. (Or wait until after the GitHub Copilot extension is installed.)

GitHub Copilot extension

  • Open VS Code.
  • In the left column, select the 'Extensions' icon.
  • Search for 'copilot'.
  • Install 'GitHub Copilot' by Github.
  • You may see a window to sign into GitHub sign in. Select 'Sign in to GitHub' and complete the steps.
  • You may see a window to signup for GitHub Copilot. This should only occur if you do not yet have a subscription associated with your GitHub account. Select 'Signup for GitHub Copilot' and complete the steps.
  • Close and restart VS Code
Read more

Code

VIDEO: Making a AI Generated Website with GitHub Copilot in 8 Minutes
CodeSalad

Credits

VIDEO: Accelerate creating IaC with Terraform and GitHub Copilot Chat
GitHub

Thanks to NASA for the starfield image.

Sources


Article information

Author: Patricia May

Last Updated: 1703684882

Views: 1526

Rating: 4.5 / 5 (69 voted)

Reviews: 87% of readers found this page helpful

Author information

Name: Patricia May

Birthday: 1990-03-30

Address: 8506 Daniel Common, North Christophertown, ND 14274

Phone: +4437415868777181

Job: Actor

Hobby: Woodworking, Kite Flying, Web Development, Beer Brewing, Billiards, Wildlife Photography, Yoga

Introduction: My name is Patricia May, I am a forthright, clever, tenacious, dazzling, enterprising, treasured, frank person who loves writing and wants to share my knowledge and understanding with you.