Arduino Uno with ChatGPT

What do you get if you combine a small microcontroller with the intelligence of OpenAI? You get a random quote generator!

Over the weekend, my son and I explored the possibility of utilizing Arduino in conjunction with ChatGPT. For those unfamiliar with Arduino, it’s an open-source hardware and software company that specializes in producing microcontroller kits and single-board microcontrollers for the user community.

Arduino has multiple different kits and single-board microcontrollers and I think that my son has most of them, and yesterday he told me that Arduino released a new kit yesterday which I’m guessing he is going to ask me about soon……Well good thing his birthday is coming up.

Anyway, he is already a wizard at coding these things and is always finding up new projects and utilizing all the different sensors and other controllers that we have. Just an example of some of those that he has already in the screenshot below.

Honestly, I’m impressed by the vast range of capabilities that can be achieved with these compact devices, alongside the hundreds of diverse sensors that are readily accessible.
Now we wanted to setup one of these controllers to communicate with ChatGPT just so see if it was possible, therefore I needed a controller that was able to handle SSL connections, as not all Arduino controllers possess this capability. However, we went with the Arduino MKR WiFi 1010 MKR WiFi 1010 | Arduino Documentation | Arduino Documentation that supports it. Furthermore, when combined with the MKR IoT Carrier, it can offer an array of sensors, including LED lights, among others.

If you want to see the entire code, you can find it here –> arduinochatgpt/chatgptarduino.ino at main · msandbu/arduinochatgpt (github.com)
The use of the code is quite simple.

1: First of you need to get an API key from OpenAI which you can get if you create a user account there –> Account API Keys – OpenAI API after you have created an account

2: Then the API key needs to be added to the authorization header within the code to successfully authenticate to OpenAI API’s as seen below.
3: This in addition with the JSON Body which contains the actual prompt we want to ask and what kind of language model we want to use. You can also see that it does directly to the completion’s endpoint, you can view the different APIs and specifications here –> API Reference – OpenAI API
Also, that the max_tokens parameter defines how much text it can return.

  if (client.connectSSL(serverHostname, serverPort)) {
    Serial.println("Connected to server");

    // Send HTTP POST request
    String requestBody = "{\"model\":\"text-davinci-003\",\"prompt\":\"make a quote\",\"max_tokens\":150}";
    String request = "POST /v1/completions HTTP/1.1\r\n";
    request += "Host: api.openai.com\r\n";
    request += "Authorization: Bearer API-Key\r\n";
    request += "Content-Type: application/json\r\n";
    request += "Content-Length: " + String(requestBody.length()) + "\r\n";
    request += "Connection: close\r\n\r\n";
    request += requestBody;

It should also be noted that to setup an SSL based connection you needed to use the client.connectSSL part or else it will only use regular HTTP. After it sent the HTTP POST with the payload it will return a JSON payload which we need to parse. The final part was to display the return text on the attached LED display on the carrier that was connected to the Arduino controller.

    String response = client.readString();
    Serial.println("Response:");
    Serial.println(response);

     // Parse JSON
    DynamicJsonDocument doc(1024);
    DeserializationError error = deserializeJson(doc, response);

    if (error) {
      Serial.println("Failed to parse JSON");
    } else {
      String completionText = doc["choices"][0]["text"];
      Serial.println("ChatGPT response: "+ completionText);
      carrier.display.setTextSize(3);
      carrier.display.println("ChatGPT Respons" + completionText);
      }

So, the process ended up being quite simple, when the Arduino boots up it will try and authenticate to the WiFi network, then build the HTTP payload and send it to OpenAI API’s then process the JSON payload, parse it, and display the result on the screen. The result ended up like this, where we asked ChatGPT to generate a random quote of the day.

Useful? well probably not.. but we had much fun trying to figure it out and especially figuring out what didn’t work, spending a lot of time with different libraries in Arduino! but now we got thinking about other ways to use this and lot at some other possibilitets.

  • Use of DALL-E to generate picture to display on another bigger OLED screen
  • Use of record and Whisper API to transcribe from audio-to-text
  • Use with a touch screen and keyboard to allow direct chat integration to ChatGPT

Leave a Reply

Scroll to Top