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

Last revision 2026/01/18

This article provides an example code for reading digital pins on Arduino using internal pull-up resistors, detailing hardware setup, software configuration, and the benefits of internal pull-up in simplifying circuit design.

Hardware Preparation

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

Software Preparation

Wiring Diagram

  • R: current-limiting resistor for the key, connect between the key and GP0 with a resistance of 220Ω.
  • KEY: control key, connect between the 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 and resistor to GP0 (D0) as per the diagram.

Sample Code

Function: use the statement pinMode(buttonPin, INPUT_PULLUP) to set port GP0 to internal pull-up. The onboard LED lights up when the key connected to port GP0 is pressed and goes out when released.

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

int buttonState = 0;         

void setup() {
  pinMode(ledPin, OUTPUT);
  pinMode(buttonPin, INPUT_PULLUP); //set the key port as input and internal pull-up.
}

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.
Change pinMode(buttonPin, INPUT_PULLUP) to pinMode(buttonPin, INPUT) to cancel the internal pull-up and re-download the program, after that, the onboard LED is on and can’t work normally, which means the port supports internal pull-up. After testing one by one, it proved that the 8 ports of Beetle RP2040 all support internal pull-up.

Additional Information

  • Internal pull-up resistors simplify circuit design by reducing external components.
  • The INPUT_PULLUP mode sets the pin to a high level by default, which is pulled low when the button is pressed.

Was this article helpful?

TOP