MCP23008 turn off ralay

hi, i want to activate a relay with a push button with this partial code

 buttonState1 = digitalRead(swSubirc);

if ( buttonState1==LOW)

{
 subirCristal();

}

 

void subirCristal(){

 


  Wire.beginTransmission(Addr);
  Wire.write(0x09);
  Wire.write(0x01);
  Wire.endTransmission();
    buttonState1 = digitalRead(swSubirc);
}

the question is, how can i turn off the relay, i need that only work while the button is pressed.

 

 

 

 

T

Hi,

So you want:

press down on the button = relay on

release button = relay off

?

Mike Sum's picture

Yes

T

I would take a look at Particle's Interrupt functionality here:

https://docs.particle.io/reference/firmware/photon/#interrupts

If you attach the interrupt on change you can run a short snippet of code any time the input state changes.  In this snippet simply evaluate the state of the input(Button pressed or released) then send the command.

Turn on relay:

Wire.beginTransmission(Addr);
  Wire.write(0x0A);
  Wire.write(0x01);
  Wire.endTransmission();

Turn off relay:

Wire.beginTransmission(Addr);
  Wire.write(0x0A);
  Wire.write(0x00);
  Wire.endTransmission();

Also we have libraries for the controller on Particle's Build IDE.  Just search community libraries for NCD1Relay, NCD2Relay, NCD4Relay, NCD8Relay, etc.  Then you don't have to mess with Wire.  Just call controller.turnOnRelay/controller.turnOffRelay

Let me know if this helps.