Willem Aandewiel
Published © MIT

Controlling Lots of Relays with only 2 GPIO pins

How can you control many relays with only two GPIO pins? In this project I show you how!

IntermediateProtip2 hours1,084
Controlling Lots of Relays with only 2 GPIO pins

Things used in this project

Hardware components

ATmega 328
×1
2N7000
×2
Ceramic Disc Capacitor, 100 pF
Ceramic Disc Capacitor, 100 pF
×2
Resistor 10k ohm
Resistor 10k ohm
×3
Resistor 4k7
×2
Tactile Switch, Top Actuated
Tactile Switch, Top Actuated
×1
LED (generic)
LED (generic)
×1
ISP connector
×1
Header female 2x10
×1
Header 4 pins
×1
16MHz ceramic resonator
×1

Story

Read more

Schematics

Schematic I2C multiplexer

Code

Code snippet #1

Plain text
	registerLayout:
	  byte      status;         // 0x00 (RO)
	  byte      majorRelease;   // 0x01 (RO)
	  byte      minorRelease;   // 0x02 (RO)
	  byte      lastGpioState;  // 0x03 (RO)
	  byte      whoAmI;         // 0x04 (R/W)
	  byte      numberOfRelays; // 0x05 (R/W)
	  byte      Command;        // 0xF0 (W)

Code snippet #2

Plain text
  bool    begin(Wire, deviceAddress);
  bool    isConnected();
  byte    getMajorRelease();
  byte    getMinorRelease();
  byte    getWhoAmI();
  byte    getNumRelays();
  byte    getStatus();
  bool    writeCommand(COMMAND);
  bool    digitalRead(relayNr); 
  bool    digitalWrite(relayNr, {HIGH|LOW}); 
  bool    setI2Caddress(newAddress);  // 1 .. 127 (dec)        
  bool    setNumRelays(numRelays);    // 8 or 16 (dec)        
  void    showRegister(regSize, *regPtr, &Stream);

Code snippet #4

Plain text
	Wire.begin();
	if (!relay.begin(Wire, 0x48)) 
	{
	  Serial.println(“No device found. Abort!”);
	  while (true) {}
	}

Code snippet #5

Plain text
  relay.digitalWrite(2, HIGH);    // close relay 2 
  state = relay.digitalRead(2);   // state should now be 1     
  relay.digitalWrite(5, HIGH);    // close relay 5     
  relay.digitalWrite(2, LOW);     // open relay 2
  for (int r=1; r<=16; r++)       // switch all relays HIGH
  {
      relay.digitalWrite(r, HIGH);
  }

Github

https://github.com/mrWheel/I2C_RelaysMux

Credits

Willem Aandewiel
6 projects • 17 followers
Contact

Comments

Please log in or sign up to comment.