element14 Community
element14 Community
    Register Log In
  • Site
  • Search
  • Log In Register
  • About Us
  • Community Hub
    Community Hub
    • What's New on element14
    • Feedback and Support
    • Benefits of Membership
    • Personal Blogs
    • Members Area
    • Achievement Levels
  • Learn
    Learn
    • Ask an Expert
    • eBooks
    • element14 presents
    • Learning Center
    • Tech Spotlight
    • STEM Academy
    • Webinars, Training and Events
    • Learning Groups
  • Technologies
    Technologies
    • 3D Printing
    • FPGA
    • Industrial Automation
    • Internet of Things
    • Power & Energy
    • Sensors
    • Technology Groups
  • Challenges & Projects
    Challenges & Projects
    • Design Challenges
    • element14 presents Projects
    • Project14
    • Arduino Projects
    • Raspberry Pi Projects
    • Project Groups
  • Products
    Products
    • Arduino
    • Avnet Boards Community
    • Dev Tools
    • Manufacturers
    • Multicomp Pro
    • Product Groups
    • Raspberry Pi
    • RoadTests & Reviews
  • Store
    Store
    • Visit Your Store
    • Choose another store...
      • Europe
      •  Austria (German)
      •  Belgium (Dutch, French)
      •  Bulgaria (Bulgarian)
      •  Czech Republic (Czech)
      •  Denmark (Danish)
      •  Estonia (Estonian)
      •  Finland (Finnish)
      •  France (French)
      •  Germany (German)
      •  Hungary (Hungarian)
      •  Ireland
      •  Israel
      •  Italy (Italian)
      •  Latvia (Latvian)
      •  
      •  Lithuania (Lithuanian)
      •  Netherlands (Dutch)
      •  Norway (Norwegian)
      •  Poland (Polish)
      •  Portugal (Portuguese)
      •  Romania (Romanian)
      •  Russia (Russian)
      •  Slovakia (Slovak)
      •  Slovenia (Slovenian)
      •  Spain (Spanish)
      •  Sweden (Swedish)
      •  Switzerland(German, French)
      •  Turkey (Turkish)
      •  United Kingdom
      • Asia Pacific
      •  Australia
      •  China
      •  Hong Kong
      •  India
      •  Korea (Korean)
      •  Malaysia
      •  New Zealand
      •  Philippines
      •  Singapore
      •  Taiwan
      •  Thailand (Thai)
      • Americas
      •  Brazil (Portuguese)
      •  Canada
      •  Mexico (Spanish)
      •  United States
      Can't find the country/region you're looking for? Visit our export site or find a local distributor.
  • Translate
  • Profile
  • Settings
Raspberry Pi
  • Products
  • More
Raspberry Pi
Blog Google’s new Gemini Code Assist extension for VS Code creates a 6 DoF accelerometer + magnetometer driver for my Raspberry Pico 2 W board.
  • Blog
  • Forum
  • Documents
  • Quiz
  • Events
  • Polls
  • Files
  • Members
  • Mentions
  • Sub-Groups
  • Tags
  • More
  • Cancel
  • New
Join Raspberry Pi to participate - click to join for free!
Featured Articles
Announcing Pi
Technical Specifications
Raspberry Pi FAQs
Win a Pi
GPIO Pinout
Raspberry Pi Wishlist
Comparison Chart
Quiz
  • Share
  • More
  • Cancel
Group Actions
  • Group RSS
  • More
  • Cancel
Engagement
  • Author Author: BigG
  • Date Created: 30 Mar 2025 9:20 PM Date Created
  • Views 1820 views
  • Likes 9 likes
  • Comments 11 comments
Related
Recommended
  • RP2350
  • pico 2 w
  • pico c sdk
  • vscode
  • LM303

Google’s new Gemini Code Assist extension for VS Code creates a 6 DoF accelerometer + magnetometer driver for my Raspberry Pico 2 W board.

BigG
BigG
30 Mar 2025
Google’s new Gemini Code Assist extension for VS Code creates a 6 DoF accelerometer + magnetometer driver for my Raspberry Pico 2 W board.

Table of Contents

  • Introduction
    • Validating that the sensor works
    • Challenges switching to MCU of choice
  • AI assisted coding workflow
    • Step 1: Installation and Sign-in
    • Step 2: Create Pico template code
    • Step 3: Ask Gemini for driver template code
    • Step 4: Ask Gemini to solve problems with the code
  • Closing remarks

Introduction

Buoyed by my recent success with using Google’s Gemini AI to assist me in creating some I2C driver code for an MCP23017 IC (view the blog here), I wanted to see if this was a complete fluke or whether Gemini could actually reliably help accelerate code development by trying it out with another random sensor. Would it work with something a little more complicated, perhaps?

Of course, I was also dying to test out Google’s recently launched Gemini Code Assist extension for VS Code, which provides a more integrative and immersive experience.

So, I had a browse around the den and found an accelerometer carrier board from Pololu (still in its packaging, unused). The carrier board used an STMicroelectronics LSM303DLM 3-axis accelerometer and 3-axis magnetometer sensor, which after all these years had become obsolete. Still, I felt this was a great use case as there are different versions of this sensor on the market, which require unique settings for the sensor to work, and multiple I2C addresses can be used for the accelerometer and the magnetometer has its own I2C address. On the positive side, the sensor is well documented and there is also plenty of open-source code out there for other microcontrollers - just not for the RP2350.

LSM303DLM

However, before jumping straight in, experience kicks in. It's always well worth confirming that the sensor on the carrier board still actually works.

Validating that the sensor works

To validate sensor functionality, a standard approach I employ, and I suspect many others do as well, is to establish a test bed. I typically start with an Arduino UNO, or a similar board, and wire up the carrier. Then, I locate a relevant library with example code to check the sensor's output. Finding an open-source library is usually straightforward, especially with established sensors, making this my go-to method for initial verification.

This use case was certainly no different. All I had to do was search out and download a library for the LSM303 from within the Arduino IDE (I found two options), then find a suitable sketch and run. Within minutes I was relieved to see that the carrier board does still works.

You don't have permission to edit metadata of this video.
Edit media
x
image
Upload Preview
image

Challenges switching to MCU of choice

I consistently find the next stage challenging, especially when a dedicated sensor library is unavailable for my target microcontroller SoC. Often, I an tempted or pressured to compromise due to time constraints, resorting to a less-than-ideal but familiar development platform.

This led me to wonder if AI could fundamentally change things for the better.

I wanted to see if it genuinely simplifies sensor integration or just adds another layer of potential headaches.

I hope this blog helps others kickstart a positive change in their own development methodologies.

AI assisted coding workflow

I’ll start with some spoiler alerts…

I got it to work, but it took a few iterations. Developing my driver code for the LM303DLM sensor was almost exclusively a cut and paste exercise.

My observations...

I've just discovered that this approach, where AI handles the majority of the coding process, is referred to as "vibe coding". This is a novel concept to me, as I had no prior experience with, or knowledge of, this term; I simply encountered it on social media: [Wikipedia link]

Anyway, I've now learnt, through this exercise, that the success of using AI-assisted coding to jumpstart a project hinges heavily on the precision of my prompts. Even subtle changes in wording can drastically alter the generated code. I also noticed inconsistencies in coding syntax, particularly with Pico functions for I2C initialization and verification.

When encountering compiler errors or subtle parameter issues—those the compiler misses—the speed of resolution depends significantly on my own sensor expertise. For instance, Gemini fabricated a Pico library function for I2C initialization, leading to a compilation failure. A bizarre error, indeed. However, after pointing it out, Gemini corrected it. Then, when the compiled code failed to work on the Pico, I had to explicitly request Gemini to consult the datasheet, which it did—an impressive feat! Subsequently, a manual review was necessary on my part to highlight the potential parameter error, requiring a further query to Gemini for further verification.

The truly remarkable aspect of AI is its ability to confirm my suspicions, by checking it's own responses. It then provided me with the corrected code.

Conclusion...

Ultimately, Gemini had failed to read the manuals… so it’s universal… no one ever reads the frigging manual!

Here's a detailed walk-through of my setup and embedded development process. Note, videos do not include audio.

Step 1: Installation and Sign-in

I began by installing the Gemini Code Assist extension for VS Code. Once installed, it was not immediately obvious about any login requirements or login screen. It was only that I noticed something new on my screen… at the bottom right of my VS Code screen was a new gold coloured diamond icon. Clicking on this icon opened up the Google Account login screen. This turns out to be a one-time procedure.

image

Step 2: Create Pico template code

I then generated some C template code for the Raspberry Pi Pico, using the Raspberry Pi Pico VS Code extension. As usual, I incorporated the necessary I2C and USB console access libraries. I also decided to experiment with the optional timer feature (out of curiosity), even though this wasn't necessary for the sensor driver.

image

Once the code was generated I then amended the code to reflect the I2C pins I had used to connect the sensor to the Pico on the breadboard. In my case, I used pins 16 and 17 for I2C0.

image

Step 3: Ask Gemini for driver template code

This is where things got interesting. I prompted Gemini for assistance, using this request syntax: "Create an I2C library for the LSM303DLM sensor to read acceleration and magnetometer data", and within seconds, it provided a response directly within VS Code.

Gemini had generated code for two new library files, namely, "lsm303dlm.h" and "lsm303dlm.c," along with modified code for my main file, "Pico2W_ReadMagAccelData.c". It also provided the necessary text amendments to the CMakeLists.txt file. Even though my template code had included some timer initialisation code and a repeating "hello_world" printf statement, these were both removed in the amended Pico2W_ReadMagAccelData.c code. So if you wanted to keep, don't block copy and paste.

After the code additions/amendments, Gemini then provided a "Explanation and Key Improvements" section, detailing the steps taken in a clear, bullet-point format. I found this quite helpful.

It's worth mentioning that when I didn't specifically request a library in my prompt request, Gemini always returned a modified main project file with the added or amended functions.

You don't have permission to edit metadata of this video.
Edit media
x
image
Upload Preview
image

Step 4: Ask Gemini to solve problems with the code

In contrast to my previous blog, the driver library code generated by Gemini Code Assist failed to compile. Gemini had included this function check If (!i2c_is_enabled(i2c){}, which resulted in the compilation error 'implicit declaration of function i2c_is_enabled()'.

I've learned from experience that if I get an 'implicit declaration' error, it almost always means that function or variable isn't in the SDK or the libraries I'm using. However, when prompting Gemini with the error message, it failed to properly check and returned another unworkable suggestion.

So, it's not just me. Even AI struggles to understand error messages. However, in this case I think it should have been fairly obvious.

It required an explicit prompt to check the SDK, before it acknowledged the error and provided a fix.

This solved the compile problem, but the code still did not work.

You don't have permission to edit metadata of this video.
Edit media
x
image
Upload Preview
image

But, I did get a "sensor failed to initialise" error. Based on my experience, this typically indicates that this is an I2C address issue; however, definitive confirmation required reviewing the datasheets.

I was curious to see if Gemini AI would deduce the I2C address problem. However, the response received, merely highlighted the importance of precise phrasing, as Gemini merely confirmed that SA0 low address it had used was correct, rather than diagnosing the error.

I had to explicitly tell Gemini to check the datasheet before it spotted the error.

Then it finally produced some working code.

You don't have permission to edit metadata of this video.
Edit media
x
image
Upload Preview
image

Closing remarks

I originally thought that the availability of open-source libraries for platforms like Arduino would be a significant advantage but when I did try (post these results) to get Gemini to port Arduino code across to the Pico 2 W it only introduced other errors (none of which I investigated). Hence, I was proven wrong in this instance.

Having only just begun exploring the potential of AI in embedded driver development, I'm already impressed by its possibilities. I can already see that with a well-documented SDK and a detailed sensor datasheet, AI tools like Gemini can make a significant improvement in developing drivers for sensors. And in this instance, the Raspberry Pi Pico's exceptional documentation makes it a particularly strong platform for leveraging these AI capabilities. Well done to Raspberry Pi. I'm sure they never imagined this benefit when they started.

All I can say is the future is very bright when it comes to productivity improvement. I estimated that this probably took me about 20 minutes in man effort (excluding some thinking time) to develop a new sensor driver for the RP2350 / Pico 2 W board. That is significantly quicker than anything I had done previously. The power comes not in how much code it provided, but in knowing just how much code to provide (all too often when porting any existing library across ourselves we include everything).

Truly remarkable.

  • Sign in to reply
  • michaelkellett
    michaelkellett 5 months ago in reply to Jan Cumps

    I write code which generally complies with MISRA guidelines and the Barr Group Embedded C Coding Standard. There are exceptions that I routinely apply: 

    1) I don't use an automated tool for style checking and I currently use PCLint for MISRA and static code checking

    2) I don't observe 2.2h "Each module and function shall be commented in a manner suitable for automatic documentation generation, e.g., via Doxygen."

    3) I don't document exceptions.

    If I'm writing code for a customer I say this:

    Code will be supplied as full readable source and project files produced with the tools described in [tools location]

    XXXXXXXX makes no claims with regard to fitness for purpose or compliance with any external or internal standards.

    It is the responsibility of the customer to ensure that the code supplied is suitable for the intended use.

    It's a bit brutal, but most suppliers say the same thing in one way or another, perhaps shrouded by a lot of legalistic language.

    When I write code for myself I don't use Lint which means that there will certainly be accidental non compliances but I try (quite hard) to stick to the rules.

    The problem with MISRA is that you can get bogged down in silly non compliances with things like register definitions from the processor supplier and it can require self defeating wriggles to force the checking tools it ignore the instances of such behaviour that you intend, while still trapping the ones you didn't and documenting all this.

    Despite the problems I think that MISRA has made my code much better.

    There are (many) places where the MISRA guidelines are not suitable and some of the demands come as a bit of a shock eg:

    4.12 Dynamic memory allocation shall not be used (show stopping for many things)

    or

    7.2 A "u" or "U" suffix shall be applied to all integer constants that are represented in an unsigned type (you get used to it and a U doesn't cost much)

    The rationale behind these things is usually good, and certainly improves the portability of code.

    And even if you end up with an exception the rules help you spot, and with any luck mitigate, potential problems.

    So far I've never encountered an instance where the application of a rule would demand a different processor, but this is probably because of the kind of work I do (deeply embedded and very close to the hardware).

    I work on my own, but I can see that in a team of many people the enforcement of MISRA type practice with formal sign off of exceptions, would provide benefits.

    Thanks for bringing this up - it's encouraged me to have a good think about my coding practice which is always a good thing !

    MK

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • More
    • Cancel
  • Jan Cumps
    Jan Cumps 5 months ago in reply to michaelkellett

    I have theoretical experience with MISRA. Studied when I was learning to code for TI's Hercules safety microcontrollers.

    I'd be interested to know how it impacts your developments after many years of experience? Do you use the guidelines to your advantage? Do you curse it from time to time because you can't do some fancy code wizardry?

    Have you ever been forced to do something sub-optimal (maybe needing a bigger µC) because some efficient design approach failed MISRA checks?

    ... aaaand: do you use the guidelines for projects that don't require it?

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • More
    • Cancel
  • michaelkellett
    michaelkellett 5 months ago in reply to BigG

    Hello, thanks for trying that.

    I'm puzzled - I couldn't see any point where it identified anything as non compliant although there are many violations of the MISRA C2012 guildelines.

    for example:

    Rule 15.5, Advisory  A function shall have a single point of exit at the end

    MK

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • More
    • Cancel
  • BigG
    BigG 5 months ago in reply to michaelkellett

    It appears so. Although, I wouldn't know the validity of the feedback received having never done a MISRA compliance review before...

    image

    Here's a video capture showing all the feedback received following review.

    You don't have permission to edit metadata of this video.
    Edit media
    x
    image
    Upload Preview
    image

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • More
    • Cancel
  • michaelkellett
    michaelkellett 5 months ago in reply to BigG

    Thanks for that.

    I wonder if it can make MISRA compliant code !

    MK

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • More
    • Cancel
>
element14 Community

element14 is the first online community specifically for engineers. Connect with your peers and get expert answers to your questions.

  • Members
  • Learn
  • Technologies
  • Challenges & Projects
  • Products
  • Store
  • About Us
  • Feedback & Support
  • FAQs
  • Terms of Use
  • Privacy Policy
  • Legal and Copyright Notices
  • Sitemap
  • Cookies

An Avnet Company © 2025 Premier Farnell Limited. All Rights Reserved.

Premier Farnell Ltd, registered in England and Wales (no 00876412), registered office: Farnell House, Forge Lane, Leeds LS12 2NE.

ICP 备案号 10220084.

Follow element14

  • X
  • Facebook
  • linkedin
  • YouTube