Analogue Sensors Worked Example

Self-calibrating LDR light sensor bar

Build a light-sensitive voltage divider and let the Arduino UNO R4's 12 × 8 LED matrix show the changing room brightness.

Getting started: what you'll need

This project uses the UNO R4 WiFi's built-in LED matrix. An LDR GL5528 photoresistor and 10 kΩ resistor. Alternative parts should work but might will need to adjust the code for it to fully work.

Equipment list

PartDescriptionQty
ABX00087Arduino UNO R4 WiFi with built-in 12 × 8 LED matrix1
USB-C data cableUSB-C to USB-A/C data cable (not charge-only)1
BB830 (or equivalent)830-point solderless breadboard1
Jumper wire kitMale-to-male breadboard jumper wires3
GL5528 LDRGL5528 CdS photoresistor for light sensing1
10 kΩ resistor10 kΩ fixed resistor for the voltage divider1

New to Arduino? Start with Arduino's UNO R4 getting-started guide to set up the board and upload a sketch.

Arduino code

This sketch averages 16 readings from A0, learns the dimmest and brightest values it has seen, and fills the LED matrix from the bottom-left. It waits until it has seen a 20-count brightness range before mapping values, avoiding a divide-by-zero during startup. Copy the sketch into the Arduino IDE and upload it to an UNO R4 WiFi.

#include "Arduino_LED_Matrix.h"

ArduinoLEDMatrix matrix;

const int LDR_PIN = A0;

// Matrix pixel buffer (12x8 = 96 LEDs)
uint8_t frame[96];

// Auto-calibration values
int minBrightness = 4095;
int maxBrightness = 0;

// --------------------------------------------------
// Read averaged ADC
// --------------------------------------------------
int readSensor()
{
  uint32_t sum = 0;

  for (int i = 0; i < 16; i++)
  {
    sum += analogRead(LDR_PIN);
    delay(2);
  }

  return sum / 16;
}

// --------------------------------------------------
// Clear matrix buffer
// --------------------------------------------------
void clearDisplay()
{
  memset(frame, 0, sizeof(frame));
}

// --------------------------------------------------
// Set one LED
// --------------------------------------------------
void setPixel(int x, int y, bool on)
{
  if (x < 0 || x >= 12 || y < 0 || y >= 8)
    return;

  frame[y * 12 + x] = on ? 1 : 0;
}

// --------------------------------------------------
// Draw LED bar graph
// Fills left->right, bottom->top
// --------------------------------------------------
void drawBarGraph(int ledsToLight)
{
  clearDisplay();

  ledsToLight = constrain(ledsToLight, 0, 96);

  for (int i = 0; i < ledsToLight; i++)
  {
    int x = i % 12;
    int y = 7 - (i / 12);

    setPixel(x, y, true);
  }

  matrix.loadPixels(frame, 96);
}

// --------------------------------------------------
// Setup
// --------------------------------------------------
void setup()
{
  matrix.begin();

  analogReadResolution(12);

  Serial.begin(115200);

  Serial.println("GL5528 Light Meter");

  // Startup flash
  for (int j = 0; j < 3; j++)
  {
    memset(frame, 1, sizeof(frame));
    matrix.loadPixels(frame, 96);
    delay(150);

    clearDisplay();
    matrix.loadPixels(frame, 96);
    delay(150);
  }

  Serial.println("Auto-calibrating...");
}

// --------------------------------------------------
// Main loop
// --------------------------------------------------
void loop()
{
  int adc = readSensor();

  // More light means lower LDR resistance and a lower ADC reading
  int brightness = 4095 - adc;

  // Learn min/max automatically
  if (brightness < minBrightness)
    minBrightness = brightness;

  if (brightness > maxBrightness)
    maxBrightness = brightness;

  int range = maxBrightness - minBrightness;
  int ledsToLight = 0;

  // Wait for enough calibration range before mapping
  if (range >= 20)
  {
    ledsToLight = map(
        brightness,
        minBrightness,
        maxBrightness,
        96,
        0);
  }

  ledsToLight = constrain(ledsToLight, 0, 96);

  drawBarGraph(ledsToLight);

  Serial.print("ADC=");
  Serial.print(adc);
  Serial.print(" Brightness=");
  Serial.print(brightness);
  Serial.print(" Min=");
  Serial.print(minBrightness);
  Serial.print(" Max=");
  Serial.print(maxBrightness);
  Serial.print(" LEDs=");
  Serial.println(ledsToLight);

  delay(50);
}
Build the LDR Potential Divider Circuit

Breadboard wiring

Breadboard wiring for a GL5528 LDR and 10 kΩ resistor connected to 5V, A0 and GND
Connect the GL5528 LDR and 10 kΩ resistor to 5V, A0 and GND as shown.

Pinout wiring

FromToPurpose
5VOne LDR legTop of the voltage divider
Other LDR leg10 kΩ resistor and Arduino A0Divider output read by the ADC
Other 10 kΩ resistor legGNDBottom of the voltage divider

How it works

The GL5528's resistance falls as more light reaches it. With the LDR connected to 5V and the 10 kΩ resistor connected to GND, brighter light pulls the A0 voltage and ADC reading down. The sketch inverts that reading to make a brightness value, then automatically tracks the lowest and highest brightness seen since startup.

As it learns the range, the matrix fills from the bottom-left in reverse: the brightest light seen lights no LEDs, while the dimmest lights all 96. So more ambient light means fewer LEDs, and a darker room means more. The bar adapts to the conditions it has encountered since the board was powered on; reset the board to start a fresh calibration.

Open the Serial Monitor at 115200 baud to see the raw ADC reading, learned brightness range and current LED count. The LED bar stays empty until the sensor has seen a brightness span of at least 20 ADC counts, which gives the mapping a safe non-zero range.

Testing and results

Photos of the LED bar graph for three light levels:

Arduino UNO R4 LED matrix with a few LEDs lit in bright light
Bright light: only a few LEDs lit.

Brightest: the LDR resistance is low, so the A0 voltage is low and the brightness value is high. The bar shows only a few LEDs.

Arduino UNO R4 LED matrix with a mid-range number of LEDs lit with the LDR partially covered
LDR partially covered: a mid-range number of LEDs lit.

Partially covered: less light reaches the LDR, so a mid-range number of LEDs is lit.

Arduino UNO R4 LED matrix with the most LEDs lit with the LDR in darkness
LDR in the dark: the most LEDs lit.

Darkest: the LDR resistance is high, so the A0 voltage is high and the brightness value is low. The bar shows the most LEDs.