In this video, we will learn how to read analog inputs on the Pico. We will start by diving into an example where we read the voltage set by a potentiometer and then cover some important information about the Analog to Digital Converter (ADC) that lives on the Pico.

Transcript

Ah, we finally arrive at analog signals. This is where we really start to get into some cool stuff.

Analog and digital signals are completely different things. If we use the examples of clouds in the sky again, a digital input would be like asking, are there clouds in the sky? There is only a binary set of answers to this question. In other words, the answer is either going to be yes or no, and you could write this answer as a 1 or a 0 to represent that.

An analog input of the same kind of question would be like asking, what percentage of the sky is covered by clouds? There is way more than one answer to that, and they will range between 0 and 100%. The answer to this could be 42.371, it could be 99.222, it can be any number between 0 and 100%.

What this means for our Pico is that when we set up a pin as an analog input, we can read the exact voltage on that pin between 0 and 3.3 volts. If you remember this example from a couple of videos ago where I was manually adjusting a voltage on the pins, the orange line was the result of reading a digital input of this voltage and could only return 1 or 0, and the blue line was actually just the analog reading of that voltage and we got from 0 to 3.3 volts. It was just reading whatever voltage I was supplying to the pin. It's that easy. Well, not quite that easy. There is a little bit more on that, but we'll get stuck into that later.

You should be able to guess by now, we're going to jump straight into an example and go over it. Sample code can be found on our course page on our site, link in the description for our YouTube audience. And if you haven't already wired up this chapter circuit, you'll need this circuit at a very minimum for this video.

We start with our imports as usual, but this time we're going to import ADC, which stands for Analog to Digital Converter, which will allow us to set up our analog inputs. Now here is where we set up pin 26 of the Pico as an analog input, which is connected to the potentiometer on our breadboard. A very important thing here is that only pins 26, 27, and 28 can be used as analog inputs. The Pico only has three pins that can do this. This is because converting analog to digital signals needs a special piece of hardware called the ADC, analog to digital converter. And only those three pins have access to that piece of hardware on the Pico. If you try it on any of the other pins, it just won't work.

Then we also have this variable here called conversion factor. And I'm just going to ask you to ignore that for now. It's going to be the elephant in the room. We will get to that in a bit.

Then in our while true loop, we read the potentiometer with ".read_U16()" with empty brackets on the end. This is where the actual magic of reading the analog inputs happens. For now, we're just going to do a little bit of math here and multiply our reading by conversion factor and then store it in a variable called pot voltage, which stands for potentiometer voltage. It's just a naming convention we're using here. We then finish with a print and a quick sleep.

Plug in your Pico, run the code, and you should be able to see that when you twist the potentiometer, the voltage reading in the shell should change to report whatever voltage is currently being set on that pin. And you can also enable the plotter, which is really cool in this example, as you twist the potentiometer and see a line moving on your computer. And you can see that signal has a little bit of noise as it kind of just jitters around a little bit.

Now, how our potentiometer works here is that we have one side of it connected to ground and the other to 3.3 volts. When we turn the knob in the middle, you can see it kind of mixes these two voltages. Now, we are cheating a little. Analog inputs on the Pico don't perfectly read between zero and 3.3 volts. The conversion factor here is modifying it to this nice voltage range before we print it. And this highlights another cool way to use variables, using them as constants like this, numbers that will never change. Like, you can assign gravity to a variable and call it G. And whenever you need to use gravity, you just go something times G.

Let's now remove that multiplying by the conversion factor to see what the raw data input is. You can see that it's kind of behaving the same, but instead of printing between zero and 3.3, it's just reading in the range between zero and 65,535. And this is because of how the ADC and MicroPython works.

The Pico is a digital device, and the world around us is very analog. Voltage, for example, is an analog thing. So in order for digital devices like the Pico here to read analog signals like the voltage on one of its pins, it needs to use an ADC, analog to digital converter. And it does exactly what the name implies. It takes an analog voltage on one of the Pico's pins and turns it into something that can be used by the digital Pico. But the way it does this is that it turns that voltage into a nice whole number, a nice integer number. It's a bit weird, and it's just kind of how digital devices work. But a reading here on our Pico of zero volts will return a number of zero, and a reading of 3.3 will return 65,535. Halfway between zero and 3.3 volts will return halfway between zero and 65,535. A quarter of the way will give a quarter of the way. It maps perfectly between those two ranges.

Doing a little bit of math, you can find that if you multiply by the number that we store in the conversion factor variable here, it will turn it back to ranging between zero and 3.3. You don't always need to do this, but our goal here is to read the voltage on the pin, and it makes it more human-readable in this case. So we will choose to do so. But you should do whatever your project calls for or you find most comfortable.

Okay, so we can read a voltage on a pin of our Pico as long as it ranges between zero and 3.3 volts. What else can we do with this? Well, we can read sensors. The world around us is very analog, which means that a lot of the sensors you encounter will provide you with an analog signal. And they are usually a lot more interesting and give you a lot more information than their digital counterparts.

For example, this is a microphone sensor board, and it has two outputs. The digital output returns zero if it's quiet or one if it's loud, but the analog output returns a voltage that corresponds to how loud the room is. So you can get more data than if is it quiet or is it loud. You can figure out how loud the room is. This little thing is a temperature sensor and it has an analog output as well. At negative 50 degrees, it outputs zero volts, and at 125 degrees, it outputs 1.7 volts. So you can plug that into your Pico, do some analog reading, and then you can do a little bit of math to convert the voltage you read to the actual temperature. This is a soil moisture sensor, and it outputs a voltage depending on how much water it kind of detects in the soil. You could place it into dry soil and see what voltage it reads, and then place it into some freshly watered soil and then see the voltage read higher. And then you could use the Pico to monitor the voltage level that the sensor is outputting, and then turn on a water pump when it gets too dry. We do exactly this in a cool self-watering plant kit.

Another idea is using this. It's a force-sensitive resistor, and we can wire it up so that it reads zero volts, and then when we put some force on it, it will output a higher voltage. We could then place it under a mat and then use a Pico to measure the spikes in voltage as people step on it. You might even be able to detect whether it's a pet or a human. A cat or a dog would put less pressure on it and will result in a smaller change in voltage, while a heavier human will result in a larger signal. What I'm trying to say here is there is just an incredible world that opens up now that we can use analog sensors.

All right, three key takeaways. One, only pins 26, 27, and 28 of the Pico can be used as analog inputs. Two, to read an analog signal, you will need to import ADC, then set up the pins using ADC, and read it with ".read_U16()". And three, the analog reading will input a number from zero to 65,535, which represents a voltage of the pin from zero to 3.3 volts."

Comments


Loading...
Feedback

Please continue if you would like to leave feedback for any of these topics:

  • Website features/issues
  • Content errors/improvements
  • Missing products/categories
  • Product assignments to categories
  • Search results relevance

For all other inquiries (orders status, stock levels, etc), please contact our support team for quick assistance.

Note: click continue and a draft email will be opened to edit. If you don't have an email client on your device, then send a message via the chat icon on the bottom left of our website.

Makers love reviews as much as you do, please follow this link to review the products you have purchased.