Even if I don't push the button, it reads HIGH value

I'm a new Aurdino user and I'm trying to create a project to hsow heat, moisture and clock&date information to PCD854 screen. with bellow connection I can read the values and print them on to screen without problem. I want to add three button to setup clock and date butwhen I connect the buttons to digital pins, Aurdino read the digital pin value as a High always and I couldn't find the what is problem. Right I just add below code to see if button press function is working or not but even if I don't press the button, it shows as pressed all time. Is there any suggestion to solve issue?

below is my wiring of project:

Welcome to the forum

I see no code

Sorry, all code is below:

#include "DHT.h"
#include <SPI.h>
#include <Adafruit_GFX.h>
#include <Adafruit_PCD8544.h>
#include "RTClib.h"

// Define the connections pins:
#define CLK 2
#define DIO 3

const int buttonPin1=9;
const int buttonPin2=10;
const int buttonPin3=11;

// Create rtc and display object:
RTC_DS3231 rtc;

// Declare LCD object for software SPI
Adafruit_PCD8544 display = Adafruit_PCD8544(7, 6, 5, 4, 3);

#define DHTPIN 2
#define DHTTYPE DHT11

DHT dht(DHTPIN, DHTTYPE);

String extractLastTwoChars(const String &str) {//sağdan trimleme için fonksiyon yazılıyor
  // Check if the string has at least two characters
  if (str.length() >= 2) {
    // Extract the last two characters
    return str.substring(str.length() - 2);
  } else {
    // Handle the case where the string has less than two characters
    return str;
  }
}


void setup() {
  Serial.begin(9600);
  pinMode(buttonPin1, INPUT);
  pinMode(buttonPin2, INPUT);
  pinMode(buttonPin3, INPUT);
  Serial.println("DHRxx test!");
  delay(3000);

  // Check if RTC is connected correctly:
  if (!rtc.begin()) {
    Serial.println("Couldn't find RTC");
    while (1);
  }

  // Check if the RTC lost power and if so, set the time:
  if (rtc.lostPower()) {
    Serial.println("RTC lost power, lets set the time!");
    rtc.adjust(DateTime(2024, 1, 10, 15, 28, 05));
  }

  dht.begin();

  // Initialize Display
  display.begin();
  display.setContrast(57);
}

void loop() {
  float h = dht.readHumidity();
  float t = dht.readTemperature();
  float f = dht.readTemperature(true);

  if (isnan(h) || isnan(t) || isnan(f)) {
    Serial.println("DHT sensöründen veriler okunamadı");
    return;
  }

  float hic = dht.computeHeatIndex(t, h, false);

  display.clearDisplay();
  display.setTextColor(BLACK);
  display.setCursor(0, 0);
  display.setTextSize(0.5);
  display.println(String(t) + "*C "+ String(h) + "%");
  display.println("HeatIn:" + String(hic) + "*C");

  DateTime now = rtc.now();

  String originalString = String(now.year(), DEC);
  String lastTwoChars = extractLastTwoChars(originalString);
  if (now.hour() < 10) {
    if (now.minute()<10){
      display.println("0" + String(now.hour()) + ((now.second() % 2 == 0) ? " " : ":") + "0"+String(now.minute())+" "+String(now.day(), DEC) + "." + String(now.month(), DEC) + "." + lastTwoChars);
    } else {
      display.println("0" + String(now.hour()) + ((now.second() % 2 == 0) ? " " : ":") + String(now.minute())+" "+String(now.day(), DEC) + "." + String(now.month(), DEC) + "." + lastTwoChars);
    }
    
  } else {
    if (now.minute()<10){
    display.println(String(now.hour()) + ((now.second() % 2 == 0) ? " " : ":") + "0" + String(now.minute())+" "+String(now.day(), DEC) + "." + String(now.month(), DEC) + "." + lastTwoChars);
    } else{
      display.println(String(now.hour()) + ((now.second() % 2 == 0) ? " " : ":") + String(now.minute())+" "+String(now.day(), DEC) + "." + String(now.month(), DEC) + "." + lastTwoChars);
    }
  }

   int durum1= digitalRead(buttonPin1);
   int durum2=digitalRead(buttonPin2);
   int durum3=digitalRead(buttonPin3);

if (durum1 == HIGH) {
    display.println("1. buton");
  }

  if (durum2 == HIGH) {
    Serial.println("2. buton");
  }

  if (durum3 == HIGH) {
    Serial.println("3. buton");
  }

  display.display();
  delay(1000);

}

You clearly put much time into preparing your neat fritzing diagram. But it seems to be at a lower resolution than similar images I’ve seen, making the UNO text difficult to read.

@udemirdere
Your diagram looks OK and so does the code.
So I suspect you have a wiring error on your actual breadboard.
Are you sure you have the switches in the right way?
Maybe show a picture of the actual breadboard.

Hi, @udemirdere
Welcome to the forum.
Thanks for posting your code in tags. :+1:

Can you please post some pictures of your project?
So we can see your component layout.

Thanks.. Tom.. :grinning: :+1: :coffee: :australia:

You are taking a big risk. Using the blue/black bus line of the breadboard for 5V is an accident waiting to happen. Always use the blue/black bus for ground. Maybe use the red line on one side of the BB for 5V and the red line on the other side of the BB for 3.3V. This won't prevent all mistakes, if course, but it should significantly reduce the risk.

Here is something to check: often the bus lines on BBs can have a break in the middle which you need to connect across with wire. Normally the break is indicated by a gap in the red & blue/black lines also.

Such little push buttons with their very short legs may not make (good) contact with the breadboard.

Use your multimeter to confirm that they actually make contact - and that the voltage on the Arduino input pin actually changes when you press the button.

1 Like

@udemirdere
On some breadboards there is an interruption of the +V and GND tracks as I marked in the image.
See if this is not the case with your breadboard.

If so, make a jumper connecting the parts.

if (durum1 == HIGH) {
    display.println("1. buton");
  }

  if (durum2 == HIGH) {
    Serial.println("2. buton");
  }

  if (durum3 == HIGH) {
    Serial.println("3. buton");
  }

  display.display();
  delay(1000);

line 108 : display.println("1. buton"); display.println ???? Is correct?
line 112: Serial.println("2. buton"); Seria.println
line 116: Serial.println("3. buton"); Serial.println

thank you for your all messages.

I took below pictures from my bread board. in addition to these pictures, I tried to connect buttons to separate ground from UNO and it didn't make any difference. do you have any suggestion for button confugurition?





you right I forgot to correct code display all three button on screen. but I can read the value from serial monitor and it's again show that I push the button. I add screen shot on my general answer.
thank you

you right,
all night I jusst changed the 5V pins to buttons but I didn't changed ground pins. I just realized that when I'm writing to answer your entry. thank you, now it's solved.

Well, THIS is the problem:
image
Here you must connect (with a jumper or another cable) one blue/red rail with the other blue/red (topmost blue one is the culprit in your case) because top and bottom blue/red rails aren't connected from one side to the other.

Besides, for the future I suggest you a better wire color coding.
First of all, ALWAYS use black wires for GND and red for +5V. This is crucial (and sometimes vital for the devices...).

For other wire types make your kinda "standard" choice (e.g. like yellow for analog inputs, blue for digital, green for buttons, and so on), but always get stuck with "your" standard and avoid mixing colors for the same connection type.

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