# OLED Display problem

**URL:** <https://forum.arduino.cc/t/oled-display-problem/1325812>\
**Category:** Programming\
**Created:** [November 25, 2024, 10:20pm UTC](https://forum.arduino.cc/t/oled-display-problem/1325812 "2024-11-25T22:20:45Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![slovakfan83](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/slovakfan83/32/1098708_2.png) [@slovakfan83](https://forum.arduino.cc/u/slovakfan83)\
**Post date:** [November 25, 2024, 10:20pm UTC](https://forum.arduino.cc/t/oled-display-problem/1325812/1 "2024-11-25T22:20:45Z")

</div>

Hello, I stopped my project at the point, when I need to display current position at my OLED. Im not able to make any working code. When "acivate" void updateDisplay, program stops. Last I tried to update it each 500 millis, this doesnot help me too. I cant slow down whole program because of rotary encoder too, it is skipping steps if I put any display function into the main loop too frequently. Thank you for any help.

```arduino
#include <Adafruit_GFX.h> 
#include <Adafruit_SH110X.h>
#include <AccelStepper.h>
#include <EZButton.h>

// PIN definitions motor driver
#define STEP_PIN 2 //step
#define DIR_PIN 5 //direction
#define EN_PIN 6 //stepper driver enable pin

// Define a stepper and the pins it will use 6400 steps per round with TCM2208 and NEMA 17 Creality 42-34
AccelStepper stepper(AccelStepper::DRIVER, STEP_PIN, DIR_PIN); // (Type of driver: with 2 pins, STEP, DIR - for TMC2208 Aliexpres driver)
int motorMaxSpeed = 150; //50 maximum steps per second (about 3rps / at 16 microsteps)
int motorAccel = 10; //steps/second/second to accelerate

// rotary encoder 
#define ENCODER_CLK 8
#define ENCODER_DT 3
#define BTN_1_PIN 9
#define BTN_1 0

int lastClk = HIGH;
int counterEnc = 0;
int pulse_cnt = 0;
unsigned long previousMillis = 0;
unsigned long previousPulseMillis = 0;
float filtered_rpm = 0.0;
int counter2 = 0;
void ReadButtons(bool *states, int num) {
  //Read all button states however you want
  states[BTN_1] = !digitalRead(BTN_1_PIN);
}

EZButton _ezb(1, ReadButtons, 10000, 15000, 50);

int dock = 1;

// 1.3 OLED I2C displej Aliexpress
#define i2c_Address 0x3C //initialize with the I2C addr 0x3C Typically eBay OLED's
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
#define OLED_RESET -1 // QT-PY / XIAO
Adafruit_SH1106G display = Adafruit_SH1106G(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);

// Banner
#define PROJEKT "Turnout v1.1"

//relay 2 module
int relay_2 = 4;
int relay_1 = 7;

//DCC orientation timer
unsigned long previousPolarityMillis;

// 'OK', 64x64px
const unsigned char epd_bitmap_OK [] PROGMEM = {
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
	0x00, 0x00, 0x00, 0x38, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x38, 0x00, 0x00, 0x00, 0x00, 
	0x00, 0x00, 0x00, 0x38, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x38, 0x00, 0x00, 0x00, 0x00, 
	0x00, 0x00, 0xe0, 0x38, 0x0e, 0x00, 0x00, 0x00, 0x00, 0x00, 0xf0, 0x10, 0x1e, 0x00, 0x00, 0x00, 
	0x00, 0x00, 0x78, 0x00, 0x3c, 0x00, 0x00, 0x00, 0x00, 0x00, 0x38, 0x00, 0x38, 0x00, 0x00, 0x00, 
	0x00, 0x00, 0x18, 0x00, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x3f, 0x00, 0x00, 0x00, 0x00, 
	0x00, 0x00, 0x00, 0x7f, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0x80, 0x00, 0x00, 0x00, 
	0x00, 0x00, 0x00, 0xe3, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0xc3, 0x80, 0x00, 0x00, 0x00, 
	0x00, 0x00, 0x01, 0xc1, 0xc0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0xc1, 0xc0, 0x00, 0x00, 0x00, 
	0x00, 0x00, 0x01, 0xc1, 0xc0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0xc0, 0xe0, 0x00, 0x00, 0x00, 
	0x00, 0x00, 0x01, 0xc0, 0xf0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0xc0, 0x70, 0x00, 0x00, 0x00, 
	0x00, 0x00, 0x01, 0xc0, 0x78, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0xc0, 0x3c, 0x03, 0xff, 0x80, 
	0x00, 0x00, 0x00, 0xe0, 0x1e, 0x0f, 0xff, 0xc0, 0x00, 0x00, 0x00, 0xe0, 0x0f, 0x1f, 0xff, 0xc0, 
	0x00, 0x00, 0x00, 0xfe, 0x07, 0xfc, 0x01, 0xc0, 0x00, 0x0f, 0xff, 0xff, 0x03, 0xf8, 0x01, 0xc0, 
	0x00, 0x1f, 0xff, 0xff, 0x01, 0xf8, 0x01, 0xc0, 0x00, 0x1c, 0x00, 0x00, 0x00, 0x38, 0x01, 0xc0, 
	0x00, 0x3c, 0x00, 0x00, 0x00, 0x38, 0x01, 0xc0, 0x00, 0x38, 0x00, 0x00, 0x00, 0x38, 0x01, 0xc0, 
	0x00, 0x3c, 0x00, 0x00, 0x00, 0x38, 0x01, 0xc0, 0x00, 0x1f, 0xe0, 0x00, 0x00, 0x38, 0x01, 0xc0, 
	0x00, 0x1f, 0xe0, 0x00, 0x00, 0x38, 0x01, 0xc0, 0x00, 0x1f, 0xe0, 0x00, 0x00, 0x38, 0x01, 0xc0, 
	0x00, 0x3c, 0x00, 0x00, 0x00, 0x38, 0x01, 0xc0, 0x00, 0x38, 0x00, 0x00, 0x00, 0x38, 0x01, 0xc0, 
	0x00, 0x38, 0x00, 0x00, 0x00, 0x38, 0x01, 0xc0, 0x00, 0x1c, 0x00, 0x00, 0x00, 0x38, 0x01, 0xc0, 
	0x00, 0x1f, 0xe0, 0x00, 0x00, 0x38, 0x01, 0xc0, 0x00, 0x1f, 0xe0, 0x00, 0x00, 0x38, 0x01, 0xc0, 
	0x00, 0x3c, 0x00, 0x00, 0x00, 0x38, 0x01, 0xc0, 0x00, 0x38, 0x00, 0x00, 0x00, 0x38, 0x01, 0xc0, 
	0x00, 0x38, 0x00, 0x00, 0x00, 0x38, 0x01, 0xc0, 0x00, 0x3c, 0x00, 0x00, 0x00, 0x38, 0x01, 0xc0, 
	0x00, 0x1f, 0xe0, 0x00, 0x00, 0x38, 0xe1, 0xc0, 0x00, 0x0f, 0xe0, 0x00, 0x00, 0x38, 0xe1, 0xc0, 
	0x00, 0x07, 0xc0, 0x00, 0x00, 0x38, 0xe1, 0xc0, 0x00, 0x07, 0x00, 0x00, 0x00, 0x38, 0x01, 0xc0, 
	0x00, 0x07, 0x00, 0x00, 0x01, 0xf8, 0x01, 0xc0, 0x00, 0x03, 0x80, 0x00, 0x0f, 0xf8, 0x01, 0xc0, 
	0x00, 0x03, 0xff, 0xff, 0xff, 0xff, 0xff, 0xc0, 0x00, 0x01, 0xff, 0xff, 0xfe, 0x1f, 0xff, 0xc0, 
	0x00, 0x00, 0xff, 0xff, 0xf0, 0x0f, 0xff, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
};

// 'gears-6281075_640', 128x64px
const unsigned char bitmap_gears [] PROGMEM = {
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x3f, 0xc0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x3f, 0xc0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x30, 0xc0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
	0x00, 0x00, 0x00, 0x00, 0x00, 0x1c, 0x30, 0xc3, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
	0x00, 0x00, 0x00, 0x00, 0x00, 0x3e, 0x70, 0xe7, 0xc0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
	0x00, 0x00, 0x00, 0x00, 0x00, 0x77, 0xf0, 0xfe, 0xc0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
	0x00, 0x00, 0x00, 0x00, 0x00, 0xe3, 0xc0, 0x3c, 0x70, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
	0x00, 0x00, 0x00, 0x00, 0x00, 0xc1, 0x00, 0x08, 0x30, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
	0x00, 0x00, 0x00, 0x00, 0x00, 0xc0, 0x00, 0x00, 0x38, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
	0x00, 0x00, 0x00, 0x00, 0x00, 0xe0, 0x00, 0x01, 0xf0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
	0x00, 0x00, 0x00, 0x00, 0x00, 0x70, 0x1f, 0x83, 0xff, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
	0x00, 0x00, 0x00, 0x00, 0x00, 0x70, 0x3f, 0xe3, 0x80, 0xc0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
	0x00, 0x00, 0x00, 0x00, 0x00, 0x60, 0xf0, 0xf3, 0x80, 0xc0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
	0x00, 0x00, 0x00, 0x00, 0x03, 0xe1, 0xc0, 0x3b, 0x80, 0xc0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
	0x00, 0x00, 0x00, 0x00, 0x0f, 0xc1, 0x83, 0x1b, 0x80, 0xc0, 0xc0, 0x00, 0x00, 0x00, 0x00, 0x00, 
	0x00, 0x00, 0x00, 0x00, 0x0f, 0xc3, 0x87, 0xdb, 0x80, 0xc1, 0xb0, 0x00, 0x00, 0x00, 0x00, 0x00, 
	0x00, 0x00, 0x00, 0x00, 0x0c, 0x03, 0x0c, 0x7f, 0x00, 0xf3, 0x18, 0x00, 0x00, 0x00, 0x00, 0x00, 
	0x00, 0x00, 0x00, 0x00, 0x0c, 0x03, 0x18, 0x3c, 0x00, 0x1e, 0x0c, 0x00, 0x00, 0x00, 0x00, 0x00, 
	0x00, 0x00, 0x00, 0x00, 0x0c, 0x03, 0x30, 0x10, 0x00, 0x04, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 
	0x00, 0x00, 0x00, 0x00, 0x0c, 0x01, 0xe0, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 
	0x00, 0x00, 0x00, 0x00, 0x0f, 0xc1, 0xc0, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 
	0x00, 0x00, 0x00, 0x00, 0x03, 0xe1, 0xe0, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 
	0x00, 0x00, 0x00, 0x00, 0x00, 0x60, 0xf0, 0x00, 0x00, 0x00, 0x06, 0x00, 0x00, 0x00, 0x00, 0x00, 
	0x00, 0x00, 0x00, 0x00, 0x00, 0x70, 0x78, 0x00, 0x00, 0x00, 0x0c, 0x00, 0x00, 0x00, 0x00, 0x00, 
	0x00, 0x00, 0x00, 0x00, 0x00, 0x70, 0x18, 0x00, 0x3e, 0x00, 0x18, 0x00, 0x00, 0x00, 0x00, 0x00, 
	0x00, 0x00, 0x00, 0x00, 0x00, 0xe0, 0x38, 0x00, 0xff, 0x80, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 
	0x00, 0x00, 0x00, 0x00, 0x00, 0xfc, 0x38, 0x03, 0xf8, 0xe0, 0x0c, 0x00, 0x00, 0x00, 0x00, 0x00, 
	0x00, 0x00, 0x00, 0x00, 0x01, 0xff, 0x30, 0x07, 0xff, 0x30, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 
	0x00, 0x00, 0x00, 0x00, 0x01, 0x83, 0xf0, 0x0f, 0xc1, 0x90, 0x06, 0x00, 0x00, 0x00, 0x00, 0x00, 
	0x00, 0x00, 0x00, 0x00, 0x02, 0x01, 0xff, 0xff, 0x80, 0x58, 0x07, 0xf0, 0x00, 0x00, 0x00, 0x00, 
	0x00, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x00, 0x28, 0x00, 0x30, 0x00, 0x00, 0x00, 0x00, 
	0x00, 0x00, 0x00, 0x00, 0x0c, 0x00, 0x00, 0x00, 0x00, 0x2c, 0x00, 0x10, 0x00, 0x00, 0x00, 0x00, 
	0x00, 0x00, 0x00, 0x00, 0x0c, 0x00, 0x00, 0x00, 0x00, 0x2c, 0x00, 0x10, 0x00, 0x00, 0x00, 0x00, 
	0x00, 0x00, 0x00, 0x00, 0x0c, 0x00, 0x00, 0x00, 0x00, 0x2c, 0x00, 0x10, 0x00, 0x00, 0x00, 0x00, 
	0x00, 0x00, 0x00, 0x00, 0x0c, 0x00, 0x00, 0x00, 0x00, 0x2c, 0x00, 0x10, 0x00, 0x00, 0x00, 0x00, 
	0x00, 0x00, 0x00, 0x00, 0x0c, 0x00, 0x00, 0x00, 0x00, 0x2c, 0x00, 0x10, 0x00, 0x00, 0x00, 0x00, 
	0x00, 0x00, 0x00, 0x00, 0x06, 0x00, 0xff, 0xff, 0x00, 0x4c, 0x00, 0x30, 0x00, 0x00, 0x00, 0x00, 
	0x00, 0x00, 0x00, 0x00, 0x03, 0x01, 0xff, 0xff, 0xc0, 0xd8, 0x03, 0xf0, 0x00, 0x00, 0x00, 0x00, 
	0x00, 0x00, 0x00, 0x00, 0x01, 0x81, 0xf0, 0x0c, 0x73, 0x90, 0x07, 0xe0, 0x00, 0x00, 0x00, 0x00, 
	0x00, 0x00, 0x00, 0x00, 0x00, 0x81, 0x10, 0x06, 0x1c, 0x30, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 
	0x00, 0x00, 0x00, 0x00, 0x00, 0x81, 0x10, 0x03, 0x00, 0x60, 0x0c, 0x00, 0x00, 0x00, 0x00, 0x00, 
	0x00, 0x00, 0x00, 0x00, 0x00, 0x81, 0x08, 0x01, 0xc1, 0xc0, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 
	0x00, 0x00, 0x00, 0x00, 0x00, 0x81, 0x08, 0x00, 0x7e, 0x00, 0x18, 0x00, 0x00, 0x00, 0x00, 0x00, 
	0x00, 0x00, 0x00, 0x00, 0x00, 0x81, 0x18, 0x00, 0x00, 0x00, 0x0c, 0x00, 0x00, 0x00, 0x00, 0x00, 
	0x00, 0x00, 0x00, 0x00, 0x00, 0x81, 0x30, 0x00, 0x00, 0x00, 0x06, 0x00, 0x00, 0x00, 0x00, 0x00, 
	0x00, 0x00, 0x00, 0x00, 0x00, 0x81, 0x60, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 
	0x00, 0x00, 0x00, 0x00, 0x00, 0x81, 0xc0, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 
	0x00, 0x00, 0x00, 0x00, 0x00, 0x81, 0x60, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 
	0x00, 0x00, 0x00, 0x00, 0x00, 0x81, 0x30, 0x00, 0x00, 0x00, 0x06, 0x00, 0x00, 0x00, 0x00, 0x00, 
	0x00, 0x00, 0x00, 0x00, 0x00, 0x81, 0x18, 0x3c, 0x00, 0x1e, 0x0c, 0x00, 0x00, 0x00, 0x00, 0x00, 
	0x00, 0x00, 0x00, 0x00, 0x00, 0x81, 0x0c, 0x67, 0x00, 0xf3, 0x18, 0x00, 0x00, 0x00, 0x00, 0x00, 
	0x00, 0x00, 0x00, 0x00, 0x00, 0x81, 0x06, 0xc1, 0x00, 0xc1, 0xb0, 0x00, 0x00, 0x00, 0x00, 0x00, 
	0x00, 0x00, 0x00, 0x00, 0x00, 0x81, 0x83, 0x81, 0x00, 0xc0, 0xe0, 0x00, 0x00, 0x00, 0x00, 0x00, 
	0x00, 0x00, 0x00, 0x00, 0x01, 0x80, 0xc0, 0x01, 0x00, 0xc0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
	0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x60, 0x01, 0x00, 0xc0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
	0x00, 0x00, 0x00, 0x00, 0x03, 0x00, 0x20, 0x01, 0x80, 0xc0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
	0x00, 0x00, 0x00, 0x00, 0x02, 0x00, 0x20, 0x00, 0xff, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
	0x00, 0x00, 0x00, 0x00, 0x02, 0x00, 0x20, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
	0x00, 0x00, 0x00, 0x00, 0x02, 0x00, 0x20, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
	0x00, 0x00, 0x00, 0x00, 0x03, 0x00, 0x60, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
	0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
	0x00, 0x00, 0x00, 0x00, 0x00, 0x80, 0xc0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
	0x00, 0x00, 0x00, 0x00, 0x00, 0x63, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
	0x00, 0x00, 0x00, 0x00, 0x00, 0x3e, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
};

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

  //OLED power up
  display.begin(i2c_Address, true); 
  display.display();
  delay(2000);
  

  //relay position set
  pinMode(relay_1, OUTPUT);
  pinMode(relay_2, OUTPUT);
  digitalWrite(relay_1, LOW);
  digitalWrite(relay_2, LOW);

 // Set encoder pins as inputs
  pinMode(ENCODER_CLK, INPUT);
  pinMode(ENCODER_DT, INPUT);

//initialize pins
  pinMode(BTN_1_PIN, INPUT_PULLUP);
  pinMode(LED_BUILTIN, OUTPUT);

 //subscribe to needed events
  _ezb.Subscribe(BTN_1, Btn1Pressed, PRESSED);
  _ezb.Subscribe(BTN_1, Btn1Released, RELEASED);
  _ezb.Subscribe(BTN_1, Btn1Hold, HOLD);
  _ezb.Subscribe(BTN_1, Btn1HoldReleased, HOLD_RELEASED);

 // pinMode(PWMPin, INPUT_PULLUP);
  pinMode (EN_PIN, OUTPUT);
  digitalWrite(EN_PIN, LOW);
  stepper.setMaxSpeed(motorMaxSpeed);
  stepper.setAcceleration(motorAccel);
  stepper.moveTo(0);
  delay(1000);

  display.clearDisplay();
  display.setTextSize(1);
  display.setTextColor(SH110X_WHITE);
  display.setCursor(30, 30);
  display.print(PROJEKT);
  display.display();
  delay(1000);

//init done information
  Serial.print("Init done");
  delay(10);
  display.clearDisplay();
  delay(2000);
  display.drawBitmap(32, 0, epd_bitmap_OK, 64, 64, SH110X_WHITE);
  display.display();
  display.setTextSize(8);

}

void loop() {

 /* unsigned long currentMillis = millis();*/

  int newClk = digitalRead(ENCODER_CLK);
  if (newClk != lastClk) {
    // There was a change on the CLK pin
    lastClk = newClk;
    int dtValue = digitalRead(ENCODER_DT);
    // Rotating clockwise causes the CLK pin to go low first, and then the DT pin goes low too.
    if (newClk == LOW && dtValue == HIGH) {
      counterEnc--;
      if (counterEnc <= 1 ) {
        counterEnc = 1;
        }
      Serial.print("Rotated clockwise ⏩ Pos:");
      Serial.println(counterEnc);

    
    }
    // Rotating counterclockwise causes the DT pin to go low first, and then the CLK pin go low.
    if (newClk == LOW && dtValue == LOW) {
      counterEnc++;
      if (counterEnc >= 20 ) {
        counterEnc = 20;
        }
      Serial.print("Rotated counterclockwise ⏪ Pos:");
      Serial.println(counterEnc);

    }
    //pulse_cnt++;
        
     counter2 = counterEnc;

  }

/*
  // Calculate speed by counting the pulses during a second
  if (currentMillis-previousMillis >= 1000) {
    // After counting the pulses we have to calculate the speed(revolutions per minute),
    // by knowing that KY-040 Rotary Encoder module has 20 steps per revolution.
    // We derive the equation: RPM =(counts_second / 20)* 60s = counts_second * 3
    int RPM = pulse_cnt * 3;
    previousMillis = currentMillis;
    pulse_cnt = 0;
    Serial.print("RPM = ");
    Serial.println(RPM);
  }*/

_ezb.Loop();
/*
Serial.println(counter2);

    
    dock = counter;
    if(counter <= 9) {
    display.clearDisplay();
    display.setCursor(40, 3);
    display.println(dock);
    display.display();
    }
    else {
    display.clearDisplay();
    display.setCursor(15, 3);
    display.println(dock);
    display.display();
    }*/

//correct DCC polarity to position
unsigned long currentPolarityMillis = millis();
if(currentPolarityMillis - previousPolarityMillis >= 500 ) { //mala hodnota seka program
previousPolarityMillis = currentPolarityMillis;
checkPolarity();
updateDisplay();
   }

}

void updateDisplay() {
    /*dock = counterEnc;
    if(counterEnc <= 9) {
    display.clearDisplay();
    display.setCursor(40, 3);
    display.println(dock);
    display.display();
    }
    else {
    display.clearDisplay();
    display.setCursor(15, 3);
    display.println(dock);
    display.display();
    } */
}

// button pressed
void Btn1Pressed() {
  digitalWrite(LED_BUILTIN, HIGH);
  Serial.println("pressed");
}

// button released
void Btn1Released() {
  digitalWrite(LED_BUILTIN, LOW);
  Serial.println("released");
}

// button hold
bool state = true;
void Btn1Hold() {
  state = !state;
  digitalWrite(LED_BUILTIN, state);
  Serial.println("hold");
}

// button released after hold
void Btn1HoldReleased() {
  for (int i = 0; i < 6; i++) {
    state = !state;
    digitalWrite(LED_BUILTIN, state);
    delay(50);
  }
  digitalWrite(LED_BUILTIN, LOW);
  Serial.println("hold released");
}

void checkPolarity() {
if (counter2 >= 5 && counter2 < 15) {
//Serial.println("Normal Polarity");
digitalWrite(relay_1, LOW);
digitalWrite(relay_2, LOW);
    }
    else {
digitalWrite(relay_1, HIGH);
digitalWrite(relay_2, HIGH);
    }
}

```

The code works perfect until serial monitoring, immidiatelly I want to send the encoder position to the display, it is not stabil.

---

<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:** [November 26, 2024, 2:00am UTC](https://forum.arduino.cc/t/oled-display-problem/1325812/2 "2024-11-26T02:00:20Z")

</div>

> [@slovakfan83](#):
>
> code works perfect until

"works perfect" does not equal "does not work"

> [@slovakfan83](#):
>
> rotary encoder too, it is skipping steps

How is an encoder an OLED problem? Your code is a mashup and you lost control. Start here... does this compile and show on your OLED?

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

#define i2c_Address 0x3C
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
#define OLED_RESET -1
Adafruit_SH1106G display = Adafruit_SH1106G(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);

void setup() {
  Serial.begin(115200);
  display.begin(i2c_Address, true);
  display.clearDisplay();

  display.setTextColor(SH110X_WHITE);
  display.setCursor(30, 30);
  display.print("Hello, World!");
  display.display();
}

void loop() {
}

```

Side note... your delays probably caused the problems you observe, from the rotary encoder sketch you used in your mashup...

> Note: your code will need to read the state of the pins frequently in order to detect the rotations correctly. If your `loop()` takes too long (e.g. you use `delay()` in your code), we recommend using [attachInterrupt()](https://www.arduino.cc/reference/en/language/functions/external-interrupts/attachinterrupt/) to listen for changes in the CLK pin. Assuming CLK is connected to pin 2, and DT to pin 3

Continuing with testing devices, add the rotary switch...

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

#define i2c_Address 0x3C
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
#define OLED_RESET -1
Adafruit_SH1106G display = Adafruit_SH1106G(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);

#define ENCODER_CLK 2
#define ENCODER_DT 3
int lastClk = HIGH;

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

  pinMode(ENCODER_CLK, INPUT);
  pinMode(ENCODER_DT, INPUT);

  display.begin(i2c_Address, true);
  display.clearDisplay();

  display.setTextColor(SH110X_WHITE);
  display.setCursor(30, 30);
  display.print("Hello, World!");
  display.display();
}

void loop() {
  int newClk = digitalRead(ENCODER_CLK);
  if (newClk != lastClk) {
    lastClk = newClk;
    int dtValue = digitalRead(ENCODER_DT);
    if (newClk == LOW && dtValue == HIGH) {
      display.setTextColor(SH110X_BLACK);
      display.setCursor(20, 20);
      display.print(" < CCW");
      display.setTextColor(SH110X_WHITE);
      display.setCursor(80, 20);
      display.print(" CW >");
      display.display();
    }
    if (newClk == LOW && dtValue == LOW) {
      display.setTextColor(SH110X_BLACK);
      display.setCursor(80, 20);
      display.print(" CW >");
      display.setTextColor(SH110X_WHITE);
      display.setCursor(20, 20);
      display.print(" < CCW");
      display.display();
    }
  }
}

```

Add a counter...

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

#define i2c_Address 0x3C
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
#define OLED_RESET -1
Adafruit_SH1106G display = Adafruit_SH1106G(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);

#define ENCODER_CLK 2
#define ENCODER_DT 3
int lastClk = HIGH, counterEnc, counterEncOld;

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

  pinMode(ENCODER_CLK, INPUT);
  pinMode(ENCODER_DT, INPUT);

  display.begin(i2c_Address, true);
  display.clearDisplay();

  display.setTextColor(SH110X_WHITE);
  display.setCursor(30, 30);
  display.print("Hello, World!");
  display.display();
}

void loop() {
  int newClk = digitalRead(ENCODER_CLK);
  if (newClk != lastClk) {
    lastClk = newClk;
    int dtValue = digitalRead(ENCODER_DT);
    if (newClk == LOW && dtValue == HIGH) {
      counterEnc--;
      display.setCursor(20, 20);
      display.setTextColor(SH110X_BLACK);
      display.print(" < CCW");
      display.setCursor(80, 20);
      display.setTextColor(SH110X_WHITE);
      display.print(" CW >");
    }
    if (newClk == LOW && dtValue == LOW) {
      counterEnc++;
      display.setCursor(80, 20);
      display.setTextColor(SH110X_BLACK);
      display.print(" CW >");
      display.setCursor(20, 20);
      display.setTextColor(SH110X_WHITE);
      display.print(" < CCW");
    }
    if (counterEncOld != counterEnc) {
      if (counterEncOld < 0)
        display.setCursor(60, 20);
      else
        display.setCursor(65, 20);
      display.setTextColor(SH110X_BLACK);
      display.print(counterEncOld);

      counterEncOld = counterEnc; // store count

      if (counterEnc < 0)
        display.setCursor(60, 20);
      else
        display.setCursor(65, 20);
      display.setTextColor(SH110X_WHITE);
      display.print(counterEnc);
    }
    display.display();
  }
}

```

> **diagram.json for wokwi.com**
>
> ```arduino
> {
> "version": 1,
> "author": "Anonymous maker",
> "editor": "wokwi",
> "parts": [
> { "type": "wokwi-arduino-nano", "id": "nano", "top": -4.8, "left": -0.5, "attrs": {} },
> {
> "type": "board-ssd1306",
> "id": "oled1",
> "top": 41.54,
> "left": 173.03,
> "attrs": { "i2cAddress": "0x3c" }
> },
> { "type": "wokwi-ky-040", "id": "encoder1", "top": -84.7, "left": -48.8, "attrs": {} },
> { "type": "wokwi-relay-module", "id": "relay1", "top": -67, "left": 163.2, "attrs": {} },
> { "type": "wokwi-relay-module", "id": "relay2", "top": -143.8, "left": 163.2, "attrs": {} },
> {
> "type": "wokwi-led",
> "id": "led1",
> "top": -166.8,
> "left": 291.8,
> "attrs": { "color": "red" }
> },
> { "type": "wokwi-led", "id": "led2", "top": -90, "left": 291.8, "attrs": { "color": "red" } }
> ],
> "connections": [
> ["oled1:GND", "nano:GND.1", "black", ["v-9.6", "h38.4"] ],
> ["oled1:VCC", "nano:5V", "red", ["v-38.4", "h-19.05"] ],
> ["oled1:SCL", "nano:A5", "green", ["v-28.8", "h-66.9"] ],
> ["oled1:SDA", "nano:A4", "green", ["v-19.2", "h-86.33"] ],
> ["encoder1:CLK", "nano:2", "green", ["h0"] ],
> ["nano:3", "encoder1:DT", "green", ["v0"] ],
> ["encoder1:VCC", "nano:5V", "red", ["h0"] ],
> ["encoder1:GND", "nano:GND.1", "black", ["h0"] ],
> ["nano:GND.1", "relay1:GND", "black", ["v0"] ],
> ["nano:5V", "relay1:VCC", "red", ["v0"] ],
> ["nano:4", "encoder1:SW", "green", ["v0"] ],
> ["nano:5", "relay1:IN", "green", ["v0"] ],
> ["nano:6", "relay2:IN", "green", ["v0"] ],
> ["nano:5V", "relay2:VCC", "red", ["v0"] ],
> ["nano:GND.1", "relay2:GND", "black", ["v0"] ],
> ["relay1:COM", "led2:A", "red", ["h0"] ],
> ["nano:5V", "relay1:NO", "red", ["v-48", "h192", "v-28.8"] ],
> ["nano:GND.1", "led2:C", "black", ["v-57.6", "h163.2"] ],
> ["nano:GND.1", "led1:C", "black", ["v-134.4", "h153.6", "v-28.8"] ],
> ["nano:5V", "relay2:NO", "red", ["v-48", "h201.6", "v-105.6"] ],
> ["relay2:COM", "led1:A", "red", ["h0"] ]
> ],
> "dependencies": {}
> }
> 
> ```

---

<div class="post-metadata">

**Author:** ![slovakfan83](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/slovakfan83/32/1098708_2.png) [@slovakfan83](https://forum.arduino.cc/u/slovakfan83)\
**Post date:** [November 26, 2024, 7:58pm UTC](https://forum.arduino.cc/t/oled-display-problem/1325812/3 "2024-11-26T19:58:23Z")

</div>

Thank you, I did not see what was wrong, I rebuilt this code again and all works.

```arduino
#include <Wire.h>
#include <U8g2lib.h>
#include <AccelStepper.h>
#include <EZButton.h>

// Stepper motor driver
#define STEP_PIN 2 //step
#define DIR_PIN 5 //direction
#define EN_PIN 6 //stepper driver enable pin

// Define a stepper and the pins it will use 6400 steps per round with TCM2208 and NEMA 17 Creality 42-34
AccelStepper stepper(AccelStepper::DRIVER, STEP_PIN, DIR_PIN); // (Type of driver: with 2 pins, STEP, DIR - for TMC2208 Aliexpres driver)
int motorMaxSpeed = 150; //50 maximum steps per second (about 3rps / at 16 microsteps)
int motorAccel = 10; //steps/second/second to accelerate

//Display
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
U8G2_SH1106_128X64_NONAME_F_HW_I2C u8g2(U8G2_R0, /* reset=*/ U8X8_PIN_NONE);
bool needDisplayUpdate = false;

//Encoder with switch
#define ENCODER_CLK 3 // CLK pin 
#define ENCODER_DT 8 // DT pin 

//encoder
volatile int encoderPos = 0;
int lastEncoderPos = 0;
unsigned long lastInterruptTime = 0;
unsigned long debounceDelay = 40; // debounce time 20 default

//button
#define BTN_1_PIN 9
#define BTN_1 0
void ReadButtons(bool *states, int num) {
  //Read button states
  states[BTN_1] = !digitalRead(BTN_1_PIN); }

EZButton _ezb(1, ReadButtons, 1000, 500, 5);

//relay 2 module
int relay_2 = 4;
int relay_1 = 7;

//DCC orientation timer
unsigned long previousPolarityMillis;
unsigned long previousButtonMillis;

int encoderPolarity;

void setup() {

  // encoder 
  pinMode(ENCODER_CLK, INPUT_PULLUP);
  pinMode(ENCODER_DT, INPUT_PULLUP);
 
  // button 
  pinMode(BTN_1_PIN, INPUT_PULLUP);
  pinMode(LED_BUILTIN, OUTPUT);

   //subscribe to needed events for button
  _ezb.Subscribe(BTN_1, Btn1Pressed, PRESSED);
  _ezb.Subscribe(BTN_1, Btn1Released, RELEASED);
  _ezb.Subscribe(BTN_1, Btn1Hold, HOLD);
  _ezb.Subscribe(BTN_1, Btn1HoldReleased, HOLD_RELEASED);

  // motor driver;
  pinMode (EN_PIN, OUTPUT);
  digitalWrite(EN_PIN, LOW);
  stepper.setMaxSpeed(motorMaxSpeed);
  stepper.setAcceleration(motorAccel);
  stepper.moveTo(0);

  //relay set
  pinMode(relay_1, OUTPUT);
  pinMode(relay_2, OUTPUT);
  digitalWrite(relay_1, LOW);
  digitalWrite(relay_2, LOW);

  //display
  u8g2.begin();

  u8g2.clearBuffer();
  u8g2.setFont(u8g2_font_fub49_tn); 
  u8g2.setCursor(22 , 57);
  u8g2.println("00");
  u8g2.sendBuffer();

  attachInterrupt(digitalPinToInterrupt(ENCODER_CLK), handleEncoder, CHANGE);

  delay(500);

}

void loop() {

_ezb.Loop();

  if (encoderPos != lastEncoderPos) {
    lastEncoderPos = encoderPos;
    needDisplayUpdate = true;
  }

 if(needDisplayUpdate) {
    if(encoderPos <= 9) {
    u8g2.clearBuffer();
    u8g2.setCursor(37, 57);
    u8g2.println(encoderPos);
    u8g2.sendBuffer();
    }
    else {
    u8g2.clearBuffer();
    u8g2.setCursor(22, 57);
    u8g2.println(encoderPos);
    u8g2.sendBuffer();
    }
		needDisplayUpdate = false;
 }

//correct DCC polarity to position
unsigned long currentPolarityMillis = millis();
if(currentPolarityMillis - previousPolarityMillis >= 1000 ) { 
previousPolarityMillis = currentPolarityMillis;
checkPolarity();
  }

}

/////////////////////////////////functions////////////////////////////////

void handleEncoder() {
  unsigned long currentMillis = millis();

  if (currentMillis - lastInterruptTime > debounceDelay) {
    int CLKstate = digitalRead(ENCODER_CLK);
    int DTstate = digitalRead(ENCODER_DT);

    if (CLKstate == DTstate) {
      encoderPos--;
      if (encoderPos <= 1 ) {
        encoderPos = 1;
        }
    } else {
      encoderPos++;
      if (encoderPos >= 20 ) {
        encoderPos = 20;
        }
    }

    lastInterruptTime = currentMillis;
  }
}

///////////////////////////////////////button/////////////////////////////////////

void Btn1Pressed() {
  digitalWrite(LED_BUILTIN, HIGH);
    int motor_1_Position = ( 320 * encoderPos );
    stepper.runToNewPosition(motor_1_Position);
    encoderPolarity = encoderPos;
}

void Btn1Released() {
  digitalWrite(LED_BUILTIN, LOW);
}

bool state = true;
void Btn1Hold() {
  
  state = !state;
  digitalWrite(LED_BUILTIN, state);
  
}

void Btn1HoldReleased() {
  for (int i = 0; i < 6; i++) {
    digitalWrite(EN_PIN, HIGH);
    state = !state;
    digitalWrite(LED_BUILTIN, state);
    
    delay(5);
  }
  digitalWrite(LED_BUILTIN, LOW);
  digitalWrite(EN_PIN, LOW);
}

////////////////////////////////////////polarity////////////////////////////////

//change polarity if necessary
void checkPolarity() {
if (encoderPolarity >= 5 && encoderPolarity < 15) {
//Serial.println("Normal Polarity");
digitalWrite(relay_1, LOW);
digitalWrite(relay_2, LOW);
    }
    else {
digitalWrite(relay_1, HIGH);
digitalWrite(relay_2, HIGH);
    }
}

```

---

<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:** [May 25, 2025, 7:59pm UTC](https://forum.arduino.cc/t/oled-display-problem/1325812/4 "2025-05-25T19:59:18Z")

</div>

Closed on May 25, 2025, 7:59pm UTC

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