# previous project we learned hwo to detect if GPIO is on(3.3V) or off(0V)
# in this projectwe will use ADC(analog to digital converter) GPIOs allow
# to read voltage between 0 and 3.3V
# For ESP32 voltage is assigned to a value between 0 and 4095
# For ESP8266 voltage is assigned to a value between 0 and 1023
# A potentiometer(电位计) is used to read analog value
# For ESP32 analog pins are 0,2,4,12,13,14,15,25,26,27,32,33,34,35,36.
# The resolution is 12 bit,and can change the resolution in code.
from machine import Pin,ADC#import ADC class
from time import sleep
pot=ADC(Pin(34))#creat an ADC object on pin 34
pot.atten(ADC.ATTN_11DB)
#apply atten method to pot
#atten method can teke the following argumens:
#1,ADC.ATTN_0DB the full range voltage is 1.2V
#2,ADC.ATTN_2_5DB the full range voltage is 1.5V
#3,ADC.ATTN_6DB the full range voltage is 2.0V
#4,ADC.ATTN_11DB the full range voltage is 3.3V
while True:
pot_value=pot.read()
#apply read method to pot and pass the reading to pot_value
print(pot_value)
sleep(0.1)
# note:
# we can also change the resolution:
#eg:
#1,ADC.WIDTH_9BIT,range 0 to 511
#1,ADC.WIDTH_10BIT,range 0 to 1023
#1,ADC.WIDTH_11BIT,range 0 to 2047
#1,ADC.WIDTH_12BIT,range 0 to 4095
# use the code ADC.width(bit)
#eg: ADC.width(ADC.WIDTH_12BIT)