PRESTO with IO Expander - PWM and ADC advice

I’ve just added an Breakout IO Expander to the Qw/ST I2C connection of my PRESTO to provide extra Input and output… (Just change the setup line in the examples to

PINS_PRESTO = {“sda”: 40, “scl”: 41}

get the board running on a PRESTO)

I can read the value of a button and turn an LED ON/ OFF.

I can read a potentiometer but the example program IO_adc.py example only provides a voltage - a FP value between 0 and 3.3v… Is it possible to get a integer in the normal MicroPython range of 0 to 65535?

Could someone please show me how to set up basic PWM to control the full brightness range of an LED.on a pin on the IO Expander?

Is there a document which lists all the MicroPython instructions for this board?

I’ve sorted out the PWM to drive LED brightness. ADC so duty-cycle range
is 0 - 16383

# IO_PWM
# Tony Goodhew 15th Feb 2025
import time
from pimoroni_i2c import PimoroniI2C
from breakout_ioexpander import BreakoutIOExpander

PINS_BREAKOUT_GARDEN = {"sda": 4, "scl": 5}
PINS_PICO_EXPLORER = {"sda": 20, "scl": 21}
PINS_PRESTO  = {"sda": 40, "scl": 41}

ioe_servo_pin = 6

# Settings to produce a 50Hz output from the 24MHz clock.
# 24,000,000 Hz / 8 = 3,000,000 Hz
# 3,000,000 Hz / 60,000 Period = 50 Hz
divider = 8
period = 60000 

i2c = PimoroniI2C(**PINS_PRESTO)
ioe = BreakoutIOExpander(i2c, address=0x18)

ioe.set_pwm_period(period)
ioe.set_pwm_control(divider)

ioe.set_mode(ioe_servo_pin, BreakoutIOExpander.PIN_PWM)

for dc in range(0, 16384,127):
    print(dc)
    ioe.output(ioe_servo_pin, dc)
    time.sleep(0.02)

for dc in range(16383, -1,-127):
    print(dc)
    ioe.output(ioe_servo_pin, dc)
    time.sleep(0.02)
    
ioe.output(ioe_servo_pin, 0)

Just integers in the same range directly rather than a backwards calculation from voltage. Any ideas?

Is this the ADC example you were trying? pimoroni-pico/micropython/examples/breakout_ioexpander/adc.py at main · pimoroni/pimoroni-pico · GitHub

If so, then change .input_as_voltage to .input to give you the integer value.

For what it’s worth, you can see all the Micropython bindings for this breakout here: pimoroni-pico/micropython/modules/breakout_ioexpander/breakout_ioexpander.h at main · pimoroni/pimoroni-pico · GitHub
(and similarly for most of our other breakouts that use bindings rather than native python modules)

For your servo and LED control, it’s worth looking at the regular Python examples for this breakout too:

Servo: ioe-python/examples/servo.py at main · pimoroni/ioe-python · GitHub
RGBLED: ioe-python/examples/rgbled.py at main · pimoroni/ioe-python · GitHub (which uses: ioe-python/ioexpander/devices.py at main · pimoroni/ioe-python · GitHub)

@ ZodiusInfuser Thank you - Just what I was after. I can now do all things I was looking to do.

This board helps makes up for the lack of normal IO on the PRESTO. I think it would be a good idea to advertise it on the PRESTO page as many PRESTO users may not find it otherwise.

1 Like