Download PDF Parts List View on YouTube Download Code

Today, we will use the amazing ChatGPT chatbot to write C++ and MicroPython code for both an Arduino Uno and an ESP32.

From today onward, you now have an AI Assistant to help you with your microcontroller coding!


Since its release on November 30, 2022, ChatGPT has taken the world by storm. Its ability to deliver human-like responses to non-structured questions has both amazed and frightened people.

While most of the focus on ChatGPT has been on its astounding ability to produce original, human-sounding text, it is also capable of writing programming code.

Actually ChatGPT can code in a wide variety of languages, including languages like C++ and MicroPython that can be used with microcontrollers.  And today, we will test its ability to write code for both an Arduino Uno and an ESP32 module.

While most of our coding examples are C++ sketches for the Arduino IDE, we will also have ChatGPT use MicroPython to create an ESP32 web server.


ChatGPT is an AI chatbot that works like no other chatbot you’ve previously encountered.  It’s been trained on a large language model that has taught it basically everything and every subject in every language.

ChatGPT is NOT connected to the Internet, at least its dataset isn’t. The current public beta is trained on data that was finalized at the beginning of 2022, so it has limited knowledge of events after 2021.  

The ChatGPT interface is pretty simple, consisting of a text box for entering your question or command. The result will be displayed at the top of the web page.


ChatGPT is the brainchild of OpenAI, an artificial intelligence research laboratory consisting of the for-profit OpenAI LP and its parent company, the non-profit OpenAI Inc. The company is focused on developing and promoting friendly AI in a way that benefits humanity as a whole. OpenAI’s research areas include machine learning and artificial intelligence.

Its founding members include:

  • Sam Altman – Former president of technology accelerator Y Combinator.
  • Elon Musk – CEO of Tesla, SpaceX, and Twitter.
  • Reid Hoffman – Co-Founder of LinkedIn.
  • Jessica Livingston – Founding partner of Y Combinator.
  • Peter Thiel – Co-founder of PayPal, one of Facebook’s first investors.

ChatGPT is not the only product that OpenAI has released. They also released the DALL.E 2 image generator, which uses AI to create artwork from a text description.

How ChatGPT Works

Essentially, ChatGPT is an autoregressive language model that uses deep learning to produce human-like speech, which may not mean that much to you if you haven’t been working with artificial intelligence, machine learning, or neural networks.

ChatGPT is based upon OpenAI’s GPT-3.5 Language Model, so understanding how it works is the key to understanding ChatGPT.  You should be familiar with some basic AI terminology, starting with Language Models themselves.

Language Models

Language models are computer programs that can understand and generate human-like speech. They do this by predicting which word will most likely come next in a sentence. These models are important for making computers understand and communicate with people. To understand how they work, you would need to know more about how computers learn and process information.

Machine Learning

Machine learning is a way for computers to learn and improve by using data. It’s like giving a computer a set of examples and then having it figure out the rules and patterns on its own. There are different types of machine learning, like supervised, unsupervised, and reinforcement.

The GPT-3.5 model is a type of machine-learning model called a language model. It was trained and fine-tuned using supervised and reinforcement learning. The way the model works is based on a technology called deep learning, which is a specific type of machine learning that uses artificial neural networks.

Deep Learning

Deep learning is a highly advanced form of machine learning that utilizes multiple layers of neural networks to process and understand data. The structure and function of the human brain inspired this technology.

The GPT-3.5 language model is one of the most powerful examples of how deep learning can be used for natural language processing. The model is trained on a vast amount of text data and uses deep learning to learn how to understand and generate human-like speech.

Neural Networks

A neural network is a type of computer program that is designed to work like the human brain. It is made up of layers of interconnected “nodes” or “neurons”. Each node is like a tiny computer that processes information and sends it on to the next node.

The information enters the neural network at the input layer, where it is assigned a weight based on its importance for the final output. If the weight is high enough, the node is activated and sends the information to the next layer for further processing. As the information moves through the layers, it is gradually transformed and abstracted, allowing the neural network to learn increasingly complex patterns and features in the data.

The GPT-3.5 language model is an example of a neural network that uses this technology to understand and generate human-like speech. 

How ChatGPT was Trained

While the actual operation of ChatGPT and the GPT-3.5 Language Model is propriety, OpenAI did release a diagram illustrating the training process used to train the language model.

Essentially, the training involved many human operators, both inputting data and querying the system for it. The trainers set up a system of “rewards” given when the computer produces the desired output. This went on through many iterations, with the computer getting smarter every time.

While the OpenAI diagram may be a bit hard to interpret, it can be simplified by breaking down each of the three steps:

Step 1 – Collect demonstration data and train a supervised policy

SIMPLIFIED – Train with human examples

The first step in creating GPT-3.5 was to train the model using examples of human-generated text. This is called supervised learning. The model was given prompts and example responses, and it learned how to generate text that is similar to the examples it was shown.

Step 2 – Collect comparison data and train a reward model

SIMPLIFIED – Get feedback and learn from it

The next step was to get feedback on the model’s performance. OpenAI collected new data from model-generated responses and then asked human trainers to rank them from best to worst. This feedback was used to create a new model, called a reward model, which helped the model understand what makes a good response.

Step 3 – Optimize a policy against the reward model using the PPO reinforcement learning algorithm

SIMPLIFIED – Use feedback to improve the model

Finally, the GPT-3.5 model was fine-tuned using the feedback from the reward model. This means that the model was given new prompts, and its responses were ranked by the reward model. The feedback was used to make the model perform better. This process was repeated several times before OpenAI released the GPT-3.5 model to the public.

Getting Access to ChatGPT

As of this writing (late January 2023), access to ChatGPT is completely free. You will require an OpenAI account, which you can get by supplying an email address.

You can log in to ChatGPT from either the OpenAI website or directly on the ChatGPT login page.  There is occasionally a waiting list to get access. Still, many of the access problems seem to have disappeared as OpenAI scales up its servers to accommodate the enormous demand for ChatGPT.

The ChatGPT user interface is about as simple as it can get. There is a main page and a left sidebar.

The Sidebar contains your previous “chats,” as well as a button to start a new chat. When you first log in, you will be on a New Chat screen.  You can start there or go back to one of your older chats and carry on the conversation.

Below the chats on the sidebar are some menu items. You can clear chats, set the interface to “dark mode,” and jump to the OpenAI Discord Server or an FAQ page. The bottom button lets you log out.

The main interface is on the larger section of the screen, and it is super simple – a text box and an Enter (arrow) button.  You just type your query or command into the text box and either hit your Enter key or click the arrow icon beside the text box. ChatGPT will move the conversation to the top of the screen and respond.

The interface is used in the same fashion for coding as it is for text. Just tell ChatGPT what you want, and it will do its best to deliver it.

Remember, the current version (January 9, 2023) does not know any events from the beginning of 2022, so it isn’t going to be up on the latest news. Although it may seem unbelievable, the database is NOT connected to the internet!

ChatGPT Coding Basics – Arduino

Let’s work with ChatGPT and get it t write some code for us. We will start with an Arduino Uno, with a few components hooked up to it.

Hooking up the Arduino

The hookup for our Arduino Uno is pretty simple.  You’ll need the following:

  • Arduino Uno or clone
  • LED, any color will do
  • Resistor, 220 ohms or any value from 150 to 470 ohms
  • Potentiometer, 5K or greater, linear taper preferred
  • SSD1306 OLED Display with I2C interface
  • SPST momentary-contact normally-open pushbutton

Hook everything up as follows using a solderless breadboard and some jumper wires:

Click on the wiring diagram to open a larger image

Now let’s get ChatGPT to write code for our Arduino and its peripherals!

ChatGPT Blink

It is customary to start any Arduino programming journey with a recreation of the famous Blink sketch, and I am not one to break tradition.  So for our first foray into the world of AI-Generated Programming, we will get the onboard LED on or Arduino flashing once per second!

Open ChatGPT and start a new conversation. Type (or paste) the following command into the input text box:

Write code to flash the onboard LED on an Arduino Uno once per second

Press Enter or click the arrow when you are done.

ChatGPT should start writing code. In some cases, there can be a delay of up to a minute before it responds; this is probably a result of the server being taxed by all the active sessions. Sometimes there is no delay at all.

ChatGPT should respond with a code sample. After the code sample, it will give you a brief explanation of the code.

The code you receive may not be exactly like the code I received. In fact, if you give it the same query, you will likely end up with a different code and/or explanation.

You can copy the code sample using the “Copy Code” icon at the top of the ChatGPT code output. This will copy the code into your clipboard, and from there, you can paste it into the Arduino IDE.

Upload the code and observe the Arduino Uno. A flashing onboard LED should greet you.

Now that we have that rite of passage done with, we can move on to bigger and better things!

ChatGPT PWM LED Control

We have an LED connected through a dropping resistor to pin nine on the Arduino Uno, and by fortunate happenstance, that pin is PWM-capable.  And, in another lucky break, we also have a potentiometer wired as a voltage divider with its output connected to analog input A0.

Obviously, we need to take advantage of this situation and write some code to control the brightness of that LED with the potentiometer! 

Let’s let ChatGPT do that for us. Open a new session (or stay in the current one) and type the following:

Control the brightness of an LED attached to pin 9 on the Arduino Uno with a potentiometer connected to analog input A0, using PWM

Once again, our new AI code assistant should get to work producing a sketch similar to this one:

As I said earlier, ChatGPT can produce different outputs for the same query. I have seen this sketch generated correctly (as the copy here is) and also generated incorrectly, with a missing “map” function call.

If you don’t like the sketch, ask ChatGPT for a new one by clicking the “Regenerate Response” button.

The code I uploaded to the Arduino worked perfectly, allowing my LED to be controlled by the potentiometer.  And I didn’t actually write any code myself; I just stated what I wanted, and it magically appeared. 

At least, it seems a lot like magic! 

Correcting Code with ChatGPT

You can also use ChatGPT to correct code that doesn’t work. This can be a valuable tool in conjunction with the compiler errors that your IDE is giving you. In fact, ChatGPT can go one step further than your compiler, it can rewrite the code with the errors corrected!

To test this out, I intentionally put an error into the previous code (the variable brightness LED sketch). I changed the name of one of the variables, which will cause the code to fail to compile.

I then asked ChatGPT:

Correct the following code:

Now, I could have just pasted the code right after this sentence, and ChatGPT would have been fine with that; the AI chatbot doesn’t really care about text formatting (at least, I don’t think it does).  But it is much neater to place it on the line below.

The trouble is, if I hit Enter, it will enter the text “correct the following code” into ChatGPT, without the code itself.

To move down a line in the ChatGPT interface, hold down your Shift key and press Enter. This moves your cursor to a new line and expands the size of the text box. You can then paste your code there.

Once you enter your code, ChatGPT will “think” for a moment and then, hopefully, give you back the corrected code. It should also have inserted a comment into the code where it needed to make changes.

I haven’t tried this with many code samples; it would be interesting to test its capabilities here.  Having an assistant coder who can check your work and find your errors sounds like a pretty good thing to me!

An Arduino Project with ChatGPT

Let’s get ChatGPT to build a complete project for us.

We already have the wiring for our project completed, as we will be building a temperature and humidity meter with a pushbutton that toggles the display between Celsius and Fahrenheit.  All we need is the sketch.

One thing that I want to illustrate while building this project is how you can use ChatGPT in a “step-by-step” mode, building the project up as you go.

Using a DHT22

We will start with the DHT22 temperature and humidity sensor. 

The DHT22 is a pretty common device; it also goes by the name AM2302, and it is a pretty basic temperature and humidity sensor. It also has a cousin, the DHT11, which you can use as well. If you do use a DHT11, you’ll need to tell ChatGPT, as the coding is a bit different.

We’ll test the sensor by displaying its readings on the serial monitor. I like to build up projects this way, testing each major component step-by-step. It’s a great way to catch wiring or coding errors early in the project design process.

So I asked ChatGPT to write some code for me:

Write code to display the temperature and humidity readings from a DHT22 sensor on the serial monitor. DHT is connected to pin D3 of an Arduino Uno

It complied and wrote the following sketch:

ChatGPT also informed me that I would need to install the DHT library in my Arduino IDE, although, in my case, I already had it installed.

I pasted the sketch into the Arduino IDE, and it compiled. I uploaded it to the Arduino and opened the Serial Monitor to observe the results:

The sketch worked properly and displayed the temperature and humidity every two seconds.

With the DHT22 working, it’s now time to get the display up and running.

Adding an OLED Display

The SSD1306 is a very common OLED display. It’s available with both SPI and I2C interfaces, this project uses a display that connects via I2C.

To get the code for the display, I presented ChatGPT with the following query:

Write code to display the temperature and humidity readings from a DHT22 sensor on an SSD1306 OLED display. DHT is connected to pin D3 of an Arduino Uno. SSD1306 uses I2C

And, once again, ChatGPT returned with some code:

It also told me that I would need a couple of Adafruit libraries for the project to work. The way that the instructions for the library installation were worded leads me to believe that ChatGPT is more familiar with IDE 1.8 instead of the current Arduino IDE 2.0, but of course, that makes perfect sense as the data it was trained on was from 2021, before IDE 2.0 became the production version.

Again, the code worked properly and produced a temperature and humidity reading on the OLED display.

Add a Celsius/Fahrenheit Button

The final modification to the code is to provide a way of toggling the temperature display between Celsius and Fahrenheit.   We’ll use the momentary contact pushbutton for that; one side of it is connected to I/O pin D4, and the other side is grounded.

Instead of describing the whole project, we will just ask ChatGPT to update the last bit of code. You can do this as long as you stay in the same conversation, and you can return to the conversation later to update the code further.

Add a pushbutton connected to pin D4 to the above circuit. Code the pushbutton to toggle the temperature display from Celsius to Fahrenheit

ChatGPT wrote us the code with the pushbutton, and it looks like it will work. 

However, it also mentions that we will either need to add a pull-up (or pull-down) resistor to the circuit or modify the code to use the internal pull-up resistors.

But why modify the code ourselves? It’s 2023, not 2022!  We’ll just ask ChatGPT to do it for us:

Modify the above code to use the internal pullup resistor for the pushbutton

ChatGPT rewrites the code, this time using the internal pull-up resistor for the switch input. 

So I loaded up the code that ChatGPT created and tried it out. And it “sort of works”!

The problem is that when you press the pushbutton briefly, nothing happens, at least most of the time. But if you hold it down, the button works and toggles the temperature unit from Celsius to Fahrenheit.

Looking at the code that ChatGPT wrote reveals the issue. The 2-second delay at the end of the loop is the problem, while the delay is running, the pushbutton is being ignored.

The delay is there for a reason; the DHT22 requires two seconds between readings for the sensor to stabilize.

Fixing this isn’t easy, so I’m not really blaming ChatGPT. A proper fix would have the DHT22 being read using a 2-second timer interrupt or a counter that counts elapsed milliseconds. You could also use an interrupt for the pushbutton as well.

Or just change the instructions to read “hold down the pushbutton until the display unit changes,” and then it works 100 percent!

I would say that ChatGPT proved that while it might not write all the code for your next project, it could very well write most of it.

ESP32 with ChatGPT

Now we will move over to another microcontroller, the ESP32. This little 32-bit wonder also includes wifi and Bluetooth, and we will be using its wifi capabilities in our experiments.

We don’t need to attach any additional components to the ESP32, but you will need your Arduino IDE set up with an ESP32 Boards Manager. I explain that in my introduction to the ESP32.  And pretty well any ESP32 module will work.

Our second experiment will be running MicroPython. I would recommend using the Thonny IDE for this, especially as you can also load the MicroPython firmware directly to the ESP32 inside Thonny.

ESP32 WiFi Scanner

Our first experiment is a WiFi Scanner, written in C++ for the Arduino IDE. Getting the code was as simple as asking ChatGPT the following:

Write code for an ESP32 WiFi Scanner

As if by magic (it really does look a lot like magic), ChatGPT will pump out some code, ready to paste into your Arduino IDE. Mine looked like this:

Running the code does indeed produce a list of WiFi networks, along with their signal strength and the type of encryption they use.

Using ChatGPT with MicroPython

So far, all the code we have had ChatGPT generate for us has been written in C++. But this little AI chatbot is capable of writing code in pretty well any programming language.

So to finish off our experiments, we will have ChatGPT build us a small web server on the ESP32. We’ll specify what text we want on the home page as well.

Setting up MicroPython on the ESP32

To perform this experiment, we will need an ESP32 with the latest version of the MicroPython interpreter loaded. You can do this in a few different ways, including from the command line, but the easiest method is to use the Thonny IDE.

STEP 1 – Install Thonny IDE

Of course, you must have the Thonny IDE installed on your computer first, so head over to their download page and pick the correct file for your system. It can be installed on Windows, Linux, or macOS.

If you happen to have a Raspberry Pi, then you’ll find Thonny is already installed in your OS, in the Programming section.


After you get Thonny installed, head over to the MicroPython Downloads page and search for your ESP32. Most boards can just use the generic ESP32 image, but there are also images for specific boards.

On the board page, pick out the most recent version of the interpreter. You want to download the ”.bin” version.


Ensure your ESP32 board is connected to the computer and open the Thonny IDE. 

  • Go to Tools/Options/Interpreter.
  • Select “MicroPython ESP32” as your interpreter.
  • Select the port your board is connected to.
  • Click the link for “Install or update firmware”.
  • On the next page, select the same port you selected earlier.
  • Use the Browse button to select the .bin file you downloaded.
  • Click the Install button

The new firmware with the MicroPython interpreter will be flashed to your ESP32 board.


Go back to the Tools/Options/Interpreter menu and ensure that the selected interpreter and board are correct.

This should connect the ESP32 to the Thonny IDE. If you are successful, you will see a MicroPython prompt in the Shell display at the bottom of the interface.

You are now ready to run MicroPython code on your ESP32.

Getting the Code using ChatGPT

As with every other experiment we have performed today, we will get our code courtesy of ChatGPT. Here is what I asked ChatGPT:

Can you write the MicroPython code for an ESP32 web server, with a home page with “ChatGPT” written in H1 text

And it gave me my code:

Now all that remains is to test it.

Testing with Thonny IDE

I will assume that you have an ESP32 set up with the MicroPython interpreter, you have it connected to your computer, and are running the Thonny IDE with the ESP32 selected as the source.

Copy the code that ChatGPT gave you and paste it into the Thonny IDE editor. Now save it on the ESP32 (as opposed to on your computer) under the name Using as the name will allow the code to execute whenever the ESP32 is powered up or reset.

Now find a device (computer, phone, tablet) with a WiFi connection, and scan for a new wifi network – mine was given an SSID of ChatGPT Server, but you might be running different MicroPython code, as ChatGPT does not always give the same results.

The final step is to use the device connected to the ESP32 wifi network to go to If all is working, you should see your ChatGPT screen.

If all is NOT working, then check out the code ChatGPT provided. Ask it to correct it or to rewrite it.

It’s your AI assistant – make it earn its keep!

Limitations & Future of ChatGPT

This product has only existed for two months, so much of the following is speculative.

Personally, I think ChatGPT and similar products like GitHub Copilot (see the video for a comparison between the two) definitely have their place in a programmer’s toolkit.  But I don’t see them replacing programmers and developers soon.

A lot of programming, especially advanced programming, involves getting and greeting code snippets and libraries and making them all work together. ChatGPT can really be a lot of help here, writing snippets of code and finding libraries to use with oddball components.

Right now, we see limitations due to the age of the dataset that ChatGPT uses, but I’m sure that will change soon.  And of course, one serious limitation is that the chatbot is not perfect, and you can waste a lot of time with poor or non-working code. You probably shouldn’t use it to code something that you couldn’t code yourself, as you may need to troubleshoot it to get it all to work.

ChatGPT Pro?

As of this writing, there is a waitlist for ChatGPT Pro, which is rumored to be a more robust version of ChatGPT. It should alleviate many of the issues experienced with the current free version.

Only this week (the last week of January 2023) have we had an indication of the price. ChatGPT Pro is rumored to be priced at 42 US dollars per month.  But it is not yet available to the public.


I remember being around at the beginning of the microprocessor revolution in the mid-1970s and thinking that this new product could change the world. I had a similar feeling in 1987 when, as a government employee, I was given exclusive access to a worldwide network of networks, allowing me access to information from servers across the globe. This, of course, was the Internet.

I think it’s fair to say that both of those developments changed the world, and while ChatGPT itself may not be planet-changing, it represents a new era of artificial intelligence applications that will transform how we do business and the way we live.

It will be very interesting to see what other AI products will be released this year; with Microsoft investing in OpenAI, you can be sure that offerings from Google and Apple can’t be far behind.

I really urge you to try ChatGPT and write some code. I’m sure you’ll find value in it, and it’s a lot of fun as well.

And yes, I still think it’s really magic!


Parts List

Here are some components that you might need to complete the experiments in this article. Please note that some of these links may be affiliate links, and the DroneBot Workshop may receive a commission on your purchases. This does not increase the cost to you and is a method of supporting this ad-free website.

Arduino Uno – Arduino Store

ESP32 DEVKIT C – Digikey



Code Used – This is the code I received from ChatGPT, which may differ from the code you get for the same queries.

ChatGPT – The login for ChatGPT.

Thonny IDE – You can use the Thonny IDE to work with MicroPython. It’s available for Linux, Mac, and Windows.

MicroPython Downloads page – Download the latest MicroPython firmware for your ESP32 board.


Using ChatGPT to Write Code for Arduino and ESP32
Using ChatGPT to Write Code for Arduino and ESP32
Article Name
Using ChatGPT to Write Code for Arduino and ESP32
Learn to use ChatGPT to write both C++_ and MicroPython code for an Arduino and ESP32. Put the power of AI-assisted coding on your workbench!
Publisher Name
DroneBot Workshop
Publisher Logo
Tagged on:     
Notify of

Inline Feedbacks
View all comments
Randal Everitt
1 year ago

My thoughts, not significant imo. You, Bill do a great job relaying the information on a wide variety of topics, ur not just clickbait. thanks for everything.

John Colonias
1 year ago


1 year ago

This topic left me totally flabbergasted. Quite a lovely coding tool that could facilitate better programming and learning. Thank you Sir, for pointing me in this direction. Stay well.

1 year ago

HELP. Loved your video about ChatGPT! Trying to use Thonny. Your instruction: Ensure your ESP32 board is connected to the computer and open the Thonny IDE.  § Go to Tools/Options/Interpreter. OK § Select “MicroPython ESP32” as your interpreter. Using “MicroPython on a generic device”OK § Select the port your board is connected to. USB-SERIAL CH340 (COM4) OK “Manage packages is greyed out” § Click the link for “Install or update firmware”. ?WHERE IS THIS COMMAND? I am trying to install “esp8266-20220618-v1.19.1.bin” § On the next page, select the same port you selected earlier. CANNOT FIND?? § Use the Browse button to select the .bin file you downloaded. CANNOT FIND???? § Click… Read more »

1 year ago

Hi Bill, first of all, I would like to thank you for all you do. Your teachings have been a great source of inspiration. As an ex software engineer, now moving into electronics, I feel I can ‘hypothesise’ as to the reasons for the delays in response…The many devices which are being controlled by the MCU, could be from an Asymmetric Multi-Processing architecture, which I understand, a multicore MCU like the ESP-32 is, and this may present racing conditions for events like reading temperature and displaying result, as one does not know, with certainty, which Core is handling which pin, and… Read more »

1 year ago

It is a very interesting article that opens up many possibilities for application development. I found it very complete and very well explained.

Greetings from Cuba. Good job. I have Voice GPT on my cell phone I have managed to download codes for a web server with Nano 33 IoT. I am new and I have achieved the wifi connection but the 11 ON and Off are activated when sending it. Hope help. Thanks in advance.

1 year ago

I was curious to see what openAI could do. All very entertaining asking it questions about books and movies.
I just asked it to design a Smart home system for me and it did. I asked it to write firmware code for the ESP32 MQTT 8 relays with GPIO assignment… and it did. I looked at the the code and it looked like it would probably work … Impressed… I am not quite ready to test it yet .. but it shows promise.

An interesting but Scary tool…

LENAERT Philippe
1 year ago

Bonjour,En français ? Première lecture sur le site, grand bravo pour les détails et l’effort de vulgarisation.

11 months ago

One thing i am curious to know or test is “upgrading” code from say an arduino nano to a circuit that uses an ESP32, as in translation, not even going from C++ to micropython, just adapting libraries and pins etc.
Or going from a 2 row B/W LCD to an SSD1306 oled

8 months ago

I enjoyed the article very much!
I am interested in using ChatGPT and Arduino to control an existing casio synthesizer. I would guess that I would need to supply ChatGPT with the schematics of the casio synthesizer before it could program the Arduino correctly.
How would I do this?