Potentiometers and Pico with MicroPython

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey robot makers do you want to know how to use a potentiometer with a raspberry pi picot or micropython then keep watching we're going to build a very simple circuit to read in analog values from a potentiometer and then read those in as values into our raspberry pi pico once we've read those values in we can then convert it to a percentage so that we can do some more things with it so we can set the value of an led so a potentiometer has three legs we have a ground we have a voltage in and we have the voltage out and the voltage out is where we read the analog values into the raspberry pi pico so to wire up the potentiometer we only need to connect the voltage to the 3.3 output on the raspberry pi pico the voltage output to one of the adc pins we're using pin 26 here and the ground just goes to one of the raspberry pi pcos ground pins in micropython the code is very simple we simply bring in from machine adc which is the analog to digital converter we're then going to set a variable called pot to the adc pin 26 on the pico and that's one of the special pins that's designed for converting analog values into digital and then we've got a simple loop here that says while true print the value is and then from that pot variable which is the adc converter we're going to read in an unsigned 16-bit integer which just means a number so let's run this code and see what happens so we're running the code and we can see that we're getting values of around 300 and something so if i twist the pot and increase that in value i can see that the number is going high and if i turn it all the way to the right as far as it'll go we get 65 535 and as i scroll back down to the left the minimum value we get again around that 304 288 value it never quite goes down to zero and that's just the behavior of this particular potentiometer [Music] so we're now going to bring an led into our project and we're simply going to bring in a couple of extra things into this code to make it work so we're going to bring in the pin and we're going to bring in the pulse width modulation from the machine library so we've created a new variable there called led and that's a pulse width modulation pin and that's on pin 15 and we're going to set the frequency there the refresh frequency of the led to 1000 and then in our loop we're simply going to say the led duty which is the duty cycle which is an unsigned 16-bit variable which just happens to be the same as what we're outputting from our pot because that's an unsigned 16-bit variable as well so we run this code now and see what happens as i turn this knob here you can see that the led is getting brighter and brighter and then as i turn it down to the left you can see that it's getting dimmer and dimmer the led never actually goes to zero there and the value of the potentiometer never goes to zero so to add the led to our project we simply need to add the long leg of the led to pin 15 on the picot and the short leg to one of the grounds i'm using pin 38 on the picot there the third program i'm going to try here is we're going to change the value of the led but based on a percentage rather than the actual value coming from the potentiometer so we've got a new map function here and what that will do is it'll map two ranges together so if we've got a value a minimum and a maximum of our potentiometer so around 300 to 65 000 and then we've got the percentage from 0 to 100. it's going to take in those two values map them together we're going to pass it a value from the potentiometer range and it's going to output a percentage of that but we could actually use any range of numbers in this function and then what we're then going to do is we're going to create a new variable that's called percentage we're going to map the value from the pot which we've read in there we're going to pass in the minimum value that that can be so from 288 ish to 65 535 is the maximum pot value and then from zero to 100 percent what percentage will be is the percentage of that pot value we're then going to map the led value based on that percentage so from zero to 100 and then now we're going to change from zero to sixty five thousand five hundred and thirty five so we'll see if that makes any difference to the brightness of our led we're then going to print out what the percentage is what the raw pot value is and what the new led value is and then we're gonna set the led to that new value so let's see what happens there when we run this code so as i turn that you can see the led is getting brighter you can see that the value is changing on the very right-hand side to 65 535 and the value on the left says 100 and then if i move this right back down again so we're about 50 there and then let's move it right back down to zero so percentage is now zero the led output value is zero but we can see the led is still on interestingly but the raw value that's coming out there is around 288 304 which is the minimum range of that potentiometer so if you enjoyed this video about potentiometers and you'd like to know when more videos are going to come out why not hit the subscribe button and then you'll get notified when new videos come out if you'd like to leave a like as well and a comment that can also help me understand which videos are good and which videos you want more of so i'll see you next time
Info
Channel: Kevin McAleer
Views: 6,959
Rating: undefined out of 5
Keywords: Potentiometer Pico MicroPython, potentiometers how they work, potentiometers, pico, micropython, LEDs, how to add a potentiometer to Raspberry Pi Pico, Raspberry Pi Pico, Pots, analog to digital conversion, Pico ADC, Pico ADC LED, Pico ADC input, Kevin McAleer, Small Robots, ADC, analog to Digital, MicroPython, Pico, raspberry pi pico projects, raspberry pi pico programming, raspberry pi pico micropython, raspberry pi pico tutorial, micropython for beginners
Id: mTz-nFvCBhA
Channel Id: undefined
Length: 5min 2sec (302 seconds)
Published: Mon May 17 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.