Control 2 relays with 1 button

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
Post Reply
slowcheetah
Posts: 2
Joined: Mon May 01, 2017 10:35 am

Control 2 relays with 1 button

Post by slowcheetah » Mon May 01, 2017 11:10 am

I'm trying to mimic the behaviour of a garage door control board as follows:

relay_1 drives a motor in one direction
relay_2 drives it in the opposite direction

I need to be able to push a button once to set it in motion and when I press it again it needs to stop. A third press should drive the motor in the opposite direction. This sounds so simple but I'm a little lost, so I would appreciate a nudge in the right direction.

I have the following main.py file running on a NodeMCU V2 board. Currently the code does the following:
- All relays are off initially
- When I press the button the motor goes one way. When I press it again it goes the other way.

How can I have it stop every 2nd button press? Similar to how any gate motor driven my a remote pulse.

Existing code is here:
https://pastebin.com/XRMUSEQP

thetreerat
Posts: 15
Joined: Thu Apr 27, 2017 6:40 pm

Re: Control 2 relays with 1 button

Post by thetreerat » Mon May 01, 2017 1:51 pm

you need to make the button that controls the relay a virtual button. Think a variable that holds the state, and it flips thur your list of opptions. fowared, off, reverse, start over. the state change happens when your button is pressed.

if you look at peter repo: https://github.com/peterhinch/micropython-async he implants a virtual button

you can also look at https://github.com/thetreerat/WaterPump ... WaterPumps
look at buttons.py I don't explain it as well as peter project. but it was derived from his examples.

def returnNextState(self,current):
"""Toggle state"""
state = None
if current=='forward':
state==OFF
elif current=='OFF'
state='reverse'
elif current=='reverse':
state='off'
elif current=='off':
state='forward'
return state

thetreerat
Posts: 15
Joined: Thu Apr 27, 2017 6:40 pm

Re: Control 2 relays with 1 button

Post by thetreerat » Mon May 01, 2017 2:08 pm

sorry this would work better and you use it in place of tagglestate method in the buttons.py

class states(object):
def __init__(self,states=False):
if states:
self.states = ['forward','off','reverse']
else:
self.states = states
def NextState(self):
"""Toggle state"""
state = states.pop()
self.states.insert(0,state)
return state

User avatar
dhylands
Posts: 3821
Joined: Mon Jan 06, 2014 6:08 pm
Location: Peachland, BC, Canada
Contact:

Re: Control 2 relays with 1 button

Post by dhylands » Mon May 01, 2017 8:35 pm

Normally, you would use a DPDT realy to control direction and a separate relay to control on/off.

Here's an example wiring where they use one of the relays for direction and a transistor for on/off (replace the transistor with a relay.
http://www.me.umn.edu/courses/me2011/ar ... Motor.html

I don't recommend using one relay for one direction and one relay for the other, since if they both get turned on at the same time, you've generally shorted something out and bad things happen.

Also be aware that you need to debounce your switches. Pressing the switch can (and will) make multiple fast transitions.
http://www.ganssle.com/debouncing.htm

User avatar
pythoncoder
Posts: 5956
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: Control 2 relays with 1 button

Post by pythoncoder » Tue May 02, 2017 5:45 am

The code referenced by @thetreerat https://github.com/peterhinch/micropython-async handles debouncing of switches and pushbuttons.
Peter Hinch
Index to my micropython libraries.

Post Reply