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
| From | Through | To | Note |
|---|---|---|---|
| Arduino 5V | — | Photoresistor Leg 1 | |
| Arduino A0 | — | Photoresistor Leg 2 | Analog pin 0 in the block means A0 |
| Arduino A0 | — | 10 kΩ resistor End 1 | |
| Arduino pin 13 | 220 Ω resistor | LED Long leg (anode) | |
| Arduino GND | — | 10 kΩ resistor End 2 | Brighter 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
| Setting | New block starts at | In the settings panel |
|---|---|---|
| Analog pin | 0 | Editable |
Step by step
- 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.
- 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.
- 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.
- 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.
- 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.
- 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.
- 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.
| From | Through | To |
|---|---|---|
| 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) |
- Read the light
Start a sequence with a Photoresistor block on Analog pin 0.
- Check for brightness
Add a Check with If set to >, To set to % and Choose set to 70.
- 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.
- 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Ω).