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.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)

    while True:
            if GPIO.input(Switch) == False:
                    GPIO.output(Motor, True)
                    GPIO.output(LED, True)

            if GPIO.input(Switch) == True:
                    GPIO.output(Motor, False)
                    GPIO.output(LED, False)

except KeyboardInterrupt:
    print "CTRL-C pressed.  Program exiting...Exited"

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.

Circuit diagram

  • Perhaps you could add a photo of your circuit?
    – Darth Vader
    Apr 25, 2017 at 20:07

1 Answer 1


This script will toggle the motor on and off.

It uses my pigpio but you can do the same with RPi.GPIO.

The pigpio daemon needs to be running (sudo pigpiod).

#!/usr/bin/env python

import pigpio
import time

LED = 20
Motor = 22
Switch = 24

Motor_On = False

def callback(gpio, level, tick):
   # Toggle motor on and off.
   global Motor_On
   Motor_On = not Motor_On
   pi.write(LED, Motor_On)
   if Motor_On:
      pi.set_PWM_dutycycle(Motor, 200) # 0 is off, 255 is fully on.
      pi.set_PWM_dutycycle(Motor, 0) # 0 is off, 255 is fully on.

pi = pigpio.pi()
if not pi.connected:

pi.set_mode(LED, pigpio.OUTPUT)

pi.set_PWM_dutycycle(Motor, 0) # 0 is off, 255 is fully on.

pi.set_mode(Switch, pigpio.INPUT)
pi.set_pull_up_down(Switch, pigpio.PUD_UP)

cb = pi.callback(Switch, pigpio.FALLING_EDGE, callback)

   while True:

except KeyboardInterrupt:
    print("\nCTRL-C pressed.  Program exiting...Exited")

pi.set_mode(LED, pigpio.INPUT)
pi.set_mode(Motor, pigpio.INPUT)
  • Hi Joan, thank you for your reply - much appreciated. i have tried this code but it is only running the motor for a split second after pressing the switch instead of keeping it on constantly.
    – addyc1986
    Apr 25, 2017 at 21:54
  • The first button press starts PWM on the motor GPIO. The second button press stops PWM. Does your circuit work with PWM? You can view the GPIO outputs with piscope.
    – joan
    Apr 26, 2017 at 1:16
  • The circuit currently does and i was hoping to go more into depth with it once this part is figured out. I will give piscope a go, thank you for the recommendation
    – addyc1986
    Apr 26, 2017 at 17:35
  • @addyc1986 As a quick test you could change the 200 to 255 above. That would be the equivalent of your current code which is full on or full off.
    – joan
    Apr 26, 2017 at 17:39
  • Hi Joan, i think i may have tried that last night, will confirm later on
    – addyc1986
    Apr 26, 2017 at 17:41

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.