-1

I am working on a project, with MicroPython.

Hardware: LoPy 4 + 3.0 expansion board, 1 button, 1 led, 2 resistors (1k)

Schematic (Red wires are P13 and P15)

And my code:

import machine
from machine import Pin
import time


button1 = "P13"

LED = "P15"

button_1 = Pin(button1, mode=Pin.IN)
led = Pin(LED, mode=Pin.OUT)

def button_press():
    while True:
        if button_1.value():
            led.value(1)
            print('Button pressed...')
            print('Sensor is HIGH')
            time.sleep(0.5)

            
def main():
    button_press()


if __name__ == "__main__":
    main()

Problem: For some reason, I don't know if it is an hardware issue or a problem with my code. But I already get values without pressing the button

So for some reason I already get the print without pressing the button. And I can't figure it out, does someone know a solution?

Thanks in advance?

Zino
  • 1
  • 4

1 Answers1

0

4 things:

  1. You should init your peripherals pulled in a certain direction. Since your button is connected to ground you should init it as pulled up. Since your led is also tied to ground you should init it as pulled down. If you pulled it up then it would complete the connection, turning it on.

  2. Since your button is pulled up and pressing it will tie it to ground, you should check if not button1.value(). Ground is zero and pressing your button is going to "make a zero".

  3. You don't need import machine. You are importing Pin from machine right after. Basically, your code says "import all of machine and also import just Pin from machine". You only use Pin so the first import isn't necessary. Actually you are importing all of time but only using sleep. You should change that line to from time import sleep_ms, and change time.sleep(0.5) to sleep_ms(500).

  4. There's no need to store those pin IDs. Especially if you are going to store the ID on button1 and the button as button_1. You're begging for headaches.

Aside: Good job on the if __name__ == '__main__' but you may want to consider that your program is stuck in a loop that it can never leave. The only thing you can do is press a button. You might want to look into threading if your device supports it or using interrupts if it doesn't.

from machine import Pin
from time import sleep_ms

btn = Pin('P13', Pin.IN, Pin.PULL_UP)
led = Pin('P15', Pin.OUT, Pin.PULL_DOWN)

def button_press():
    while True:
        if not btn.value():
            led.value(1)
            print('Button pressed...')
            print('Sensor is HIGH') #technically your button is low
            sleep_ms(500)

            
def main():
    button_press()


if __name__ == "__main__":
    main()
Delrius Euphoria
  • 14,910
  • 3
  • 15
  • 46
OneMadGypsy
  • 4,640
  • 3
  • 10
  • 26
  • It is the same result, it keeps printing without pressing and stops printing if I press it. And the LED won't go HIGH. It is a good code tho! because it is more optimized. But I think maybe it is something with my schematic – Zino Apr 02 '21 at 14:17
  • @Zino are you sure you have the button connected to the negative rail like the schematic shows? If not, either connect it to negative, or flip the code to pull the button down and check `button.value()` without `not`. Also, take the wire going from the LED to the microcontroller and plug it directly into the positive rail. In other words, make sure it works. Make sure the LED isn't backwards, as-well. – OneMadGypsy Apr 02 '21 at 14:38
  • UPDATE: I had to change the pins, now it works (P20-P21) Thanks for the help! – Zino Apr 02 '21 at 14:50