# Butto count OLED

**URL:** <https://forum.arduino.cc/t/butto-count-oled/1348187>\
**Category:** Displays\
**Created:** [January 29, 2025, 12:18am UTC](https://forum.arduino.cc/t/butto-count-oled/1348187 "2025-01-29T00:18:45Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![christianarduino](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/christianarduino/32/1190528_2.png) [@christianarduino](https://forum.arduino.cc/u/christianarduino)\
**Post date:** [January 29, 2025, 12:18am UTC](https://forum.arduino.cc/t/butto-count-oled/1348187/1 "2025-01-29T00:18:45Z")

</div>

I was wondering if anyone could help me fix this code i'm new to coding and have no clue what to do. Basically i'm trying to make a button count to oled display but the code i found uses code for a SSD1306 OLED display and I have a SH1106 OLED display so i'm wondering if anyone could change the code so it works for SH1106.

Thank you

The code:

/\*

- Created by [ArduinoGetStarted.com](http://ArduinoGetStarted.com)
- 
- This example code is in the public domain
- 
- Tutorial page: [Arduino - Button Count - OLED | Arduino Tutorial](https://arduinogetstarted.com/tutorials/arduino-button-count-oled)  
\*/

#include \<Wire.h\>  
#include \<Adafruit\_GFX.h\>  
#include \<Adafruit\_SSD1306.h\>  
#include \<ezButton.h\>

#define SCREEN\_WIDTH 128 // OLED display width, in pixels  
#define SCREEN\_HEIGHT 64 // OLED display height, in pixels

Adafruit\_SSD1306 oled(SCREEN\_WIDTH, SCREEN\_HEIGHT, &Wire, -1); // create SSD1306 display object connected to I2C  
ezButton button(7); // create ezButton object that attach to pin 7;  
unsigned long lastCount = 0;

void setup() {  
Serial.begin(9600);

// initialize OLED display with address 0x3C for 128x64  
if (!oled.begin(SSD1306\_SWITCHCAPVCC, 0x3C)) {  
Serial.println(F("SSD1306 allocation failed"));  
while (true);  
}

delay(2000); // wait for initializing  
oled.clearDisplay(); // clear display

oled.setTextSize(2); // text size  
oled.setTextColor(WHITE); // text color  
oled.setCursor(0, 10); // position to display

button.setDebounceTime(50); // set debounce time to 50 milliseconds  
button.setCountMode(COUNT\_FALLING);  
}

void loop() {  
button.loop(); // MUST call the loop() function first

unsigned long count = button.getCount();  
if (lastCount != count) {  
Serial.println(count); // print count to Serial Monitor  
oled.clearDisplay(); // clear display  
oled.println(count); // display count  
oled.display(); // show on OLED  
lastCount != count;  
}  
}

---

<div class="post-metadata">

**Author:** ![xfpd](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/xfpd/32/1120236_2.png) [@xfpd](https://forum.arduino.cc/u/xfpd)\
**Post date:** [January 29, 2025, 1:57am UTC](https://forum.arduino.cc/t/butto-count-oled/1348187/2 "2025-01-29T01:57:43Z")

</div>

Format your code...

 ![paste](https://europe1.discourse-cdn.com/arduino/original/4X/c/b/4/cb40e1987fa5e6f7f7cc8197b07ef618b42f4a73.png)

If you installed the SH1106 library, and choose the example, `SH_1106_128x64_I2C.ino` you will see it uses the same library calls as your code...

```arduino
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>

```

If your OLED is not working, perhaps your connections are not good. Show a picture.

---

<div class="post-metadata">

**Author:** ![ledsyn](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/ledsyn/32/1203827_2.png) [@ledsyn](https://forum.arduino.cc/u/ledsyn)\
**Post date:** [January 29, 2025, 1:58am UTC](https://forum.arduino.cc/t/butto-count-oled/1348187/3 "2025-01-29T01:58:24Z")

</div>

Please format the code as code. Highlight it, and click on CODE

---

<div class="post-metadata">

**Author:** ![TomGeorge](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/tomgeorge/32/215261_2.png) [@TomGeorge](https://forum.arduino.cc/u/TomGeorge)\
**Post date:** [January 29, 2025, 2:02am UTC](https://forum.arduino.cc/t/butto-count-oled/1348187/4 "2025-01-29T02:02:16Z")

</div>

Hi,  
Look at this link and follow the link to the documentation.

> **[OLED SSD1306 - SH1106 - Arduino Reference](https://reference.arduino.cc/reference/en/libraries/oled-ssd1306-sh1106/)**
>
> The Arduino programming language Reference, organized into Functions, Variable and Constant, and Structure keywords.

Use the library examples to write your code.

Tom.... 😃 👍 ☕ 🇦🇺

---

<div class="post-metadata">

**Author:** ![christianarduino](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/christianarduino/32/1190528_2.png) [@christianarduino](https://forum.arduino.cc/u/christianarduino)\
**Post date:** [January 29, 2025, 7:20pm UTC](https://forum.arduino.cc/t/butto-count-oled/1348187/5 "2025-01-29T19:20:28Z")

</div>

```arduino
/*
 * Created by ArduinoGetStarted.com
 *
 * This example code is in the public domain
 *
 * Tutorial page: https://arduinogetstarted.com/tutorials/arduino-button-count-oled
 */

#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <ezButton.h>

#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels

Adafruit_SSD1306 oled(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1); // create SSD1306 display object connected to I2C
ezButton button(7); // create ezButton object that attach to pin 7;
unsigned long lastCount = 0;

void setup() {
  Serial.begin(9600);

  // initialize OLED display with address 0x3C for 128x64
  if (!oled.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
    Serial.println(F("SSD1306 allocation failed"));
    while (true);
  }

  delay(2000); // wait for initializing
  oled.clearDisplay(); // clear display

  oled.setTextSize(2); // text size
  oled.setTextColor(WHITE); // text color
  oled.setCursor(0, 10); // position to display

  button.setDebounceTime(50); // set debounce time to 50 milliseconds
  button.setCountMode(COUNT_FALLING);
}

void loop() {
  button.loop(); // MUST call the loop() function first

  unsigned long count = button.getCount();
  if (lastCount != count) {
    Serial.println(count); // print count to Serial Monitor
    oled.clearDisplay(); // clear display
    oled.println(count); // display count
    oled.display(); // show on OLED
    lastCount != count;
  }
}

```

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [July 28, 2025, 7:20pm UTC](https://forum.arduino.cc/t/butto-count-oled/1348187/6 "2025-07-28T19:20:44Z")

</div>

Closed on July 28, 2025, 7:20pm UTC

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.
