dark mode light mode Search Menu

Can ChatGPT Code?

Jernej Furman on Flickr

Everyone is talking about AI and chatbots at the moment, largely because AI is more accessible and being incorporated into websites, search engines, and chatbots. Naturally, people are playing with it and seeing how it can be useful. Let’s look at how it could help us write code for projects.

ChatGPT and similar AI tools work using requests that people type into the tools. These requests are called prompts. Figuring out how to write prompts to get the best results from these tools has created a new skill called prompt engineering. Because ChatGPT results can differ based on using different words, writing prompts takes lots of practice and skill.

To put it to the test we signed up for an account with Open AI at OpenAI.com. To register you need an email address and a mobile phone number to receive a one-time verification. Once registered we opened up ChatGPT and asked some basic requests. We’ve asked it to write MicroPython code for the Raspberry Pi Pico for all our examples. Our first request was for a simple “blink” type sketch.

We asked: “Could you write a simple MicropPython example for Raspberry Pi Pico that blinks the onboard LED at a rate of twice a second please.”

You can see that ChatGPT responds with a pretty good example of one way to write a simple LED blinking script. Usefully, when ChatGPT returns a code example, it places it in a code box with a button option for you to be able to copy the code to the clipboard on your computer. This means you can simply copy the code over to your MicroPython environment such as Thonny.

The returned code also contains some comments within the code that help clarify what some of the lines of code mean. Also, note that it correctly identified that for a Raspberry Pi Pico the onboard LED is controlled by GPIO number 25. Below the code box, ChatGPT then walks through the code example it has provided and steps through what each of the main code components means and does. It’s pretty impressive, and this code script would work perfectly copied over to a Pico board.

One thing of note is that it uses the toggle method to turn on and off the LED. This is a perfectly good and correct approach but it does highlight how ChatGPT has returned a direct solution to a query rather than write a tutorial. If someone was writing a tutorial for Raspberry Pi Pico and was using this “blink” example, you might actually start by writing a script that, in the while loop, writes the LED pin high, then pauses, then writes the LED pin low and again pauses. This achieves exactly the same results but is perhaps an important learning point for someone new to coding on microcontrollers before moving on to show the toggle example.

Stepping up the complexity we asked ChatGPT if it could create a code example that essentially created an altimeter that measured and logged the comparative altitude of the device in meters at a rate of 5 times per second. We asked:

“Can you write some MicroPython code for a Raspberry Pi Pico with an attached bmp280 sensor? The code should measure the relative altitude increasing in meters from the initial position of the system at power on. This should be logged at the rate of 5 times per second. The data should be passed through a Kalman filter.”

The last line of the request calls for a Kalman filter, this essentially uses some reasonably complex maths to compensate for noise and inaccuracy in the sensor and therefore smooths out the altitude data the system would report.

In this image, you can see that once again ChatGPT creates a code example in a code box for easy copying. It also, again, includes some useful comments in the code to highlight sections and their functionality. At the top of the code, it appears to import some built-in libraries (machine and time) and then it imports two libraries, one for the bmp280 and one for the Kalman filter.

At first glance, this code looks excellent. It looks like all you need to find are these two external libraries, “bmp280.py” and “kalman.py”, add them to your Pico, and then add the code to a separate .py file to be up and running. However, searching for “kalman.py” it became clear that this library didn’t actually exist. Furthermore, looking for “bmp280.py” returned quite a few different links to various libraries and different code examples, none of which appeared compatible with the code created in ChatGPT.

Returning to ChatGPT we asked it to clarify the location to download “kalman.py”. Interestingly it acknowledged it had made a mistake. It appears that it either“imagined” that a library called kalman.py existed or that the reader had enough experience to create a library that would function correctly.

It’s an interesting situation, someone using ChatGPT for code examples might try to compile this code using found libraries online, and, of course, they would be very unlikely to match and work correctly. In the response it wrote an example of a basic implementation of a Kalman filter and, on inspection, you could copy this code into Thonny and save it as kalman.py to the Raspberry Pi Pico and it should act as the library that it imported in the larger code example.

The problem is again that it didn’t explain that you could do this and it’s only due to experience that we noticed and checked that this would work. Again for someone new to coding, this would perhaps be an insurmountable problem that puts them off coding. 

It gave a similar response when we asked about the whereabouts of the bmp280.py library. However we then explicitly asked it to create a code library called bmp280.py that we could save and use in the earlier response examples. This returned what looked like an accurate response and the code box included a section of text describing how we could save this code as bmp280.py and add it to our root folder on a Pico alongside our earlier code.

However, we weren’t out of the woods yet. Within the bmp280.py code response we noticed a few interesting comments that simply read “# …”. We also felt that where these comments were there were missing sections of code. Asking ChatGPT to clarify it did eventually confirm that it meant that the “# …” comments were placeholders for parts of the code where “some calculations” occur. It’s totally unclear why ChatGPT would miss out these parts of the code. Asking it, in turn, to fully populate any missing sections of code indicated by “# …” seems to get ChatGPT to return an accurate code library.

So in conclusion, it’s been an interesting experiment! While it’s clear that ChatGPT can return really accurate code and solutions to queries, it can definitely include faults and omissions that can be difficult to see. These may well be understandable only if you already know a reasonable amount about the code language and system you are trying to work with. If you are a beginner at coding a particular language we think that you would be better served working through some tutorial content before jumping to ChatGPT too quickly.

However, if you have some of the basics understood and you can appreciate where ChatGPT might fall short, using ChatGPT could help you approach some slightly more complex projects and could give you some general incomplete ideas about how to structure your code. We think it’s a similar situation to using computers in general, the computer and ChatGPT are tools to help you but aren’t going to do all the work for you.

Learn More

 How to use Chat GPT to Write a Code


Coding with Chat GPT


Using Chat GPT to Write Code


Chat GPT for Coding: Strengths and Weaknesses


How to Use Chat GPT to Write Code