Example Code for Arduino-Point Counter
Last revision 2025/12/18
This guide offers step-by-step instructions and example code for creating a point counter using an Arduino, OLED display, and button modules, providing an excellent starting point for beginners in electronics.
Hardware Preparation
- DFRduino UNO R3 (SKU: DFR0216) × 1
- Gravity: IO Expansion Shield for Arduino V7.1 (SKU: DFR0216) × 1
- Gravity: I2C OLED-128x64 Display (SKU: DFR0486) × 1
- Gravity: Digital Blue LED Light Module (SKU: DFR0021-B) × 1
- Gravity: I2C RGB LED Button Module (SKU: DFR0991) × 2
- Gravity-4P I2C/UART Sensor Cable(Module is equipped with one of these lines) × 3
- USB Cable × 1
- Dupont Wires
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
Connect the interrupt pins (INT) of the two button modules to the digital pins 2 and 3 of the UNO board, and the OLED display to another I2C interface on the UNO mainboard.

Sample Code
Dual RGB buttons interrupt count, OLED shows add/sub & difference
#include <DFRobot_RGBButton.h>
#include <U8g2lib.h>
DFRobot_RGBButton RGBButton_add(&Wire, /*I2CAddr*/ 0x23);
DFRobot_RGBButton RGBButton_sub(&Wire, /*I2CAddr*/ 0x24);
U8G2_SSD1306_128X64_NONAME_F_HW_I2C u8g2(/* rotation=*/U8G2_R0, /* reset=*/ U8X8_PIN_NONE);
/* Interrupt flag */
volatile bool flag_add = false, flag_sub = false;
int add_num=0,sub_num=0,total=0;
/* External interrupt flag */
void interrupt_add()
{
if(digitalRead(2)==HIGH){
flag_add = true;
add_num+=1;
}else{
flag_add = false;
}
}
void interrupt_sub()
{
if(digitalRead(3)==HIGH){
flag_sub = true;
sub_num+=1;
}else{
flag_sub = false;
}
}
void setup(void)
{
Serial.begin(115200);
Wire.begin();
u8g2.begin();
u8g2.enableUTF8Print();
u8g2.setFont(u8g2_font_t0_17b_tr );
Wire.setClock(100000);
/**
* @brief Init function
* @return bool type, true if successful, false if error
*/
while( ! RGBButton_add.begin() ){
Serial.println("1!");
delay(3000);
}
while( ! RGBButton_sub.begin() ){
Serial.println("2!");
delay(3000);
}
Serial.println("Begin ok!\n");
attachInterrupt(/*Interrupt No*/0, interrupt_add, CHANGE); // Open the external interrupt 0, connect INT to the digital pin of the main control:
attachInterrupt(/*Interrupt No*/1, interrupt_sub, CHANGE); // Open the external interrupt 0, connect INT to the digital pin of the main control:
}
void loop()
{
Wire.setClock(100000);
if(flag_add){
RGBButton_add.setRGBColor(RGBButton_add.eRed);
}else{
RGBButton_add.setRGBColor(RGBButton_add.eBlue);
}
if(flag_sub){
RGBButton_sub.setRGBColor(RGBButton_sub.eGreen);
}else{
RGBButton_sub.setRGBColor(RGBButton_sub.eBlue);
}
String str_add="ADD:"+String(add_num);
String str_sub="DED:"+String(sub_num);
String str_total="TOTAL:"+String(add_num-sub_num);
u8g2.clearBuffer(); // clear the internal memory
u8g2.setFont(u8g2_font_t0_17b_tr ); // choose a suitable font
u8g2.drawStr(0,10,str_add.c_str()); // write something to the internal memory
u8g2.drawStr(0,30,str_sub.c_str()); // write something to the internal memory
u8g2.drawStr(0,50,str_total.c_str()); // write something to the internal memory
u8g2.sendBuffer();
}
Result
After uploading the codes, the two buttons light up blue. We can attach labels to them, the green for plus and red for minus. When pressed, the plus button lights up green and the minus button lights up red, and the points change can be clearly seen on the display.
Was this article helpful?
