ESP32 whit MCP23017 16Bit I/O Expansion conected to 16 channel relay module

Place your projects here
Post Reply
f1test
Posts: 15
Joined: Tue Nov 29, 2022 3:13 pm
Has thanked: 2 times
Been thanked: 11 times

ESP32 whit MCP23017 16Bit I/O Expansion conected to 16 channel relay module

Post by f1test »

1701628287399.jpg
Hello.

This program drives a 16-channel relay module coupled to the MCP23017 16-bit I/O expansion board module.
These modules are cheap and can be bought on AliExpress for a few euros.

The supply, 12V, is directly connected on the relay board, it is equipped with an LM2596. It supplies 5 V for the internal circuits of the relay board. The LM2596 has a maximum current of 3 A, so there is enough reserve left to power the ESP 32 directly from the relay board.
The MCP23017 module needs 4 mA / channel, that means a total of 64 mA, will be powered from the ESP 32 at a voltage of 3.3 V. In this way we ensure that each circuit receives the correct voltage and the logic level is compatible. For avoiding ground loop all grounds must be connected in the same place on the relay board


The source code:

Code: [Local Link Removed for Guests]

r1 = 1 : r2 = 1 : r3 = 1 : r4 = 1 : r5 = 1 : r6 = 1 : r7 = 1 : r8 = 1
r9 = 1 : r10 = 1 : r11 = 1 : r12 = 1 : r13 = 1 : r14 = 1 : r15 = 1 : r16 = 1

reg_OLATB = &b11111111 : reg_OLATA = &b11111111

' MCP23017 Driver for Annex
I2C.SETUP 21, 22 ' set I2C port on pins 21 and 22
device_address = 32 'set to module i2c address
'MCP23017 internal registers

IODIRA = 0 : IODIRB = 1 : IPOLA = 2 : IPOLB = 3

GPINTENA = 4 : GPINTENB = 5 : DEFVALA = 6 : DEFVALB = 7

INTCONA = 8 : INTCONB = 9 : IOCONA = 10 : IOCONB = 11

GPPUA = 12 : GPPUB = 13 : INTFA = 14 : INTFB = 15

INTCAPA = 16 : INTCAPB = 17 : GPIOA = 18 : GPIOB = 19

OLATA = 20 : OLATB = 21

i2C.WriteRegByte device_address, IODIRA, &b00000000
i2C.WriteRegByte device_address, OLATA, reg_OLATA
i2C.WriteRegByte device_address, IODIRB, &b00000000
i2C.WriteRegByte device_address, OLATB, reg_OLATB

gosub paint
onhtmlreload paint

wait

paint:
CLS
a$ = ""
a$ = a$ + "Test relee<br><br>"
a$ = a$ + button$("Rel1", Rel1, "but1")
css CSSID$("but1", "background-color:green;")
a$ = a$ + button$("Rel2", Rel2, "but2")
css CSSID$("but2", "background-color:green;")
a$ = a$ + button$("Rel3", Rel3, "but3")
css CSSID$("but3", "background-color:green;")
a$ = a$ + button$("Rel4", Rel4, "but4")
css CSSID$("but4", "background-color:green;")
a$ = a$ + button$("Rel5", Rel5, "but5")
css CSSID$("but5", "background-color:green;")
a$ = a$ + button$("Rel6", Rel6, "but6")
css CSSID$("but6", "background-color:green;")
a$ = a$ + button$("Rel7", Rel7, "but7")
css CSSID$("but7", "background-color:green;")
a$ = a$ + button$("Rel8", Rel8, "but8")
css CSSID$("but8", "background-color:green;")
a$ = a$ + button$("Rel9", Rel9, "but9")
css CSSID$("but9", "background-color:green;")
a$ = a$ + button$("Rel10", Rel10, "but10")
css CSSID$("but10", "background-color:green;")
a$ = a$ + button$("Rel11", Rel11, "but11")
css CSSID$("but11", "background-color:green;")
a$ = a$ + button$("Rel12", Rel12, "but12")
css CSSID$("but12", "background-color:green;")
a$ = a$ + button$("Rel13", Rel13, "but13")
css CSSID$("but13", "background-color:green;")
a$ = a$ + button$("Rel14", Rel14, "but14")
css CSSID$("but14", "background-color:green;")
a$ = a$ + button$("Rel15", Rel15, "but15")
css CSSID$("but15", "background-color:green;")
a$ = a$ + button$("Rel16", Rel16, "but16")
css CSSID$("but16", "background-color:green;")

HTML a$
return



Rel1:
Gpio = &b00000001
if r1 = 0 then
css CSSID$("but1", "background-color:green;")
reg_OLATB = reg_OLATB+Gpio
else
css CSSID$("but1", "background-color: red;")
reg_OLATB = reg_OLATB-Gpio
end if
r1 = 1 - r1
i2C.WriteRegByte device_address, OLATB, reg_OLATB
return
end

Rel2:
Gpio = &b00000010
if r2 = 0 then
css CSSID$("but2", "background-color: green;")
reg_OLATB = reg_OLATB+Gpio
else
css CSSID$("but2", "background-color: red;")
reg_OLATB = reg_OLATB-Gpio
end if
r2 = 1 - r2
i2C.WriteRegByte device_address, OLATB, reg_OLATB
return
end

Rel3:
Gpio = &b00000100
if r3 = 0 then
css CSSID$("but3", "background-color: green;")
reg_OLATB = reg_OLATB+Gpio
else
css CSSID$("but3", "background-color: red;")
reg_OLATB = reg_OLATB-Gpio
end if
r3 = 1 - r3
i2C.WriteRegByte device_address, OLATB, reg_OLATB
return
end

Rel4:
Gpio = &b00001000
if r4 = 0 then
css CSSID$("but4", "background-color: green;")
reg_OLATB = reg_OLATB+Gpio
else
css CSSID$("but4", "background-color: red;")
reg_OLATB = reg_OLATB-Gpio
end if
r4 = 1 - r4
i2C.WriteRegByte device_address, OLATB, reg_OLATB
return
end

Rel5:
Gpio = &b00010000
if r5 = 0 then
css CSSID$("but5", "background-color: green;")
reg_OLATB = reg_OLATB+Gpio
else
css CSSID$("but5", "background-color: red;")
reg_OLATB = reg_OLATB-Gpio
end if
r5 = 1 - r5
i2C.WriteRegByte device_address, OLATB, reg_OLATB
return
end

Rel6:
Gpio = &b00100000
if r6 = 0 then
css CSSID$("but6", "background-color: green;")
reg_OLATB = reg_OLATB+Gpio
else
css CSSID$("but6", "background-color: red;")
reg_OLATB = reg_OLATB-Gpio
end if
r6 = 1 - r6
i2C.WriteRegByte device_address, OLATB, reg_OLATB
return
end

Rel7:
Gpio = &b01000000
if r7 = 0 then
css CSSID$("but7", "background-color: green;")
reg_OLATB = reg_OLATB+Gpio
else
css CSSID$("but7", "background-color: red;")
reg_OLATB = reg_OLATB-Gpio
end if
r7 = 1 - r7
semn = r7
i2C.WriteRegByte device_address, OLATB, reg_OLATB
return
end

Rel8:
Gpio = &b10000000
if r8 = 0 then
css CSSID$("but8", "background-color: green;")
reg_OLATB = reg_OLATB+Gpio
else
css CSSID$("but8", "background-color: red;")
reg_OLATB = reg_OLATB-Gpio
end if
r8 = 1 - r8
i2C.WriteRegByte device_address, OLATB, reg_OLATB
return
end

Rel9:
Gpio = &b00000001
if r9 = 0 then
css CSSID$("but9", "background-color: green;")
reg_OLATA = reg_OLATA+Gpio
else
css CSSID$("but9", "background-color: red;")
reg_OLATA = reg_OLATA-Gpio
end if
r9 = 1 - r9
i2C.WriteRegByte device_address, OLATA, reg_OLATA
return
end

Rel10:
Gpio = &b00000010
if r10 = 0 then
css CSSID$("but10", "background-color: green;")
reg_OLATA = reg_OLATA+Gpio
else
css CSSID$("but10", "background-color: red;")
reg_OLATA = reg_OLATA-Gpio
end if
r10 = 1 - r10
i2C.WriteRegByte device_address, OLATA, reg_OLATA
return
end


Rel11:
Gpio = &b00000100
if r11 = 0 then
css CSSID$("but11", "background-color: green;")
reg_OLATA = reg_OLATA+Gpio
else
css CSSID$("but11", "background-color: red;")
reg_OLATA = reg_OLATA-Gpio
end if
r11 = 1 - r11
i2C.WriteRegByte device_address, OLATA, reg_OLATA
return
end

Rel12:
Gpio = &b00001000
if r12 = 0 then
css CSSID$("but12", "background-color: green;")
reg_OLATA = reg_OLATA+Gpio
else
css CSSID$("but12", "background-color: red;")
reg_OLATA = reg_OLATA-Gpio
end if
r12 = 1 - r12
i2C.WriteRegByte device_address, OLATA, reg_OLATA
return
end


Rel13:
Gpio = &b00010000
if r13 = 0 then
css CSSID$("but13", "background-color: green;")
reg_OLATA = reg_OLATA+Gpio
else
css CSSID$("but13", "background-color: red;")
reg_OLATA = reg_OLATA-Gpio
end if
r13 = 1 - r13
i2C.WriteRegByte device_address, OLATA, reg_OLATA
return
end


Rel14:
Gpio = &b00100000
if r14 = 0 then
css CSSID$("but14", "background-color: green;")
reg_OLATA = reg_OLATA+Gpio
else
css CSSID$("but14", "background-color: red;")
reg_OLATA = reg_OLATA-Gpio
end if
r14 = 1 - r14
i2C.WriteRegByte device_address, OLATA, reg_OLATA
return
end


Rel15:
Gpio = &b01000000
if r15 = 0 then
css CSSID$("but15", "background-color: green;")
reg_OLATA = reg_OLATA+Gpio
else
css CSSID$("but15", "background-color: red;")
reg_OLATA = reg_OLATA-Gpio
end if
r15 = 1 - r15
i2C.WriteRegByte device_address, OLATA, reg_OLATA
return
end


Rel16:
Gpio = &b10000000
if r16 = 0 then
css CSSID$("but16", "background-color: green;")
reg_OLATA = reg_OLATA+Gpio
else
css CSSID$("but16", "background-color: red;")
reg_OLATA = reg_OLATA-Gpio
end if
r16 = 1 - r16
i2C.WriteRegByte device_address, OLATA, reg_OLATA
return
end
You do not have the required permissions to view the files attached to this post.
User avatar
Electroguard
Posts: 857
Joined: Mon Feb 08, 2021 6:22 pm
Has thanked: 273 times
Been thanked: 321 times

Re: ESP32 whit MCP23017 16Bit I/O Expansion conected to 16 channel relay module

Post by Electroguard »

Hi, thanks for the contribution.

If you wish, you could edit your code by clicking the pen icon at the top of the post, then click and drag to select just the program code,
Then if you click the </> code icon at the top, it will wrap the selected code between code tags.
Now here's the trick... if you add "=annex" in the opening code tag, eg: [code=annex] it will cause all the following code to be context-sensitive colour high-lighted.
f1test
Posts: 15
Joined: Tue Nov 29, 2022 3:13 pm
Has thanked: 2 times
Been thanked: 11 times

Re: ESP32 whit MCP23017 16Bit I/O Expansion conected to 16 channel relay module

Post by f1test »

Update version:

'

Code: [Local Link Removed for Guests]

' You must specify when calling the subroutine the number of the relay and the state (closed/open)
' The number of relay is the whole value. State of relay is 1 or 0. 0 is closed, 1 is open 
' the position of relays can be found by reading the  value of reg_OLATA , reg_OLATA 
'
' 
'
OPTION.BASE 1   
Dim relay(8,3) =  1, 2, 4, 8, 16, 32, 64, 128, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1     
reg_OLATB = &b11111111  : reg_OLATA = &b11111111
I2C.SETUP 21, 22  ' set I2C port on pins 21 and 22
device_address = 32  'set to module i2c address
'MCP23017 internal registers

IODIRA   = 0  : IODIRB   = 1 : IPOLA   = 2  : IPOLB   = 3

GPINTENA = 4  : GPINTENB = 5 : DEFVALA = 6  : DEFVALB = 7

INTCONA  = 8  : INTCONB  = 9 : IOCONA  = 10 : IOCONB  = 11

GPPUA    = 12 : GPPUB   = 13 : INTFA   = 14 : INTFB   = 15

INTCAPA  = 16 : INTCAPB = 17 : GPIOA   = 18 : GPIOB   = 19

OLATA    = 20 : OLATB   = 21

i2C.WriteRegByte device_address, IODIRA, &b00000000
i2C.WriteRegByte device_address, OLATA, reg_OLATA
i2C.WriteRegByte device_address, IODIRB, &b00000000
i2C.WriteRegByte device_address, OLATB, reg_OLATB


' *****************
'
'  main program

releu 2 , 0     ' sub call   will close relay 2

'
'  end of main program
'
' **********


sub releu(rel,semn)

if rel < 9 then                     'x.0
  rel1=rel
  r_releu=2 
  else                            'x.1
   rel1 = rel-8
  r_releu=3 
end if                            'x.2 



if semn <> relay(rel1,r_releu) then                                         '0.0

  if semn = 1  then                                                
          if rel < 9 then                                                      '1.0 
                 reg_OLATB = reg_OLATB+relay(rel1,1) 
                 relay(rel1,2) = 1
           i2C.WriteRegByte device_address, OLATB, reg_OLATB
                  else                                                       '1.1     
                  reg_OLATA = reg_OLATA+relay(rel1,1)
                relay(rel1,3) = 1   
          i2C.WriteRegByte device_address, OLATA, reg_OLATA
                end if                                                       '1.2

        else                                                       'z.1

         if rel < 9 then      
                 reg_OLATB = reg_OLATB-relay(rel1,1) 
                 relay(rel1,2) = 0
           i2C.WriteRegByte device_address, OLATB, reg_OLATB
                  else                                                 '2.1
                  reg_OLATA = reg_OLATA-relay(rel1,1)
                relay(rel1,3) = 0   
          i2C.WriteRegByte device_address, OLATA, reg_OLATA
               end if   

      end if

end if   
                                                   '0.2                                        
end sub[/i]
Post Reply