HELLO THERE!!!
How are you, guys? As usual this week, I would like to share with you guys an experiment involving piezo speaker and potentiometer. the objective of this experiment is to control the piezo speaker by using two potentiometers. As usual let's see the hardware and software used in this experiment.
HARDWARE:
1.Arduno UNO
How are you, guys? As usual this week, I would like to share with you guys an experiment involving piezo speaker and potentiometer. the objective of this experiment is to control the piezo speaker by using two potentiometers. As usual let's see the hardware and software used in this experiment.
HARDWARE:
1.Arduno UNO
2. Potentiometers(2 Units)
3.Piezo speaker
SOFTWARE:
1, Arduino IDE
After obtaining the hardware and the software, let's see on how to assemble the circuit. The circuit is fairly simple. The potentiometer has 3 legs, at one end is the vcc( or ground), the middle leg is the ouput pin, and the other end is the ground(or vcc).The red wire from the piezo is connected to the output pin and the black wire to the ground. For more clarification, refer to the circuit sketch below.
Pretty simple, right? Now let's see the Arduino code for this experiment. At the beginning of the code, we have declarations of the variables that are going to be used in this experiment.
int spk = 9; //the piezo speaker is connected to pin 9
int pot1 = A0; //the first potentiometer is connected to analog 0 pin
int pot2 = A1;//the second potentiometer is connected to analog 1 pin
int val1;
int val2;//both variables are used to read pot1 and pot2
In void setup, we declare our variables either input or output.
void setup() {
Serial.begin(9600);//Baud rate per second for serial communication
pinMode(spk,OUTPUT);//our piezo speaker is configured as output
}
next to our void loop. This is where all the circuit function happens.
void loop() {
val1= analogRead(pot1);//Read the first potentiometer
Serial.print(val1);//print the value in serial monitor(optional)
Serial.print(",");//adding a comma(optional)
val2 = analogRead(pot2);//Read the value of the second potentiometer
Serial.print(val2);
Serial.println(" ");
tone(spk,val1,val2);//The tone will set the output as follows tone(pin,frequency,duration)
//This means that the frequency is controlled by pot1 and duration by pot2
}
well that's it, upload the code and have fun on tinkering!!! You can also add some more variables such as LED's to light up when certain values of potentiometer is reached. The full code is shown below:
int spk = 9; //the piezo speaker is connected to pin 9
int pot1 = A0; //the first potentiometer is connected to analog 0 pin
int pot2 = A1;//the second potentiometer is connected to analog 1 pin
int val1;
int val2;//both variables are used to read pot1 and pot2
void setup() {
Serial.begin(9600);//Baud rate per second for serial communication
pinMode(spk,OUTPUT);//our piezo speaker is configured as output
}
void loop() {
val1= analogRead(pot1);//Read the first potentiometer
Serial.print(val1);//print the value in serial monitor(optional)
Serial.print(",");//adding a comma(optional)
val2 = analogRead(pot2);//Read the value of the second potentiometer
Serial.print(val2);
Serial.println(" ");
tone(spk,val1,val2);//The tone will set the output as follows tone(pin,frequency,duration)
//This means that the frequency is controlled by pot1 and duration by pot2
}
Below is the sample video for this experiment. See you guys next week!!
Comments
Post a Comment