Arduino101 PhotoBlink

From Twin Cities Maker wiki
Jump to: navigation, search

Here we add a photoresistor to the Blink circuit. We use this to sense the amount of light in the room, and adjust the blink rate – the darker the room, the faster the LED blinks.

Breadboard layout

Photo-blink bb.png

Code

int ledPin = 3;     // LED connected to pin 3
int sensorPin = 0;  // photoresistor on analog 0

void setup() {
  pinMode(ledPin, OUTPUT);
  Serial.begin(9600);
}

void loop() {
  int rate = analogRead(sensorPin);
  Serial.println(rate);
  digitalWrite(ledPin, HIGH);
  delay(rate);
  digitalWrite(ledPin, LOW);
  delay(rate);
}