Difficulty in Reading a Push Button

Status
Not open for further replies.

AbhiBjee

New member
Hi All;

I am currently working on a project development, which involves sensor fusion of IMU and Force sensors, but there is also a reset button(pushbutton) involved for recalibration purpose. Initially we used Arduino pro mini for its form factor but faced certain issues with speed and memory due to which we had to switch our controller to Teensy3.2. I have installed the latest version of Teensyduino 1.42 and I am using Arduino IDE version 1.8.5

While using teensy 3.2 I am currently facing an issue in reading Pushbutton states (HIGH or LOW) using the digitalRead arduino command. The circuit which I used to read a pushbutton in arduino pro mini is unaltered and it worked fine with Atmega328p 5V 8Mhz version. But using teensy 3.2 the button value is always zero. I post the test code below.

///////////////////////////////////////////////////////////////////////

Code:
const int buttonPin = 14;     // the number of the pushbutton pin
const int ledPin =  13;      // the number of the LED pin


int buttonVal = 0;         // variable for reading the pushbutton status
int LstBtnVal = 0;
int BtnCnt = 0;

void setup() {
  // initialize the LED pin as an output:
  Serial.begin(115200);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB.
  }
  Serial.println("Starting Code");
  pinMode(ledPin, OUTPUT);
  // initialize the pushbutton pin as an input:
  pinMode(buttonPin, INPUT);
}

void loop() {
  // read the state of the pushbutton value:
  buttonVal = digitalRead(buttonPin);
  
  if (LstBtnVal>buttonVal){
    BtnCnt = BtnCnt+1;    
  }
  Serial.print(buttonVal);
  Serial.print(" ");
  Serial.println(BtnCnt);
  LstBtnVal = buttonVal; 
  delay(50);
}
////////////////////////////////////////////////////////////////////////

The circuit is connected in a way that one end of button goes to 3.3V power pin, other end is connected to a pulldown resistor 10K which is grounded. The MCU readpin D14 is connected to node where the button connects the resistor.

Technically the (buttonVal) should read 1 when the switch is pressed, But it never reads 1. It is always zero.


Please let me know if I am going wrong in any part, May be there can be some silly mistake.

Thanks in advance
 
Last edited:
First up, thanks for taking the time to make test code which made it dead simple to test

And on my hardware here the code works as you intend, showing the input changing between 0 and 1 and the counter increasing.

Suggest checking with a multimeter what voltage is present on the pin as you press and release the button.
 
First up, thanks for taking the time to make test code which made it dead simple to test

And on my hardware here the code works as you intend, showing the input changing between 0 and 1 and the counter increasing.

Suggest checking with a multimeter what voltage is present on the pin as you press and release the button.

Hey Gremlin;

Thanks for getting back to me, I have tried to check the same code on different teensy boards, the result is same. I tested the digital pin voltage too, it reads 3.3V when power is 3.3V also 5 V when the power is 5V. I think the digitalRead syntax is not being able to read the voltage at the pin.

Any suggestions? how to fix it ??
 
The input pin should read 0 when the pin voltage is less than ~1V and 1 above about 2.8V. If you are measuring 3.3 and 5V then you have an electrical problem to sort out.

Suggest fixing things so electrically you get clean digital logic on the pin both button up and button down and only then revisit the code.

Do remember that teensy are 3.3V logic and most others OTHER than the 3.2 will die if you randomly apply 5V to pins, same as applying more than 5V to a uno.
 
I think the digitalRead syntax is not being able to read the voltage at the pin.

digitalRead(14) absolutely does work. I do not know what is causing the problem you're seeing, but I can say with certainty it is NOT caused by a defect in digitalRead().


Any suggestions? how to fix it ??

We can help much more if you show photos of how you've actually connected the hardware. Often these sorts of problems are a simple misunderstanding, which we can't anticipate without seeing photos.

But I can at least confirm digitalRead() and your program do work when the hardware is connected properly. (even though GremlinWrangler already confirmed) Here is the result I see in the serial monitor after quickly pressing the button many times (same as GremlinWrangler described).

sc.png

Here is a photo of the Teensy 3.2 and pushbutton I used to run this test.

DSC_0196_web.jpg

Hopefully this can as least show you proof that digitalRead is *NOT* the problem. Something else is wrong. Focusing on digitalRead is only going to distract you from finding the real issue!

Sharing photos is easy. Let us try to help you figure out what's wrong by posting photos to show how you have really connected the hardware.
 
Status
Not open for further replies.
Back
Top