ArduinoStudio

Photoresistor

Read the light level on A0 and let your sequence continue only when it’s dark enough, or bright enough.

A photoresistor changes its resistance with light. Paired with a 10 kΩ resistor, it gives the board a reading from 0 (dark) to 1023 (bright) on an analog pin. The Photoresistor block takes that reading, and the Check block right after it decides whether the sequence goes on.

It runs in the simulator, over USB direct and through the Bridge, so you don’t need to install anything to use it with a real board. The block does nothing useful on its own: always put a Check directly after it.

Wiring

Photoresistor and 10 kΩ divider on A0, with an LED on pin 13Arduino 5V to Photoresistor Leg 1; Arduino A0 to Photoresistor Leg 2; Arduino A0 to 10 kΩ resistor End 1; Arduino pin 13 to a 220 Ω resistor, then LED Long leg (anode); Arduino GND to 10 kΩ resistor End 2; Arduino GND to LED Short leg (cathode).Arduino Uno5VA013GNDPhotoresistorEither leg, either wayLeg 1Leg 210 kΩ resistorShares the A0 rowEnd 1End 2LEDLong leg (anode)Short leg (cathode)220 Ω
Photoresistor and 10 kΩ divider on A0, with an LED on pin 13. Wire colours: blue for digital signals, green for analog readings, orange for power, grey dashed for ground.
FromThroughToNote
Arduino 5V—Photoresistor Leg 1
Arduino A0—Photoresistor Leg 2Analog pin 0 in the block means A0
Arduino A0—10 kΩ resistor End 1
Arduino pin 13220 Ω resistorLED Long leg (anode)
Arduino GND—10 kΩ resistor End 2Brighter light now gives a higher reading
Arduino GND—LED Short leg (cathode)

The photoresistor and the resistor form a voltage divider. More light lowers the photoresistor’s resistance and pushes the reading toward 1023. Darkness pulls it toward 0. The LED is only there for the example below.

Block settings

SettingNew block starts atIn the settings panel
Analog pin0Editable

Step by step

  1. Build the circuit

    Wire the photoresistor from 5V to A0 and the 10 kΩ resistor from A0 to GND. Add an LED on pin 13 through a 220 Ω resistor.

  2. Add the Photoresistor block

    Click + on the sequence, open the Components tab, open Inputs and choose Photoresistor. Analog pin is already 0, which means A0.

  3. Add a Check right after it

    From the Actions tab, add a Check directly after the Photoresistor. Set If to <, set To to % (Choose turns into a text box) and type 30. That reads as “continue only while the reading is below 30% of 1023”, about 307.

    Use %, not Custom. Through the Bridge, Custom compares against a second analog pin, not a number. The default On/Off never passes for a light reading.

  4. Blink the LED

    Add a Standard LED (open LED and choose Standard) with Digital pin 13 and Off/On switched on, then Wait 500 ms, another Standard LED on pin 13 switched off, and Wait 500 ms.

  5. Turn on Infinity Loop

    Right-click the sequence name (long-press on a touchscreen) and tick Infinity Loop, so the light is read again on every pass.

  6. Try it in the simulator

    Press Play without a board. A0 appears on the Virtual Board with a slider at 512. Drag it below about 307: the Check shows PASS and the LED blinks. Drag it back up and each pass stops at FAIL.

  7. Run it on your board

    Connect with USB direct (the toolbar button, then Connect on the USB direct row) and press Play. Cover the sensor with your hand: the LED blinks while it’s dark and stops when you uncover it. If it never triggers, raise the number in Choose. If it blinks all the time, lower it.

Mini-project: Morning flag

Raise a paper flag when the room gets bright. Tape the flag to a servo horn, wire the servo to pin 9 and keep the divider on A0. You can leave the LED off for this one.

Photoresistor divider on A0 and a servo on pin 9Arduino 5V to Photoresistor Leg 1; Arduino A0 to Photoresistor Leg 2; Arduino A0 to 10 kΩ resistor End 1; Arduino GND to 10 kΩ resistor End 2; Arduino 5V to Servo Red (5V); Arduino pin 9 to Servo Orange (signal); Arduino GND to Servo Brown (GND).Arduino Uno5VA09GNDPhotoresistorLeg 1Leg 210 kΩ resistorEnd 1End 2ServoPaper flag on the hornRed (5V)Orange (signal)Brown (GND)
Photoresistor divider on A0 and a servo on pin 9. Wire colours: blue for digital signals, green for analog readings, orange for power, grey dashed for ground.
FromThroughTo
Arduino 5V—Photoresistor Leg 1
Arduino A0—Photoresistor Leg 2
Arduino A0—10 kΩ resistor End 1
Arduino GND—10 kΩ resistor End 2
Arduino 5V—Servo Red (5V)
Arduino pin 9—Servo Orange (signal)
Arduino GND—Servo Brown (GND)
  1. Read the light

    Start a sequence with a Photoresistor block on Analog pin 0.

  2. Check for brightness

    Add a Check with If set to >, To set to % and Choose set to 70.

  3. Raise and lower the flag

    Add a Servo with Angle 90 and Duration 800, then Wait 5000, then another Servo with Angle 0 and Duration 800.

  4. Loop and play

    Turn on Infinity Loop and press Play. In the simulator, drag A0 past about 716 to see the flag rise.

When the room is bright the flag rises for five seconds, drops, and rises again while it stays bright. It works over USB direct, in the simulator and through the Bridge, where the block waits for brightness. For a one-way night light, see the automatic night light idea.

Questions

What numbers does the Photoresistor block read?

0 to 1023, from the board’s analog input. With the photoresistor on the 5V side and the 10 kΩ resistor on the GND side, brighter light gives a higher number. In a Check, To % turns the Choose value into a share of 1023, so 30 means about 307.

Should I use To % or Custom in the Check?

Use %. It means the same thing on every connection. Over USB direct and in the simulator, Custom compares against the raw number you type. Through the Bridge, Custom compares against a second analog pin with that number. The default On/Off never passes for a light reading.

Can the light level dim an LED or move a servo gradually?

Not yet. The reading only feeds a Check, which either lets the sequence continue or stops that pass. No block can use the reading as its brightness, angle or speed. What you can build today are thresholds: do something when it’s darker or brighter than a level.

Why does my sequence stop instead of waiting for it to get dark?

Over USB direct and in the simulator, a Check reads once. If it isn’t dark at that moment, the pass ends, so turn on Infinity Loop to keep checking. Through the Bridge, the block waits until the condition is true instead.

Where can I see the live light reading?

In the simulator, the Virtual Board slider is exactly the value the Check sees. On a real board the editor doesn’t show the reading yet. Find your threshold by testing: start at 30% and adjust while you cover and uncover the sensor.

Which resistor should go with the photoresistor?

10 kΩ suits most small photoresistors indoors. In a very bright room, a smaller resistor (around 4.7 kΩ) spreads the readings out. In a dim room, try a larger one (around 47 kΩ).

See the editor in action with a guided demo, no account or install needed: open the demo