0

I am trying to write a loop to read pin 14 on a pi pico. Normally the pin will be 0 (Low) but when it goes 1 (High) I would like to exit the while loop and continue with the rest of the script. I have it running and it is printing "HELLO" but when I put a high on pin 14 it doesn't "break". What I have just for this while portion is as follows:

import machine
from machine import Pin
import utime
from machine import Timer

#Create input for Pin 4
p14 = Pin(14, Pin.IN)

#Set Pin 14 Value Low Initially
#p14.value(1)

#(Section is for reading Input High on Pin 4)
check = (p14.value())
print(p14.value())
while check != 1:
  check2 = (p14.value())
  print("HELLO")
  if check2 == 1:
      print("Pin 14 went HIGH")
      break

I've tried numerous iterations of while but still cannot get this to work.

2
  • I have no logs. And I never get Pin 14 went high message only the constant scrolling HELLO even when I jumper pin 36 (3.3VDC) to pin 19 (GPIO 14)
    – Brian
    Commented May 14, 2023 at 5:08
  • 2
    You read the pin and assign it to check outside of the loop. You test check every time through the loop, but you never update it. Simplify the loop, get rid of all the 'check's and test the pin value directly in the while condition. Also, you might want to use Pin(14, Pin.IN, Pin.PULL_UP) Have fun!
    – aMike
    Commented May 14, 2023 at 12:57

1 Answer 1

0

You could try and set up a call back so that when the pin changes state, an action occurs:

from machine import Pin

interrupt_flag=0
pin = Pin(5,Pin.IN,Pin.PULL_UP)
def callback(pin):
    global interrupt_flag
    interrupt_flag=1

pin.irq(trigger=Pin.IRQ_FALLING, handler=callback)
while True:
    if interrupt_flag is 1:
        print("Interrupt has occured")
        interrupt_flag=0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.