Nano digital pin mapping guidance

Hello,

I am using a Nano manufactured by LAFVIN (suckered into buying 3 for $13). Unfortunately, when I read the digital pins these pins are always in HIGH state even if I have an inline button to disable the connectivity. I suspect that my interpretation of the pinout diagrams is incorrect.

I used the standard Arduino button example with the following modifications:

const int buttonPin = 2;          // the number of the pushbutton pin
const int ledPin =  LED_BUILTIN;  // the number of the LED pin
const int laserPin = 5;

// variables will change:
// int buttonState = 0;              // variable for reading the pushbutton status

void setup() {
  // initialize the LED pin as an output:
  pinMode(ledPin, OUTPUT);
  // initialize the pushbutton pin as an input:
  pinMode(buttonPin, INPUT);
  digitalWrite(buttonPin, LOW);
  
  pinMode(laserPin, OUTPUT);
  digitalWrite(ledPin, HIGH);
  delay(5000);
  digitalWrite(ledPin, LOW);
  delay(5000);
}

void loop() {

  // read the state of the pushbutton value:
  int buttonState = digitalRead(buttonPin);

  // check if the pushbutton is pressed. If it is, the buttonState is HIGH:
  if (buttonState == HIGH) {
    // turn LED on:
    digitalWrite(ledPin, HIGH);
    digitalWrite(laserPin, HIGH);
  } else {
    // turn LED off:
    digitalWrite(ledPin, LOW);
    digitalWrite(laserPin, LOW);
  }
}

Both the BUILTIN_LED and external one on 5 (as shown in the above code) respond correctly to statements in the code but the digitalRead for buttonPin is always HIGH irrespective of assignment to other digital pins. I have tried all three boards that came in the package and their performance is identical. The same code runs correctly on my UNO by which my conclusion is that my understanding of the pin mapping in Nano is naive.

I want the LEDs to be lit only when pinButton is pressed (i.e. HIGH) but it seems that digitalRead is always returning HIGH even if the button is not pressed. Doesn't make sense to me except that I am using incorrect pin assingments for the button. I didn't do a digitalWrite in the first few iterations because I have a 100K resistor in the circuit from the button pin (on the board's digital pin side) to GND but the digitalWrite (for internal pullup) seems to have no effect.

I would like to read the value of the button's status. Please let me know what changes in the code are needed. Thanks.

Kind regards.

baqwas:
I used the standard Arduino button example with the following modifications:

Did you also follow the wiring diagram of the example by connecting a pull-down resistor between pin 2 and ground?
https://www.arduino.cc/en/Tutorial/Button

Yessir, I have a 10K resistor between D2 and GND. D2 connects to a push button and then the forwarding connection is to 5V (also tried 3V3 unsuccessfully of course).

To clear the cobwebs in my mind I used D5 (with 220 ohm external pullup) to light up an external LED in the same flow as the BUILTIN_LED. Both stay on permanently in the loop section becasue digitalRead() is always HIGH (which leads me to interpret that D2 is something else for program code purposes). Also, assuming that the type is int and not some other size.

If you don't mind, please tell me what I should be using instead of the following statement to have the push button working with D2 (on the Nano specifically):

const int buttonPin = 2;          // the number of the pushbutton pin

The full code is in the original post. Many thanks.

Kind regards.

That schematic (which should be fixed or removed) will not work if the tac button is turned 90 degrees.
Connect the pulldown resistor between GND and the wire going to pin 2 or directly from pin 2 to GND, NOT to the button.

JCA34F:
That schematic (which should be fixed or removed) will not work if the tac button is turned 90 degrees.

A tactile switch will only fit across the center divider of a breadboard in one orientation, so it's impossible to do that.

JCA34F:
Connect the pulldown resistor between GND and the wire going to pin 2 or directly from pin 2 to GND, NOT to the button.

I agree. It's not at all obvious that the opposite legs on the tactile switches are always electrically connected. The reliance of this circuit on the internal connections of the switch makes it unnecessarily difficult to understand, with absolutely no benefit in return. This is one of the first tutorials a beginner will see, so it's especially important that it be as easy to understand as possible.