[SOLVED] GPIO 33-35 button strange behavior. Changing value without button being pressed !

GeorgeFlorian1
Posts: 160
Joined: Thu Jan 31, 2019 2:32 pm

[SOLVED] GPIO 33-35 button strange behavior. Changing value without button being pressed !

Postby GeorgeFlorian1 » Wed May 08, 2019 8:28 am

Hello !

I am trying to figure out why my simple button works so randomly

This is the code that worked in the past:

Code: Select all

#define BUTTON 34
#define PRESSED HIGH
#define NOT_PRESSED LOW

void setup {
  pinMode(BUTTON,INPUT_PULLUP);
  delay(50);
  digitalWrite(BUTTON, NOT_PRESSED);
  int q = 0;
  if (digitalRead(BUTTON) == PRESSED) {
    q++;
  }
  delay(2000);
  if (digitalRead(BUTTON) == PRESSED) {
    q++;
  }
}

void loop {}
With this code the button is either always PRESSED or NOT_PRESSED. Pressing it doesn't change its value.

With the following changes, the button behaves randomly:

Code: Select all

#define BUTTON 34
#define PRESSED LOW
#define NOT_PRESSED HIGH
With this code the button register values on its own. On startup it can be either PRESSED LOW or NOT_PRESSED HIGH. When it starts in NOT_PRESSED HIGH it works as expected. But when it starts in PRESSED LOW I can't use it.

I've checked with the multi-meter and the button works.

This is the setup:
https://i.postimg.cc/5NMQ1y2P/20190508-115947.jpg
Last edited by GeorgeFlorian1 on Wed May 08, 2019 9:29 am, edited 2 times in total.

GeorgeFlorian1
Posts: 160
Joined: Thu Jan 31, 2019 2:32 pm

Re: GPIO 33-35 button strange behavior. Changing value without button being pressed !

Postby GeorgeFlorian1 » Wed May 08, 2019 9:29 am

I've fixed it.

One of the libraries that I was using for the project was using GPIO 34.
Switching to GPIO 33 fixed it.

Here is the code that works now:

Code: Select all

#define BUTTON 33
#define PRESSED LOW
#define NOT_PRESSED HIGH

void setup {
  pinMode(BUTTON,INPUT_PULLUP);
  delay(50);
  digitalWrite(BUTTON, NOT_PRESSED);
  delay(50);
  int q = 0;
  if (digitalRead(BUTTON) == PRESSED) {
    q++;
  }
  delay(2000);
  if (digitalRead(BUTTON) == PRESSED) {
    q++;
  }
}

void loop {}

Who is online

Users browsing this forum: No registered users and 102 guests