Chapter 7. Basic Experiments
7.4. Experiment with Beeper
Except for display components in a MCU system, sound generators are also used. The most common sound generator is beeper, also called buzzer, which will generate different frequency sound at different frequency square wave.
This experiment introduces the user how to make a beeper generate sound.
Compared with speaker, beeper is very easy to control. Only certain current is needed to drive the beeper, make it generate sound. Therefore, it is basically the same to control a LED or a beeper.
Because beeper has inductance, it’s not recommended to control it by I/O pin directly. Transistor and diode can be used to isolate the beeper from I/O. In this experiment, we only use transistor.
The PNP transistor in below diagram controls the beeper. When MCU P3.6 is set to low level, the beeper will sound.
01 #include <reg51.h>
03 sbit BUZZER=P3^7;
05 void main(void)
07 BUZZER = 0;
Line 1: include the 8051 register definition header file
Line 3: sbit define beeper to P3.7
Line 5-9: main function
Line 7: set P3.7 to output low level, making the beeper sound
Line 8: loop