How 5-way tactile switch works ? (also called multi directional or navigation )

Hi! (Sorry for my bad enlglish :slight_smile: )
So I'm working with 0.91 oled and I'm trying to print the direction switch was touched. I haven't find any tutorial for this button (which I bought) https://www.aliexpress.com/item/5pcs-lot-10-10-9-SMD-5-five-way-switch-10-10-9-MM-multi-function/32807613480.html nor any similiar module.

After a while I found circuit diagram, so I wired pins 2-6 to digital pins 2-6 , pin 1 to GND (diagram in attachment)

#include <Arduino.h>
#include <U8g2lib.h>
#include <SPI.h>
#include <Wire.h>
#include <elapsedMillis.h>

elapsedMillis timeElapsed;

U8G2_SSD1306_128X32_UNIVISION_F_HW_I2C u8g2(U8G2_R0); 

void setup() {
  u8g2.begin();
  pinMode(2,INPUT);
  pinMode(3,INPUT);
  pinMode(4,INPUT);
  pinMode(5,INPUT);
  pinMode(6,INPUT);
}

void loop() {
   char buf[20];
//time elapsed
   u8g2.clearBuffer();         // clear the internal memory
   u8g2.setFont(u8g2_font_astragal_nbp_tf);  // choose a suitable font at https://github.com/olikraus/u8g2/wiki/fntlistall
   u8g2.drawStr(60,32,itoa(timeElapsed,buf,10));  // write something to the internal memory
   u8g2.sendBuffer();         // transfer internal memory to the display
   delay(20);
//direction
   if((digitalRead(2) == HIGH)  || (digitalRead(3) == HIGH)  || (digitalRead(4) == HIGH)  || (digitalRead(5) == HIGH)  || (digitalRead(6) == HIGH)) {   
    u8g2.setFont(u8g2_font_astragal_nbp_tf);
    u8g2.drawStr(1,32,"high"); 
    u8g2.sendBuffer();    
   }
       
   else {    
    u8g2.setFont(u8g2_font_astragal_nbp_tf);
    u8g2.drawStr(1,32,"low"); 
    u8g2.sendBuffer();  }
}

Code does print elapsed time in miliseconds just fine but when it comes to if statement - it only blinks "high" (if one or more of inputs is high) even if I haven't pressed the button. when I press the button to the left it shows "low" (pin 2 - is controled first in if statement) , when I press the button in center (pin 3) it shows "low" only sometimes. Every other directions Oled shows as "HIGH".

I also tried printing the direction you pressed. (pseudocode : if pin 2 == high then print "left" - this for every pin ) Oled printed "left" 70% of time, "center" 20% of time and "up" the rest. When I pressed left direction - oled shown "left" , center - mostly "center" but sometimes left, up - left or center ...

So whats the problem - wiring, code or my knowledge of buttons ? :smiley: thanks for help

Enable the internal pullup resistors on the switch inputs, so the inputs are not floating when the switch is open, with
pinMode(pin, INPUT_PULLUP). The inputs will read HIGH when the switch is open and LOW when the switch is closed. Adjust the switch logic in the program, accordingly.

Good job with the use of code tags on Your first post. +1

It's working now, thank you so much :).