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