# Seg 2: Experiment, principle, and code

5.4 Experimental principle

The principle is simple in this experiment, which is summarized as follows. The buzzer can make a sound, since the current on the buzzer is powered on the electromagnetic coil, which can make coil generate magnetic field. Then it can drive vibrating diaphragm to generate a sound. This is the reason why the buzzer must need the current. Its schematic principle is drawn in Figure 5-3. In our such experiment, port 9 on the Arduino board can provide a voltage for the passive buzzer. Then it can generate a sound. It is worth noting that, the rated voltage for the buzzer is 5V, while the Arduino’s max voltage is 5V as well. Therefore, the circuit cannot connect serially a resistor. If connected, then the buzzer doesn’t work.

5.5 Experiment and code

In fact, the circuit connection of such experiment is very simple, which is seen in Figure 5-3. After run Program 5, Arduino can generate alarm sound.

Figure 5-3 Experimental circuit

 01 // Program 5: how to use Arduino and buzzer generate alarm sound 02  03 void setup() 04 { 05 } 06   07 void loop() 08 { 09 for(int i=200;i<=800;i++)   // frequency from 200Hz to 800Hz 10 { 11   pinMode(4,OUTPUT); 12   tone(4,i);       //output frequency at port 4, i.e., generate a sound 13  delay(5);       //generate a sound for 5ms  14 } 15 delay(4000);           //the highest frequency lasts for 4ms 16 for(int i=800;i>=200;i–) 17 { 18   pinMode(4,OUTPUT); 19   tone(4,i); 20  delay(10); 21 } 22 }

Here, a new Arduino function “tone” is introducted. Its syntax is:

tone(pin, frequency)

tone(pin, frequency, duration)

where,

pinwhich is defined on the Arduino board

frequency: the voice frequency, unit is Hz, unsigned int

durationvoice lasting time, unit is ms (optional), unsigned long

5.6 Key points and summaries

1) The buzzer can be divided into active and passive. The active one can make a sound by directly connecting power, while the passive one can generate a sound only for the changing frequency. Therefore, the buzzer can make a sound on the Arduino board by PWM technique. But, it cannot connect serially a resistor.

2) The principle of buzzer is that, when the current passes through the electromagnetic coil, it will generate a magnetic field, which can drive the vibrating diaphragm make a sound periodically.