digitalRead always high

Hello,
I have a problem with my leonardo board. If i digitalRead pin 7, it always comes back high. It happens on all of the other pins too. If I analogRead pin 6 it says ~255. The board worked before, could you help me with this?

did you set it as an INPUT ?
what's connected to it ?

I plug the board into my pc, no other wires are connected to it.

here's the code I ran to check the problem:

  const int dtaPin = 7;
void setup() {

  pinMode(dtaPin, INPUT);

}

void loop() {
  Serial.println(digitalRead(dtaPin));

}

So you have a floating input pin and the value that you read can be anything. Connect the pin to GND and test again.

That fixed it, now it's on low. analogread(pin 6) stays below 30.
How should i use buttons if i want to? (with it connecting to gnd)

analogRead works with "analog Pins" - those are connected to an ADC (Analog to Digital Converter)

either you use PIN number 14 to 19 or for better readability A0 to A5

int x = analogRead(A0);

make sure the voltage is compatible with your board.

I don't need analogread, i just used it to debug my problem. I would like to know how i can connect a button to pin 7, so that it works

well you used it wrongly then....


that's the first tutorial people do after connecting a LED... Have you looked at any training materials ?

https://docs.arduino.cc/built-in-examples/digital/Button/

and once you got it, look at what pullup can do for you

https://docs.arduino.cc/tutorials/generic/digital-input-pullup/

buttons are bouncing, so I'd suggest you check the code for one of the numerous button library such as Button in easyRun or OneButton or Toggle or EasyButton or Bounce2, ...

If I wire up the schematic shown on the website, it doesn't work. The low and high values start jumping.

here's the code I use for debug:

const int dtaPin = 6;
void setup() {

  pinMode(dtaPin, INPUT);
  Serial.begin(9600);
}

void loop() {
  Serial.println(digitalRead(dtaPin));
  delay(50);
}
output:
1:34:20.463 -> 1
11:34:20.509 -> 0
11:34:20.555 -> 1
11:34:20.601 -> 0
11:34:20.694 -> 1
11:34:20.740 -> 0
11:34:20.786 -> 1
11:34:20.832 -> 0
11:34:20.878 -> 1
11:34:20.925 -> 0
11:34:20.971 -> 1
11:34:21.016 -> 0
11:34:21.063 -> 1
11:34:21.109 -> 0
11:34:21.155 -> 1
11:34:21.247 -> 0

which one ? if that's the first link, did you notice they mention a pull down resistor?

Connect three wires to the board. The first two, red and black, connect to the two long vertical rows on the side of the breadboard to provide access to the 5 volt supply and ground. The third wire goes from digital pin 2 to one leg of the pushbutton. That same leg of the button connects through a pull-down resistor (here 10K ohm) to ground. The other leg of the button connects to the 5 volt supply.

if it is the schematic of the second link, then notice they used INPUT_PULLUP and not INPUT for the pinMode...

did you read the text or just went right ahead and wire stuff?

I just checked the first website, and i wired it up correctly. I even double checked which resistor i used.
Something strange that happens is that after i press the button the value stays on high.

well show us picture (in focus) of your gig

Arduino_Button_InternalLED_Animation_Edited
(from L1: Using buttons | Physical Computing)


Here's the picture.

E: I tested the red jumper cable, it doesn't connect I think it's broken. If I switch it out to another cable it works perfectly.
Thank you for your help.

OK - yes those cables coming from the Far East are not always of great quality

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