Skip to content

Commit f96be30

Browse files
committed
finished initial version of tone.md for esp32
1 parent 1e583c5 commit f96be30

File tree

2 files changed

+91
-8
lines changed

2 files changed

+91
-8
lines changed

esp32/index.md

+6-2
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,14 @@ In this [lesson](led-fade.md), you'll learn how to use PWM output on the ESP32 t
3737

3838
In this [lesson](pot-fade.md), you'll learn how to use analog input on the ESP32 by building a potentiometer-based LED fader.
3939

40-
## [Lesson 5: Capacitive Touch Sensing](capacitive-touch-sensing.md)
40+
## [Lesson 5: Playing Tones](tone.md)
41+
42+
Arduino's [`tone()`](https://www.arduino.cc/reference/en/language/functions/advanced-io/tone/) method is not supported on the ESP32. In this [lesson](tone.md), you'll learn how to play tones using the `ledcWriteTone` and `ledcWriteNote` in [esp32-hal-ledc.c](https://github.com/espressif/arduino-esp32/blob/master/cores/esp32/esp32-hal-ledc.c).
43+
44+
## [Lesson 6: Capacitive Touch Sensing](capacitive-touch-sensing.md)
4145

4246
The ESP32 has built-in circuitry and software for capacitive touch sensing ([docs](https://github.com/espressif/esp-iot-solution/blob/master/documents/touch_pad_solution/touch_sensor_design_en.md#1-introduction-to-touch-sensor-system)). In [this lesson](capacitive-touch-sensing.md), we’ll use the touch sensing functionality to turn on an LED.
4347

44-
## [Lesson 6: Internet of Things](iot.md)
48+
## [Lesson 7: Internet of Things](iot.md)
4549

4650
The ESP32 is exciting not just because of its speed, memory, and GPIO capabilities but also because it is truly a modern Internet of Things (IoT) board with Wi-Fi and Bluetooth support. In this lesson, we'll learn how to use WiFi and the IoT platform [Adafruit IO](https://learn.adafruit.com/welcome-to-adafruit-io) to upload sensor data in real-time.

esp32/tone.md

+85-6
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ usetocbot: true
1818
---
1919

2020
<iframe width="736" height="414" src="https://www.youtube.com/embed/zFg1fSFGL7o" title="YouTube video player" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>
21-
**Video.** A video demonstrating the [Tone32.hpp](https://github.com/makeabilitylab/arduino/blob/master/MakeabilityLab_Arduino_Library/src/Tone32.hpp) class, which supports play durations on the ESP32. The code running on the ESP32 is available [here](https://github.com/makeabilitylab/arduino/blob/master/ESP32/Tone/AnalogInputTone32WithOLED/AnalogInputTone32WithOLED.ino).
21+
**Video.** A video demonstrating the [Tone32.hpp](https://github.com/makeabilitylab/arduino/blob/master/MakeabilityLab_Arduino_Library/src/Tone32.hpp) class, which supports play durations on the ESP32. The code running on the ESP32 is available [here](https://github.com/makeabilitylab/arduino/blob/master/ESP32/Tone/AnalogInputTone32WithOLED/AnalogInputTone32WithOLED.ino). Make sure you have your sound on to hear.
2222
{: .fs-1 }
2323

2424
On Arduino, the [`tone()`](https://www.arduino.cc/reference/en/language/functions/advanced-io/tone/) function generates a square wave of a specified frequency on a pin and is used to "play" tones on piezo buzzers or speakers; however, it is [famously unsupported](https://www.thomascountz.com/2021/02/21/arduino-tone-for-esp32) on the ESP32. In this lesson, we will provide some context about this problem and then show you how to play tones on the ESP32 using the [LEDC PWM library](https://github.com/espressif/arduino-esp32/blob/master/cores/esp32/esp32-hal-ledc.c), which we also used in our [ESP32 LED Fade lesson](led-fade.md).
@@ -184,23 +184,102 @@ Let's get making!
184184

185185
## Let's make stuff
186186

187-
In the activities below, we are going to first play a scale and various raw frequencies before introducing the Tone32.hpp class, which helps abstract some complexity. Though we try to embed mp4 videos directly into our lessons, we are going to be using YouTube embeds here instead so you can more easily control the sound.
187+
In the activities below, we are going to first play a scale and various raw frequencies before introducing the Tone32.hpp class, which helps abstract some complexity. Though we try to embed mp4 videos directly into our lessons, we are going to be using YouTube embeds here instead so you can more easily control the sound. So, make sure you have your sound on (and possibly wear headphones, to limit annoyance to others around you).
188+
189+
<!-- TODO: add in circuit diagrams of everything here -->
188190

189191
### Playing the C scale
190192

193+
First, let's play a simple C major scale based on Thomas Countz's comment on [GitHub Issue 1720](https://github.com/Thomascountz). While we could use an array to step through notes, let's keep things super simple and just write out each note directly. The full code is:
194+
195+
{% highlight C %}
196+
// Change this depending on where you put your piezo buzzer
197+
const int TONE_OUTPUT_PIN = 26;
198+
199+
// The ESP32 has 16 channels which can generate 16 independent waveforms
200+
// We'll just choose PWM channel 0 here
201+
const int TONE_PWM_CHANNEL = 0;
202+
203+
void setup() {
204+
ledcAttachPin(TONE_OUTPUT_PIN, TONE_PWM_CHANNEL);
205+
}
206+
207+
void loop() {
208+
// Plays the middle C scale
209+
ledcWriteNote(TONE_PWM_CHANNEL, NOTE_C, 4);
210+
delay(500);
211+
ledcWriteNote(TONE_PWM_CHANNEL, NOTE_D, 4);
212+
delay(500);
213+
ledcWriteNote(TONE_PWM_CHANNEL, NOTE_E, 4);
214+
delay(500);
215+
ledcWriteNote(TONE_PWM_CHANNEL, NOTE_F, 4);
216+
delay(500);
217+
ledcWriteNote(TONE_PWM_CHANNEL, NOTE_G, 4);
218+
delay(500);
219+
ledcWriteNote(TONE_PWM_CHANNEL, NOTE_A, 4);
220+
delay(500);
221+
ledcWriteNote(TONE_PWM_CHANNEL, NOTE_B, 4);
222+
delay(500);
223+
ledcWriteNote(TONE_PWM_CHANNEL, NOTE_C, 5);
224+
delay(500);
225+
}
226+
{% endhighlight C %}
227+
228+
And a video demo below:
229+
191230
<iframe width="736" height="414" src="https://www.youtube.com/embed/H7MOhibjOO0" title="YouTube video player" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>
192231
**Video.** A video demonstration of [PlayScale.ino](https://github.com/makeabilitylab/arduino/blob/master/ESP32/Tone/PlayScale/PlayScale.ino). The actual version shown in the video is [PlayScaleWithOLED.ino](https://github.com/makeabilitylab/arduino/blob/master/ESP32/Tone/PlayScaleWithOLED/PlayScaleWithOLED.ino).
193232
{: .fs-1 }
194233

195234
### Reading analog input, outputting raw frequencies
196235

236+
OK, now let's make a slightly more complicated version that reads in an analog input and translates this value into an output frequency. In our demo video below, we are using a potentiometer.
237+
238+
{% highlight C %}
239+
// Change this depending on where you put your piezo buzzer
240+
const int TONE_OUTPUT_PIN = 26;
241+
242+
// Change this depending on where you hook up your sensor
243+
const int SENSOR_INPUT_PIN = A1;
244+
245+
// The ESP32 has 16 channels which can generate 16 independent waveforms
246+
// We'll just choose PWM channel 0 here
247+
const int TONE_PWM_CHANNEL = 0;
248+
249+
const int MIN_FREQ = 32; // min frequency in hz
250+
const int MAX_FREQ = 1500; // max frequency in hz (1500 is a bit ear piercing; higher even more so)
251+
const int MAX_ANALOG_VAL = 4095;
252+
253+
void setup() {
254+
ledcAttachPin(TONE_OUTPUT_PIN, TONE_PWM_CHANNEL);
255+
}
256+
257+
void loop() {
258+
259+
int sensorVal = analogRead(SENSOR_INPUT_PIN);
260+
int pwmFreq = map(sensorVal, 0, MAX_ANALOG_VAL, MIN_FREQ, MAX_FREQ);
261+
262+
// The ledcWriteTone signature: double ledcWriteTone(uint8_t chan, double freq)
263+
// See: https://github.com/espressif/arduino-esp32/blob/master/cores/esp32/esp32-hal-ledc.c
264+
ledcWriteTone(TONE_PWM_CHANNEL, pwmFreq);
265+
266+
delay(50);
267+
}
268+
{% endhighlight C %}
269+
270+
Here's a video demo.
271+
197272
<iframe width="736" height="414" src="https://www.youtube.com/embed/xr_G_fkHcSo" title="YouTube video player" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>
198273
**Video.** A video demonstration of [AnalogInputTone.ino](https://github.com/makeabilitylab/arduino/blob/master/ESP32/Tone/AnalogInputTone/AnalogInputTone.ino). The actual version shown in the video is [AnalogInputToneWithOLED.ino](https://github.com/makeabilitylab/arduino/blob/master/ESP32/Tone/AnalogInputToneWithOLED/AnalogInputToneWithOLED.ino).
199274
{: .fs-1 }
200275

201276
### Introducing the Tone32.hpp class
202277

203-
To make it easier to use tone functionality on the ESP32 and to support play durations, we created [Tone32.hpp](https://github.com/makeabilitylab/arduino/blob/master/MakeabilityLab_Arduino_Library/src/Tone32.hpp). Tone32.hpp is part of the [Makeability Lab Arduino Library](https://github.com/makeabilitylab/arduino/tree/master/MakeabilityLab_Arduino_Library), follow the instructions here for installation and use.
278+
The examples above demonstrated how to use `ledcWriteTone` and `ledcWriteNote` from [esp32-hal-ledc.c](https://github.com/espressif/arduino-esp32/blob/master/cores/esp32/esp32-hal-ledc.c) to drive specific PWM frequencies on output pins—these square waves manifest as sound with the piezo buzzers.
279+
280+
However, these methods aren't as easy to use as the Arduino [`tone()`](https://www.arduino.cc/reference/en/language/functions/advanced-io/tone/) library and don't support play durations. To address these limitations, we created [Tone32.hpp](https://github.com/makeabilitylab/arduino/blob/master/MakeabilityLab_Arduino_Library/src/Tone32.hpp). Tone32.hpp is part of the [Makeability Lab Arduino Library](https://github.com/makeabilitylab/arduino/tree/master/MakeabilityLab_Arduino_Library). Follow the instructions [here](https://github.com/makeabilitylab/arduino/tree/master/MakeabilityLab_Arduino_Library) for installation and use.
281+
282+
#### Key Tone32 differences with tone library
204283

205284
There are a few key differences with: [`tone()`](https://www.arduino.cc/reference/en/language/functions/advanced-io/tone/) library:
206285

@@ -210,6 +289,8 @@ There are a few key differences with: [`tone()`](https://www.arduino.cc/referenc
210289

211290
- Third, unlike [`tone()`](https://www.arduino.cc/reference/en/language/functions/advanced-io/tone/), you play tones via either `playNote` or `playTone`, both of which are overloaded functions with `duration` options.
212291

292+
#### Primary Tone32 methods
293+
213294
Here are the key Tone32 methods:
214295

215296
{% highlight C %}
@@ -362,12 +443,10 @@ void loop() {
362443
}
363444
{% endhighlight C %}
364445

365-
##### Video of AnalogInputTone32
366-
367446
Here's a video demonstration of [AnalogInputTone32.ino](https://github.com/makeabilitylab/arduino/blob/master/ESP32/Tone/AnalogInputTone32/AnalogInputTone32.ino):
368447

369448
<iframe width="736" height="414" src="https://www.youtube.com/embed/zFg1fSFGL7o" title="YouTube video player" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>
370-
**Video.** A video demonstrating [AnalogInputTone32](https://github.com/makeabilitylab/arduino/blob/master/ESP32/Tone/AnalogInputTone32/AnalogInputTone32.ino). Note that this video is running a slight variation with OLED output called [AnalogInputTone32WithOLED.ino](https://github.com/makeabilitylab/arduino/blob/master/ESP32/Tone/AnalogInputTone32WithOLED/AnalogInputTone32WithOLED.ino).
449+
**Video.** A video demonstrating [AnalogInputTone32](https://github.com/makeabilitylab/arduino/blob/master/ESP32/Tone/AnalogInputTone32/AnalogInputTone32.ino). Note that this video is running a slight variation with OLED output called [AnalogInputTone32WithOLED.ino](https://github.com/makeabilitylab/arduino/blob/master/ESP32/Tone/AnalogInputTone32WithOLED/AnalogInputTone32WithOLED.ino). Make sure you have your sound on to hear.
371450
{: .fs-1 }
372451

373452
## Next Lesson

0 commit comments

Comments
 (0)