Huzzah32 ESP32: PWM Sounds using Micropython

Huzzah32 ESP32: PWM Sounds using Micropython

Huzzah32 ESP32 has a lot of great features with Micropython. One of the simplest and most sensitively interesting projects is the PWM sounds using Micropython, which stands for Pulse Width Modulation, a technique for modulating pulse waves by varying their duty ratio, and is used in various electrical controls. This tutorial guides you on how to play PWM sounds using Micropython.

Scientific Pitch Notation:

In order to enable PWM sounds using Micropython, this can be accomplished by assigning each frequency as a value to a variable and storing the values as a list. The table of the note frequency is referred from Wiki.

Preliminary Preparations

Before getting started with this tutorial, please go through the setup tutorial of Micropython on Huzzah32 ESP32.

If you followed the tutorial on the preliminary preparations above, the directory tree should be as follows:

Commands:

In order to define AMPY_PORT as an environment variable, you should use these commands:

Usages:

There are two ways to test the PWM sounds using Micropython: one is to copy and paste the code below by creating a file by yourself, and another is to git-clone the code from GitHub.

Way 1: Copy and Paste

The first way is to copy and paste the code below, but before that, please create a file that you would like to paste to. In this case, I will create a file named “pwmsound.py” under the “src” directory.

Then, the directory tree should be as follows:

Open the file in your favorite IDE. In this tutorial, I use Microsoft Visual Studio. Next, copy and paste the code below into the file.

Eventually, execute the file using the ampy command. Please make sure you are in the src directory and already exported the AMPY_PORT as the environment variable.

If no errors have occurred, you should be able to hear PWM sounds using Micropython!

Way 2: GitHub

This is a little bit advanced way to execute the script because it is assumed that you have already known how to use GitHub. First of all, clone the repository from my GitHub under the “src” directory.

Then, the directory tree should be as follows:

Next, jump into the subdirectory named “pwmsound”.

Once you exported the AMPY_PORT as the environment variable successfully, you simply execute the script as follows:

Demo:

Related Post

Leave a Reply

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

CAPTCHA