[SOLVED]Push button to activate relay shield

I need to wire in a manual push button into my setup to activate a relay on the relay shield. This would be for exiting the building, you would just press the “EXIT” button and the relay would activate for a given amount of time.

I have the button defined as “D7”

I won’t post the entire code for my project, its long and everything else is working, but here are the pertinent parts for this addition:

void setup() {
Particle.subscribe("xxxxx", myHandler, MY_DEVICES);
pinMode(relay, OUTPUT);
pinMode(rex, INPUT_PULLUP);
  if (digitalRead(rex) == LOW){
      digitalWrite(relay,HIGH);
      delay(5000);
      digitalWrite(relay,LOW);
  }

I have tried making pinmode “rex” INPUT_PULLUP, and INPUT_PULLDOWN, and obviously changing the digitalRead state as well, and nothing changes, even when jumping straight across with a jumper wire. Any idea as to what I may be doing wrong?

1 Like

What does your D7 LED do when you press or release the button.

When your button is closing to GND you need INPUT_PULLUP and when closing to 3v3 you need INPUT_PULLDOWN

2 Likes

D7 comes on when I have it in INPUT_PULLDOWN and jump from VIN to D7, and is on all the time and goes out when under INPUT_PULLUP and I jump across D7 and GND

When you have internal pull-resistors you are NOT allowed to connect to Vin - hence I said 3v3.
https://docs.particle.io/datasheets/photon-datasheet/#peripherals-and-gpio

BTW, have you tried another pin? This would free the onboard LED to give you code controlled feedback about the digitalRead(rex) results.

1 Like

I haven’t tried another pin, D2 and D3 are being used, D4 and D5 are first the other relays, and I wasn’t sure about D0 and D1 being for I2C use, I know that messes with my wiegand code, so I just stayed away until I could get everything working. Good to know about the pull_up and VIN issues, should have looked deeper in the docs. I’ll try running to 3v3 and post back.

You can use D6 and any of the A-pins too.

I’m using D6 already, I’ll use one of the A pins. In the meantime, I’m having an issue getting into the Web IDE to change the pinmode. I’m getting an application error from something called Heroku, I guess I should send in a support ticket, and wait for a response.

Update - build.particle.io and login.particle.io are now affected by the AWS outage.
Feb 28, 11:17 PST

Identified - Several of our developer tools, including docs.particle.io, console.particle.io, and setup.particle.io, are currently down due to an unconfirmed outage with AWS. build.particle.io is hosted elsewhere and does not appear to be impacted.
Feb 28, 09:52 PST

http://status.particle.io

1 Like

Thanks @Rickkas7, I’ll keep looking for updates.

@ScruffR, in the meantime, I have this in pinmode INPUT_PULLUP for D7, so jumping to GND should work correct? The D7 light goes out, but still no relay activation.

Not seeing the context of your relay logic does not help. Hence I suggested to use D7 as software controlled indicator to find out if you get to that piece of code at all.
Adding some Serial.print() statements might help getting an idea of what’s going on in your code too.

1 Like

I’ll post the full code as soon as I can. Thanks @ScruffR

Here is the rest of the code for this, the Web IDE came back up a few minutes ago.
@ScruffR I put the rex back to INPUT_PULLDOWN with no change jumping to 3v3.
I also tried changing to A0, same result.

I see you have your button logic inside the subscription handler. So you need to have the button pressed before the event is fired and keep it pressed till this subscription has been processed. Is this intended?
I’d put that block in loop() to have the button checked regularly - at least while you are testing the button feature.

I didn’t realize I included it with the myhandler function, I’ll pull it outside of that and report back. Thank you.

@ScruffR, That did the trick! Sorry for all the simple problems, I bought my first photon last January and it was my first time ever writing any kind of code, so I’m a little behind the curve. Thanks for all the help.

You mean January 2016, or not?

Yea, January 2016, shortly before my daughter was born, so you can imagine how much time gets devoted to this haha.

1 Like

I know what you mean (three times :dizzy_face: )

1 Like