Generating Sound - Buzzer + Transistor
Next
lets see how we can generate sound using a Buzzer. The Buzzer would
require more current than provided by the pins of the arduino. To
provide the buzzer with more current, we shall use a transistor to
trigger the buzzer. The transistor in turn will be triggered by a pin on
the arduino. If you do not know about transistors, its advisable you
check it out
http://en.wikipedia.org/wiki/Transistor
Here's what our Transistor 2N2222A looks like
![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjHcUQ2Jx7CtGNzHwjh0HTTOa52hEwt5a1-hSl1TVWp4mrJIChH1XVZfo5Zhcd6zwc9xWosF8QrH7oTRJmr1waw4_plY4u-8kFlSIZeE_vWRtvk5xRptXzLD0ZM-6N133q_HoDIuSk9IwuM/s640/Tran1.jpg) |
2N2222A |
![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjO2STE_DgHSHFK54nWc-jILBBv8TTeGk7DmaHTVaerizI685j5TiOLULft2yrOhcih4mOuQzU56v4BDxCSNaZ_J6nzuJJ-sObo0oZR134rdBr798u_UgHd5mBmuV5AHIQDnUgO0yFM1Wy3/s400/2n2222A.gif) |
Pin Mappings for 2N2222A |
E = Emitter
B = Base
C = Collector
The
Emitter of a transistor is connected to ground, the device to be
triggered is connected between the Collector of the transistor and
SUPPLY. The transistor can be triggered by Supplying a Trigger voltage
to the Base (preferably through a resistor). Depending upon the trigger
voltage, Emitter and Collector of a transistor get in contact thereby
allowing current flow through the device connected.
![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgqWGE5n4IW3cmV4FXgVFAW_rYIN-InG3gvc5xjpL23zt4pmaxFtBT6E8S7keVE-msI9AZbhc3nrltizJKTKgyvt0uj-e6UB4mkd0F7MKQesCeP0yISWRKMFxhQu-VN1FWWsH2Bo7MaJM3_/s640/Buzzer1.jpg) |
Buzzer - The Red Wire is Positive & The Blue Wire is Negative. |
![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiHP-ZnfToiVJYpPqgk-lcug49ZTg__pIPWMh-b3ouToc3DDekhx4-4nAXZ3e9ipOywi373HnTN65zJVJVgslza8Suz3m04hT_Mfh82lYQG8YkqrFFrj1VAShZlinOzHa1ImlpTIOxfte32/s640/Buzzer2.jpg) |
First Setup the Transistor Side like in the Image |
![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiaMU4SV6VgwC8S2qJ-9xd3xaK-3jBjzhzB6dzr7DiGMd-V5ZxgoxCtuw0CWu6pTh17JVRorIVcyzIRzu2GMM9LLsbRLO07loDKJlV99cAbGV7-xJcHWDLr5yTp6c8YxrN9mWyewWTPGLsF/s640/Buzzer3.jpg) |
The Blue wire
going to the Collector here is the Negative of the Buzzer and the Red
wire (the Positive of the Buzzer ) is connected to the '+'ve terminal of
the Breadboard |
![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhQqEXKO_jVbRKGCfgaGE8Fbz6PN11Vy6OBjerufaNu9JxDVkW0FEZxsbG_EIWtlu8HONhzGXgq3Bqc-wReAvIMUvAhEfJTY_DXp79kFpcpBllOk8kjJCkH73sCprwNinGdGw3BS4V2ZROG/s640/Buzzer4.jpg) |
Now Connect a Wire from the Other end of the Resistor to the 7th pin (digital) of the Arduino |
![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj6OGo521V4KpJksx24T7t-FzfXo6T-HzMMWTpDeUxBhbkiaDX3uJkf0AVUEVfFAB_L3gqJN5pCLBwMGfyzKBgVX55a-jXJ0zND2ePUoLVPyzi_wU8_niDZwn2hqW1op5dKLy3IwUGvi4Z2/s640/Buzzer5.jpg) |
Here is how the Final Setup Looks Like |
Try the following code where the Buzzer stays on as
you keep pressing the Button in our previous program
[RGB_button_ldr_buzzer.ino]
/*
Pressing the Button changes the current active color of the RGB LED & prints the current temperature value to the Serial Monitor and Generates a Buzzer tone
for the duration the button is being pressed.
The light intensity detected by the LDR determines the intensity level of the active color
*/
int intensity = 0, pin = 9;
void setup() {
pinMode(2,INPUT);
pinMode(7,OUTPUT);
Serial.begin(9600);
}
void loop() {
if(digitalRead(2)==0) // Switch being pressed
{
digitalWrite(7,HIGH);
Serial.print("Temperature is : ");
int temp = analogRead(2)/2;
Serial.println(temp);
if(pin<11)
pin++;
else
pin=9;
analogWrite(9,0);
analogWrite(10,0);
analogWrite(11,0);
while(digitalRead(2)==0);
delay(100);
digitalWrite(7,LOW);
}
intensity = analogRead(4)/4;
analogWrite(pin,intensity);
Serial.print(pin);
Serial.print(" ");
Serial.println(intensity);
}
No comments:
Post a Comment