An electronic project for Arduino with ChatGPT

ChatGPT, a language model trained by OpenAI, can be used to generate projects for Arduino. Arduino is an open-source development platform that allows users to create interactive projects with sensors, actuators, and control devices. In order to integrate both platforms, you need to provide the model with information about the desired project, such as the type of sensors or devices you want to use, the purpose of the project, and the control specifications. The model, then, can generate the source code, which can be uploaded to the Arduino board to run immediately.

How to “talk” to ChatGPT?

For instance, if you wanted to create a project that uses a temperature sensor and an LCD display to show the detected temperature, you could provide the following information to the model:

“I want to create a project for Arduino that uses a temperature sensor and an LCD display to show the detected temperature. The code needs to read the data from the sensor and indicate it on the display.”

The model is then able to generate the source code for the project, suitable for the user’s specific request. They, of course, can make as many changes to the code as they wish, according to their needs. However, it is important to note that although ChatGPT is a very sophisticated language model, sometimes it may not generate the correct code on the first try, and it might be necessary to make some changes to the generated listing to achieve the desired outcome. Future artificial intelligence models promise much more refined results, but what the present engine proposes to us is sensational. In addition, it is very important that users have some understanding of programming and Arduino, and also of electronics, to be able to use the generated code adequately.

Our request: drive two displays using the multiplexing technique

The multiplexing technique allows a limited number of pins of a microcontroller to be used to control a larger number of devices, such as 7-segment displays. Theoretically, 14 pins (7 for each display) would be required to drive two 7-segment displays, but with the multiplexing technique (see generic schematic in Figure 1) only 8 pins are required. The technique consists of having each display shown for a short period of time and repeating this operation quickly for all displays. In this way, the human eye perceives all displays lit at the same time. To drive two 7-segment displays with the multiplexing technique, the following steps shall be followed:

  1. initialize the microcontroller pins as outputs and assign them the values corresponding to the segments of the displays
  2. define a variable to keep track of the currently selected display
  3. use an iterative loop to handle all displays
  4. within the iterative loop, use a select instruction to set the pins corresponding to the currently selected display
  5. use a “delay” function to keep the currently selected display on (for a short period of time)
  6. increment the variable that keeps track of the currently selected display so that the next cycle will display the next display
  7. repeat steps 3-6 quickly so that the human eye perceives all the displays turned on at the same time.

It is important to note that the delay times should be short, so that the human eye does not perceive the flashing displays. It is also possible to use specific libraries for multiplexing to simplify code writing and make program execution faster.

Figure 1: Principle diagram of driving multiple 7-segment LED displays using the multiplexing technique.

Figure 1: Principle diagram of driving multiple 7-segment LED displays using the multiplexing technique

Now ChatGPT works

Well, we are ready to ask ChatGPT to begin producing the project in question. As mentioned earlier, the project will consist of the following components:

  • an Arduino Uno board (or any compatible version)
  • two 7-segment common-cathode displays
  • seven 220-ohm, 0.25-watt resistors
  • two 2N2222 transistors (or equivalent)
  • two 2.2 kOhm and 0.25 Watt resistors.

The artificial intelligence system works wonders, but user requests need to be posed clearly and richly detailed, perhaps not worrying so much about syntax. The important thing is the amount of information passed to the model.

Those beginning to undertake such a project need to know the basics of electronics. Therefore, they should, at the very least, design a draft electrical circuit with all the electrical connections and the required electronic components. The digital ports in this project are all configured in output. Once the actual wiring diagram, shown in Figure 2, is prepared, it is possible to start placing the request to ChatGPT to implement the project.

Figure 2: The final wiring diagram of the two-digit multiplex counter.

Figure 2: The final wiring diagram of the two-digit multiplex counter

After accessing the chatGPT site, which can be reached at https://chat.openai.com/chat, one must, log in to use the service. It is possible to ask the question in any international language and in a “human” style. For our project we enter the following text in the chatGPT box:

“Please write the listing for Arduino Uno for a countdown from 99 to 0, with a one-second pause between one number and the next. The two seven-segment LED displays are to be driven by multiplexing through two NPN transistors. The two displays have a common cathode. Do not use external libraries. The segments of the displays should be connected to Arduino as follows: a = 2, b = 3, c = 4, d = 5, e = 6, f = 7, g = 8, digit1 = 9, digit2 = 10. Thank you”.

This request, also visible in Figure 3, contains enough information for the Bot to fully understand our request. Currently, the site is visited by millions of people and the traffic is huge. The response may, in fact, take a few more moments. It is often necessary to repeat the request, for the same reasons. After a few seconds of waiting, then, ChatGPT responds with a very human and professional response, with terms appropriate to the subject matter. The response also includes a long source listing for Arduino, ready to be uploaded to the device. The listing is quite correct and there are very few points where the human programmer needs to intervene.

Figure 3: The project request posed to chatGPT.

Figure 3: The project request posed to chatGPT

Final testing of the project

After properly assembling the electronic components, making all electrical connections, and loading the executable program onto the Arduino (see the source listing in Figure 4), testing of the prototype can be carried out immediately. When the power is supplied, the two seven-segment LED displays light up and show, on two digits, a countdown from 99 to 0, at the rate of one second. It is interesting to observe how the value of the two digits to be displayed is calculated, relative to the tens and units. To separate the tens and units of a two-digit number, the modulus operator (%) can be used. The modulus returns the remainder of the division of a number by another number. To separate the tens and units of a two-digit number, you can use the following two formulas:

An electronic project for Arduino with ChatGPT

In this way, the variable “tens” will contain the number of tens of the original number, while the variable “ones” will contain the number of units of the original number. Another special feature of the listing is the repetition of the display of the two digits, in multiplex, for a specified number of times. This technique makes it possible to determine how long the number is to be displayed and therefore also determines the speed of the count.

Figure 4: The source listing of the two-digit counter firmware.

Figure 4: The source listing of the two-digit counter firmware

Conclusion

Although the achievements of artificial intelligence techniques are flattering, the technology is still in its infancy and a major revolution in this field is expected in the near future. GPT-3 (Generative Pre-trained Transformer 3) is a language model developed by OpenAI that has been trained on a large amount of data. GPT-3 has been employed to generate text in a wide range of applications, such as document generation, machine translation, speech synthesis, and question answering. It has also been noted for its ability to generate plausible and coherent text and for its ability to understand context and language usage. In the future, it can be hypothesized that an eventual GPT-4 would be an even more advanced language model than GPT-3, with a greater ability to understand context and language use, a greater ability to generate plausible and coherent text, and a greater ability to handle a wide range of applications. In conclusion, ChatGPT proves to be a powerful tool for creating complete projects for Arduino. With its automatic code generation capabilities, it can help developers save time and effort, increasing the efficiency and quality of their work. Furthermore, its ability to understand and answer questions naturally makes it easy for users to get answers to their questions and guidance with their projects. The use of ChatGPT can be expected to become increasingly widespread in electronics and the Internet of Things in the future. Today, artificial intelligence can help write firmware for microcontrollers in a variety of ways. It is important to remember that AI can only be used to support firmware development but cannot yet completely replace the work of human programmers.

Download Code


Copyright Statement: The content of this article is collected from the Internet and is only for personal learning purposes. If your rights are violated, please contact us to delete it in time. Email: [email protected]

Leave a Comment