Does anyone happen to have a similar example for a python code when pressing a button a relay is turned on which is powering a motor to open a gate for 5 seconds and then closes by giving a signal to another relay which is connected with opposite positive and negative to the motor to close the gate (  Relay Diagram this picture shows how i an going to try and connect the motor and relays) would i also be possible to add motion detection so if there would be motion the second relay isn't powered so the gate wouldn't close


  • I am sure that googling "python relay button" would give you a good starting place..... Then if you get stuck on a specific problem, you can post your code and schematic and ask another question.
    – Chad G
    Commented May 4, 2018 at 15:44
  • Nothing to do with the Pi.
    – joan
    Commented May 4, 2018 at 18:24

2 Answers 2


Your design has some shortcommings

  • Thank you LotPings for the response, yes i noticed that and just purchased 2 relays that work with 3.3v which come out of the GPIO. do you think i'll managed to make it work with these ? ebay.co.uk/itm/…
    – Matthew
    Commented May 5, 2018 at 10:14
  • Can't say anything about unknown components, If your specs are correct you can only try and if it fails still better the design.
    – LotPings
    Commented May 5, 2018 at 10:26
  • true i am going to try them out and see what happens, you wouldn't have a similar code as i'm new to this subject
    – Matthew
    Commented May 5, 2018 at 11:13

DO NOT TRY THE 3.3V realays with the rpi without a transistor. Switching a relay on requires quite a bit of current, and gpio pins can supply very little current(I think less than 8ma). if you try to operate a relay with a raspberry pi gpio pin, you will most likely break the gpio pin(and possibly other parts as well) on your pi. The solution is to use a transistor, as LotPings mentions. A transistor works by taking a very small current and using it to switch a much larger current. There is no way around using a transistor and diode to control a relay with the rpi, unless your relay is a relay module with a transistor already built in. As for the code, use python's rpi.gpio library. It is really simple. To get you started, google raspberry pi rpi.gpio hello world

Hope this helps, and let me know if you have any questions!

Not the answer you're looking for? Browse other questions tagged or ask your own question.