In our earlier articles, we have explained about the trending topics such as What is Generative Programming, How GPT-engineer Works to Generate Code and How to Develop a Simple WordPress Plugin with ChatGPT (GPT 3.5/4).
In the realm of the Internet of Things (IoT), Arduino and ESP32 are widely used microcontroller platforms known for their versatility and ease of use. However, coding for these platforms can sometimes be daunting, especially for beginners. In this article, we’ll explore how you can leverage the power of ChatGPT to streamline the process of writing code for Arduino and ESP32, making IoT development more accessible and intuitive.
Understanding ChatGPT
ChatGPT, developed by OpenAI, is a state-of-the-art language model capable of generating human-like text based on input prompts. It can be used for a wide range of natural language processing (NLP) tasks, including code generation, text completion, and conversational interfaces. Leveraging ChatGPT’s capabilities, developers can interactively generate code snippets, troubleshoot issues, and conversationally explore programming concepts.
---
Simplifying Arduino and ESP32 Development
Arduino and ESP32 development often involve writing code in C/C++ using specialized development environments such as Arduino IDE or PlatformIO. While these platforms provide powerful tools for coding, beginners may find the syntax and concepts challenging to grasp. By integrating ChatGPT into the development workflow, developers can benefit from its natural language understanding to simplify coding tasks and receive contextual guidance.
Streamlining Development Workflow
With ChatGPT integration, developers can interactively generate code snippets, troubleshoot errors, and explore programming concepts in real time. Beginners can ask questions, seek clarification on syntax, or request code examples directly from the Arduino or ESP32 board. Advanced users can use ChatGPT to automate repetitive tasks, generate boilerplate code, or explore new programming paradigms.
Steps to Write Code For Arduino UNO With ChatGPT
You need to register to log in to https://chat.openai.com/. In the ChatGPT website’s dialogue box, I entered the text “Write code for Arduino UNO where an LED is attached to pin 5, a potentiometer is attached to pin A0, turning the potentiometer increases and decreases the blink rate of the LED”. ChatGPT returned this output:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 | // Define the LED pin const int ledPin = 5; // Define the potentiometer pin const int potentiometerPin = A0; // Variables to store potentiometer value and blink rate int potValue = 0; int blinkRate = 0; // Variable to store the previous millis value unsigned long previousMillis = 0; // Interval at which to blink the LED (initial value) const long interval = 1000; void setup() { // Initialize LED pin as an output pinMode(ledPin, OUTPUT); } void loop() { // Read the potentiometer value (0-1023) potValue = analogRead(potentiometerPin); // Map the potentiometer value to the blink rate (50-1000 milliseconds) blinkRate = map(potValue, 0, 1023, 50, 1000); // Check if it's time to blink the LED unsigned long currentMillis = millis(); if (currentMillis - previousMillis >= blinkRate) { // Save the last time the LED blinked previousMillis = currentMillis; // Toggle the LED state digitalWrite(ledPin, !digitalRead(ledPin)); } } |
ChatGPT also informed me:
In this code:
The potentiometer’s analog input is read using analogRead() function, which returns a value between 0 and 1023 representing the voltage level.
The map() function is used to map the potentiometer value to the desired blink rate range (50-1000 milliseconds). Adjust this range as needed.
The LED is blinked based on the calculated blink rate. The millis() function is used to track time without blocking the execution of other code.
The LED pin is toggled on and off using digitalWrite() function, and the blink rate is controlled by adjusting the potentiometer.
I quickly created a project in TinkerCAD (www.tinkercad.com) and created the circuit like this one:

After pasting the Arduino sketch from ChatGPT’s output (written above) to TinkerCAD, I noticed that the simulation is working nicely:
Conclusion
ChatGPT (with or without TinkerCAD) is a helpful assistant for creating an Arduino/ESP32 project. However, it is not a human and lacks the logic of a human. You can not expect ChatGPT to create flawless large projects out of the box.
I do not suggest this kind of tool for learning purposes. They are simply like the scientific calculators.