Example Code for Arduino-Read Digital Pins with External Pull-up

Last revision 2026/01/18

This article outlines the process of reading digital pins using an Arduino with external pull-up resistors, including hardware setup, wiring diagrams, and sample code for effective and stable connections.

Hardware Preparation

  • DFRobot Beetle RP2040, 1, Purchase Link
  • USB Type-C Cable, 1
  • Pushbutton (KEY), 1
  • Pull-up resistor (10KΩ), 1
  • Current-limiting resistor (220Ω), 1

Software Preparation

Wiring Diagram

Wiring Diagram

  • R1: pull-up resistor, for ensuring stable high level of the port, connect between 3V3 and GP0 with a resistance of 10K.
  • R2: current-limiting resistor of the key, connect between the key and GP0 with a resistance of 220Ω.
  • KEY: control key, connect between current-limiting resistor R2 and GND. When pressed, it pulls GP0 down to a low level.

Other Preparation Work

  1. Open Arduino IDE.
  2. Select "DFRobot Beetle RP2040" as the development board.
  3. Connect the Beetle RP2040 to your computer.
  4. Wire the pushbutton, resistors, and LED to GP0 (D0) as per the diagram.

Sample Code

Function: when the key connected to the P0 port is pressed, the onboard LED lights up; when released, the LED goes out.

const int buttonPin = 0;     
const int ledPin = 13;      

int buttonState = 0;         

void setup() {
  pinMode(ledPin, OUTPUT);
  pinMode(buttonPin, INPUT);//set the button pin to input.
}

void loop() {
  buttonState = digitalRead(buttonPin);
  if (buttonState == HIGH) {
    digitalWrite(ledPin, LOW);
  } else {
    digitalWrite(ledPin, HIGH);
  }
}

Result

After downloading the code, the onboard LED lights up when the KEY connected to GP0 is pressed, and goes out when released. If the LED is not lit, please check the hardware connection according to the connection diagram or check the resistance values.

Additional Information

  • External pull-up resistors ensure the pin remains at a stable high level when the button is not pressed.
  • The current-limiting resistor (R2) protects the pin from excessive current when the button is pressed.

Was this article helpful?

TOP