Introduction
Buzzers can be categorized as active buzzers and passive ones (See the following picture).
Components
– 1 * SunFounder Uno board
– 1 * USB data cable
– 1 * Buzzer module
– Several jumper wires
Experimental Principle
Place the pins of two buzzers face up and you can see the one with a green circuit board is a passive buzzer, while the other with a black tape, instead of a board, is an active buzzer, as shown below.
An active buzzer has a built-in oscillating source, so it will make sounds when electrified. But a passive buzzer does not have such source, so it will not beep if DC signals are used; instead, you need to use square waves whose frequency is between 2K and 5K to drive it. The active buzzer is often more expensive than the passive one because of multiple built-in oscillating circuits.
Experimental Procedures
Passive Buzzer
Step 1: Build the circuit
Passive Buzzer Module SunFounder Uno
S ————————————- D11
– ———————————— GND
+ ————————————– 5V
Step 2: Program (Please refer to the example code in LEARN -> Get Tutorial on our website)
Step 3: Compile
Step 4: Upload the sketch to SunFounder Uno
Now, you can hear the passive buzzer beep for warning.
Active Buzzer
Note: The active buzzer has built-in oscillating source, so it will beep as long as it is electrified, but it can only beep with a fixed frequency.
Step 1: Build the circuit
Active Buzzer Module SunFounder Uno
S ———————————— D1
– ———————————– GND
+ ———————————— 5V
Step 2: Program (Please refer to the example code in LEARN -> Get Tutorial on our website)
Step 3: Compile
Step 4: Upload the sketch to SunFounder Uno
Now, you can hear the active buzzer beep.
Code for Passive
/*******************************************/ const int buzzerPin = 3;//the buzzer pin attach to int fre;//set the variable to store the frequence value /*******************************************/ void setup() { pinMode(buzzerPin,OUTPUT); } /*******************************************/ void loop() { for(int i = 200;i <= 800;i++) //frequence loop from 200 to 800 { tone(3,i); //turn the buzzer on delay(5); //wait for 5 milliseconds } delay(4000); //wait for 4 seconds on highest frequence for(int i = 800;i >= 200;i–)//frequence loop from 800 downto 200 { tone(3,i); delay(10); } } |
Code for Active
int buzzer = 11;//the pin of the active buzzer void setup() { pinMode(buzzer,OUTPUT);//initialize the buzzer pin as an output } void loop() { unsigned char i,j; while(1) { //output an frequency for(i=0;i<80;i++) { digitalWrite(buzzer,HIGH); delay(1);//wait for 1ms digitalWrite(buzzer,LOW); delay(1);//wait for 1ms } //output another frequency for(i=0;i<100;i++) { digitalWrite(buzzer,HIGH); delay(2);//wait for 2ms digitalWrite(buzzer,LOW); delay(2);//wait for 2ms } } } |