Accelerate your Arduino projects with GitHub Copilot AI

Increase your coding speed, learn more about Arduino libraries, and explore Arduino hardware via AI -- and all from a single editor!

BeginnerProtip1 hour3,985

Things used in this project

Hardware components

Arduino Nano RP2040 Connect
Arduino Nano RP2040 Connect
×1

Software apps and online services

GitHub account
GitHub Copilot subscription
Arduino IDE
Arduino IDE
VS Code
Microsoft VS Code
Visual Studio Code Extension for Arduino
Microsoft Visual Studio Code Extension for Arduino
Visual Studio Code Extension for GitHub Copilot

Story

Read more

Code

Copilot for Arduino Nano RP2040 Connect Accelerometer

C/C++
// 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);
}

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.println(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 (abs(x) > 0.5 || abs(y) > 0.5) {
        digitalWrite(LED_BUILTIN, HIGH);
        Serial.println("Tilted");
    } else {
        digitalWrite(LED_BUILTIN, LOW);
        Serial.println("Not Tilted");
    }
}

Credits

Mollie Muñoz

Mollie Muñoz

6 projects • 9 followers
A lifelong learner, creator, and problem solver for whatever adventure or new idea comes my way. Software Engineer ll @ Microsoft.
Jen Fox

Jen Fox

34 projects • 139 followers
Dabbled in dark matter, settled into engineering w/ a blend of inventing and education! Sr.PM @ MicrosoftFounder/CEO of FoxBot Industries
Adrian Bonar

Adrian Bonar

6 projects • 10 followers
Always looking to make positive impacts with technology on society, empower others, and grow through culture and engineering.
nberdy

nberdy

5 projects • 4 followers
Mark Wilson-Thomas

Mark Wilson-Thomas

2 projects • 0 followers
Thanks to NASA for the starfield image.

Comments