I2C OLED intermittently freezing

I have this tiny OLED for the DataSleeve, but every now and then, the output freezes and the Arduino has to be reset. I found that reducing the refresh rate of the screen helps, but it still happens. I found another thread where someone had the same problem, and replacing their counterfeit Arduino fixed it.


I'm using both a different library AND a different board (still knockoff, my Micro has a corrupted bootloader right now), and it's still happening. Furthermore, the board in my final project isn't genuine either, it's Adafruit's KB2040 with a 3rd party core to get it working in Arduino.

Hardware details:
Knockoff Nano (CH340)
MPU6050 (I2C)
Rotary encoder

Code: (this code is pointless and serves as R&D)

#include <SPI.h>
#include <MPU6050.h>
#include <Wire.h>
#include <U8g2lib.h>

MPU6050 mpu;

int16_t ax, ay, az, gx, gy, gz;

U8G2_SSD1306_128X32_UNIVISION_F_HW_I2C u8g2(U8G2_R0);

const int buttonPin = 4;

int counter = 0;
int buttonState;        
int lastButtonState = LOW;   

unsigned long lastDebounceTime = 0;  
unsigned long debounceDelay = 50;    

void setup() {
  pinMode(buttonPin, INPUT);
  if (!mpu.testConnection()) {
    while (1);

void loop() {
  mpu.getMotion6(&ax, &ay, &az, &gx, &gy, &gz);

  int reading = digitalRead(buttonPin);
  if (reading != lastButtonState) {
    lastDebounceTime = millis();

  if ((millis() - lastDebounceTime) > debounceDelay) {

    if (reading != buttonState) {
      buttonState = reading;

      if (buttonState == LOW && counter >= 0 && counter < 10) {
      else if (buttonState == LOW && counter >= 10) {
        counter = 0;

  lastButtonState = reading;

  switch (counter) {
    case 1:
      printInt(10, 10, ax);
    case 2:
      printInt(10, 10, ay);
    case 3:
      printInt(10, 10, az);
    case 4:
      printInt(10, 10, gx);
    case 5:
      printInt(10, 10, gy);
    case 6:
      printInt(10, 10, gz);
      printInt(10, 10, 0);

Uses a secondary function called printInt()

void printInt(int _cursorX, int _cursorY, int _varToPass) {
  u8g2.setCursor(_cursorX, _cursorY);

First thing I would check is if the power (i.e. 5v supply) is good / add a capacitor between them.
I can't tell you how many times I have spent hours trying to find a problem only to later realise it is just a power issue.

Power's coming from USB, and I've never had problems with it before.

Did you find any solution yet ?

@kisscoool18 Do you have problems with the MPU-6050 as well ? It could be a fake sensor. There are so many troubles lately with the MPU-6050 on this forum, you better look for a newer and better sensor.

No, but I have a very similar issue with an OLED display so I'm searching answers.
I've posted more details on my setup here

Start a new topic please.

Well my intention wasn't to talk about my issue but to know more about this one, to give me ideas. But yes I will write a new post.

Not sure if it’s any help, but I have a similar unresolved problem - occurs after a couple of days running .
I put a flashing led in my code and I can see that the code is still running fine , but the display is frozen .
So watching this with interest

kisscoool18 created a new topic here: OLED display eventually freeze

If anyone is wondering, I have not found a solution. Actually, I haven't messed with the OLED since I posted. I'll break it back out later and see what happens.

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