Can ChatGPT generate all the code for my Macgyver Project?
Does ChatGPT understand what 7 segment displays and shift registers are, and can it integrate that knowledge with Arduino programming? Let's find out!
This is Part 3 of the Magyver project.
Part 1:
Part 2:
00:00 - The Macgyver Project Part 3
00:48 - Change of plans, let's use ChatGPT!
01:35 - The functional schematic
04:08 - Describing our circuit so ChatGPT's language based system can understand it
05:22 - The art of Prompt Engineering
09:37 - What I'm expecting
10:09 - Just a bit more information required
10:32 - Let's use ChatGPT and watch it crash!
11:25 - It works!
14:43 - It's done it!
15:30 - Let's use the conversational system to modify the code
16:42 - Are programmers out of a job?
17:47 - Does the code work on an Arduino?
20:04 - What went wrong?
21:30 - Can we hack it to work?
23:35 - Can ChatGPT fix its own code?
24:56 - It crashed again. Don't rely on online AI!
25:37 - I can't even pay for it!
25:55 - It knows it made a mistake, and the power of conversational chat
26:47 - Does it's modified code work?
27:37 - When the AI apologises it gets even more confused!
29:51 - It had it correct (but buggy) all along.
30:38 - Go back!
31:01 - Human to the rescue!
32:45 - WINNER!
34:02 - Conclusion. Can ChatGPT code?
If you find my videos useful you may consider supporting the EEVblog on Patreon:
Web Site:
Other channels:
#ElectronicsCreators #chatgpt #ai

Hi! This is part three in the MacGyver project. Sorry, it's been a while. It's been six months to get around to part three. Better late than never.

I'll link in part one and part two. If you haven't seen it, it's uh, where I took apart uh what? this? uh, gas? uh, detecting one of these industrial gas detector things and I had this really funky display on the front here that I I Thought: maybe that we could actually um, program this to do a like a countdown timer or something because this device kind of looks like something I won't use the keyword for because I don't want to get demonetized so we just reverse engineered that and uh, then we in part two down here. we actually, uh, hooked it up to a scope and we measured, uh, all sorts of uh, waveforms and and did some more reverse engineering. Anyway, my plan was in part three.

I would actually, uh, show you how that we can design a driver for this using discrete 7400 series TTL lodging I thought that'd be really cool. then maybe in part four, we'd show how that we can consolidate those you know, a couple of dozen TTL chips into one like single like eight pin micro controller that costs like 10 cents or something like that. but I've decided to jump ahead because I'm so excited I want to find out if we can use AI Artificial Intelligence I.E Chat uh GPT it's all the rage I Thought: see if we can actually use it to design a some code that will actually drive this serial driver. So I've basically got the interface that we want to drive here.

We've got our five seven, four Hc164 chips and these are 8-bit serial shift registers so it has a clock coming in here. it's got an input data pin so on the positive going clock Edge Like this, it will clock the the whatever data is there through to Q0 and then on the next positive going Edge Like that it'll clock in Q0 through to Q1 because if you have a look over here this is they're basically cascaded the Q output or the data output here is actually the then the data input for the next uh one here. So it's just a shift register and just shifts them along. so on the actual first clock pole we'll call it the first clock pulse over here because you've got to have like initial state that you're uh, talking about then the data whatever was on Q7 here.

that's the data that gets shifted to Q0 here. so it's one big effectively 5, 8, 40. It's an 8-bit shift register so 40 bits here and shifts all the way through like this. So we've got a common cathode seven segment display like this.

It's common cathode because all the cathodes are tied together so they would go down to ground. In this particular case, it goes off to a pin on the board here which we can use that for like blanking and stuff like that. So when we're shifting data in, we can actually blank the display so you don't see the data like shifting all the way through anyway. I've talked about that in a previous video.

so a basic 40-bit shift register like this driving uh, 40 different segments on this seven segment display because we've got the eighth digit which is the decimal point here like that and this is an actual mapping. So uh, Q7 here is a is segment A which is the top one there. A B B C D E F G H I J K L M N O P and decimal point is over here. So um yeah, we need to write some code or the AI needs to write some code.
We're going to see if we can do it that uh, it sends in a clock pulse like this and sends in the data and Maps the data to the specific pins. Let's see if AI or Chat GPT 3 or 3.5 think it is at the moment. check GPT can actually write some code to do this. It's going to be interesting I haven't tried it yet I Don't know because chat GPT is a language based model.

Let me know in the comments down below. if you want me to do more videos like interacting with AI and stuff like this, I'll ask on Twitter Maybe a third of my audience are interested in that sort of, uh, content. Anyway, so it's a language model so we have to try and describe this circuit in sort of like, you know, playing English so that the language model can understand now. chat.

GPT is capable of producing source code for almost anything. It's really good. Any basically anything on GitHub I believe is the source that it's taken all this stuff from. So I can I think people have talked about generating pick code and all sorts of stuff.

I've only tried it with uh, python code I think I might have tried an Arduino code once. like yeah, it just. it did work. So I can generate Arduino code.

Let us know in the comments down below: if you've actually tried to do programming, apparently it's It's pretty good. It's not 100 there yet, but is it going to be good enough for Australia Is it going to be good enough for the MacGyver project to generate this? Because I thought this is a real interesting example of how you know. It's kind of tricky to word how this thing actually works. You know we've got a 40-bit shift register.

You have to tell it to map the seven segment displays and stuff. So this is what I first came up with. I'm telling it, you are riding Arduino code so it knows off the bat that we're writing code for Arduino and not for some other microcontroller or Python or whatever. And I'm going.

there are two outputs because we need the clock up here and we need the data. So I'm telling please define two output pins and I'm saying one is a bit clock. so I don't know if we need the term bit in there, but I'm just saying it's a bit clock so it knows it's a bit data stream. maybe? I Don't know.

This is all part of basically a new field called prompt Engineering. Um is the term I like to use for it? I Don't know, you've got another term I Like prompt Engineering in that you know you have to learn how to drive these AI engines and this is will be an incredibly valuable skill. Pro Tip: Highly recommend you get on it now and learn how to drive AI engines. Um, in terms of not only generating code, but generating all sorts of useful uh results, it's going to be a killer skill in the coming years.
Let me tell you so. Anyway, we're telling that it's a bit clock and the other is a data output. Okay, so it knows it should know it's got a clock pin and a data pin. and then I'm going to say the data output connects to a 40-bit shift register that is connected to five.

I've used the word five instead of the number five? don't know if it makes a difference I don't know I I'm still relatively unskilled at this chat. GPT thing I don't know the correct sort of like syntax and this is what we're going to hopefully learn here. Will it work First go? or will I have to massage this? or will it just go completely tits up? No idea. So it's going connected to five seven segment displays with decimal point, so it should know that it has seven segments plus a decimal point.

so it should know. Hopefully it's got a we're telling it's got eight outputs. These five displays act as a five digit counter. So I'm telling it the function of what we want to do.

here. We we've got five seven segment displays and we want to use it as some sort of counter. Whether it's counting up, counting down doesn't matter, it's a counter. So and then I'm going to say write code that will count down.

So I'm specifically telling it to count down on the displays once per second starting at Nine Nine Nine Nine Nine nine. So it will actually oops. that's that's too many nines starting at 99999. So it should initialize the First Data that comes through.

It should, uh, you know, have Nine Nine, Nine, Nine Nine on it and then 99998 and Etc it should countdown at once per second, so it'll be interesting to see if it actually does If it's intelligent enough to know I Don't want a once per second clock I Don't want a One Hertz clock because then it'd be incredibly sure slower to take 40 seconds to update the just the display for one uh digit I Want it so it should know it has to clock out at 40 times. So hopefully it should use like a 40 Hertz clock to do this. I Once again, this is I think quite a challenging example. I Hope you agree and then I'm saying the first bit on the output is the least significant display digit.

So what? I'm basically saying here is as I said on the first clock pulse that goes through is our least significant digit over here and this is our most significant digit. Got a H wrong with the mouse so it knows that the first bit it sends out is the one that is going to get shifted all the way across to here. like this after 40 clock pulses. now.

hopefully that's all it needs to know. Like off the bat, I'm hoping that'll actually produce some sort of working code that allows us to at least you know base. If it doesn't immediately work um, then at least we can massage. At least it'll give us some sort of code base uh, to work from.
Hopefully I'd be. You know if it works like first go with little massaging or no massaging required of the code, then I'll be I'll be pretty impressed. but anyway up. I Don't think we need anything else I'm not going to do black display blanking or anything like that now.

I Just want to see if we can get clock and data to shift in. So what I'm expecting from any generated code is to have uh, like digits actually defined should have to bitmap. Um, the seven segments on here, right? So it should like. If it wants to do five, it should know it needs to turn on those segments like that.

Or you know number one. It needs to turn on segments B and C there. uh for example. and I haven't actually told it to use the decimal point at the moment, so hopefully it just ignores the decimal point.

but I haven't I've told it it's the least significant digit now I think I need a bit more information because I haven't mapped in the individual outputs yet have I so maybe I can add some more something extra. Each display in the bit stream starts with segment A through segment G with decimal point last. Okay, so let's let's try that. Cross our fingers and see what happens.

Okay, so here I am in uh chat GPT here I do actually have a paid version now. So I think I'm I'm paying per sort of like chunk of like there's a certain length of uh code that it will or length of response. It doesn't care whether it's code or something else. um, it like a length of response costs you know, a couple of cents or something like that.

Will it work? Will it magically work? I Don't know. Maybe you've got a better idea of how you would have worded this? Um I haven't given it a huge amount of thought. This is just like off the top my head. how I would explain that and I think any engineer could read that and go oh okay I know exactly what you want.

So is the chat. GPT Any good to do it? Let's see an error code. If this issue persists, please contact us through our help center. What? fail? Okay, I've logged out and logged back in.

Let's try that. Yes, Yes! Here we go. Here's the code to achieve the desired functionality. It's going to do it.

It's going to have a crack at it now. It's generated a clock pin, a data pin. it's initial counter, 99999 pin mode. It's set up the pins.

This is looking really promising. This is perfect. So far it's it's actually labeled them, clock pin, and data pin. Loop Shift out.

Uh, current counter value to the shift register greater than five five. get the current digit. Oh yeah, yeah, there's five digits. Sorry.

Yep, yeah, of course. Buy it. Segment pattern: get segment pattern. so it's yeah.

Okay, so down here here are the segment patterns it needs. Wow, it's doing it right off the bat. End to end delay for one second before decrementing the counter. Okay, I I called that.
There you go Returns the binary representation, the segment pattern for the given digit and if it I don't right? I would have to map I've got a Mac and see. This is like you know an annoying part of writing this code yourself. How do you like just map in making sure you map the correct seven segment displays two like in this particular case. Okay, it's it's got.

If you want a zero, you know it turns on. Hang on. it's turns on one two. Oh I didn't tell it I Didn't say anything about common cathode or common anode.

So if it was common anode you would have to Output a zero to turn on the lead. but it's output in a one. So here if we if we want a zero to display a zero, it's going to have to turn on. um, all but two of the segments that is correct so it you don't want segment uh G oh I think it's back to front depends how it shifts it out.

Yeah, but that is correct. Well it's it's it's either completely back to front or it's correct because the last two digits here will be segment F which is in the middle. and if you've got a zero around like that, you don't want segment F which is in the middle and the decimal point is next to segment F So that is correct. That is correct.

and then if you want a one, it's only turned on two segments. Yeah, this is this is correct. It looks like it hasn't done decimal point. Okay, because we didn't tell it to do anything with the decimal point.

That would be interesting if we could tell it. Add a decimal point on the third digit or something. I Wonder if we can add that code Anyway, What does it say? This code uses a for Loop to shift the output set of the segment patterns for each digit of the counter in reverse order from least significant digit to most significant digit. That is correct with the first bit on the output being the last significant display digit.

I Still haven't got my head around if it's right or wrong yet, but it's damn close that the get Segment pattern function Returns the binary representation of the segment pattern for the given digit, which is used to shift out the corresponding segment pattern for each digit. The counter is decremented once per second using the delay function. It's it. It has done it.

It's basically done it. Wow. Wow. Okay, so shift data out data pin Clock pin Yeah.

I didn't tell it the polarity of the clock and I didn't tell it on the positive I Probably should have said on the positive clock Edge Okay, because the problem here is yeah. Here is the problem. Um, is that it's going to sh. It's going to Output the clock.

It's going to Output the data and the clock at the same time and you don't want that because that violates setup and hold. Could potentially you know it didn't guarantee to give you problems. I Think Murphy's Law right. You want some setup and hold time so you want to Output the data pin first.
You want to Output the data first before the clock pin. Now because this is and chat. GPT is a conversational based system. We can tell it to modify this code right.

so it's like we don't have to tell it from scratch again. we just tell it. Can you change that to Output the data pin first before the clock pin? Certainly here's the modify code that outputs the data pin before the clock pin and it's got It's going to regenerate the code here. We go the pin mode, data pin output and come on.

you can do it. You can do it. How much does this cost to me? It's costing me probably 20 cents worth of processing time. Now the Microsoft service to to do all this.

That's a fraction of what my time costs. Uh, digital right? Data pin. There you go. Digital right? Data pin segment pattern.

Look look, it's it's done it Yeah. and then the clock pin High low like that. Did it initialize the clock pin? didn't initialize the clock pin, did it I Don't think it initialize the clock then. But geez, that's impressive huh? Fantastic.

Wow. So we told it to modify that and it and it just understood exactly what we wanted and it re-modified our code. That's impressive, isn't it? Some programmers might be out of a job, including, like you know, just klutzy programmers like me. Well, you're not actually going to be out of a job because this is just a tool that you can use to.

you know, get at least get initial code working very quickly. see I would have spent, you know I don't know how long I would have spent just you know, working out this uh, you know this bit pattern and stuff to get it just to do that alone, you know is is like a big Time Saver especially if you've got like a you know a big thing that you need to do. kind of like. That's really the monotonous part of programming.

There's mapping stuff like that. You know you're mapping characters, you're mapping. You know, graphics and stuff like that. It's you know, it's it's it's really tedious, uh, type stuff.

But I think this works without me having to figure out exactly whether or not it's got the bit data people have already screaming in the comments. Oh yeah, of course it's wrong. Of course it's going to work. Um, but yeah, that that looks like that code is going to work.

So I just cut and pasted that code from chat GPT into Arduino Here you can pile it and decomposed is fine. There's no errors whatsoever. It doesn't mean it's going to work, but there's no syntax errors. All right, let's just power it up and try it.

I Know this setup's a bit how you're doing, but uh, it's good enough for Australia Um, now as you saw in previous videos, we need a 2 volt power source for this because there's no uh dropper resistors on there. and because we've got a 5 volt uh TTL level out of the Arduino I've had to put in a bodge in a resistor divider in there. so I've just got like a 1K and a 680 or whatever and uh, that will drop it down to like under two volts to uh, actually go to the logic on here and I've grounded the common cathode pin so there's no like blank in or anything like that. and I've got a hundred milliamp current limit it I Can't remember exactly what it took, but let's just try that.
so let's power it on and see what we get. Oh hello, we've got a zero that's interesting. All right. Well, let's try and capture some waveforms drawing.

uh, 20 milliamps. Uh, the voltage hasn't dropped so our current limit's good. Yeah, right off the bat didn't work and you can save. Well AI failed, but well, uh, how many times have you done a project that didn't work? first? Go right.

Come on, admit it. Just For kicks, I'm using the new Sigma SDS 2354xhd I've got no ground connections, it's just flapping around in, uh, the breeze. I'm just going through the grounding of the Arduino so it's going to be a little bit noisy. But there you go.

Um, this is our clock signal down here. Obviously like I can run this real time, right? and you can probably just see. Yeah, we're getting patterns like that. So I'm single shot capturing this.

and sure enough, check it out. It is. It seems to be correct. We've got our five packets of clock pulses.

This should be eight, one, two, three, four, five, six, seven, eight. Yep, and there's five packets of those. so it shifts eight for the first digit in the second. look Etc And then we've got this turning on over here, which is in the middle like this.

hence why our middle digit is turning on. so it's actually working. It's clocking the data and doing everything right. but our data's not correct.

so what what? So let's have a look at the code here. Uh, well. we know that the clock and data pins are working like they're mapped actually correctly and everything. look.

I'll tell you one thing. I Did notice here. Check this out down here. I Like how the help here actually it pulls the comment before this.

It actually knows that that comment's associated with that. I I Don't know I haven't used Arduino in such a long time. This is just new to me. I Think that's cool.

Nice tattoo. Ever did that right? so we know our for Loop is correct because we're getting our five uh clock pulses right? So we're getting so. so this is working here. It's just that this digital rights segment pattern is not working now.

I Believe this is correct, right? because uh, segment pattern right? And that just so that's just a byte and that calls up get segment pattern down here. But if there's no case, then it Returns the default value of zero. now zero. In this case, if you shifted it all out, you shift out all zeros Because we're talking about a common cathode display here.

We we need a one to drive it so and an individual one in here. Each one in there represents a segment on, so a zero would be all zeros. Which means all segments are off. so it's obviously uh, for four of those five digits, it's returning zero here.
Okay, so there's nothing wrong with this. This is all. this switch looks all fine. Everything's hunky-dory The here's where it gets the current digit from the counter.

Okay, just quickly. What I'm going to do is I'm just going to force it. Digit equals what is a good map in down here. One that's got ones and zeros.

Two Two digit equals two. I'm just going to go. Digit equals two. Let's go back to the bench and just try that.

so so it'll ignore the previous code, it'll just force all twos. so hopefully we get all twos on the display. So let's put that back in and try out this. uh, quick hack? shall we output on Bingo Look at that we winner winner chicken dinner.

Two Two Two Two Two two. Can we get that to glitch? Because yeah, yeah they are. It's yep. Yep.

There you go. all eight and then switch to all twos. So the uh chat GPT actually got that uh got the functionality right? Um, like in terms of like all the bit uh, stuff output in and there it is. There's all the clock pulses, no problems, and there's the data up there.

If we scroll over to the start, you can see how the data there is going in before the clock pulse actually happens. So there's that, uh, setup time there. So yeah, everything's hunky-dory look at that. I mean that's a Bobby dazzler.

Wow. So all we're going to do is fix the uh, modulo aspect of that. Um, it. It obviously goofed that up, but hey, it got the order of the segments right and everything.

so that's actually updating. Once per second, all we need to do is fix the counter. Let's go. It's using the modulo function which is the it's a percent, it's not actually a percentage thing.

it's called the modulo function I Can can we call up the we can't call up the help on that? Let me get that. and here it is here. It's the remainder operation Uh, which calculates the remainder when one integer is divided by another. It's useful for keeping a variable within a particular range EG size of array that's not what we're using it for.

I've used it before to actually extract a digit from the thing, but because we've got AI, we can just ask it to try and fix it. Let's see if the AI can actually figure out what's wrong with it. So how will we word this? Uh, it does not get the current digit from the counter correctly. There must be something wrong with the modulo operator on the counter variable.

Can you fix it all right? Cross your fingers. Hope. I've explained that correctly to it. Apologies for the mistake in the previous code.

Here's the modify code that should correctly get the current digit from the counter. Let's see what it's done: Digit Power 10, Integer modulo 10 and then I Love how it explains itself in this modify code. The digit variable is now calculated using counter divided by Power 10 I Modulo 10 instead of counter modulo 10. this calculates the current digit by dividing the counter by the appropriate power of 10 and then taking the remainder after division by 10.
the power function is used to calculate the appropriate power of 10 to divide by based on the current position in the loop. so let's just paste that in there. We can tab that over there and we'll just comment out those two lines there. Do that? Well, Oh no, it doesn't like that.

Invalid operands of type double in into binary operator percent. Ah, okay. I'm going to copy that. Let's go back to the AI and see if we can use a complete AI solution to fix this.

Like assuming like we like, we don't have to analyze it. We can. Just like. let's let's give it a go.

I'll tell it that gave me the compile error and I copied it in there. How do I fix this? Let's let's see if it can. Let's see if we can get a response to that. An error occurred.

Oh no, the AI is broken. Um, great. Murphy's Law Just when I need to finish this video off, Maybe I need to upgrade to plus I Thought I was already on a played plan. Is there a new one already? Well, the air is persisting.

You can just imagine being on a tight deadline to finish your code and the AI breaks down. It's not like you can run like a local copy of it. The database is too huge. This is why Microsoft gave him 10 billion dollars and they had to run down Microsoft's cloud service.

There's the resources are massive. Well let me try the log out. Trick again. I can't even upgrade my plan I didn't think I was on a free pan I Thought I was on a paid plan I don't know maybe something's changed recently, but um, there you go I can't even pay for it.

There you go I've re-logged in I do like how it keeps your response although it didn't get my response last time. so I'm going to have to put that back. Okay, let's try that again. Yes, huh? I See the power function return It knows The power function returns a double value, but the modulo operator requires integer operands to fix this.

We can cast the result of power to an integer using the int type cast operator. Here's the modify code: Wow. Chat: GPT is absolutely like if it didn't get it right the first time. This is how the conversational chat.

This is the advantage of this. It's conversational. You don't have to start from scratch. Again, it knows your existing conversation.

You can tell it to make changes like I've demonstrated and now it's it. Made a mistake. It knew it. You told it you've made a mistake and you fix it.

It fixes it and and then it knows and then it gives an error and then it knows what the error is and how to fix that again. Wow, what a great example this is. This is absolutely terrific. So there we go.
All we've got to do is put the INT in there. Yeah, so there you go. We'll just copy that back. So we paste that in there like that and let's compile again.

Yep, winner winner chicken dinner. Let's run it. Let's go back to the bench. no all zeros.

it's not our counters not returning anything. I Should have analyzed the code more before. I Had a look at it. You can see that we're getting uh, clock but no data.

Aha I think I can see. Yeah, this is wrong, right? So if we got power, the power function is the uh, the 10 there to the power of I I starts at zero. So 10 to the power of zero is one. Okay, so that would be one modulo 10 which doesn't return our digit.

Oops, that still does not produce the correct digit. Can you fix it? I Apologize for the previous mistake I Noticed another issue in the code in the guest Segment Pattern function. The segment patterns are defined with the decimal point segment as the what. However, the segments are shifted out with the decimal point segment as mostly this mismatch is causing the segments to be no, no, Well, no, the problem is there.

No hang on. The problem is we're getting the incorrect digits. not that the segments are displayed incorrectly. I Get segment Pattern function to define the sigma patterns with the decimal point.

No, no, there was nothing wrong with it. I'll get segment pattern. No, please undo that. That's not the error.

The error involves calculating the digit using the modulo function. This is getting a bit tedious now. Okay, let's see. I apologize for confusion.

Here's the updated version that should correctly calculate current digit using modulo. It didn't say what the error was. All right. Let's see how different it is.

equals counter divided by into Power No, that's exactly what we got before. No, it's gone back to think In this update, we've changed it in a loop to shift out the segment. no the syntax error in the function by casting it to an INT Yeah, that, so it's It's taken into account our previous conversation: no, no, sorry AI Um, that's you haven't changed. The the root cause of the problem is this: It's not returning the correct digit.

Oops. I might have to fix this manually I Don't think I can coax it into fixing this. Um, maybe my prompt engineering skills just aren't good enough to do it. That's the same calculation for the digit.

It is still wrong. Can you please fix it? See? Because if you didn't know how to fix it, you would have to if you had enough information to do it yourself. You just do it yourself, right? If you knew how to do it like the whole idea is getting it to figure out what the mistake is. Counter in power 4 minus one.

Do Okay, now it's giving us at least something different. Write C code that extracts digits one by one from a given number. I'm going to make this AI Do it I'm going to make I'm gonna make it. work.
Here's some C code that is. Yep, it should be trivial for it, right? Digits in der are number extract digit. There you go. It's modulo 10 and number is.

You divide it by 10 just like I said. No. The interesting thing is it basically had that solution. Correct.

Back here: Counter divided by 10. Oh, I think I See it? Okay, having people are probably screaming at me right? Um, it? It actually had the code originally correct here. Okay, modulo 10 And then the counter equals divided by Uh 10. That's the same as counter equals counter divided by 10.

But the counter is like it's supposed to be decrementing. so you need to put it into a temporary variable first. You can't do it on the actual counter. So yeah, we we have to.

Um, oh, can we tell it to Ah That's pretty complicated to explain that they're overriding the counter. Go back to the original code and use a temporary variable instead of counter in the digit calculation. Let's see if it understands that. Yeah, all right.

this is so much fun. It really is. Temp counter equals counter. Oh no.

See, it didn't go back to the original code It It's no. okay, it's still doing that. But yeah, it's like temp counter modulo 10. Yep, so it's it's.

doing the temp counter thing. All right. Let's just uh yeah, let's just fix it manually and we haven't around enough. All right.

So outside of the loop, I've put temp counter equals counter. So we're copying into a temporary variable and then we're operating on that temporary variable there. So let's let's give that a try. Everyone's going to miss me screaming at me I think I figured it out.

I Was wondering this. this should work right? This original code even as a sendie check, temp counter equals temp counter divided by 10, right? and I'm gonna like this should work. And then I realized we're dealing with a five digit number Nine Nine Nine Nine nine which is greater than the integer we had declared for not only temp counter, but also for the counter. Up here it was an integer and um, yeah, no, we we need a long for that because it's going to go only go up to 65 536, right? Um, so yeah, that's it's.

just not going to do it. So if we change that to Long type, then it should work well. We can ask the AI if that's correct. It looks like the counter variable is the incorrect type.

It's an integer instead of a long. Is that the problem? Let's go. It's possible that the issue with the digital is related to the integer overflow, which can occur when the counter. Yeah, yeah, yeah, so it knows all about this.

In that case, changing the data type of the counter variable to Long counter they are and then it's going to generate the code. It's still going to use that dodgy code. Uh, no. yeah, it's still going to use the power thing.
Um, I don't think that's going to work. Yep, let's try that. Let's go to the bench. All right.

let's power it on. Whoa. Hello, Hey, it's counting. It's counting.

It's counting down. What? Why Didn't it start at 99999 though? We got 99749. But it's counting down. It's doing the thing.

Let's see. if it goes to from 40 to 39, it will. I'm sure. Yep.

Yep, no problems. Let's let's try that again. Should have initiated. It's initiating to that same value each time.

7 30. Is it yeah, pretty close to it. Why is it doing that? Doesn't like that? That's something to do with the Precision in the modulo operator perhaps. but it's counting down Anyway, it's doing the business.

so it it. Basically, we didn't have to muck around with any of the mapping for this thing. It got the correct seven segment mapping. It got the uh, it got the order of digits actually correct.

Um, whether or not that was by luck or whether or not that was. uh, you know, good prompt engineering on my part. I'm not sure I'd have to go back and check, but uh, yeah, there you go and it's and it's mapping everything correctly and it's doing the business. So there you have it.

That was almost a successful attempt to get AI to write code for this urge we know to do. It's not a hugely complex problem, but as you saw, it had like a few little tricks in it and we had to like it. Had to understand that we wanted like a physical thing like a seven segment display so it knew what that was. It knew how the segments actually uh, you know the digits map in a seven segment and the segments mapping a seven segment display.

It understood that it understood a shift register because we use like that, uh you know. So this, uh, keyword, a 40-bit shift register at Newwood All that wasn't new that we wanted five seven segment displays with and it understood that there was a decimal point and understood like and it. This code was pretty close to being bang on right off the bat. Um, this actual shift output here I I just had forgotten my Arduino libraries.

yeah, that that would have worked. We didn't have to actually. uh, specifically, that's a function that is designed to put the data first before it then shifts the clock. so we didn't actually have to change that.

So it turns out that was absolutely correct. It turns out the modulo thing here was correct. like the actual function of it is correct, except that it didn't know to use a temporary variable in there like it was overriding the counter so that that wouldn't have worked. And then it.

even though we told it a 99999 that exceeds the integer value in here which I need 32 plus minus 32 you know, a thousand, right? So um, yeah, so it didn't pick up those things, but that's that's pretty minor. Apart from that, it it really did. It really did well. Like I got all this mapping correct.
We didn't have to change any of that like the segment mapping. it got the order correct and and everything. So there was only a few little hiccups in there which you know and this video has been a long time. You know I've been going through the process of figuring out and I didn't spot the you know the integer thing.

uh, straight away and you know these are these are mistakes you make all the time. Yeah, but I'm quite impressed the AI actually did that. Um, it's not bad at all and then, but you can see that there's limitations to it. like it just got confused as we go down here.

It just like it started putting the power thing in there I haven't really correctly analyzed that, but I don't think is is that going to work I don't know. leave it in the comments. uh, down below. So yeah.

so then it got fixated on the power function after we told it it was wrong. So it really takes it to heart when you tell it it's wrong, Can you? You can please fix this or change it it it goes and thinks about it and and finds a different you know it knows you want a different way to do it so it tries to accommodate you in that uh which is really good. And then when I told it no go back to the original it didn't know that the original meant way back at the start you know and stuff like that I apologize for the previous mistake and and and then. But then it got totally confused and wanted to change.

Thought that there was something wrong with the get segment pattern function and there wasn't There was absolutely nothing wrong with that at all. It just got absolutely muddled. So yeah, whether or not we could have actually got like kept going in that same chat window to get it to actually produce the working code. probably.

but in this particular case, it did actually require a human that's me to actually um, you know, ultimately figure out that you know the integer was overflowing and stuff like that. So unless you it what didn't seem to be picking up that on its own. So yeah, um, here, a little bit of human intervention required. but damn like you know it's not the first time I've made that mistake myself.

and I'm sure you know, yeah you have two. Just admit it. Um, but yeah, no, that's that's it's it's really impressive though. How like just simply that gave me 99 of the way there.

Like it. It actually compiled fine straight off the bat. It's just that it required a temporary variable people and and the int changed and that was it. Would have been good to go.

So yeah, wow. Impressive, huh? There you go. I Hope you enjoyed that. Um, that's a chat GPT Uh, three, Airtel 3.5 isn't it? And yeah, people are scared that it's going to take your job.

but it's just it. It's another tool basically to um, you know to help you out. but yeah, no, you could Bluff yourself into a programming job with this, write a fake resume and then just you know, tune out some code using the AI and and commit it. You know.
And then it's like yeah, you can probably like. You know you might be able to Bluff your way into a job for a certain amount of time until they figure out that you're using an AI bot and you don't Actually, you know, able to find uh, things. But yeah, it's I'm really amazed with that example that it actually it. It crossed hardware and software there and it kind of knew about seven segment displays and and shift registers and stuff like that.

So for taking that that simple text and doing it, I'm I'm thoroughly impressed. Wow. Wow. Hats off.

So there you go, thoughts and comments down below. and if you like that video, please give it a big thumbs up and let me know if you want me to do the uh, like another version of this just using old school discrete TTL logic or however other way you want me to take this project but that was a cool use of AI I Really like that. That was a great example. Catch you next time.

Thank you.

Avatar photo


24 thoughts on “Eevblog 1531 – can chatgpt generate all the code for my macgyver project? part 3”
  1. Avataaar/Circle Created with python_avatars Toni Lähdekorpi says:

    This isn't costing you anything. The API features you added your card to, where you can select the model and use the API is a separate service from ChatGPT. Using GPT-3 through that service is very different to this as it is not a conversational thread and you need to formulate the prompts a bit differently.
    But you can upgrade to the Plus version of ChatGPT for faster responses and better availability for a monthly price.

  2. Avataaar/Circle Created with python_avatars Tom Laddus says:

    This is impressive at the same time somewhat disturbing.

  3. Avataaar/Circle Created with python_avatars David Kenny says:

    It's more like you leveraged your relationship with a coder, to write some routines for you. That's not to say that you wont learn anything from it. It can help you to learn coding, by examining code that works and then experimenting. You can also ask chatGPT to explain specific programming concepts that you don't understand and even ask it to rephrase it in simpler language (if you need to). You will have more success once you expand your programming vocabulary so you will be able to ask more specific questions of chatGPT.

  4. Avataaar/Circle Created with python_avatars onjoFilms says:

    I asked ChatGPT this:

    how long will you work for me before I have to upgrade to Plus?

    As an AI language model, I am available to answer your questions for as long as you need, without any charge or time limit. You do not have to upgrade to any paid plan to continue using my services. I am here to assist you to the best of my abilities and provide you with accurate and helpful responses as long as I am capable.

  5. Avataaar/Circle Created with python_avatars MartelDuV says:

    Not always the case but sometimes having a tool that almost works and seems like it should work is actually worse than not having the tool in the first place. It can take more time to try to get the tool to actually work for you than it would have taken to just do the job without the tool.

  6. Avataaar/Circle Created with python_avatars NEY Industries says:

    We can't own ChatGPT – it's an online-based service…
    It's features could be truncated any time, it could be made subscription only any time… It can just disappear from free access any time…
    This is dangerous as hell… Such a powerful tool, and it's in arms of who knows who, with agenda who knows what… IMO, it's a very sad day for humanity, once this thing had appeared on the world's arena… It will make hundreds of millions lose their work, and company that owns ChatGPT will be an unquestionable monopolist in future of IT.

  7. Avataaar/Circle Created with python_avatars Paul Koopmans says:

    As much as we think we have a 'complex' question here…. I honestly think it is NOT. After the blinky LED being a first project, like a "Hello world!", doing some simple shifting out and driving 7 segment displays is probably closely following. Shift registers are generally used to expand outputs too. So I think it must have had tens… if not hundreds or even thousands of examples of this, all with different solutions, but with lots of similarities. Because some of those are rated to be working it is pretty easy for it to take pieces and put it together.
    My software development equivalents here are after "hello world!" everybody does Fibonacci range, tictactoe, celcius/Fahrenheit conversion, etc.
    There is soo much content available on simple things and basic principles that it is not a surprise it does well on this.

  8. Avataaar/Circle Created with python_avatars Kevin Cozens says:

    Dave, you corrected the count value in the original query but you missed that you were telling it about a "40bit resister" instead of register. ChatGPT seems to have still understood what you wanted. When you were testing the code by setting digit to 2 a better option was to set digit to the value of the loop variable. That would let you see if it was able to output different values for each display. You did eventually realize a temporary variable was needed when calculating the value for digit which ChatGPT didn't. The use of pow may work but it is an expensive function to use.

  9. Avataaar/Circle Created with python_avatars pbyfr says:

    I don't know what was more hilarious between chatGPT cycling between wrong answers or Dave not realizing that he didn't reset the Arduino.

    Anyway, another demo that chatGPT is quite dumb, as he don't know right from wrong. Using directly GithubCopilot should be easier. And chances are high that chatGPT is just using GithubCopilot for code "generation".

  10. Avataaar/Circle Created with python_avatars ThePoxun says:

    On first reading the actual error in the first iteration of code was the line counter/= 10; which should not be there. This was dividing the counter by 10 every shifted digit in addition to the decrement at the end of the loop which would result in a completely wrong set of digits to be shifted. At the end of the shift operation counter would be 0 and then set back to 99999 for the next loop so the output digits while wrong would not change between loops. For this to have been potentially correct it should have assigned the current counter to a new variable and used/divided that for the digit extraction… nevermind Dave noticed it.. reminder to self watch the whole video before posting

  11. Avataaar/Circle Created with python_avatars Nicolas Jonasson says:

    It seems to keep timing even when turned off – if turned off at 950 and turned on 5 seconds later, it would show 945.

  12. Avataaar/Circle Created with python_avatars Roger Orchard says:

    Sounds like it is a little easier that trying to get our low coust centre to fix a problem.

  13. Avataaar/Circle Created with python_avatars Peter Sage says:

    "Good enough for Australia"? Don't you mean "good enough for California"? Angus MacGyver hails from California.
    Also, BCD mapping is standardized to the point of having jellybean BCD decoders. Once you get the mapping right the first time, just copy pasta. The standard mapping is obviously readily available to ChatGPT; absolutely no reason why it would get the mapping back to front.

  14. Avataaar/Circle Created with python_avatars OvalWingNut says:

    Magnum Infotainment.. "to the pool room". That's a keeper.

  15. Avataaar/Circle Created with python_avatars Bob Weiram says:

    The code isn't very efficient. The lookup function should be inlined, or even better use constexpr, if compiled using C++. Even better, it should apply boolean operations to the digit to convert it into its segment pattern representation, since it doesn't require expensive branch and compare operations. While it sounds nitpicky, microcontrollers are resource constrained to the point where every byte and clock cycle makes a difference.

    Frankly, I'm more impressed by its language processing than its coding. It can only regurgitates whatever code it can find. In other cases, it mashes up several code fragments. A decent programmer can easily Google existing to produce a far better implementation. Moreover, since ChatGPT doesn't show its source code references, there is no context to make an informed judgment on the validity of its solution.

  16. Avataaar/Circle Created with python_avatars DarkTherapy says:

    I’ve used ChatGPT to create micropython code on a raspberry pi pico. 2 potentiometers, an oled display and a button. The button flashes an LED and the 2 pots control the frequency and pulse time of the flashing. Displaying the variables on the oled. Works brilliantly. Better than I could have myself.

  17. Avataaar/Circle Created with python_avatars Petex90 says:

    It must be already April 1st 🤔 Haven't been looking into chatGPT at all this far, but this is damn impressive.

  18. Avataaar/Circle Created with python_avatars hackwell says:

    lucky you! Tried it to generate python3 code to solve a CTF , it didn't even run. It was faster to write it myself instead of debugging

  19. Avataaar/Circle Created with python_avatars The Signal Path says:

    Hey! I see me! 🙂

  20. Avataaar/Circle Created with python_avatars BRADASS OFFGRID HOMESTEAD says:

    The amount of people who are going to be out of work, will be insane. For now, there is a new market, and easier ways to make money for many!

  21. Avataaar/Circle Created with python_avatars The Bright Pixel says:

    I feel like we are witnessing the beginning of what is about to be a fundamental shift in the way we use technology, just like the invention of the internet and then the smartphone. Soon we won't even need the arduino IDE, we'll just ask an app to make us a program and upload it to the hardware in one step. Imagine when something like this is integrated into things like KiCad and 3D printing modellers. "Please design a low cost PCB with sufficient hardware to run the code you just wrote for me, and print a housing for it while you're at it"!

    The mind boggles where this will be in 5 – 10 years. When we have some kind of SoC that can run a personal instance like now I have a NAS, (imagine when you can get a Synology AI Station), this will be a gamechanger.

  22. Avataaar/Circle Created with python_avatars Insoft says:

    ChatGPT has certainly changed the way to search for source code than using a typical search engine and then searching the source code on the GitHub page that the traditional search engine points you too.

    Now we can basically ask ChatGPT and usually get straight to the source code then we can clean it do what ever needed, saving time on the initial start of writing code

  23. Avataaar/Circle Created with python_avatars chłodne jabłko says:

    BTW, I was late to the party 6 months ago, but I wanted to point out why there were serial data coming all the time from the micro. The power for LEDs was driven directly by the serial data (Pulse Frequency Modulation), so the LED's were always getting exactly the current they needed, since the amount of "1's" in data are exactly the amount of LED's turned on. One common cathode transistor driven by serial data can be used to drive all the LEDs.

  24. Avataaar/Circle Created with python_avatars Eric Merrill says:

    I'm sure you probably already figured this out, but the reason it wasn't starting at 99999 at the end was because the arduino was running that entire time – you were only resetting the display, not the processor, so it just kept counting down in the background. That was running, I assume, off your computer's USB port.

Leave a Reply

Your email address will not be published. Required fields are marked *