ESP8266 NodeMCU – Make some noise with buzzers

Introduction

This tutorial will show you how to make noise with both an active and a passive buzzer. If you need an introduction on how to start with NodeMCU board, you can check the tutorial “ESP8266 NodeMCU – Blinking a LED”.
There are two main types of buzzers for hobby use. Although they are both buzzers, they work differently. Here you will learn why and how to control them.

Parts Required

To build the global setup you must have the following parts:

Note: You do not need to have a NodeMCU board nor an ESP8266 to follow this tutorial. I am using this board because it is very famous and also because, i will post future tutorials using wifi communications. In order to use other boards, you just have to change the pin number.

Passive Buzzer

A passive buzzer (aka piezoelectric buzzer) can make different tones, but the device that controls it has to provide an output with an oscillating electronic signal at a desired frequency. The supplied frequency will determine the tone. Supplying just a fixed voltage will generate no sound, except perhaps a slight “tick” at the point when the power source is connected or disconnected from the buzzer. When mounting in the breadboard, the longer leg is the positive one.
The polarity must be respected for sound to be generated. To distinguish between Active and Passive, always check the product reference or waste your time trying.

Active Buzzer

An active buzzer has additional circuit on it, which makes it easier to use but limits the buzzer to only one type of sound. An active buzzer only requires DC power source for a beep to be generated. When mounting in the breadboard, the longer leg is the positive one. The polarity must be respected for sound to be generated. To distinguish between Active and Passive, always check the product reference, or waste your time trying.

Circuit

Connect the passive to GPIO pin 2 (D4) and the active to GPIO pin 14 (D5) as shown above. Everything will be powered from your PC using micro-usb to usb cable. The buzzers can use an external source of power, but for that we would need more parts. In this configuration, they will be powered directly from the GPIO pins. All the current for the buzzers will be drawn from the board, and since each GPIO delivers only no more than a few mA, the sound can be low. A resistor can be placed in series with the positive leg of the buzzer to protect the board from loading to much current. Let’s try it.

Note: Remember that these buzzers are rated to 5V, and the GPIO only delivers 3.3V

Start Coding

This code will turn on first the active buzzer, then the passive and then loop. The active buzzer is connected to a pin set as digital output. The passive is also connected to a pin set as digital output and PWM compatible. To generate sound in the passive buzzer, we will use the arduino “tone()” function that generates a PWM output. For more information about this function check arduino tone(). Copy the code bellow to your Arduino IDE project and save it. Connect your ESP8266 NodeMCU to your PC using micro-usb to usb cable and upload the code. Have fun!

Note: Unplug the buzzers before download the sketch, or place a capacitor in series with the buzzer to prevent a short-circuit while the ESP8266 is resetting. This happens because the buzzer internal resistance is low.

Please follow and like us:
Ricardo Carreira

Author: Ricardo Carreira

Portuguese Electrical Engineer interested in Automation, Energy, Power generation, Renewables and 3D printers . With a degree in Electrical and Computer Engineering in the field of Energy and Automation. Currently starting in Master’s degree in Electrical Engineering.

Leave a Reply

Your email address will not be published. Required fields are marked *