Example Code for Arduino-GPIO Digital Read

Last revision 2026/01/08

This article provides step-by-step guidance on using Arduino GPIO for digital read operations, including hardware setup, wiring diagrams, and sample code for controlling an LED with a button module.

Hardware Preparation

Wiring Diagram

Connection

Mainboard Pin name Module Pin name
Beetle RP2350 GND Button Module GND
Beetle RP2350 3V3 Button Module VCC
Beetle RP2350 4 Button Module D

Sample Code

int buttonPin = 4;   //  Define button pin
int ledPin = 25;     //Define LED pin
int buttonState = 0;  //Variable for reading button state

void setup(){
   pinMode(buttonPin, INPUT); 
   pinMode(ledPin, OUTPUT);
}

void loop(){
   buttonState = digitalRead(buttonPin); //Read the value of the button
   
   if(buttonState == HIGH){    //Check if the button is pressed
     digitalWrite(ledPin,HIGH);
   } else{
     digitalWrite(ledPin,LOW);
   }
}

Result

After the program is uploaded, press the button connected to D7, the on-board LED will light up, and when the button is released, the LED will go out.

Was this article helpful?

TOP