5.Arduino learning notebook - Buzzer
5.Arduino learning notebook - Buzzer Experiment
Juvtmall ( a company supply PCB Prototyping, PCBA service and sell kinds of components, modules and so on) ( Catduino has the same function of Arduino)
Querying on the Internet found that the voltage is 5V, the same as the output voltage of the digital port for the arduino control panel, so the resistance is not required. It can be used directly.
Let's start with a brief introduction of this small buzzer.
Small buzzer, because of its small size (6 mm in diameter), light weight, low price, stable structure, it is widely used in various kinds of electrical equipment needed sound , electronic production and MCU ,circuit, etc.The buzzer is divided into active buzzers and passive buzzers.
The following figure is the active buzzer
The following figure is the passive buzzer
Look from the appearance, they look the same. when the buzzer pin is up, you can see the passive buzzer has green circuit board there, and without circuit board but with black rubber seal is the active buzzer. It is absolutely difficult to distinguish them from the appearance. The most reliable approach is using a multimeter to test the buzzer resistance, in addition to check the parameters of the product manual, only Ω 8 or 16 Ω are passive buzzer, resistance over hundreds is the active buzzer.
Active buzzer directly connected to the rated power (new buzzer are indicated in the label) can continuously make the sound, while passive buzzer and electromagnetic speaker, need to connect the output circuit of the audio.
After a brief introduction of the buzzer, let’s have a look at the hardware connection schematic diagram.
Upload the following code to the arduino control board, see the results.
1. Int buzzer = 7;/ / set the digital IO pin to control buzzer
2. Void setup ()
4. PinMode (buzzer, the OUTPUT);/ / set digital IO pin mode, OUTPUT is OUTPUT
6. Void loop ()
8. Unsigned char I, j;/ / define variables
9. While (1)
11. For (I = 0;I < 80;I + +) / / output a frequency sound
13. DigitalWrite (buzzer, HIGH);/ / send voice
14. Delay (1);/ / delay 1 ms
15. DigitalWrite (buzzer, LOW);/ / no sound
16. Delay (1);/ / delay1 ms
18. For (I = 0;I < 100;I + +) / / output the sound of another frequency
20. DigitalWrite (buzzer, HIGH);/ / send voice
21. Delay (2);/ / delay 2 ms
22. DigitalWrite (buzzer, LOW);/ / no sound
23. Delay (2);/ / delay 2 ms
The first frequency has a sound of 1 millisecond and no sound of 1 millisecond.1 second is 1000 ms, 2 ms is a cycle. So the resulting frequency is 500 Hertz.
The second frequency has a sound of 2 ms but 2 ms without any sound, 4 ms for a cycle.We get a frequency of 250 Hertz.
The cycle of an event is the sound of 500HZ in 80 ms, and then the sound of 250 HZ will be ringing in 100 ms.So it's going to cycle.
While () function
This experiment use the while () function
In loop (), while is also a circular statement, the general form:
The expression is a loop condition and the statement is a loop.The semantics are: evaluate the value of the expression and execute the loop statement when the value is true (except 0).Its execution process is shown in following figure:
Function: implement the "when" cycle.When the expression isn’t 0 (true), execute the statement. A "statement" is a executed program cycled, called a "loop".