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