Example Code for Arduino-Read Analog Value

Last revision 2026/01/15

The sample to drive some analog sensors. It reads the value from the analog sensor and prints the value to the serial port. Users can learn how to interface analog sensors with Arduino and use serial communication to output data.

Hardware Preparation

Name Model/SKU Quantity Purchase Link
DFRobot Rotation Sensor V1 DFR0054 1
Arduino Board 1
Jumper Wires 3

Software Preparation

  • Development tools: Arduino IDE
  • Download link: Arduino IDE
  • Installation tutorial: Follow the official installation guide on the Arduino website.
  • Library loading: No additional libraries are required for this sample.

Wiring Diagram

Analog sensor connection diagram

Other Preparation Work

  1. Connect the Signal Output pin of the DFRobot Rotation Sensor V1 to Analog Pin 0 of the Arduino board.
  2. Connect the GND pin of the sensor to the GND pin of the Arduino.
  3. Connect the Power pin of the sensor to the 5V pin of the Arduino.

Sample Code


// # Editor     : Lauren from DFRobot
// # Date       : 30.12.2011

// #
// # Editor     : Lauren from DFRobot
// # Date       : 17.01.2012

// # Product name: Rotation Sensor v1/v2 or Analog Slide Position Sensor
// # Product SKU : DFR0054/DFR0058/DFR0053
// # Version     : 1.0

// # Description:
// # the sample to drive some analog sensors

// # Connection:
// #        Signal output pin  -> Analog pin 0
// #


void setup()
{
  Serial.begin(9600);     //Set serial baud rate to 9600 bps
}
void loop()
{
  int val;
  val=analogRead(0);      //Read slider value from analog 0
  Serial.println(val,DEC);//Print the value to serial port
  delay(100);
}

Result

Open the Arduino IDE's Serial Monitor with a baud rate of 9600. Rotate the sensor's knob, and you will see the analog value (ranging from 0 to 1023) printed to the serial port. The value changes based on the sensor's rotation angle.

Additional Information

This sample code is compatible with other DFRobot analog sensors including Rotation Sensor v2 (SKU: DFR0058) and Analog Slide Position Sensor (SKU: DFR0053).

Was this article helpful?

TOP