🍞
Breadstick
  • Breadstick Innovations Website
  • Breadsticks
    • đŸĨ–Raspberry Breadstick
      • Code Examples
        • Demo Code
        • POV Wand
        • Pride Flags POV Wand
        • 6-Axis IMU
        • RGB Blink
        • AsyncIO RGB Blink
        • Digital Input
        • Digital Output
        • PWM
        • ADC
        • Servo Motor
    • đŸĨ–Raspberry Breadstick Lite
    • 🍞Support Boards
      • I2C Devices
        • 🚌I2C Bus Rail Adapter
        • đŸŒĻī¸Weather Crouton
        • 😎Brightness Crouton
        • 📏Distance Crouton
        • đŸĢ¨Motion Crouton
      • I2S Devices
        • đŸ“ĸBoombox
      • SPI Devices
      • đŸ•šī¸Buttons/Switches
    • Learning Resources
    • Troubleshooting
  • Nougat
    • Nougat C3
    • Nougat Quad
    • Installing WLED
  • Pico Slices
    • 🔴Slice 1 - LED Mixer
      • Assembly Guide
      • CircuitPython Code
        • 1 - Blink
        • 2 - Analog Read to Plotter
        • 3 - PWM Fade
        • 4 - Pot Controlled PWM
        • 5 - Gamma Correction
    • âąī¸Slice 2 - Stopwatch
      • Assembly Guide
      • Coding Lessons
        • 1 - 7-Segment Display Intro
        • 2 - Cycling Through All Segments
    • âŦœSlice 3 - 8x8 Dot Matrix
      • Assembly Guide
      • MicroPython Code
        • 1 - Moveable Pixel
        • 2 - Snake
    • Circuit Python Setup
    • Reset Bricked Pico
  • Christmas
    • Christmas Tree DIY kit
  • Protoboards
    • â¤ī¸Proto-Heart
    • đŸĨĒProto-Toast
  • SHOP
Powered by GitBook
On this page
  • ADC Pins - A1, A2, A18, A17
  • Code

Was this helpful?

  1. Breadsticks
  2. Raspberry Breadstick
  3. Code Examples

ADC

Analog to Digital Conversion - Read a voltage and convert it to a number!

PreviousPWMNextServo Motor

Last updated 1 year ago

Was this helpful?

ADC Pins - A1, A2, A18, A17

While all ADC pins are digital pins, not all digital pins are ADC pins.

Code

"""
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)
đŸĨ–