In Arduino Tutorial 6: Alarm, we use arduino starter kit to try a new component: the buzzer! It generates sounds of different frequencies using sinusoidal waves. If you connect a LED with the same sinusoidal wave, you can make your own alarm.
Make the following connections. Notice that the longer leg on the buzzer is positive, and the shorter leg is negative. Connect the negative lead to GND and the positive lead to Pin 8.
Code
Sample code 6-1 (from “Beginning Arduino”) Sample code 6-1: // Project 6 Alarm float sinVal; int toneVal; void setup(){ pinMode(8, OUTPUT); } void loop(){ for(int x=0; x<180; x++){ //convert angle of sinusoidal to radian measure sinVal = (sin(x*(3.1412/180))); //generate sound of different frequencies by sinusoidal value toneVal = 2000+(int(sinVal*1000)); //Set a frequency for Pin-out 8 tone(8, toneVal); delay(2); } }You can hear alarm of high and low pitches after uploading the code.
Code
First, define two variables: float sinVal; int toneVal; “float” is a datatype for floating point numbers (a number that has a decimal point). Floating point numbers are often used to approximate analog and continuous values because they have greater resolution than integers. Here we use the “float” variable to store sinusoidal values. The sinusoidal wave changes quite evenly in a wave shape, so we convert it to sound frequencies. Hence, toneVal gets values from sinVal and converts it to frequencies. for(int x=0; x<180; x++){} We need to use the formula “3.1412/180)” to convert it from an angle to a radian value because the unit “sin” is radian instead of an angle.sinVal=(sin(x*(3.1412/180)));
Then we change this value to a sound frequency of an alarm: toneVal = 2000+(int(sin- Val*1000)); “sinVal” is a floating variable, a value with decimal point. We don’t want our frequency to have decimal point, so we need to change the floating value to an integer value by writing the command as below: int(sinVal*1000) Human ears can notice sound of frequencies from 20Hz to 20kHz, so we multiply the raw value by 1000 times plus 2000 to assign the value for “toneVal” to give us a range of 2000 to 3000.
tone(8, toneVal);
Here we introduce 3 functions relevant to tone: 1. tone(pin,frequency) “pin” is the digital pin connected to the buzzer. Frequency is the frequency value in Hz. 2. tone(pin,frequency, duration) The “duration” parameter is measured in milliseconds. If there is no duration, the buzzer will keep making sound of different frequencies. 3. noTone(pin) The “noTone(pin)” function is to end the sound from the specific pin.
Components
The Buzzer
A buzzer is an electronic component that can generate sound. There are generally two types: piezo buzzers and magnetic buzzers.
The Difference between Active Buzzers and Passive Buzzers
Piezo and magnetic buzzers are futher categorized in to two types: active and passive buzzers. The basic difference lies in different demands for their input signal. In this case, “active” and “passive” do not refer to power sources, but oscillation sources. An active buzzer has its own oscillation source - it buzzes as it is powered on. An active buzzer has a simple oscillator circuit that changes DC current into a pulse signal of a certain frequency. Active buzzers contain a special film called “molybdenum” the The magnetic field from the oscillation of the buzzer. Once powered, it starts to make a sound. Active buzzers are non-polarized meaning that you can connect them any way around and they will work.In this kit, active magnetic buzzers are included.
A passive buzzer has no oscillator of its own, so it needs to use a square wave from 2khz to 5khz to trigger it instead of simply using direct current.
Passive buzzers are polarized, so they have to be connected the correct way around: They have a long lead (anode) and short lead (cathode) For a beginner, passive buzzers are easier to work with.
If you want to explore buzzers further, here are some project ideas:
Passive buzzers are good for various musical effects. There are many applications based on buzzers. A lot of buzzer-based gadgets are possible like infrared sensors and ultrasonic sensors for monitoring and alerting approaching objects; temperature sensors for excess temperature alarm; gas sensors for gas leakage alarms. Besides alarms, buzzers can also be used as musical instruments using different frequencies to form different notes. Aren’t buzzers amazing?
This article takes a closer look at each of these devices, comparing them across key areas like Python support, ease of use, Python programming tools, pin design, and hardware projects.
Deciding between Scratch and Python for your child's coding journey? This article compares both languages based on learning background, goals, methods, and difficulty.