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
- DFR0022 Gravity: Analog Grayscale Sensor (SKU:DFR0022 x 1)
- DFRduino UNO R3 with IO Expansion Shield and USB Cable A-B (SKU:DFR0216-2 x 1)
Software Preparation
- Development Tool: Arduino IDE (version unspecified). Download link: Arduino IDE
Wiring 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?
