Raspberry Pi Pico: Inputs & Servo Control

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] welcome to another video from explaining explainingcomputers.com this time i'm going to return to the raspberry pi pico microcontroller specifically i'm going to look at connecting input hardware to the pico switches and a potentiometer and i'm also going to cover servo control so let's go and get started right here we are back with the raspberry pi pico connected to a raspberry pi 400 which we're going to use to write micro python code this said you can use a windows pc a mac or any linux computer as the pico's programming hardware as we can see the pico is sequencing five leds which is where we left things at the end of the last video last time however i forgot to include a circuit diagram so let's now have one to make it clear how the leds and their 220 ohm current limiting resistors are wired to five of the pico's general purpose input output pins and just in case you don't know here on a breadboard the pins in the middle of the board are connected in two blocks horizontally while the outer pins are linked vertically with one of these here providing a ground rail if we go across to the pi 400 desktop i'll run up sony under programming here the sony python integrated development environment where down the bottom right we can see it's got micro python raspberry pi pico as its interpreter so it's connected to the pico but it says device is busy or does not respond and this is because the led sequencing program is running on the pico because we saved it on the pico as main dot pi which makes it auto start as soon as the pico is powered but as it says here we can stop that code by just pressing a control c there we are and i think i'll just load in that code to remind you what's going on here we go to open we'll open from the pico there's main.pi which was running when the pico powered up and you can see here we basically import a library called pin which is going to allow us to use the pins on the pico a library called utime which allows us to use time functions with the onboard real-time clock and here i set up five pins to use as output to control our leds and then after that we set a variable called delay to be 0.25 and then we have an infinite loop while true is always true and basically in that loop it turns on led a sets the output to high waits for the delay time for sleeping turns it to low moves on to the next led turns it high another delay moves on to the third one etc all the way down and this does work but as i said last time it's not the most brilliant type of code so what i've done here is to write a better version of that code so i thought i'll show you that i've also got it stored on the pico it's called a running led better there we are let's open that one up under here let's have a bit more space on our screen same imports at the start i set our pins up as i did previously i've called them here l1 l2 r304 l5 just to make it easier in terms of typing rather than led aabb etc and then i set up a list using parentheses to square brackets here put the values inside the list for all the led names l1 to l5 with commas between still set up delay but then all we need is this we simply have inside a while true loop for x in leds or list up here and it'll put x dot high starting with l.1 do the delay go down to low etc so this code here is much more efficient than the this code here but they both do exactly the same thing but we'll just prove that we'll run this code and hopefully yes there we are it's now sequencing the leds so i think now we've got ourselves reorientated with the pico and what was going on and we'll now move on to look at connecting some input devices greetings here i am back again with a momentary push switch and a 47k potentiometer to which as you can see i've soldered some jumper leads and whilst the switch is brand new i've just bought the switch the potentiometer was one i took out of an old radio when i was 12 so this is well over 40 years old anyway let's start with the push switch which by the magic of filmmaking i've connected to the picot and if we look to the circuit diagram we can see that one lead is connected to pin 36 which is the pico's 3.3 volt output and the other lead to pin 1 which is gp or general purpose input output 0. and this is a good moment to point out but in our code all references are 2 gp numbers not to physical pin numbers talking of code here in thoni i've got a couple of programs to test out the switch the first of which is very simple it just loads in libraries as before and ready to find switch to use pin 0 as an input and also to have a pull down resistor and this last part is very important and if we look to this graphic we can see what is going on with the pull-down resistor tying our input pin to the ground rail this stops it from floating around with the pin being maintained at a logical level of zero or zero volts where the switch is not pressed and rising to a logical level of one or three point three volts when it is pressed returning to our code we've got a while true loop just to cycle on forever where as you can see we're going to print the switch value using that code there with a small delay to keep things going satisfactorily so if we run this code like that we can hopefully see there bring that up we've got a printout currently of zero but if i go across and take the switch and press it it goes to one and zero and one and two zero the lion and the tiger the zebra and the giraffe yes that works perfectly well indeed so let's uh stop that code and go across to the second piece of code which as you can guess is what we had previously are running light but with the switch included so exactly the same setup but we've now defined switch as we just did and inside a while true loop we've got another while loop which is while switch value remember switch value will be one or zero when it's one it'll run the cycle so if we just execute this code like that go across to our device nothing's going on is it if i press the switch oh look it fires an led across this is like one of those games we used to make with leds and bits of electronics when i was very young we used to go very exciting indeed anyway we proved the principle we've actually incorporated a switch into our pico setup here and code right i've now removed the switch and wired in the potentiometer with its outer pins connected to 3.3 volts and ground and this allows it to act as an adjustable voltage divider that will deliver somewhere between 0 and 3.3 volts to gp28 on the pico depending on the potentiometer's rotation note that while you can connect switches and leds to any of the pico's 28 gp pins a potentiometer delivers an analog voltage input and hence has to be wired to one the pico's analog to digital converters or adcs these are available on gp26 27 and 28 and here we're using gp28 which can be configured as adc 2. so let's go across to sony where once again i've written some test code in our first very simple test we initially have to import adc as well as pin from the machine library and then we define pot to be adc on pin 28 then we have a wild true loop and inside this loop we're going to print the value of pot using a pot.read underscore u16 where u16 will convert an input of nought to 3.3 volts to an integer of between 0 and 65535 so let's run this code and we see a range of values appear down here and if i go in and technically twiddle the pot you'll see it goes uh different directions we can take it down to uh not quite zero but pretty close we go the other direction all the way up to uh six five five three five so you can see the principle there we're reading a value from the position of the potentiometer so let's uh stop that piece of code and go across to the second example which i'm sure you've guessed is based on our running led once again this time adjusted to include the potentiometer we've imported the right library there we've defined pot down here as we just did and then inside the loop which sequences the leds we've got a delay not based on a fixed value for u time.sleep but based upon the pot redo 16 and divided by 32768 which will scale the value there to between naught and two to give us a sleep period of between nought and two seconds so let's run this code and initially the led is hardly moving it is moving but not very much is it there we are we can speed it up though if i just twiddle the potentiometer hopefully i'm going the right direction always getting faster look that's definitely faster if i keep twiddling oh it's ridiculously flashed it's four guys just flickering maybe i'll bring it back to a more sensible rate but as you can see we've proved a principle we've managed to wire a potentiometer into the pico use some code to read a value from it and done something with that [Music] value right let's now get the pico to control the servo and to try this out here i've got an sg90 which is the most common small servo in the world and like all servos it's got three wires with the red one accepting a supply voltage of between four point eight and six volts and the brown wire being the ground rail the orange wire is a news to supply a control signal which needs to be a pulse width modulation or pwm square wave like most analog servos the sg90 accepts a 50 hertz square wave and the angle of the servo's actuator arm is controlled by the length of the pulse or duty cycle the range of duty cycle control values varies between servos but for an sg90 is about two percent for a servo angle of zero degrees and about 12.5 percent for an angle of 180 degrees so let's get the sg90 hooked up to the pico and here we are as you can see i've got rid of everything else we're starting from scratch here with the servo and if we look to a circuit diagram you can see we're going to control the servo with gp0 and we also have it connected to a ground pin and the picos 5 volt rail note that it's often wise to power a servo independently with a separate battery or power pack but for a test using one small servo making use of the five volt rail on the pico is fine once again i've written some test code where initially we're importing pwm as well as pin from machine as well as importing the library utime and then we're defining servo to use pwm on the gp0 and then after that we're setting our servo frequency to be 50 hertz next we're going to control the sg90 by sending it some duty cycle values to move it back and forth between 0 and 180 degrees as i've noted in the comments i've put here in the code we're going to use dot duty underscore 16 with a value and this takes a value of between zero to six five five three five across the duty cycle range of naught to one hundred however this does not mean that we should send values of between zero and six five five three five to the servo as as i've already noted an sg90 has about a two percent duty cycle for zero degrees and about twelve point five percent for 180 degrees so what we're doing down here in the last bit of code inside a while true loop is to send a value of 1350 to the servo and then we're going to wait for a couple of seconds and then we're going to send it a value over 8 200. and i've got these exact values using a little bit of trial and error but my starting points were two percent of six five five three five which is thirteen eleven and twelve and a half percent of six five five three five which is eight one nine one so with all that explained let's bring up the servo on the screen and run the code oh look the servo's moving and moving again oh that's working very nicely isn't it it's going to a zero degrees waiting a couple of seconds 180 degrees moving back again and uh moving back again isn't that marvelous i think we can say with absolute certainty we've established control of the sg90 using the raspberry pi pico right for our final fling i've brought back the potentiometer and wired it back into the picot if we look at the wiring diagram you can see what i've done exactly what we've done before because i thought it might be nice to use a potentiometer in a servo in the same example so we go across to the test code you can imagine what's here we're importing adc pwm and pin we've set up the potentiometer we've set up the servo as previously and what this code does as i'm sure you would suspect is to use the position of the potentiometer to adjust the position of the servo actuator on and we know that the output from the potentiometer will be in the range zero to six five five three five so that needs scaling to be in the range thirteen fifty to eight two hundred to move our servo actuator on between zero and 180 degrees and i make a note of how that's done over over here in the code and it's implemented down here in our while true loop our final while true loop where we're going to calculate a value we're going to use as the duty cycle which is an integer of that range has to be an integer to pass across to the servo and then we set reduce your cycle of the servo to that value so let's bring up the servo on the screen and run the code and oh look the servo moves it's jittering a bit but if i take the potentiometer and move it it's like virtual puppeteering look like an or physical puppeteering i can move potentiometer and as i do so it moves the servo actuator on and so this is a bit jittery and i'm sure this is an issue with using a 40 plus year old potentiometer rather than the issue with the servo and i can prove that because if i go and stop the code you will see that the servo stops jittering because it's staying at the last value it was sent and also if we go down to the code here maybe let's just put in under there let's just put in a print value like that and if we now save the code oops and we run it like that it'll start jittering again but if we look on the screen like this you can see the values which we're sending to the servo which are not consistent they are across our scale should be eight one two hundred all the way down to what was it 1350ish it's roughly that but clearly the problem is the the age of this potentiometer is giving us problems in terms of the the quality of its output but anyway it still proves a principle we managed to link one device to the other in our final example of exciting things you can do with the raspberry pi pico as you probably gathered i really like the raspberry pi pico and i'm sure i'll return to it again in another video on this channel maybe try a project with it and i might also look at other microcontrollers as some of you have asked me to do but now that's it for another video if you've enjoyed what you've seen here please press that like button if you haven't subscribed please subscribe and i hope to talk to you again very soon [Music] you
Info
Channel: ExplainingComputers
Views: 61,409
Rating: undefined out of 5
Keywords: Raspberry Pi Pico, MicroPython, Raspberry Pi Pico Servo Control, Raspberry Pi Pico MicroPython, Raspberry Pi Pico Inputs, Christopher Barnatt, Barnatt, MicroPython servo control, servo, SG90, SD90 servo
Id: TDj2kcSA-68
Channel Id: undefined
Length: 17min 20sec (1040 seconds)
Published: Sun May 16 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.