Example Code for Arduino-Light Intensity Reading

Last revision 2026/01/09

This project demonstrates how to use the DFR0026 Analog Ambient Light Sensor with Arduino to read ambient light intensity and output the value via serial port. Users can learn how to interface an analog sensor with Arduino and use serial communication to monitor sensor data.

Hardware Preparation

Name Model/SKU Quantity Purchase Link
Arduino Board - 1 -
Analog Ambient Light Sensor DFR0026 1 DFR0026
Jumper Wires - Several -

Software Preparation

  • Development Tool: Arduino IDE (version 1.8.x or later). Download link: Arduino IDE
  • No additional libraries are required for this sample code.

Wiring Diagram

DFR0026_Diagram_new

Arduino Sensor
A0 GREEN
5V RED
GND GRAY

Other Preparation Work

  1. Connect the DFR0026 sensor to the Arduino board according to the Wiring Diagram.
  2. Open the Arduino IDE and create a new sketch.
  3. Copy the Sample Code into the sketch.
  4. Connect the Arduino board to your computer via USB cable.
  5. Select the correct board and port in the Arduino IDE.

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);
}

Result

Open the serial port monitor, set the baud rate according to the program for 9600.The light intensity around the sensor is different, the received data is also different.The more light, the bigger the data.

Was this article helpful?

TOP