Christian
Published © GPL3+

Control Up To 65,280 Relays With Your Raspberry Pi!

Use the IO Expander and Relay Expander with the Raspberry Pi to control up to 65,280 Relays!

IntermediateFull instructions provided1 hour7,332

Things used in this project

Hardware components

Raspberry Pi 4 Model B
Raspberry Pi 4 Model B
×1
IO Expander
×1
Relay Expander
×1

Software apps and online services

Raspbian
Raspberry Pi Raspbian

Story

Read more

Schematics

Control 64 Relays with a Single Expander

Control 64 Relays with multiple Expanders

Code

Control 64 Relays with a Single Expander

Python
#!/usr/bin/env python
import ioexpander
import time

ioexpander.ser.flushInput()
ioexpander.SerialCmdDone(b'eb4')

relay = 1

while 1:
    cmd = b'e' + bytes(str(relay),'raw_unicode_escape') + b'f'    
    ioexpander.SerialCmdDone(cmd)
    relay += 1
    if relay > 64:
        relay = 1
    cmd = b'e' + bytes(str(relay),'raw_unicode_escape') + b'o'
    ioexpander.SerialCmdDone(cmd)
   
    time.sleep(0.1)

Control 64 Relays with multiple Expanders

Python
#!/usr/bin/env python
import ioexpander9bit
import time

MAX_BOARDS = 4

ioexpander9bit.ser.flushInput()

# set IO Expander to 9-bit
ioexpander9bit.ser.write(b'\0')
# switch to simulated 9-bit mode using SPACE and MARK parity
ioexpander9bit.SerialSPACEParity()

for board in range(1, MAX_BOARDS+1):
    ioexpander9bit.SerialCmdDone(board, b'eb1')

board = 1
relay = 1

while 1:
    cmd = b'e' + bytes(str(relay),'raw_unicode_escape') + b'f'    
    ioexpander9bit.SerialCmdDone(board, cmd)
    relay += 1
    if relay > 16:
        relay = 1
        board += 1
        if board > MAX_BOARDS:
            board = 1
    cmd = b'e' + bytes(str(relay),'raw_unicode_escape') + b'o'
    ioexpander9bit.SerialCmdDone(board, cmd)
   
    time.sleep(0.1)

Credits

Christian

Christian

24 projects • 134 followers
Senior Embedded Engineer

Comments