Example Code for Arduino-Read Analog Value

Last revision 2026/01/05

This article guides readers through the process of reading analog values using Arduino, including a wiring diagram and sample code for connecting and reading values from a grayscale sensor.

Hardware Preparation

Software Preparation

  • Development Tool: Arduino IDE (version unspecified). Download link: Arduino IDE

Wiring Diagram

Analog Grayscale Sensor Diagram

Sample Code

void setup()
{
  Serial.begin(9600); // open serial port, set the baud rate to 9600 bps
}
void loop()
{
      int val;
      val=analogRead(0);   //connect grayscale sensor to Analog 0
      Serial.println(val,DEC);//print the value to serial
      delay(100);
}

Was this article helpful?

TOP