Let’s see more examples. How about using the ADC. These ADC are of 12bits, which is 4 times better than the 10 bits ADC of the Arduino. As an extra, the IC has an on-board temperature sensor which we will also test later since is connected to one of the ADC and its output is an analog value proportional to the temperature. For this example we have the 12 bits ADC on GP pins 26, 27 and 28. I define my ADC input connected at pin 28.
To create an infinite loop I make a while true. And here, all I do, is to read the ADC value with the read u16 function. Then using print I can show the value on the shell monitor. I have a potentiometer connected on the GP 28 pin. As you can see I rotate the potentiometer and the value goes from 0 to 65 536 and that’s wired because this is supposed to be a 12bit ADC and not 16. Anyway, that’s how we can read analog values with the ADC on the raspberry PCIO.
import machine import utime analog_value = machine.ADC(28) while True: reading = analog_value.read_u16() print("ADC: ",reading) utime.sleep(0.2)
Remember that the PICO has an on-board temperature sensor. That’s connected on the pin 4 and it has an analog output. So if I make the same code but instead of pin 28 I read pin 4 and then multiply this by the temperature multiplier, I get the temperature. As you can see in the video below, I run this code and when I heat the PCB, the temperature rises.
import machine import utime sensor_temp = machine.ADC(4) conversion_factor = 3.3 / (65535) while True: reading = sensor_temp.read_u16() * conversion_factor temperature = round(27 - (reading - 0.706)/0.001721,2) print("Temperature: ",temperature) utime.sleep(0.1)