on or off of 16 relay with 16 push button switch with mcp23017

Moderators: grovkillen, Stuntteam, TD-er

Post Reply
Message
Author
esptita
New user
Posts: 6
Joined: 24 Mar 2022, 16:36

on or off of 16 relay with 16 push button switch with mcp23017

#1 Post by esptita » 24 Mar 2022, 17:00

Hello how are you? I am modernizing my house little by little, now I have a 4ch relay module working + mcu8266 module + esp easy firmware + inventor app, everything works perfectly from my local network from the browser or with my cell phone, what I want now is to use a relay module 16 relay and be able to control them with 16 switch buttons as input using the mcp23017 module that communicates with 2 GPIO of the mcu8266. Now I am controlling the 16ch relay module using the pca9685 module as output and I manage to control it perfectly through the web browser, but I do not understand how to use and connect the mcp23017 module as input of the 16 switch buttons, first of all I want to tell you that I am a beginner and I don't know anything about programming rules in esp easy, if you can help me with the steps to follow with the procedure to be able to turn a relay off or on with the switch button using the mcp23017 module, I would appreciate it

I leave an image of the relay module
:oops:
Attachments
D_NQ_NP_645382-MLA31882760661_082019-O.jpg
D_NQ_NP_645382-MLA31882760661_082019-O.jpg (70 KiB) Viewed 7330 times
D_NQ_NP_645382-MLA31882760661_082019-O.jpg
D_NQ_NP_645382-MLA31882760661_082019-O.jpg (70 KiB) Viewed 7330 times

User avatar
Ath
Normal user
Posts: 3416
Joined: 10 Jun 2018, 12:06
Location: NL

Re: on or off of 16 relay with 16 push button switch with mcp23017

#2 Post by Ath » 25 Mar 2022, 14:30

Assuming this is an ESPEasy related question, this post should have been in the ESPEasy sections of the forum, not in Staff announcements...
{@moderators: Please move this thread to the appropriate (sub)forum.} Moved, thanks.

On topic:
To be able to use Rules, you will have to enable that option in the Tools/Advanced page (don't forget to submit the page after enabling that checkbox)

For using the Keypad - PCF8574/MCP23017 plugin, that's best suited for handling that amount of buttons and have some room to extend, you have to use an ESPEasy build that has TEST in the name (any from A..E would suffice), as that contains this plugin.
Documentation is not quite up to par yet, until PR #3927 is merged, so I've attached a pdf with the proposed content (also including some new features, introduced in that PR) You don't need to use a keypad for use as switches, but it could have some advantages ;)

After configuring the device, you need to add a rule in Rules to map the buttons to the relays, that would look like this:

Code: Select all

on Keypad#ScanCode do
  LogEntry,"Key ScanCode: %eventvalue1%"
  Let,1,-1 // No key pressed
  if %eventvalue1%=10
    Let,1,0 // PCA Port nr
  elseif %eventvalue1%=11
    Let,1,1 // PCA Port nr
  // add more elseif clauses when needed
  endif
  // Valid key?
  if [var#1]>-1
    PcfGpioToggle,[var#1] // Flip output state
  endif
endon
That rule (event) will be executed when a button is pressed (ScanCode=nn) or released (ScanCode=0), and toggle the corresponding pin 0..15 for the PCA9685 (assumed to use the first I2C address 0x40)

In the log output (via serial or the Tools/Log feature) you can see what scancode is used for which button, that depends on the wiring, so a logentry is in the script to show that.
Last edited by Ath on 26 Mar 2022, 09:17, edited 1 time in total.
/Ton (PayPal.me)

TD-er
Core team member
Posts: 8643
Joined: 01 Sep 2017, 22:13
Location: the Netherlands
Contact:

Re: on or off of 16 relay with 16 push button switch with mcp23017

#3 Post by TD-er » 25 Mar 2022, 20:49

I moved the topic to a more appropriate sub forum.

User avatar
Ath
Normal user
Posts: 3416
Joined: 10 Jun 2018, 12:06
Location: NL

Re: on or off of 16 relay with 16 push button switch with mcp23017

#4 Post by Ath » 26 Mar 2022, 09:15

esptita wrote: 26 Mar 2022, 04:10 1 how do I have to connect the two modules pca9685 and mcp23017, making the SCL and SDA pins parallel?
Yes, I2C devices are connected with parallel wiring to SDA and SCL.
One point to take into account is that, usually, these boards all have pull-up resistors on their sda and scl inputs, so connecting several (4 or more) in parallel will result in a too low value in the combined pull-ups. Then only on the last board in the chain the pull-ups should remain, so this may involve de-soldering them from the inbetween boards. But as you have only 2 boards connected, and the pull-ups are usually 10k, the nett result will be 5k, and that isn't considered too low.
esptita wrote: 26 Mar 2022, 04:10 2 How do I integrate the mcp32017 module into esp easy?
As already stated above, you need to upgrade your MCU with one of the TEST_A..E builds of ESPEasy, as that has the plugin for MCP23017 (keypad) included.
/Ton (PayPal.me)

User avatar
Ath
Normal user
Posts: 3416
Joined: 10 Jun 2018, 12:06
Location: NL

Re: on or off of 16 relay with 16 push button switch with mcp23017

#5 Post by Ath » 26 Mar 2022, 11:58

If you want to connect all buttons separately to a MCP23017 pin (momentary push-button or on/off flip switch, connecting the pin to GND), you can also use these rules:

Code: Select all

on System#Boot do // At startup of the ESP, activated once
  MonitorRange,mcp,0,15 // 16 pins
endon

// When using momentary push-buttons use "on mcp#0=0 do", "on mcp#1=0 do" etc., to respond to push-down only, not again on release
// When using on/off flip switches, use "on mcp#0 do", "on mcp#1 do" etc., to respond to each flip, on and off
on mcp#0=0 do
  PcfGpioToggle,0 // Mapping is 1:1, mcp pin 0 to pcf pin 0, toggle output-pin state
endon
on mcp#1=0 do
  PcfGpioToggle,1
endon
// copy/paste/adapt for all mcp input- and pcf output-pins
/Ton (PayPal.me)

esptita
New user
Posts: 6
Joined: 24 Mar 2022, 16:36

Re: on or off of 16 relay with 16 push button switch with mcp23017

#6 Post by esptita » 27 Mar 2022, 17:33

1.357 / 5.000
Resultados de traducción
Hello friends how are you? I was trying the instructions they gave me, but the mcp23017 module is not working, only the pca9685 module works, which when sending the browser example 192.168.0.5/control?cmd=pcapwm,15,4095 or 192.168.0.5/control?cmd=pcapwm ,15,0 The control of the relay works perfectly, but when I want to control it manually by sending GND to one of the pins of the mcp23017 module, it does nothing at all in the relay, I only observe data that enters the esp easy, but it does not make any changes mechanical in the 4ch relay module. I ask you to look at the photos that I leave so that you can analyze how I connect it and the esp easy data.
questions
1 in esp easy is the mcp23017 correctly configured? I leave the photo.
2 in the rules part, I leave the photo, is the code they gave me correct? Do I have to put all the complete code? or just a part of said code? I clarify that I want to use switch buttons that when pressed the relay is activated, and when pressed again, the same relay is deactivated, so with the 16 switch buttons with the 16 relay module that I plan to use.
3 that module that I bought, is the mcp23017 the correct one? because they say that it is for LCD display, but that it also works for this purpose, only the VCC, GND, SCL and SDA pins are connected in parallel.
Thank you very much from Argentina.
Attachments
mcp23017 module with the white cable that is GND to test the different pins of the chiche module
mcp23017 module with the white cable that is GND to test the different pins of the chiche module
IMG_20220326_195512754.jpg (584.01 KiB) Viewed 7195 times
IMG_20220326_201241110.jpg
IMG_20220326_201241110.jpg (689.33 KiB) Viewed 7195 times
IMG_20220326_194651264.jpg
IMG_20220326_194651264.jpg (649.75 KiB) Viewed 7195 times
IMG_20220326_194726335.jpg
IMG_20220326_194726335.jpg (670.79 KiB) Viewed 7195 times
IMG_20220326_194404968 (4).jpg
IMG_20220326_194404968 (4).jpg (1.21 MiB) Viewed 7195 times

User avatar
Ath
Normal user
Posts: 3416
Joined: 10 Jun 2018, 12:06
Location: NL

Re: on or off of 16 relay with 16 push button switch with mcp23017

#7 Post by Ath » 27 Mar 2022, 17:54

To be able to control the 3rd and 4th relay, you should also add these events:

Code: Select all

on mcp#2=0 do
  PcfGpioToggle,2
endon
on mcp#3=0 do
  PcfGpioToggle,3
endon
And they should toggle state when you close any of PA0..PA3 to GND.
I have tested this, but I also saw the stray events for the MCP pins that are unused when handling the MCP23017 board, so most likely that's caused by the high impedance of these pins, so you may need to add lower value pull-up resistors like 10k or 4k7.

Also, I haven't explicitly set the mcp pins to input, but I would expect that the monitor command does that (didn't check that though).

To avoid all the extra events for the unused mcp pins, you can change the MonitorRange command to MonitorRange,mcp,0,3 while testing with 4 relays.
/Ton (PayPal.me)

User avatar
Ath
Normal user
Posts: 3416
Joined: 10 Jun 2018, 12:06
Location: NL

Re: on or off of 16 relay with 16 push button switch with mcp23017

#8 Post by Ath » 27 Mar 2022, 17:57

And to see if the event is actually triggered, you could also add a logentry line like this:

Code: Select all

on mcp#0=0 do
  LogEntry,"MCP PA0 toggle PCF pin 0"
  PcfGpioToggle,0
endon
That should be visible in the log output when connecting PA0 to GND
/Ton (PayPal.me)

esptita
New user
Posts: 6
Joined: 24 Mar 2022, 16:36

Re: on or off of 16 relay with 16 push button switch with mcp23017

#9 Post by esptita » 29 Mar 2022, 04:52

hello, how are you, I was adding the code you sent me, but it still doesn't change the relays, I sent GND to all the pins of the mcp23017, but it doesn't do anything at all, there is only activity in the esp easy of the events, I'm going tomorrow to add the resistors that you told me, I have to add the resistors to each pin of the mcp23017 to GND???.
I'm sending you a photo of the code, correct me if it's correct since I'm very lost in programming, I'm an apprentice.
thanks my friend.
Attachments
IMG_20220328_230203105.jpg
IMG_20220328_230203105.jpg (1.66 MiB) Viewed 7111 times
IMG_20220328_230153085_HDR.jpg
IMG_20220328_230153085_HDR.jpg (1.19 MiB) Viewed 7111 times

User avatar
Ath
Normal user
Posts: 3416
Joined: 10 Jun 2018, 12:06
Location: NL

Re: on or off of 16 relay with 16 push button switch with mcp23017

#10 Post by Ath » 29 Mar 2022, 07:47

1)
Looking at the log I would say it is working as intended. If the relays are not responding to the changed state, then I'm interested to know if they change state if you manually (on the Tools page) submit this command: pcfgpiotoggle,3
If then the relay state doesn't change you have a wiring issue on the PCF side.

2)
You now have twice the event handler "on mcp#0=0 do" in your code. My intent was that *only* the "LogEntry" line was to be added to the existing "on mcp#0=0 do" event handler. By having it twice, it will be executed twice, so the end-result will be the relay keeps the same state...

3)
a) Would it be possible to not translate the log entries?, as for non-Spanish speaking people, that's quite confusing to read. And the text can't be selected/copy-pasted into a translator
b) Instead of screen-photo's, can you use the screen-shot functions available in Windows, or just copy/paste the logs into a [ code ] block in the forum, to share your information, if only for better readability
/Ton (PayPal.me)

esptita
New user
Posts: 6
Joined: 24 Mar 2022, 16:36

Re: on or off of 16 relay with 16 push button switch with mcp23017

#11 Post by esptita » 30 Mar 2022, 06:13

hello
When I enter the code pcfgpiotoggle,3 in the command page it shows me the following, but it does not make any changes in the relay.
{
"log": ": port#3: set to 0",
"plugins": 19,
"pin": 3,
"mode": "output",
"state": 0
}

However, when I enter the command in the google browser in the address bar, the following is taken; http://192.168.0.104/control?cmd=pcapwm,2,4095 the relay works perfectly.
then something is wrong that prevents the pca from communicating with the mcp.

the physical electrical connections are as follows on the pca9685-mcp23017 board and the mcu8266; the SCL and the SDA are in parallel in the mcp and the pca.
after the mcu8266 SDA: GPIO-4 (D2)
SCL: GPIO-5 (D1) to pca9685 board.
pca and mcp are powered by the mcu8266 with the 3.3v and GND in common on the 3 boards and also with the 4ch relay module.

1 question, in the code that you gave me I see the word PCF, and I have the MCP module, I don't know if that has something to do with it.

User avatar
Ath
Normal user
Posts: 3416
Joined: 10 Jun 2018, 12:06
Location: NL

Re: on or off of 16 relay with 16 push button switch with mcp23017

#12 Post by Ath » 30 Mar 2022, 07:48

esptita wrote: 30 Mar 2022, 06:13 hello
When I enter the code pcfgpiotoggle,3 in the command page it shows me the following, but it does not make any changes in the relay.
{
"log": ": port#3: set to 0",
"plugins": 19,
"pin": 3,
"mode": "output",
"state": 0
}
That looks as it should, but still, the relay isn't working. Have you tried to toggle pin 2 instead of 3? (as you are using pcapwm,2,4095 below)
esptita wrote: 30 Mar 2022, 06:13 However, when I enter the command in the google browser in the address bar, the following is taken; http://192.168.0.104/control?cmd=pcapwm,2,4095 the relay works perfectly.
then something is wrong that prevents the pca from communicating with the mcp.
I don't understand why you use a pcapwm command to "switch on" a pin, that should normally be a pcfgpio or pcfgpiotoggle, I think, for switching on/off a pin.
esptita wrote: 30 Mar 2022, 06:13 the physical electrical connections are as follows on the pca9685-mcp23017 board and the mcu8266; the SCL and the SDA are in parallel in the mcp and the pca.
after the mcu8266 SDA: GPIO-4 (D2)
SCL: GPIO-5 (D1) to pca9685 board.
pca and mcp are powered by the mcu8266 with the 3.3v and GND in common on the 3 boards and also with the 4ch relay module.
The problem is in the connection to the relays, not in connecting the boards to the ESP.
esptita wrote: 30 Mar 2022, 06:13 1 question, in the code that you gave me I see the word PCF, and I have the MCP module, I don't know if that has something to do with it.
The PCA* and PCF* commands are somewhat intertwined, they both control the PCF board.
/Ton (PayPal.me)

esptita
New user
Posts: 6
Joined: 24 Mar 2022, 16:36

Re: on or off of 16 relay with 16 push button switch with mcp23017

#13 Post by esptita » 30 Mar 2022, 18:52

Hello, I tell you the following
1 That looks like it should, but still, the relay doesn't work. Have you tried toggling pin 2 instead of 3? (since you are using pcapwm, 2 , 4095 below).

1 yes, I already alternated pin 1/2/3 and it does not make any physical changes in the relay, only changes in the esp easy.

2 I don't understand why you use a pcapwm command to "turn on" a pin, which should normally be a pcfgpio or pcfgpiotoggle I think, to turn a pin on/off.

I use the following commands in the google address bar on the one hand to prove that it does indeed make a physical change in the 4 relays of the module, and on the other hand I use that IP address to enter the inventor APP and be able to control it with my cell phone The on and off through APP inventor that in that sense if the control of the 4 relay works perfectly. what I wanted now was to add a manual on or off using switch buttons by sending GND to one of the pins of the mcp23017. but it seems that I am doing something wrong.

3 the physical electrical connections of the 4 modules is as follows:
1 of the mcu8266 using D1 and D2, to the SCL and SDA input of the PCA, and in parallel the same pins to the input of the MCP and the 4ch relay module connected to the output pins of the PCA9685. There are photos that I uploaded that show the 4 modules connected.
4 I already ordered the PCF8574 module and I am going to replace it with the MCP that maybe is defective, I don't know.
thanks for the support.

pallago
New user
Posts: 1
Joined: 27 Mar 2023, 23:46

Re: on or off of 16 relay with 16 push button switch with mcp23017

#14 Post by pallago » 27 Mar 2023, 23:54

Hello everybody,
thank you very much for this important information:
// When using momentary push-buttons use "on mcp#0=0 do", "on mcp#1=0 do" etc., to respond to push-down only, not again on release
// When using on/off flip switches, use "on mcp#0 do", "on mcp#1 do" etc., to respond to each flip, on and off
Since the "Switch input - MCP23016" does not support the button type "push button active high" / "push button active low" maybe you could add this information to
It took me almost 3h to find this information in order to control my relays by the momentary push buttons. My relays and my momentary push buttons are both connected to the MCP23016. It works very smoothly.

Post Reply

Who is online

Users browsing this forum: Bing [Bot] and 16 guests