Example Code for Arduino-Button Cascading
Last revision 2025/12/18
Designed with I2C, multiple modules are allowed to be cascaded without occupying additional ports. In this sample, six button modules will be cascaded and controlled independently without affecting each other.
Hardware Preparation
- DFRduino UNO R3 (SKU: DFR0216) × 1
- Gravity: IO Expansion Shield for Arduino V7.1 (SKU: DFR0216) × 1
- Gravity: I2C RGB LED Button Module (SKU: DFR0991) × 6
- Gravity-4P I2C/UART Sensor Cable(DFR0991 is equipped with one of these lines) × 6
- USB Cable × 1
Software Preparation
- Download Arduino IDE: Click to download Arduino IDE
- Download and install the DFRobot_RGBButton-main Library
- For Arduino IDE V1.8.19 (or earlier), install the library manually: How to Add a Library ?
- For Arduino IDE V2.0.0 (or later), directly search for the "DFRobot_RGBButton-main Library" in the Library Manager and install it.
Wiring Diagram
Cascade 6 button modules, occupying only one I2C interface of the UNO mainboard.

Sample Code
6 RGB buttons show specific color on press, white on release
#include <DFRobot_RGBButton.h>
DFRobot_RGBButton RGBButton1(&Wire, /*I2CAddr*/ 0x23); // Button 1
DFRobot_RGBButton RGBButton2(&Wire, /*I2CAddr*/ 0x24); // Button 2
DFRobot_RGBButton RGBButton3(&Wire, /*I2CAddr*/ 0x25); // Button 3
DFRobot_RGBButton RGBButton4(&Wire, /*I2CAddr*/ 0x26); // Button 4
DFRobot_RGBButton RGBButton5(&Wire, /*I2CAddr*/ 0x27); // Button 5
DFRobot_RGBButton RGBButton6(&Wire, /*I2CAddr*/ 0x28); // Button 6
void setup(void)
{ Serial.begin(115200);
while( ! RGBButton1.begin() ){
Serial.println("1");
delay(3000);}
while( ! RGBButton2.begin() ){
Serial.println("2");
delay(3000);
}
while( ! RGBButton3.begin() ){
Serial.println("3");
delay(3000);
}
while( ! RGBButton4.begin() ){
Serial.println("4");
delay(3000);}
while( ! RGBButton5.begin() ){
Serial.println("5");
delay(3000);
}
while( ! RGBButton6.begin() ){
Serial.println("6");
delay(3000);
}
Serial.println("Begin ok!\n");
RGBButton1.setRGBColor(RGBButton1.eWhite);
RGBButton2.setRGBColor(RGBButton2.eWhite);
RGBButton3.setRGBColor(RGBButton3.eWhite);
RGBButton4.setRGBColor(RGBButton4.eWhite);
RGBButton5.setRGBColor(RGBButton5.eWhite);
RGBButton6.setRGBColor(RGBButton6.eWhite);
}
uint8_t flag1 = 0, flag2 = 0, flag3 = 0, flag4 = 0, flag5 = 0, flag6 = 0;
void loop()
{ if( RGBButton1.getButtonStatus() ) { // Button 1, show red LED when pressed
flag1 = 1;
RGBButton1.setRGBColor(RGBButton1.eRed);
delay(50);
} else if( 1 == flag1 ) {
flag1 = 0;
RGBButton1.setRGBColor(RGBButton1.eWhite);
}
if( RGBButton2.getButtonStatus() ) { // Button 2, show orange LED when pressed
flag2 = 1;
RGBButton2.setRGBColor(RGBButton2.eOrange);
delay(50);
} else if( 1 == flag2 ) {
flag2 = 0;
RGBButton2.setRGBColor(RGBButton2.eWhite);
}
if( RGBButton3.getButtonStatus() ) { // Button 3, show yellow LED when pressed
flag3 = 1;
RGBButton3.setRGBColor(RGBButton3.eYellow);
delay(50);
} else if( 1 == flag3 ) {
flag3 = 0;
RGBButton3.setRGBColor(RGBButton3.eWhite);
}
if( RGBButton4.getButtonStatus() ) { // Button 4, show green LED when pressed
flag4 = 1;
RGBButton4.setRGBColor(RGBButton4.eGreen);
delay(50);
} else if( 1 == flag4 ) {
flag4 = 0;
RGBButton4.setRGBColor(RGBButton4.eWhite);
}
if( RGBButton5.getButtonStatus() ) { // Button 5, show blue LED when pressed
flag5 = 1;
RGBButton5.setRGBColor(RGBButton5.eBlue);
delay(50);
} else if( 1 == flag5 ) {
flag5 = 0;
RGBButton5.setRGBColor(RGBButton5.eWhite);
}
if( RGBButton6.getButtonStatus() ) { // Button 6, show purple LED when pressed
flag6 = 1;
RGBButton6.setRGBColor(RGBButton6.ePurple);
delay(50);
} else if( 1 == flag6 ) {
flag6 = 0;
RGBButton6.setRGBColor(RGBButton6.eWhite);
}
}
Result
6 button modules are cascaded, they all show white color by default. When a button is pressed, its corresponding RGB LED shows the color set in the program. For example, press the first button, and its button light turns red.
Was this article helpful?
