I've begun tinkering with using electronics and am also new to using Python. I was wondering if someone could help me please?
What I'm trying to achieve is to use an on/off switch to run a motor - the first press of the switch turns the motor on and keeps it running, then the second press turns the motor off.
I have tried the below code but all it does is run the motor if I hold the switch on, and then turns off when I let go of the switch.
import RPi.GPIO as GPIO import time GPIO.setwarnings(False) GPIO.setmode(GPIO.BCM) GPIO.setup(20, GPIO.OUT) # LED GPIO.setup(22, GPIO.OUT) # Motor GPIO.setup(24, GPIO.IN, pull_up_down=GPIO.PUD_UP) # Switch Button LED = 20 Motor = 22 Switch = 24 p = GPIO.PWM(22,100) p.start(0) try: while True: if GPIO.input(Switch) == False: p.ChangeDutyCycle(100) GPIO.output(Motor, True) GPIO.output(LED, True) if GPIO.input(Switch) == True: p.ChangeDutyCycle(100) GPIO.output(Motor, False) GPIO.output(LED, False) except KeyboardInterrupt: print "CTRL-C pressed. Program exiting...Exited" GPIO.cleanup()
I'm away from home at the moment but I've tried to knock up a quick circuit diagram. There is a resistor and diode inline with the transistor but the general gist is there.