Analog (ADC) Inputs on the Raspberry Pi Pico with MicroPython

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
today we're going to learn how to read analog inputs and build a circuit to include a potentiometer with this we'll be able to read and adjust voltages ranging from 0 to 3.3 volts to accomplish this we're going to use the analog to digital converter or adc built right into our microcontroller the adc converts a continuous time and continuous amplitude analog signal to a discrete time and discrete amplitude digital signal now that's a bit of a mouthful to be honest so a quick version is that the adc converts voltages to a digital integer this conversion is based on two things a reference voltage and the resolution of the adc in our case the voltage of the pico is used by default so its internal reference voltage is 3.3 volts the resolution of the picos adc is 12 bits which is represented as a range from zero to four thousand and ninety five in the case of micro python on the pico this 12 bit value is mapped to a 16 bit value which is represented as a range from zero to 65 thirty 535 however in practice you'll see values ranging from four hundred to sixty five thousand five hundred thirty five how often the signal is sampled is known as the sample rate or sample frequency in the case of the pico it has a sample rate of 500 kilosamples per second there are a total of five adc channels on the rp2040 microcontroller four of these are broken out as gpio pins 26 27 28 and 29 and one channel is dedicated to measuring the temperature sensor built into the chip our pico board gives us access to three of the gpio pins 26 27 and 28 we can still use the other two channels but gpio pin 29 is used for measuring the system voltage and as mentioned before the final channel is dedicated to the built-in temperature sensor in this example we'll be using a potentiometer to adjust the voltage being read by the adc pin a potentiometer is a type of variable resistor with three terminals the two outer terminals let's call them a and b are connected together with a resistive element the middle terminal usually called the wiper tracks along the resistive element changing the resistance between terminal a and the wiper and the terminal b in the wiper this effectively creates an adjustable voltage divider we can connect one side of the potentiometer to the positive rail and the other side to the ground bus then we connect the middle terminal the wiper to our adc pin here we'll use channel a0 or gpio pin 26 for programming we first need to import the adc class from the machine library and import the micro time library then we'll create an adc object here i'm using the gpio pin number but we could have used the channel reference if we wanted to in the main loop we'll need to call the read underscore 16 function to grab the current analog reading from our connected pin here we'll print the analog value to the serial interface which shows up in the shell we will also want to slow the process down a little so we can see each value change we can use a fairly short sleep time such as 100 microseconds in our sleep function now when we run our code the value of the potentiometer will be displayed in the shell as we adjust the knob of the potentiometer the value displayed in the shell changes if we wanted to see the voltage value instead of the integer conversion we can add a conversion factor and then multiply our reading with the conversion factor to see the actual voltage under the pot object we created earlier let's create another object called convert factor and set it equal to 3.3 divided by 65535 then in our print statement where we call the read underscore 16 function we just need to multiply it by the convert factor now when we run our code we see the actual voltage values displayed in the shell in this video we used a potentiometer as a voltage divider and read that value into one of the adc channels provided by the rp2040 microcontroller we also use the print function to write the value to the serial interface in our next video we'll use pulse width modulation to control the brightness of an led until then thanks for watching
Info
Channel: Brad Henson
Views: 14,265
Rating: undefined out of 5
Keywords: Raspberry Pi, Raspberry Pi Pico, Pico, Microcontroller, Potentiometer, ADC, Analog, Analog to Digital Converter, Breadboard, MicroPython, Python, Thonny, Programming
Id: 4XPDyKujcxI
Channel Id: undefined
Length: 4min 19sec (259 seconds)
Published: Wed Mar 03 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.