I2C Relay Met Arduino IDE

Introduction: I2C Relay Met Arduino IDE

I order a nice relayboard but there was no ArduinoIDE instruction, just Raspberry Pi e.o. I find out how to use it with Arduino and wanna share it so you can save that time.

Original RaspberryPi example:

wiki.52pi.com/index.php/DockerPi_4_Channel_Relay_SKU:_EP-0099

Nice thing is that you can stack up to 4 boards. So you can use max. 4 x 4 = 16 relay's on one I2C bus.

There are also some cons:

  • little terminas, don't fit 1 mm2 wire
  • you cannot change the wiring of the bottom connectors when they are stacked together

But still handy board's.

Step 1: Wirering

In the pictures you can see the wirering.

The 52Pi needs both 5V (for relay's) en 3.3V ( I2C chip ).

So 5 wires needed:

  • GND to GND
  • 5 V to 5 V
  • 3.3 V to 3.3 V
  • SLA to SLA
  • SCL to SCL

If you use an Arduino UNO or other 5 V be ware of the max 3,6 V from the I2C controller ! Use resistors or else to lower the 5 V out of the Arduino PIN's !

Step 2: Simple Code Example:

/* Arduino IDE (ESP) example for I2C relaisboard.  
 *  By Laurens Korste <a href="http://www.boktorrobotica.nl" rel="nofollow"> www.boktorrobotica.nl
</a>
 *  free to use.
 */
#include <Wire.h>	     // for I2C communication
void setup() {
 // this rule not for UNO or boards with dedicated I2C pins
 Wire.begin(D1, D2);          // join i2c bus with SDA=D1 and SCL=D2 for NodeMCU 
}
void loop() {
 Wire.beginTransmission(0x10);/* begin transmitting to I2C adress 10 ( to change also to 11, 12 or 13 ) */
 Wire.write(0x01);            /* choice relais 1 ( out of 4 ) on board 10 ( also 0x02, 0x03, 0x04 ) */
 Wire.write(0xFF);            /* set relais 1 on board 10 to ON. all numbers > 0 will do so */
 Wire.endTransmission();      /* stop transmitting */
 delay(3000);
 Wire.beginTransmission(0x10); /*  */
 Wire.write(0x01);
 Wire.write(0x00);            /* set relais 1 on board 10 to OFF */
 Wire.endTransmission();    /* stop transmitting */
 delay(3000);
}

Step 3: Four Relay Test Code

/*  sketch by Laurens Korste for Arduino (ESP / NodeMCU) 
 *   But other boards will do also 
 *   <a href="http://www.boktorrobotica.nl" rel="nofollow"> www.boktorrobotica.nl </a> 
 *  In this skeych al the 4 relay's will be activated en deactivated
 */
#include <Wire.h>               // for I2C communication
void setup() {
 Serial.begin(115200);          // begin serial for debug (9600 for UNO)
 Wire.begin(D1, D2);            // join i2c bus with SDA=D1 and SCL=D2 of NodeMCU no need for UNO
}
void loop() {
 for (int i=1; i<=4; i++){
 Wire.beginTransmission(0x10);  // begin with device address
 Wire.write(i);                 // choice the relais         
 Wire.write(0xFF);              // send the "on" code FF (every count from 01 to FF will do)
 Wire.endTransmission();        // stop transmitting
 Serial.write(i);              
 Serial.println(" aan ");        
 delay(1000);
 Wire.beginTransmission(0x10);  // begin with device address
 Wire.write(i);                 
 Wire.write(0x00);              // Send the "off" code 
 Wire.endTransmission();        // stop transmitting
 Serial.write(i);               
 Serial.println(" uit ");
 delay(1000);
 }
}

Step 4: My Project

I have written a code to operate my 3 shutters.
This can be done with switches but also with the BLYNK app, See image.

  • One short press will start moving one shutter ( or stop it when it's moving ).
  • One long press and all three shutters open ( or close or stop ).
  • Double pressure: the shutters go to the "holes" position.

As can be seen in the picture, I also integrated a temperature and light sensor.

Now everything on a good base PCB and neatly in a box.

Step 5: Call a Relay With One Command

It is useful if you only need one command to activate or deactivate a relay. Below is a function that can do this (with lowbyte and highbyte).

/*  sketch by Laurens Korste for Arduino (ESP / NodeMCU) with I2C relaysboard<br> 
 *   But other boards will do also
 *   href="http://www.boktorrobotica.nl ; www.boktorrobotica.nl 
 *   https://wiki.52pi.com/index.php/DockerPi_4_Channel_Relay_SKU:_EP-0099 ; www.boktorrobotica.nl 
 *  In this sketch the relay will be activated by one call;
 *//
#include <Wire.h>               // for I2C communication
// Relays declaratie. 4 PCB's Per PCB 4 relays possible. In this sketch only two PCB
// PCB and relays are merged so that they can be called with one command
const byte PutOn=0xFF;               //command to switch on
const byte PutOff=0x00;               //comand to switch off
const word Relay1bord1=0x1001;    //you can chance the names by example Relay1
const word Relay2bord1=0x1002;    //you can chance the names by example Relay2
const word Relay3bord1=0x1003;    //you can chance the names by example Relay3
const word Relay4bord1=0x1004;    //you can chance the names by example Relay4
const word Relay1bord2=0x1101;    //you can chance the names by example Relay5
const word Relay2bord2=0x1102;    //you can chance the names by example Relay6
const word Relay3bord2=0x1103;    //you can chance the names by example Relay7
const word Relay4bord2=0x1104;    //you can chance the names by example Relay8
const word Relay1bord3=0x1201;    //you can chance the names by example Relay9
const word Relay2bord4=0x1302;    //you can chance the names by example Relay14

void setup() {<br> Serial.begin(115200);          // begin serial for debug (9600 for UNO)
  Wire.begin(D1, D2);            // join i2c bus with SDA=D1 and SCL=D2 of NodeMCU no need for UNO
}

void loop() {  // to switch relay 4 on PCB 2 on
  RelayActie(Relay4bord2,PutOn);
  delay(1000);  // to switch relay 4 on PCB 2 off
  RelayActie(Relay4bord2,PutOff);
  delay(1000);
  }

void RelayActie(word Relay, byte OnOrOff){
  Wire.beginTransmission (highByte(Relay));
  Wire.write (lowByte(Relay));
  Wire.write (OnOrOff);
  Wire.endTransmission();
}

Be the First to Share

    Recommendations

    • Made with Math Contest

      Made with Math Contest
    • Robots Contest

      Robots Contest
    • Home Cooked Speed Challenge

      Home Cooked Speed Challenge

    Comments