Pico Explorer - Turn PWM off

You turn on PWM to control a Pin with:

led = machine.PWM(machine.Pin(4)) # LED with 470 Ohm resistor on GP4
led.freq(1000)
led.duty_u16(0) # Switch LED OFF

How do you take it off again and return the pin to normal operation?

In the MicroPython documentation it says:

Pin. init ( mode=- 1 , pull=- 1 , *** , value , drive , alt )

Re-initialise the pin using the given parameters. Only those arguments that are specified will be set. The rest of the pin peripheral state will remain unchanged. See the constructor documentation for details of the arguments.

If it re-initialises the pin I take it that would reset it, presumably?

EDIT: Actually, you can just re-declare the pin as a standard in/out pin and it seems to override whatever the last setting was. It feels a bit dirty but it works.

Thank you. My graphics project is now quite long (300 + lines) and I was turning on the PWM for the LED at the start with duty as 0 but the LED was still glowing slightly - no idea why.
I fixed the problem by setting the LED pin as input, pull-down at the start and left it until I needed to use the PWM and then put it back to input again afterwards. That worked well.

Have you tried reading a pot? I used ADC0 but it would not give a proper zero at the low end - (300ish u16). I had to scale and adjust the raw value to get the required accurate range 0 to 255 for colour control. I have found ESPs also have iffy ADCs but this was a surprise.

Is this the LED on the Display Pack?

I was experimenting with that last night and also couldn’t get it to turn off completely, it was always glowing slightly. I think it turns out that there’s an error on the pinout page: the diagram lists the pins as what would be 7,8, and 9 on the Pico, but they’re actually 6,7 and 8.

I haven’t tried reading a pot, but I’m hoping to do a bit more exploring with the board this weekend, so I’ll see if I can give it a try.

I’m using the Explorer - bigger screen - so put a bright blue LED on the breadboard area with a 470 Ohm resistor on pin4 and a 10K pot on ADC0. I’ll try the Display once I’ve got text-scrolling-on-a-path working. Have fun!

I tried a pot last night and couldn’t get to 0 either. If I wired the ADC directly to ground the readings dropped from ~400 to ~170, so I’ve a feeling it’s some subtle electronics thing involving the breadboard and the jumper wires. My electronics-fu isn’t good enough to know where to start picking it apart unfortunately.

Cheap potentiometers we use in breadboards are pretty noisy and the 16-bit ADCs leap about a bit while you leave them alone. I’ve put the code to adjust the pot range to suitable values in my Explorer demo.

    pot_raw = potentiometer.read_u16()
    pot = pot_raw/256
    # Adjust end values: 0 & 255
    pot = int(pot * 256.0 /255.0) - 1
    if pot > 255:
        pot = 255
    percent = int(100 * pot / 255)
    showgraph(percent)
    display.update()
    duty = pot_raw - 300 # duty must not go negative
    if duty < 0 :
        duty = 0
    led.duty_u16(duty)
    display.set_pen(pot,pot,pot) # grey to white
    display.circle(120,140,50)

Hope it helps.

Thanks. Oddly the Pico’s ADCs are actually 14-bit but they’re scaled by MicroPython to 16 bit values. The docs don’t really explain how that happens, so I can only assume that the 14-bit value is bit-shifted over. Most of the itme I’ve experimented with ADCs the hardware has just left the value at what it was, rather than shifting it to fit into a certain number of bytes.

0 -255 or 0-1023 is usually enough range for me from a pot but I do need accurate end points. I hate the dead top end on an ESP which only does 0 - 3.2 volts rather than the full range 0 - 3.3 volts.
looks like the error of 300 odd is because of the shifting.

Any ideas on the required libraries or do I have to use CircuitPython?