ADC
Analog to Digital Conversion - Read a voltage and convert it to a number!
Last updated
Analog to Digital Conversion - Read a voltage and convert it to a number!
Last updated
While all ADC pins are digital pins, not all digital pins are ADC pins.
"""
ADC Demo Code
Breadstick Innovations
April 26, 2024
https://learn.breadstick.ca/breadstick/breadsticks/raspberry-breadstick/code-examples/adc
ADC converts 0-3.3V to 16-bit integer
RP2040 has a 12-bit ADC but CircuitPython converts it to a 16-bit result.
2^16 = 65536 values
0 is one of those values,
so the range is 0-65535.
3.3V/65535 steps = 50uV/step
You won't actually measure at 50uV accuracy, closer to 1mV accuracy.
"""
from board import *
from pwmio import PWMOut
from analogio import AnalogIn
from time import sleep
from adafruit_dotstar import DotStar
def convert_voltage(ADC_value):
return 3.3 / 65535 * ADC_value
def convert_range(ADC_value, from_max, to_max):
return int(ADC_value / from_max * to_max)
pot_1 = AnalogIn(A1)
pot_18 = AnalogIn(A18)
led_5 = PWMOut(D5, frequency=500, duty_cycle=0)
leds = DotStar(DOTSTAR_CLOCK, DOTSTAR_DATA, 24, brightness=0.02, auto_write=False)
RED = (255, 0, 0)
ORANGE = (255, 75, 0)
GREEN = (0, 255, 0)
BLUE = (0, 0, 255)
OFF = (0, 0, 0)
leds.fill(OFF)
leds.show()
while True:
ADC_1 = pot_1.value
ADC_18 = pot_18.value
led_5.duty_cycle = ADC_1
voltage_1 = convert_voltage(ADC_1)
voltage_18 = convert_voltage(ADC_18)
print((voltage_1, voltage_18))
bar_graph = convert_range(ADC_18, 65535, 25)
leds.fill(OFF)
for i in range(bar_graph):
leds[i] = ORANGE
leds.show()
sleep(0.04)