Example Code for Arduino-Read Analog Pins

Last revision 2026/01/18

This article provides a comprehensive guide with example code for using Arduino to read analog pins on a DFRobot Beetle RP2040, detailing hardware and software setup, wiring instructions, and offering a sample code for monitoring potentiometer values in real-time through the serial monitor.

Hardware Preparation

  • DFRobot Beetle RP2040, 1, Purchase Link
  • USB Type-C Cable, 1
  • Potentiometer (≥100KΩ), 1

Software Preparation

Wiring Diagram

  • R: Potentiometer. The value is over 100K. The adjustable end is connected to GP28(A2), and fixed end to 3V3 and GND.

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 potentiometer to GP28 (A2) as per the diagram.
  5. Open the Serial Monitor in Arduino IDE (baud rate: 9600).

Sample Code

Function: The code will read the potentiometer connected to GP28(A2) and print the value of the rotary potentiometer in real time in the serial monitor.

const int analogInPin = A2;  //hardware connected to GP28(A2)
int sensorValue = 0;        

void setup() {
  Serial.begin(9600);
}

void loop() {
  sensorValue = analogRead(analogInPin);
  Serial.println("sensor = ");
  Serial.println(sensorValue);
  delay(200);
}

Result

Open the serial monitor, and the window prints the current potentiometer value. The printed value changes as the potentiometer is rotated to change the angle.

Result

Additional Information

  • The ADC (Analog-to-Digital Converter) converts analog voltage signals into digital values (0-1023).
  • The potentiometer’s adjustable end provides a variable voltage (0-3.3V) to the ADC pin.

Was this article helpful?

TOP