Relay activation via physical button using an MCP23017

Hello everyone .
Here I am to explain my little problem about my project. I have a NodeMcu with connected in output mode a GPIO multiplier to manage my 16ch relay using an MCP23017 output, since everything is working 100%. Now, however, I thought to insert another mcp23017 in input mode so as to enable or disable the relay via a physical button is to simultaneously display the status of the relay also sú smartphone. After several searches I could not solve my problem. By any chance, someone could kindly help me to set the NodeMcu code correctly in order to allow the relay to work even from physical buttons?

Thank you very much for your attention and I trust in your support.

You keep asking for code or hand holding in your posts… or in other’s posts :frowning:

We volunteer our help to assist you with your learning about Blynk, not how to program your unique hardware setups just because you use Blynk for part of it.

There are many websites that teach you programming or let you hire programmers… check out fiverr

Hi I was wondering only a hand of what I have to write in the code to ensure that Blynk detects the action of the button, I do not think to ask the whole project only the part of code in order for Arduino to detect input, even an exercise I ve well .

Your request doesn’t provide any real clarity or details of what you have done, can do, or are looking to do. Just a paragraph stating you have done something vaguely controlling relays and asking “help me to set the NodeMcu code correctly”.

Have you even added in your “mcp23017 in input mode so as to enable or disable the relay via a physical button” yet? You probably need to have all that in place before you could really sync it to Blynk in the first place.

As for a physical button or GPIO input syncing with a Blynk button… look at the example sketch for such and extrapolate from it.

https://examples.blynk.cc/?board=NodeMCU&shield=ESP8266%20WiFi&example=More%2FSync%2FSyncPhysicalButton

Only once you start showing actual details and progress, will others consider chipping in.

Thank you very much for the example you shared with me. We are managing relays via the Blynk application connected to an MCP23017. everything is functioning correctly from a smartphone. Now I had thought to insert the input buttons to activate the relays but as I have several physical buttons I wanted to put another mcp23017 in input all that dear Gunner

We understood that from your initial post, but whilst it’s easy(ish) to control multiple outputs via a multiplexer, is less straightforward to do it with inputs. If you can manage to get interrupts working with the multiplexer, then work our which multiplexed button was pressed, then it should work.
Otherwise, I doubt the polling the multiplexed pins on a frequent basis will work well with Blynk.

Maybe you can do some testing and share what you find so that others can benefit from your knowledge.

Pete.

Well here is the code that I am trying following the example of the link. But despite everything from the smartphone turns on and off the relay, instead of pulsed nothing works. I can not figure out where the error is if you can mipotete help. Thank you so much

#define BLYNK_PRINT Serial // Comment this out to disable prints and save space
#include <SPI.h>
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <SimpleTimer.h>
//#include <UIPEthernet.h>
//#include <BlynkSimpleUIPEthernet.h>
/**********************************************************************************/
#include <Wire.h>
#include <Adafruit_MCP23017.h> //https://github.com/adafruit/Adafruit-MCP23017-Arduino-Library
Adafruit_MCP23017 mcp;
Adafruit_MCP23017 mcp1;
Adafruit_MCP23017 mcp2;
//SimpleTimer timer;

char auth[] = "xxxxxxxx";
char ssid[] = "xxxx";
char pass[] = "xxxxx";
/********************************************************************************************************/
BLYNK_WRITE(V1) // V0 is the number of Virtual Pin For Read From BLYNK APP
{
int pinValue = param.asInt();

mcp.digitalWrite(0, pinValue);
Serial.print("pinValue0 = ");
Serial.println(pinValue);
}

// Set your LED and physical button pins here
const int relPin = V1;
const int butPin = V1;


void checkPhysicalButton();

int relState = LOW;
int butState = HIGH;

// Every time we connect to the cloud...
BLYNK_CONNECTED() {
  // Request the latest state from the server
  Blynk.syncVirtual(V0);

  // Alternatively, you could override server state using:
  //Blynk.virtualWrite(V2, ledState);
}

// When App button is pushed - switch the state
BLYNK_WRITE(V0) {
  relState = param.asInt();
  mcp1.digitalWrite(relPin, relState);
}
BlynkTimer timer;
void checkPhysicalButton()
{
  if (mcp1.digitalRead(butPin) == LOW) {
    // btnState is used to avoid sequential toggles
    if (butState != LOW) {

      // Toggle LED state
      relState = !relState;
      mcp1.digitalWrite(relPin, relState);

      // Update Button Widget
      Blynk.virtualWrite(V0, relState);
    }
    butState = LOW;
  } else {
    butState = HIGH;
  }
}
void setup()
{
Serial.begin(9600); // See the connection status in Serial Monitor
 Blynk.begin(auth, ssid, pass);
mcp.begin();for (uint8 pin = 0; pin  < 16; pin ++)
    {
       mcp.pinMode(pin, OUTPUT);
       mcp.pinMode(relPin, OUTPUT);
       mcp1.pinMode(butPin, INPUT_PULLUP);
       mcp1.digitalWrite(relPin, relState);
        
    }
timer.setInterval(100L, checkPhysicalButton);
}

void loop()
{
Blynk.run(); // Initiates Blynk
timer.run(); // Initiates SimpleTimer
}

At least read the Welcome Topic and learn a bit of forum protocol… like formating your posted code… if not searching and learning a bit before asking for others to troubleshoot your code for you :stuck_out_tongue_winking_eye:

I formatted your last post properly

Blynk%20-%20FTFC

1 Like

I’m not familiar with the Adafruit library for this multiplexer, but I can take a fairly good guess at how it works.
Your code will need to interrogate each of the multiplexed inputs, not just a single one. To do this you’ll almost certainly need to increment the pointer variable that you’re using (relPin) with a ‘for’ loop. Assigning V1 to that pointer almost certainly isn’t the right thing to do.

In addition, I can’t see how your output relays are being controlled effectively using this code either.

I’d start by simplifying the process and just reading the multiplexer pins one at a some without any Blynk or other code.

Pete.

The outputs are working correctly, in output I also use the I2C multiplexed, I do not understand the input because it does not want to work.

I don’t see any evidence in the code that you’ve posted of I2C control over 16 relays that mirror 16 virtual switch widgets.

Pete.

@Diego_La_Mattina, I’ve now bought a couple of these MCP23017 and spent some time figuring out how they work.
I’ve written it all up here, although it is a bit of a long read!..

Pete.