Lights and displaysPedestrian Crossing with a Button
Cars have green until a pedestrian presses the button. Then the cars get red, the pedestrians get green and a buzzer ticks, just like at a real crossing.
SensorsA photoresistor measures the light. When the room gets dark the LED turns on by itself and goes off again in daylight, just like street lighting.
Written by the RoboHub team. A question? Write to us.
Choose a variation and the parts list updates.
Connect one leg of the photoresistor to 5V and the other to pin A0. From that same A0 point, run a 10 kΩ resistor to ground. The brighter it is, the higher the voltage at A0.
Connect the LED through a 330 Ω resistor to pin 9, short leg to ground.
The code prints the light level to the Serial Monitor, from 0 to 1023.
const int SENSOR = A0, LED = 9;
const int DARK = 300; // below this it counts as dark: set it from your own readings
bool on = false;
void setup() {
pinMode(LED, OUTPUT);
Serial.begin(9600);
}
void loop() {
int light = analogRead(SENSOR); // 0 = dark, 1023 = bright
Serial.println(light);
if (light < DARK) on = true;
else if (light > DARK + 50) on = false; // a margin, so it doesn't flicker at dusk
digitalWrite(LED, on ? HIGH : LOW);
delay(200);
}Open the Serial Monitor (9600) and see what the sensor reads in daylight and when you cover it with your hand. Put a number between the two in DARK.

Illustration, made with AI
Illustration: on a windowsill at dusk, an LED controlled by a photoresistor is lit.
Step 4: Set the threshold
Lights and displaysCars have green until a pedestrian presses the button. Then the cars get red, the pedestrians get green and a buzzer ticks, just like at a real crossing.
SensorsAn ultrasonic sensor measures the distance. Three LEDs show how close the obstacle is, and the buzzer beeps faster the closer you get.
Lights and displaysAn RGB LED holds three small LEDs: red, green and blue. Mix their brightness and you get any colour, while the lamp slowly changes from one colour to the next.