Copy and paste the code below into a new Arduino sketch. Then, compile and upload it to your metro

// CIRC09 - Light
int lightPin = A0;
int ledPin = 9;

void setup()
  pinMode(ledPin, OUTPUT); 

void loop()
  int lightLevel = analogRead(lightPin); 
  lightLevel = map(lightLevel, 0, 700, 0, 255);
  analogWrite(ledPin, lightLevel);

Not Working?

If you're having issues with the Photo Sensor: flip it around, chances are the orientation is backwards.
LED Remains Dark

This is a mistake we continue to make time and time again, if only they could make an LED that worked both ways. Pull it up and give it a twist.

It Isn't Responding to Changes in Light.

Given that the spacing of the wires on the photo-resistor is not standard, it is easy to misplace it. Double check its in the right place.

Still not quite working?

You may be in a room which is either too bright or dark. Try turning the lights on or off to see if this helps. Or if you have a flashlight near by give that a try.

This guide was first published on Aug 18, 2017. It was last updated on Aug 18, 2017.

This page (Code) was last updated on Jan 23, 2021.