[0:00] Hey everyone, so I’m sure by now you’re aware of ChatGPT. It’s pretty amazing.
[0:06] What you might not be aware of is you can use it to generate code.
[0:09] So let’s give it a go. I want to do a little Arduino project and I want to use ChatGPT
[0:14] to generate the code for me.
[0:16] So what I’ve got is I’ve got a TinyPICO board which is based around the ESP32 and
[0:20] I’ve got a potentiometer hooked up to one of the analog inputs. I’m using pin 32.
[0:25] So let’s get ChatGPT to generate the code for us. So we’ll give it a little spec and
[0:30] we’ll see what it comes out with.
[0:34] Okay, so we have an ESP32-based dev board. We want to use the Arduino framework to read
[0:39] values from a potentiometer and write the values to the serial console. We want them
[0:43] output every 10 milliseconds and we connect it up to pin 32 as the input. So let’s see
[0:48] what it tells us to do.
[0:49] So the first thing is connect the potentiometer to the dev board. The middle pin should be
[0:53] connected to pin 32 and the other two pins should be connected to a power source EG3.3
[0:59] and ground. So that seems pretty good.
[1:01] And then we just need the Arduino header in the setup function. We’ll start off the
[1:06] serial. It’s picked 9600 as the bits per second. That’s quite slow but it will work.
[1:12] We don’t really need to do this. I think that happens automatically in the analog read
[1:15] but it’s probably okay. And then in our loop, we read the value and print out the value
[1:20] and delay for 10 milliseconds. So that seems pretty good.
[1:25] So let’s get the complete code so we don’t have to copy and paste little snippets. Show
[1:29] me the complete code. There is some information here as well. So it says the analog read function
[1:35] returns a value between 0 and 1023. Now I know for a fact on the ESP32 that’s actually
[1:42] 0-4095. So that’s not quite right. But let’s see what it gives us for the complete code.
[1:48] So here we go. We set up the serial port and then we read the values and output them. Now
[1:54] I know that the ESP32 A2D converter is pretty noisy. So this is going to produce
[2:01] quite a noisy output. So let’s try and get an average. We’ll ask it to generate us some
[2:05] code to smooth the values. So the output is very noisy. How can I remove the noise? So
[2:15] it’s saying we could have a low pass filter, do it electronically. That is an option. Or
[2:20] we can average the readings. So what it’s saying here is instead of reading it every
[2:25] 10 milliseconds, read it multiple times and average the value. So let’s see what it suggests
[2:29] us to do. So now it’s saying inside our loop function we’ll take the value sum and the
[2:34] value count. We’ll take 10 values and then we’ll take an average. Now it’s interesting
[2:40] since it’s just doing 10 it could just use 10 here. We don’t really need to have this
[2:44] counting up but it will work. So let’s copy this code and see what happens. Show me the
[2:51] complete code using averaging. Okay. So let’s also get it to give us a higher
[3:01] board rate and we’ll change the delay which is doing it 100 milliseconds despite us asking
[3:06] for 10 milliseconds. So let’s fix these and generate the code again. So generate the code
[3:13] okay. So it’s picked up our instructions. It knows what it wants to do. So here we have
[3:22] the correct build rate and we have the collect delay. So let’s copy this into the Arduino
[3:28] IDE and see if it actually works. So let’s run this. So it compiles which is good. That’s
[3:35] always a good sign. So let’s bring up the serial plotter and see what we’re getting.
[3:42] And it looks like we’re getting actual values. As I turn the potentiometer it goes up and
[3:47] down. So we can take it down to 0 and we can take it up to 4095. So that’s pretty cool.
[3:54] It’s generated a sketch that works. So let’s try and do something slightly more complicated.
[3:58] So the Tiny Pico has a built-in dot star LED. So let’s try and change the color of the dot
[4:04] star LED based on the value of the potentiometer. So we have a dot star LED. Its data pin is
[4:15] 2 and the clock pin is 12. We want to change the color to green if the value is less than
[4:20] 1024, yellow if it’s less than 2048 and otherwise red. So what’s it saying we should do? Connect
[4:26] the dot star LED to the dev board. Well fortunately it’s already connected. Then it’s saying
[4:31] use the Adafruit dot star library. So then we define the pins. It’s interesting it’s
[4:37] done a hash define for the number of LEDs but it’s put these as hard-coded values. So
[4:41] that’s quite interesting. Maybe we can make that better. We can probably tell it to pull
[4:45] those out as constants. So then it says begin the strip, set the brightness to a low value.
[4:51] Interesting. I don’t know why it’s doing that. Then it’s saying read the values. If
[4:56] it’s less than 1024, do this. If it’s less than 2048, do yellow. Otherwise set it to
[5:02] red and then show it. So let’s try and make the code a bit better. So I’d quite like
[5:09] these to be constants. So let’s say and I want the brightness to be as bright as possible.
[5:18] So let’s see what it comes up with. OK, so that’s a bit better. It’s using const for
[5:23] the data pin and the clock pin. It should really use const for the number of LEDs but
[5:27] we’ll let it off on that one. So in the setup we now set the brightness to 255. In the loop
[5:33] we read the value and then we check the value and set the colours as appropriate. So that’s
[5:38] pretty cool. So let’s copy this out and we’ll see if it actually works. Once again it compiles
[5:44] which is always nice it’s uploading and here we go. So we have green, yellow, and red.
[5:54] So that’s pretty cool. I’m pretty pleased with that. It’s not bad at all. We’ve generated
[5:58] some nice code and it seems to work. So let’s see what else we can do. So one thing that’s
[6:04] missing is it’s not using our averaging of the analog value. Now I’ve read about exponential
[6:10] smoothing so let’s see if it can add in some functionality for that. So that’s pretty cool.
[6:22] It’s given us a nice bit of code for exponential smoothing and it’s given us a nice bit of
[6:26] explanation. And now it’s giving us the complete code. So our smoothing factor is going to
[6:35] be 0.1 and our smooth value will start off with 0. So the set up function is the same
[6:42] but now in the loop we read the raw value and then we calculate the smooth value using
[6:46] exponential smoothing and it’s creating the same code we had before for showing the different
[6:51] colours based on the value coming from the potentiometer. So that’s pretty amazing.
[6:59] So let’s try and do something really complicated. Now I’m not sure if this will work but wouldn’t
[7:04] it be great if we could stream the values from the potentiometer over the network to
[7:09] my laptop. So let’s try and get this thing working over Wi-Fi. Okay so the first thing
[7:21] it says is we need to actually connect to Wi-Fi which is obviously necessary if we’re
[7:26] doing networking. So this is pretty cool. It’s got all the stuff for doing Wi-Fi. So
[7:35] what’s interesting is it’s also creating the code to receive the values but we want to
[7:40] run that on our laptop. So let’s see if we can make it do something a bit more interesting.
[7:47] So here it’s telling us how to actually send the values to a server. So I think we now
[7:54] need to prompt it to continue the code because it’s getting quite long. So I think here it’s
[8:04] starting to get slightly confused. So it’s trying to create a server where actually we
[8:09] just need to do a Wi-Fi client. So let’s tell it and let’s make sure we spell potentiometer
[8:22] correctly. So I want it just to show the code for sending the values not for doing the server.
[8:30] So looks promising. We’re connecting up the Wi-Fi. We’re reading the value with the potentiometer.
[8:45] And we should be sending the value off to the server. So this is pretty interesting.
[8:49] I actually think this might work. I don’t know if it will build but let’s try it. So
[8:56] we’ll copy this into the Arduino IDE. I’ll replace the Wi-Fi SSD. Everybody look away
[9:03] now. Now what’s the IP address of my computer? So let’s find that out. And we’ll just run
[9:11] on a random port number. So let’s see if this actually compiles first. Amazingly it does
[9:21] compile. So we need to have a server. So let’s get chat GPT to generate us a server. Okay.
[9:33] So we’re getting some Python code. So I think this will just run once. I think it needs
[9:40] to be in a loop. So this is pretty cool. Let’s give it a go. I have absolutely no idea if
[9:53] this will actually work. So I think our host we can probably just bind to 0.0.0.0.0. Our
[10:06] port number should match what we have here. So 7654. And I think that’s it. So if we save
[10:18] this and we run Python 3 server. So that should now be listening. And if we go to the Arduino
[10:28] IDE and we upload our sketch. Something should happen. Now it will be quite amazing if this
[10:37] actually works. So let’s see. Do we have anything coming on our server? No we don’t. So maybe
[10:48] we should do a bit of troubleshooting and see what’s going on. So one thing I can see
[10:54] is we don’t actually begin our serial port. So let’s just do that just to make sure we
[10:58] can actually get some output from it. So it’s trying to connect to the Wi-Fi and it’s not
[11:10] connecting. So what I’ve done is I’ve set up a hotspot on my phone and the ESP32 is
[11:16] connecting to that. We can see that if we hit the reset button we get a few dots and
[11:21] then eventually we see Wi-Fi connected. Now if we go back to the terminal where we’re
[11:26] running our Python code we can see that we’re getting values from the potentiometer. It’s
[11:31] all coming through nicely. I can turn the potentiometer and the values change. So this
[11:36] is pretty amazing. We’ve used ChatGPT to generate the code for the ESP32 to send values
[11:42] over the network to our laptop and we’ve had ChatGPT generate the code for the server.
[11:47] I’m pretty impressed. There’s been a few interesting bits where it’s got interesting
[11:51] bits of code but on the whole, it’s pretty amazing. Now of course ChatGPT is not just
[11:58] for generating code. You can use it for all sorts of things. Now you may not be aware
[12:02] but we’re sponsored by PCBWay and what you can do with ChatGPT is generate all sorts
[12:07] of marketing content. So if I just copy out some of the content off the PCBWay website
[12:14] then I can ask it to generate us a nice little bit of content. So we’ll paste this in.
[12:18] It’s quite interesting how because I stopped off halfway through writing something it’s
[12:23] just continued on writing copy for PCBWay. But let’s ask it to generate us a nice little
[12:29] plug for PCBWay to go in the video. So let’s see what ChatGPT comes up with. So here we
[12:38] go. Hi everyone, I’m excited to share with you a fantastic company that has sponsored
[12:41] my YouTube channel PCBWay. PCBWay is a leading manufacturer and assembler of PCBs and offers
[12:46] a wide range of services including quick term prototyping, advanced PCBs and PCB assembly
[12:49] for functional prototyping and low volume production. They also offer CNC machine and
[12:52] 3D printing, sheet metal and injection molding services. This is pretty cool. I might actually
[12:56] use this in future videos for the PCBWay plug. Now one of the things I’ve noticed when
[13:01] I do a little plug for PCBWay is everyone skips it. So how can I keep you more interested?
[13:08] So these are some great hints on how to keep people more engaged when I do a little plug
[13:12] for PCBWay. So that’s pretty cool. So if you haven’t used ChatGPT I’d really recommend
[13:17] giving it a go. It’s pretty impressive stuff. So thanks for watching and I’ll see you
[13:22] in the next video.
[0:00] Hey everyone, so I’m sure by now you’re aware of ChatGPT. It’s pretty amazing.