Built-in project · Arduino Uno R3

Photocell module on A0, on an Arduino Uno

A photoresistor module’s analog output on A0, printed twice a second: more light, a different number. Drag the light level and watch it change. The whole build, an Arduino Uno and 1 more part, runs here in your browser on the firmware below; open it in the editor to change the wiring or the code and run it again.

Teaching? Assign it to your class in one click.

Beginner About 10 minutes to build and run. Runs in your browser. Free, and no account needed.

The parts retail for about $28. On Mokxi they're free.

The circuit runs here once the page has loaded.

What you'll learn

  • A light-dependent resistor in a divider
  • Reading an analog pin
  • The 0 to 1023 range of the Uno's ADC
  • Watching values in the serial monitor
  • Difficulty: Beginner
  • Time: About 10 minutes
  • Board: Arduino Uno R3
  • Parts: 2

How it works

The module has a photoresistor in a voltage divider. Its AO pin gives a voltage that moves with the light, and it connects to A0. Power comes from 5V and GND.

Twice a second the sketch reads A0 and prints the number, from 0 to 1023. Drag the module's light level in the simulator and watch the number change.

Parts list

2 parts, plus the jumper wires. They retail for about $28; on Mokxi they're free.

Kits that can build it

This starter kit has every part in the circuit. Each kit's page lists what else it can build.

Wiring table

Every connection, pin by pin, read from the circuit itself. Each row joins the pin on the left to the pin on the right, by a jumper wire or a breadboard strip.

Part and pinConnects to
Photoresistor sensor module pin VCCArduino Uno R3 pin 5V
Photoresistor sensor module pin GNDArduino Uno R3 pin GND
Photoresistor sensor module pin AOArduino Uno R3 pin A0

The code

The firmware exactly as the editor opens it. Change a line there and press Run: it compiles in the browser.

sketch.ino · Arduino Uno R3
// Photocell module on A0.
//
//   module AO -> A0   VCC -> 5V   GND -> GND
//
// The photoresistor sits in a divider on the module, so its analog output
// moves with the light. analogRead turns that voltage into 0 to 1023.
//
// Drag the module's light level.

void setup() {
  Serial.begin(9600);
  Serial.println("Mokxi Uno: Photocell module on A0");
}

void loop() {
  int reading = analogRead(A0);
  Serial.println(reading);
  delay(500);
}

Code walkthrough

The key functions of the sketch, and what each one does.

setup sketch.ino

Starts the serial port at 9600 baud and prints a hello message. The analog pin needs no setup.

void setup() {
  Serial.begin(9600);
  Serial.println("Mokxi Uno: Photocell module on A0");
}

loop sketch.ino

Calls analogRead(A0) to turn the voltage into a number from 0 to 1023, prints it, and waits half a second with delay(500).

void loop() {
  int reading = analogRead(A0);
  Serial.println(reading);
  delay(500);
}

Troubleshooting

The usual mistakes with this circuit, and the fix for each.

  • The number is stuck at 0 or 1023. This project
    Check that the module's AO goes to A0, and VCC and GND are on 5V and ground.
  • You expected the number to rise in the dark. This project
    Which way it goes depends on the module. Drag the light level and watch what it does.
  • The digital output never changes, or is always on, no matter the light. Photoresistor sensor module
    Turn the trimmer on the module until DO flips at the light level you want. The digital output is set by that threshold, and which direction means bright depends on the board.
  • Analog readings go down when it gets brighter, and the code expects the opposite. Photoresistor sensor module
    The LDR sits in a divider, so the direction depends on how the module is built. Print the raw value in the light and dark, then flip it in code with 1023 minus the reading if needed.
  • Upload fails or nothing runs after wiring pins 0 and 1 to a module. Arduino Uno R3
    Pins 0 and 1 are the USB serial port. Move the module to other pins, or unplug it while uploading. Also check the right board is selected.
  • Pins misbehave or the board resets when you power motors or servos from it. Arduino Uno R3
    Each I/O pin gives about 20 mA, 40 mA max. Power motors and servos from their own supply and join its ground to the Uno's GND.

Change it and keep it

Open it in the editor, change the circuit or the code, and keep your version in a free account.