Not getting expected output from digitalRead()

What I am trying to do is recognise when power is given to a digital pin, in this case pin 2. When power comes to this pin the LED that is connected to pin 4 does light up but the output of "val" remains at 1 whether I push the button or not.

So my question is, am I trying to do something that is not possible or am I just doing it wrong?

Thanks, Daniel.

int ledPin = 4;  // LED connected to digital pin 4
int inPin = 2;    // 5v to digital pin 2 from push button
int val = 0;      // variable to store the read value

void setup() {

  Serial.begin(9600);
  pinMode(ledPin, OUTPUT);  // sets the digital pin 4 as output
  pinMode(inPin, INPUT);    // sets the digital pin 2 as input
}

void loop() {
  val = digitalRead(inPin);   // read the input pin
  digitalWrite(ledPin, val);  // sets the LED to the button's value
  Serial.print(val);

}

IT all depends on whether you have "pullup" or a "pulldown" resistor on pin 2. You can't just leave it floating and expect it to function the way you have programmed.

Paul

How is pin 2 wired ? Have you got a pulldown resistor in place to keep it LOW excepts when the buyyon is pressed ?

FYI

Did you look for val thousands of characters down the line?

Your loop() function will execute tens of thousands of times per second until the Serial output buffer fills, and then it will execute about a thousand times per second because of the rate that you are using. Start with the power on, and then start with the power off, and you may see the correct value of val - except that applying power to an input pin while the Arduino power is off can be a bad thing.

Perhaps you should only print the value of val when val changes, but this typically requires another variable.

Good Luck!

It would have been REALLY nice if the OP had provided a wiring diagram, but the issue is unlikely to be the wiring of a switch or of an LED because the OP reported that the LED responded correctly. We sometimes get false information but in this case I tend to believe the OP.

I'm sorry that I don't have a wiring diagram for this.

I understand that I will be printing the val to the serial output screen a lot but I was only trying to detect if I could see a value change.

I did try your suggestion of having the button pressed and then powering on the board and then trying the same with the button not pressed at power on.

Oddly I get a few zero's at the start then the output changes to one's if I power on with the button pressed and if I power on with the button not pressed, I just get a series of one's.

This is not what I was expecting - I thought that I would be able to see a value change in the output screen.

I have not used a resistor but I will go and research if that will help me.

Thanks, Daniel.

OK, so I now see my error, I was trying to read the INPUT pin for a change in state. If I read the OUTPUT pin for this change I get a change of state to the output screen.

Is it not possible to read the state of an INPUT pin?

Thanks, Daniel.

Is it not possible to read the state of an INPUT pin?

Of course it is. Otherwise how would it be possible to know its state ?

Is it not possible to read the state of an INPUT pin?

Yes, this is a normal thing to do.

OK, so I now see my error, I was trying to read the INPUT pin for a change in state. If I read the OUTPUT pin for this change I get a change of state to the output screen.

This is not the explanation for what you are experiencing.

Do you have the “Autoscroll” box checked in the lower left corner of the serial monitor?

UKHeliBob: Of course it is. Otherwise how would it be possible to know its state ?

OK, thats great, do you know how to read it?

cattledog: Yes, this is a normal thing to do.

This is not the explanation for what you are experiencing.

Do you have the "Autoscroll" box checked in the lower left corner of the serial monitor?

I do have the autoscroll checked but I am having a problem reading the state of an input pin to print it to the output screen.

Am I correct in this:-

variable = digitalRead("INPUT PIN #")

variable = digitalRead("INPUT PIN #")

Yes. There several examples in the IDE examples which demonstrate how to read a digital pin. File>Examples>01 Basics and 02 Digital.

You will need to learn to use INPUT_PULLUP or an external pullup or pull down resistor to prevent a floating pin. You will also need to control the serial printing to keep it from filling the screen too fast.

Hi cattledog, thank you for your help. I found my answer where you had pointed me.

Many thanks for you time, Daniel.