Arduino101 PhotoBuzzyBlink

From Twin Cities Maker wiki
Jump to: navigation, search

Continuing on from the Photo-Blink circuit, we add a piezo buzzer. This is guaranteed to drive anyone near you over the age of 10 completely crazy. (Under 10, they tend to love anything that makes noise.)

Breadboard layout

Photo Buzzy Blink bb.png

Code

int ledPin = 3;     // LED connected to pin 3
int sensorPin = 0;  // photoresistor on analog 0
int buzzerPin = 9;  // pin where the buzzer is connected

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

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