Photo Buzzy Blink

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

Code

photo_buzzy_blink.ino
const int ledPin = 3;     // LED connected to pin 3
const int sensorPin = 0;  // photoresistor on analog 0
const int buzzerPin = 9;  // pin where the buzzer is connected
 
// minimum and maximum blink rates
const int minDuration = 100;
const int maxDuration = 1000;
 
void setup() {
  pinMode(ledPin, OUTPUT);
  Serial.begin(9600);
}
 
void loop() {
  int rate = analogRead(sensorPin);
  Serial.println(rate);
//  rate = map(rate, 0, 800, minDuration, maxDuration);
  digitalWrite(ledPin, HIGH);
  tone(buzzerPin, rate, rate);
  delay(rate);
  digitalWrite(ledPin, LOW);
  delay(rate);
}
Print/export
Toolbox