Skip to content

Instantly share code, notes, and snippets.

@topshed
Last active May 6, 2019 13:48
Show Gist options
  • Save topshed/e325ee33f63ee6ac1b6c762352a3b6d9 to your computer and use it in GitHub Desktop.
Save topshed/e325ee33f63ee6ac1b6c762352a3b6d9 to your computer and use it in GitHub Desktop.

Arduino Light Meter

You can use another analog device - a Light Dependent Resisitor (LDR) - to meausure how bright it is (light intensity). As you can probably guess from its name. the resistance of an LDR changes depending on how much light falls on it. By making a voltage divider with another resistor, you can measure a voltage that will change depending on how bright it is.

You can then use an array of LEDs to create a visual indicator of light intensity. As the light level increases, the number of LEDs that are on will also increase. Don't foget every LED needs a current-limiting resistor. To make building the circuit easier you can use a resistor array that has a number of resistors linked togther with a common pin.

Imgur

const int analogPin = A0;   // the pin that the LDR is attached to
const int ledCount = 10;    // the number of LEDs in the bar graph

int barGraph[] = {
  2, 3, 4, 5, 6, 7, 8, 9, 10, 11
};   // an array of pin numbers to which LEDs are attached


void setup() {
  // loop over the pin array and set them all to output:
  for (int i = 0; i < ledCount; i++) {
    pinMode(barGraph[i], OUTPUT);
  }
}

void loop() {
  // read the potentiometer:
  int LDRReading = analogRead(analogPin);
  // map the result to a range from 0 to the number of LEDs:
  int ledLevel = map(LDRReading, 0, 1023, 0, ledCount);

  // loop over the LED array:
  for (int i = 0; i < ledCount; i++) {
    // if the array element's index is less than ledLevel,
    // turn the pin for this element on:
    if (i < ledLevel) {
      digitalWrite(barGraph[i], HIGH);
    }
    // turn off all pins higher than the ledLevel:
    else {
      digitalWrite(barGraph[i], LOW);
    }
  }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment