ESP8266 NodeMCU – Create a melody

This tutorial will show you how to play a melody with a passive buzzer. If you need an introduction on how to start with buzzers, you can check the tutorial “ESP8266 NodeMCU – Make some noise with buzzers“. Here you will learn how to impress Mozart.

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.

Push-Button and Pull Down Resistor

Figure 1

For better understanting, lets consider the figure 1. A Push-Button is a simple mechanism, that allows you to connect the point A with point B. Thus, each time you press the button you connect pin D5 to GND. We will use this button, to trigger an interrupt. An interrupt is an event that occurs randomly in the midle of the program. It is just like a call you have when you are busy with some work. When the button is not pressed, the pin D5 must be connected. That’s why we activate in the code, an internal “Pull-Up” resistor. Therefore, the microcontroller always know the logic value of the pin.

Circuit

Connect the buzzer to GPIO pin 2 (D4) and the button to GPIO pin 14 (D5) as shown in figure 2. The setup will get power from your PC using micro-usb to usb cable. The buzzer can use an external source of power, but for that we would need more parts. In this configuration, the power came directly from the GPIO pins.

Coding

This code will use a header file with the notes and their frequencies. Such notes, go to a vector organized acording to the music you want. To start and stop the melody when ever you want we will use the button, that is connected to an interrupt pin. NodeMCU based ESP8266 has interrupt feature on its GPIO pins. This function is available on D0-D8 pins. Copy the main sketch bellow to your Arduino IDE project and save it.

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.

The code above uses an header file, ALL_NOTES.h. This file contains all the pitch values for typical notes, and was based in the table originally written by Brett Hagman. Thus, each time you want to make a melody, just have to directly choose the notes from the ALL_NOTES.h file. To create this file, click on “New Tab” as shown in the figure 3.

Figure 3

That will open a blank page, then paste in the following code.

Connect your ESP8266 NodeMCU to your PC using micro-usb to usb cable and upload the code.

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 *