Using ChatGPT to write Raspberry Pi Python code

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
foreign [Music] welcome to another video from explaining computers.com this time and following several requests we're going to experiment using for chat GPT AI chat bot to write Raspberry Pi python code so let's go and get started right here we are running a two gigabyte Raspberry Pi 4 with a clean install of raspberry pios bullseye over on the desktop I've done very little other than creating a folder called python code in which we're going to store our python code and I've also made a few scaling changes so let's go across to programming and run up Sony which we're going to use for executing our python code and I've also here already launched a web browser where I've gone to the open AI website and on this site we can access chat GPT by clicking on try on the web like that and as you can see we have to log in and we can either log in using an openai account which we can create if we want to or we can use an existing account and here I'm going to use a Microsoft account so I'll just log in and here we are in the chat GPT interface where it asks us to send it a message I'm going to give it a command I've already saved in the buffer I've got that there which as you can see I'm asking it please write some python code that asks the user their name and then prints it seven times so let's press enter on that and let it do its thing and it's doing it already very exciting oh look it said certainly that was nice of it doesn't it and it's written the code and it's even explained what it's done and if we just scroll up here we can see the code and if we just click on copy code like that we can go back to Sony and we can paste the code into there like that and I'll save it just to be tidy and if we now execute the code there we go it's asking for my name if I just press enter there we are it's printed it seven times so already I've shown the principle of asking chat GPT to do something copying the code it's created pasting it into Sony executing it and it works however as you've probably noticed we haven't just got a two gigabyte Raspberry Pi 4 here we've also got it connected to an LED which is wired to pin 12 and then through a current limiting resistor to the ground rail so can chat GPT make this LED flash well we'll have a go we'll go back across two chart GPT and we'll ask it to please write some python code to continuously flash an LED connected to physical pin 12 on a Raspberry Pi so let's execute this there it goes and we'll speed on through until it stopped generating and there we are it's finished and we have to scroll to find what it's done it should be somewhere under here there we are that's what we asked and here is the code it has actually created so let's copy that code like that and go back across to Sony create ourselves a new document like that paste in the code and we'll save it just to be a nice and neat and just before we execute this code let's just take a look at the code it has written where as we can see at the top it's importing the libraries for gpio and time and when it's set in with GPI mode to use board numbering and this is important because there's two ways you can address the gpio pins on the Raspberry Pi using either board numbering which is based on the physical PIN numbers or BCM which is based on a set of defined gpio numbers as we can see in this diagram and that's why when I ask the question I did note we had our LED connected to physical pin 12. anyway it's then gone on here in the code to set the PIN to an output which is obviously a good idea and then if we go down a bit further it's got a little Loop to continuously Flash the LED so shall we try it out why not let's bring in a shot of the LED and also run the code and uh yes we've got a flashing LED isn't it exciting when we get LEDs flashing or just generally in the world it's a very exciting thing to be happening however you might be thinking well chat GTP has written the code to flashy LED but how did you know to wire it up maybe you don't know how to wire things up and you therefore need to have chat GTP tell you about the wiring and the code can it do that well of course it can so that's just a go back to chat gtt like that where we could say something like provide wiring and python code to Fashion LED connected to a Raspberry Pi there we go and once again we'll wait until it's stopped generating and if we now scroll down we can probably find what's going on where are we where's what we asked there it is look please provide wiring and python code it says certainly here's the details on the wiring and then beneath that it's provided details for the code foreign let's now try something harder and for this I've added three more LEDs which are wired up like this and I'm going to ask chat GPT if it can code a running light just to note I plan to use five LEDs but I've lost one of my current limiting resistors in the carpet anyway Let's test out chant GPT by asking it the question I've written down here which is to write python code to make a continuously running light using LEDs connected to physical pins 12 16 18 and 22 on a Raspberry Pi so let's press enter under cross our fingers and whilst this is generating note that in the last section of the video I did expect things to work as I tried them out first but from this point forward I have no idea what's going to happen this is very much an experiment anyway it's done something so let's uh scroll up and find the code there it is let's copy the code and I've got Sonny waiting for us and we'll paste that in like that and shall we just simply save and run and see what happens and uh oh yes that worked pretty well doesn't it we've got a little running light seems to be a bit sort of hesitant between the different parts but uh that's perfectly good I've got no problems with that although let's go back to the code over here and stop it and I don't actually like having that little sleep thing there that's to get rid of that I think it would be better without that and to get rid of that like that and let's make that slightly longer like say that let's now save that and run it again and uh that's more like what I was thinking about but that's what a human thinks rather than what chat gtt thinks that's uh that's working very well however there is one more refinement I would like to see which is to get the light to bounce back and forth so let's go back to chat GPT and of course it all comes down to how we phrase the question and I'm going to start out being cheeky I'm simply going to ask it initially altercode to make LEDs bounce back and forth like a Cylon and of course it might not have watched Battlestar Galactica but let's see what this does there we are it has finished and if we just go down here like a Cylon we can copy the code has it done the whole code again I presume it has that's just a copy that that's what I was expecting let's go back here Ctrl a get rid of that and just paste that back in like that and hopefully let's just save again and run and uh yes it's doing roughly what we anticipated although again I'd like to edit it a little bit so let's just come back and do that there we go and if we run it to final time yes there we are oh dear it's doing strange at the end isn't it something slightly strange going it comes back and it doesn't quite come about right does it let's just go back to the code and stop it again see exactly what's going on here it's running across a pattern and then reversing a pattern and ah but the pattern is a whole lot isn't it 12 16 18 20 to 18 16 back to 12 again why have we got this second bid I think we can just get rid of that entirely like that and that should work okay shall we try that and save and run and uh yes that's exactly what I thought it should do there we are adding some human editing we've got the expected result right I've now wired up an sg90 Servo to the Raspberry Pi and if we look in the wiring diagram we can see more clearly what's going on we've got the 5 volt power and ground connected to pins four and six on the Raspberry Pi you don't normally power a Servo from a Raspberry Pi are normally powered separately but one sg90 for a test will be fine and then the pwm the pulse width modulation control signal for the servo is connected to Raspberry Pi pin 11. so let's go across to chat GPT where I phrased an instruction for it as we can see write python code to control an sg90 Servo with its pwm input connected to physical pin 11 on a Raspberry Pi Servo should turn to 90 degrees pause two seconds then turn to zero degrees wait for two seconds and repeat three times and if this works I'll be very impressed indeed so let's give it a go and there we are it's finished that took it quite a while to generate all of that but uh that's understandable it had quite a bit to do so let's uh copy the code go across to Sony and paste the code like that there it is it looks like it's set up things to control the server but we will see we'll just save that and run the code it works it works again yes oh coming back a bit quickly then didn't it but it depends what it's doing on its repeats doesn't it but uh and there and uh back again and there we are has it finished I think it's finished hasn't it there we are let's run it again it's too exciting to run just once not a brilliant Servo and controller from the Raspberry Pi but the principle works and if you want to learn more about controlling servos from a Raspberry Pi I've got a whole video about Servo control on the Raspberry Pi but for now I think we proved that chat GPT is capable of coding for a Raspberry Pi servo right I've now added a camera to our Raspberry Pi the Raspberry Pi high quality camera no less and I'd like chat GPT to write some python code to capture video however we may have some issues here because as chat GPT notes it has limited knowledge of the worlds and events after 2021 because it was trained on data sets up to this point and this matters because the latest version of Raspberry Pi OS known as bullseye on launched in November 2021 and which is what we're running here removes support for the pi camera and other modules traditionally use the camera control and so it's likely that the code we're about to generate will not work in this latest version of Raspberry Pi OS where different camera software now exists but that all said let's give things a try as you can see I've phrased our request I've connected a high quality camera to the Raspberry Pi please write some python code that will record 10 seconds of video so let's give this a go there we are and as expected it has used the pi camera module this is coming off the screen now where is it but up there somewhere definitely it's using the pi camera module but I will copy the code show you what happens we'll put it across here and paste it in and save and when we run this it won't work it'll uh presumably give us an error there we are module not found we look down here it probably tells us more about yes things aren't working so what I'm going to do is this and here we are on exactly the same Raspberry Pi running a different version of Raspberry Pi OS to be specific we're now running Raspberry Pi OS Legacy which supports the traditional camera commands if we look online on the page for Raspberry Pi operating systems you can see here where we can download raspberry pios Legacy it's still pretty recent but it's just got that Legacy camera support so if we go across to Sunny like that I've loaded in the code that chatgpt created for us so let's now run the code like that and nothing will seem to happen but I'm just going to move my hand around you don't know what I'm doing do you but anyway I'll be moving my hand around because it actually recording some video for 10 seconds and in the second it'll hopefully finish there we are video recording is complete and where is it recorded it to it hasn't actually given it a folder so it'll be wherever this code is which is sitting in Python code so let's just uh find that hopefully this has worked there is a python code yes there's our video file and if we run it up very exciting oh look it's our friend the hamster and there's my hand moving around as I talked about a second ago so as you can see if you want to use chat GPT to write python code involving a camera on a Raspberry Pi you have to use raspberry pios Legacy and this is not a chat GPT issue it's a Raspberry Pi issue linked to the latest versions of raspberry pios no longer supporting Legacy camera software thank you generative AI software such as chat GPT May signal the next step in the evolution of computer programming rather than writing code from scratch increasingly the job of a programmer may be to accurately describe what needs to be done and then to edit code generated by an a i but now that's it for another video if you've enjoyed but you've seen here please press start like button if you haven't subscribed Please Subscribe and I hope to talk to you again very soon foreign [Music]
Info
Channel: ExplainingComputers
Views: 96,482
Rating: undefined out of 5
Keywords: ChatGPT, ChatGTP, Raspberry Pi, Python, ChatGPT Python, How to write code in ChatGPT, Raspberry Pi ChatGPT, ChatGPT Raspberry Pi camera, ChatGPT Raspberry Pi GPIO, Christopher Barnatt, Barnatt, Rasberry Pi OS, Bullseye, Raspberry Pi OS Legacy, Raspberry Pi OS (Legacy), Raspberry Pi Bullseye camera support
Id: 2vYf2CGxjZQ
Channel Id: undefined
Length: 15min 42sec (942 seconds)
Published: Sun Jul 16 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.