I have a strange situation.
I developed a project using the raspberry pi pico w and the pimoroni display pack 2. The project is basically a device which controls a small 5v motor with the press of the display pack 2 buttons. Works like a charm.
I tried re-creating the same project with some spare buttons I had laying around with everything else being the same.
The buttons on my project work fine if I print out a message or turn on an LED.
The problem I’m having is that I’m experiencing some phantom button presses when I hook up the same 5v motor and turn it on with the use of a button.
I do have a debounce and everything looks fine but as soon as I turn the little motor on, I get phantom presses non stop.
Fyi, I’m using the L293D motor driver to control the fan.
I have isolated one button and the motor from my project to test but I still see the same behavior.
I don’t understand why this does not happen when I connect the pimoroni display pack 2 (it may be something having to do with the display pack buttons)
I’ve been having a hard time with this for a few days now and haven’t found any answers online.
I hope someone here may be able to help me. Thank you in advance.
Wiring diagram and code below:
[Wiring diagram]
±------------------+
| Pico |
| |
Pin 2 - GP1 ---------- Button Pin
Pin 3 - GND ---------- Button Pin
Pin 8 - GND ---------- Board (-)
Pin 19 - GP14 -------- L293D Pin 7
Pin 20 - GP15 -------- L293D Pin 2
Pin 39 - VSYS -------- Board (+)
| |
±------------------+
±--------------+
| L293D |
| |
Pin 1 ----------- Board (+)
Pin 2 ----------- Pico GP15
Pin 3 ----------- FAN (+)
Pin 4 ----------- Board (-)
Pin 5 ----------- Board (-)
Pin 6 ----------- FAN (-)
Pin 7 ----------- Pico GP14
Pin 8 ----------- Board (+)
| |
±--------------+
[Code]
main.py
from fan import Fan
from machine import Pin
import time
fan = Fan()
last_clicked_time = 0
# interrupt callback
def button_handler(pin):
global fan
# Software debounce
global last_clicked_time
current = time.ticks_ms()
if time.ticks_diff(current, last_clicked_time) > 350:
print("Button pressed!")
fan.turn_on()
last_clicked_time = current
button = Pin(1, Pin.IN, Pin.PULL_UP)
button.irq(trigger=Pin.IRQ_FALLING, handler=button_handler)
print("Ready. Press the button...")
while True:
time.sleep(1)
print('running...')
fan.py
import machine
import time
class Fan:
def __init__(self):
self.motor1A = machine.Pin(14, machine.Pin.OUT)
self.motor2A = machine.Pin(15, machine.Pin.OUT)
def turn_off(self):
self.motor1A.low()
self.motor2A.low()
print('fan is off')
def turn_on(self):
self.motor1A.high()
self.motor2A.low()
print('fan is on')
def toggle(self):
if self.motor1A.value() == 0:
self.turn_on()
else:
self.turn_off()