There are other, more detailed analyses, looking behind the curtains of ChatGPT, this is just a short summary of my own experiences. For more details, look e.g. here: ChatGPT: Interacting via a conversational way!
ChatGPT is this (in)famous artificial intelligence which has been all over the media lately. It describes itself as a language model and in one thin it is amazing: it can produce grammatically and syntactically correct, long stretches of text, which could as well have come from a human writer. And it can do this in many different languages.
ChatGPT can also interpret a wide variety of user input and assemble its answers in a matching context and for the most even style. It has got a very ambivalent reception among others by teachers, which see their pupils and students cheating on home essays and exams by just copying the output of the AI and claim it as their work.
I have been experimenting with ChatGPT for a couple of weeks now and mostly tried to focus on its weaknesses. In this context I have also discussed ChatGPT with colleagues, students and friends over the internet and face-to-face.
Detailed knowledge of particular microcontrollers
Last week, a student asked me for help with a code for his microcontroller project. It was supposed to generate a PWM signal from an ATmega328. We had previously used this function in a guided lab exercise, so I was a bit confused to not see traces of our previous, known-good code, but hey, there are many ways to accomplish this goal. However, the student’s code, initializing a timer of the ATmega328 had plenty of mistakes and I asked the student, how he came up with the code.
The he told me that he had not come up with the code from the tables in the datasheet, but instead he had asked ChatGPT. Coincidentally I had experimented with ChatGPT on exactly this topic a couple of weeks ago, trying to find a good new question for a re-exam.
“Describe how to configure and use TIMER1 on the ATmega328 microcontroller to generate a square wave with a frequency of 1 kHz and 50% duty cycle, using the output compare mode. Also, explain the function of the registers used in the process and the steps required to set up the timer.”
This question tests a student’s knowledge of how to configure and use TIMER1 on the ATmega328 microcontroller, including setting up the timer in output compare mode, configuring the registers, and calculating the appropriate values for the registers to generate a square wave with a specific frequency and duty cycle. It also requires the student to explain the function of the registers used in the process.
In order to answer this question, a student should have a good understanding of how TIMER1 on the ATmega328 works, including its registers, modes of operation and how to configure it to generate a square wave. They should also have a good understanding of how to calculate the values for the registers to achieve a specific frequency and duty cycle.
Uwe: and the correct answer would be?
ChatGPT: The correct answer to the exam question would depend on the specific details of how the question is asking the student to implement TIMER1 on the ATmega328. However, I can give you a general overview of the process of configuring and using TIMER1 on the ATmega328 to generate a square wave with a frequency of 1 kHz and 50% duty cycle, using the output compare mode.
In order to generate a square wave with a frequency of 1 kHz and 50% duty cycle using TIMER1, the student would need to:
-
- Configure TIMER1 to operate in output compare mode by setting the appropriate bits in the TCCR1A and TCCR1B registers.
- Calculate the value to be loaded into the OCR1A register to achieve the desired frequency and duty cycle. For example, with a clock frequency of 8 MHz, the OCR1A register should be loaded with 4,000.
- Enable output compare match interrupts for the A channel by setting the OCIE1A bit in the TIMSK1 register.
- Configure the appropriate output pin (OC1A) to be connected to the timer output by setting the appropriate bits in the DDRB register.
- Write an Interrupt Service Routine (ISR) for the Output Compare A match interrupt, and in this ISR the student should toggle the state of the OC1A pin.
- Start the timer by setting the appropriate bits in the TCCR1B register.
It’s important to note that the above steps [here the conversation was interrupted for this session]
And while ChatGPT came up with a nice question, it was not able to solve this question by itself – as hard as I tried to push it. Using an Interrupt Service Routine here is possible, but totally unnecessary – the PWM can be created completely in hardware. Counting to 8000 or 4000 is wrong, since we start at 0. The question ended up on the exam with the comment that it had been created by ChatGPT.
For my own solution manual, I sat down and I found the following solutions:
In order to get a frequency of 1 kHz which corresponds to a period time of 1 ms we need to count 8000 CPU clock cycles (for fast PWM). This can be done without prescaler in a 16 bit timer. We need to restart the timer after 8000 counts, so we need a PWM mode with variable TOP value. According to the datasheet these are modes 14 and 15, the difference is that in mode 14 the TOP value is stored in ICR1 and for mode 15 it is stored in OCR1A. I choose mode 14. We can then use either OC1A or OC1B or both as the outputs for our 1 kHz clock signal, here I configure both in non-inverting mode, one would be enough:
ICR1 = 7999; // TOP value OCR1A = 3999; // on-time for OC1A OCR1B = 3999; // on-time for OC1B TCCR1A = (1 << COM1A1)|(0 << COM1A0)|(1 << COM1B1)|(0 << COM1B0)|(1 << WGM11)|(1 << WGM10); TCCR1B = (1 << WGM13)|(1 << WGM12)|(0 << CS12)|(0 << CS11)|(1 << CS10); DDRB = 0b00000110; // OC1A and OC1B as outputs
There are other possibilities as well:
- using a standard-CTC mode (no PWM) or modes 14 and 15, one could use the TOGGLE function of the OC1A and OC1B pins: let the timer count from 0 to 3999, switch on the pin and let the timer restart from 0 to 3999 then switch off the pin
- using phase-correct PWM we would let the timer count up to 3999 and then down to 0 again (TOP=3999 in either ICR1 or OCR1A) and set the PWM compare value to 1999.
- an approximately ok solution would be to use 1:8 prescaler, giving us a timer tick frequency of 1 MHz and then use a 10-bit PWM mode which counts from 0 to 1023. This gives a frequency of 977 Hz. And then the OCR-value should be set at 511 for a 50% duty cycle.
- finally, we could trigger an interrupt and toggle a port pin in software
- some possible, alternative settings:
Mode | WGM | CS | COM1A | COM1B | ICR | OCR1A | OCR1B | comment |
4 – CTC | 0100 | 001 | 01 | – | – | 3999 | – | output on OC1A, toggle |
4 – CTC | 0100 | 001 | – | 01 | – | 3999 | 3999 | output on OC1B, toggle |
8/10 – PWM | 10_0 | 001 | 10/11 | 3999 | 1999 | – | output on OC1A | |
8/10 – PWM | 10_0 | 001 | 10/11 | 3999 | – | 1999 | output on OC1B | |
9/11 – PWM | 10_1 | 001 | 10/11 | – | 3999 | 1999 | output on OC1B | |
12 – CTC | 1100 | 001 | 01 | – | 3999 | 3999 | – | output on OC1A, toggle |
12 – CTC | 1100 | 001 | – | 01 | 3999 | – | 3999 | output on OC1B, toggle |
14 – PWM | 1110 | 001 | 10/11 | 7999 | 3999 | – | output on OC1A | |
14 – PWM | 1110 | 001 | – | 10/11 | 7999 | – | 3999 | output on OC1B |
15 – PWM | 1111 | 001 | 10/11 | – | – | 7999 | 3999 | output on OC1B |
What ChatGPT was unable to do – both for me and for the student – was to correctly put together the bits for the individual registers, and which value would be the TOP value or the selected mode of the timer. In the student’s code it messed things up even more. It might be quite confusing for an AI that there are so many similar, yet different settings which could be used, but which under no circumstances work when mixed together.
More electronics
In the presence of some colleagues I put ChatGPT to a test. Previously I had found out that it must have chewed up tons of electronic components’ datasheets during its training. So I asked it to compare two transistors – with a little twist:
Uwe: Compare the BC238 to the 2N3904
ChatGPT: The BC238 and 2N3904 are both bipolar junction transistors (BJTs), which are electronic devices used to amplify or switch electrical signals. The main differences between the two are:
-
- Type: The BC238 is a PNP transistor, while the 2N3904 is an NPN transistor. This means that the BC238 has a positive polarity, while the 2N3904 has a negative polarity.
- Vce saturation voltage: The Vce saturation voltage is the voltage drop across the collector-emitter junction when the transistor is in saturation. The Vce saturation voltage of the BC238 is typically around 600 mV, while the Vce saturation voltage of the 2N3904 is typically around 400 mV.
- Collector current: The collector current of the BC238 is typically around 100 mA, while the collector current of the 2N3904 is typically around 40 mA.
- Gain: The gain of a transistor is a measure of its amplification ability. The gain of the BC238 is typically around 100, while the gain of the 2N3904 is typically around 400.
- Package: The BC238 and 2N3904 are available in different packages, such as TO-92, SOT-23, etc.
In general, the 2N3904 is a more commonly used transistor due to its higher gain and lower saturation voltage, while the BC238 is used in applications that require a higher collector current. The choice between the two will depend on the specific requirements of the application.
And those of you a bit familiar with older, European style marked transistors, might have noticed the quirk already quite at the beginning: “BC238 is a PNP transistor” – well, no it is not. Both of them are NPN transistors. Knowing this, you immediately become more skeptical about the rest of the answer, and yes, most of the “details” do not really hold a scrutiny of the corresponding datasheets, BC238 and 2N3904.
BC238 | 2N3904 | |
VCE,sat | < 0.2 V < 0.6 V (under extreme conditions) |
<0.3 V |
max continuous collector current | 100 mA | 200 mA |
typical current gain | 290 | about 200 (depends on manufacturer) |
Conclusion
- ChatGPT appears to be amazing in writing fluent text in different languages, but looking at the details, the text is often repetitive and does not quite come to the point.
- ChatGPT is also fluent in a variety of programming languages. Currently it is stuck in knowledge about language dialects prior to 2021, but this will probably change in the future. However, since it does not really grasp more complex concepts and keeps those in mind, it seems unable to write longer, coherent pieces of code.
- ChatGPT is also amazing in the fact that it knows many common and obscure electronic devices. But its knowledge about details of these devices is very limited.
Latest Comments