Voltage Sensor – with pico w 2

  • R1 – 7.5K
    R2 – 30K
from machine import ADC
from time import sleep

# Use ADC0 (GPIO26)
adc = ADC(26)

# Constants
VREF = 3.3              # Pico ADC reference voltage
ADC_RESOLUTION = 65535  # For read_u16() scale
DIVIDER_RATIO = 5       # Because 7.5k / (30k + 7.5k) = 0.2 → inverse is 5

def read_input_voltage():
    raw = adc.read_u16()
    v_out = (raw / ADC_RESOLUTION) * VREF
    v_in = v_out * DIVIDER_RATIO
    return round(v_in, 3)

# Main loop
while True:
    voltage = read_input_voltage()
    print("Measured Voltage:", voltage, "V")
    sleep(1)

Since the Pico ADC max is 3.3V and you’re dividing by 5: Vin max=3.3V×5=16.5VV = 3.3V \times 5 = 16.5V

Vin max​=3.3V×5=16.5V

So you can safely measure up to about 16.5 volts.

Pico 2 w – HC-SR04 ultrasonic distance sensor

Pins:

from machine import Pin, time_pulse_us
from time import sleep

# Define GPIO pins
TRIG = Pin(3, Pin.OUT)
ECHO = Pin(2, Pin.IN)

def get_distance():
    # Ensure trigger is low
    TRIG.low()
    sleep(0.002)  # Let sensor settle
    
    # Send a 10µs pulse to trigger
    TRIG.high()
    sleep(0.00001)
    TRIG.low()

    # Measure time for echo
    try:
        duration = time_pulse_us(ECHO, 1, 30000)  # 30ms timeout
    except OSError as ex:
        print("Pulse timed out")
        return None

    # Distance calculation: time (us) × speed of sound (cm/us) / 2
    distance_cm = (duration * 0.0343) / 2
    return round(distance_cm, 2)

# Main loop
while True:
    dist = get_distance()
    if dist:
        print("Distance:", dist, "cm")
    else:
        print("No distance measured.")
    sleep(1)

Pi Pico 2 W – UF2 flash – install

  • While connect to your PC first press the BOOTSEL cutton on the pico 2 w and then connect the USB
  • It will be connected as new drive(FS mode). you can view in “files”