[Python] laser turns on without input

Discussão em 'Python' iniciado por Stack, Maio 30, 2025.

  1. Stack

    Stack Membro Participativo

    i have a setup with a microcontroller where pushing a button i attached with gpio pins turns on a laser pointer using gpio pins. this is part of a larger project including speakers and other leds and buttons etc, so even if i do just hook them up directly and add a battery, the issue would just move to other components. the issue is that about every second it turns its self on and then off the next second. unless i am massively blind and stupid, this isnt a code issue, but here is the code anyway:

    import digitalio
    import board
    import time

    laser = digitalio.DigitalInOut(board.GP25)
    laser.direction = digitalio.Direction.OUTPUT

    button = digitalio.DigitalInOut(board.GP3)
    button.direction = digitalio.Direction.INPUT

    led = digitalio.DigitalInOut(board.LED)
    led.direction = digitalio.Direction.OUTPUT

    while True:
    if button.value == True:
    laser.value = True
    else:
    laser.value = False

    time.sleep(0.1)
    led.value = True
    time.sleep(0.1)
    led.value = False

    print(str(laser.value) + str(time.time()))



    (written in circutpython. if there are typos, i manually copied it from my pi to here. long story. to be clear im using the pi to edit it and run on a couple microcontrollers im not running it on my pi directly)

    when the laser is on, the code reads out that the value of button is True.

    there is also a problem where if i push the side of the pins sometimes it causes it to read true too.

    i soldered 3 different microcontrollers thinking it was a soldering problem. 1 raspberry pi pico and 2 xiao seeed 2040. the last xiao board i did did not have the problem where pushing pins caused it to read true, but it did cycle through true false every 2 seconds.

    Continue reading...

Compartilhe esta Página