Description:
The goal of this lab was to learn to use the piezo buzzer to make sound using inputs from the Arduino and a sensor.
In Part 1, using the example code provided I was able to construct the circuit to connect the Arduino to the piezo buzzer and for it to make sound.
In Part 2 of the lab, I learned to construct a Theremin by building a circuit that utilizes the input from a photosensor to control the sounds that the piezo buzzer makes.
For the input output coincidence exercise, I chose to create a scenario where I imagine that a rocket ship is taking off. As you can see in the attached photo, a toy rocket ship rests on a small piece of copper tubing, the launch pad. When the rocket ship hasn't yet taken off and is resting in position on the copper tubing a red LED, positioned to its left, is blinking (signaling that it will take off soon). When the rocket ship takes off (is lifted slowly from the copper tubing), the LED stops blinking and the Piezo speaker begins making sounds.
How it works:
There is a photocell placed inside of the copper tubing. When the rocket ship is resting on the tubing, the lack of light input into the photocell tells the red LED to blink. When the rocket ship is removed, light enters the copper tubing and the Piezo speaker is told to make sound according to the input from the photocell. Testing was conducted in order to determine the threshold for which the LED should remain blinking and for which the Piezo speaker should begin making sound. It was important to add a line to the code that prints the value of the photocell so that I could determine what value will work best. Therefore, when running this demonstration in a different place and time than when these values were determined, I will need to run it again and adjust the values accordingly.
Components Used:
1 - Arduino Uno
1 - Breadboard
1 - 10K Resistor
1 - 220K Resistor
1 - LED
1 - Photocell
wires
1 - foam, toy rocket ship
1 - small piece of copper tubing
Code:
/*
* This code tells the LED to blink when the photocell is in the dark (the rocket has not taken off.)
* When light touches the photocell (the rocket has taken off), the piezo buzzer begins to sound loudly.
*/
int sensorPin = 0; // select the input pin for the sensor (photocell)
int ledPin = 13; // select the output pin for the LED
int valPhotocell = 0; // variable to store the value coming from the sensor
int speakerPin = 7;
void setup() {
pinMode(ledPin, OUTPUT);
Serial.begin(9600);
pinMode(speakerPin, OUTPUT);
}
void loop() {
valPhotocell = analogRead(sensorPin); // read the value from the sensor, 0-1023
Serial.println(valPhotocell);
if (valPhotocell <= 300){ //trouble shooting was conducted to estimate this value
digitalWrite(ledPin, HIGH); // turn the LED on (HIGH is the voltage level)
delay(500); // wait for a half a second
digitalWrite(ledPin, LOW); // turn the LED off by making the voltage LOW
delay(500); // wait for half a second
}
else {
digitalWrite(ledPin, LOW); // turn the LED off by making the voltage LOW
digitalWrite(speakerPin, LOW);
valPhotocell = valPhotocell*2; // process the value a little
for( int i=0; i<50; i++ ) { // play it for 50 cycles
digitalWrite(speakerPin, HIGH);
delayMicroseconds(valPhotocell);
digitalWrite(speakerPin, LOW);
delayMicroseconds(valPhotocell);
}
}
}
- Login to post comments