You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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).
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.
43
47
44
-
## [Lesson 6: Internet of Things](iot.md)
48
+
## [Lesson 7: Internet of Things](iot.md)
45
49
46
50
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.
Copy file name to clipboardExpand all lines: esp32/tone.md
+85-6
Original file line number
Diff line number
Diff line change
@@ -18,7 +18,7 @@ usetocbot: true
18
18
---
19
19
20
20
<iframewidth="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.
22
22
{: .fs-1 }
23
23
24
24
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!
184
184
185
185
## Let's make stuff
186
186
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 -->
188
190
189
191
### Playing the C scale
190
192
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
+
191
230
<iframewidth="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>
192
231
**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).
193
232
{: .fs-1 }
194
233
195
234
### Reading analog input, outputting raw frequencies
196
235
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)
<iframewidth="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>
198
273
**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).
199
274
{: .fs-1 }
200
275
201
276
### Introducing the Tone32.hpp class
202
277
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
204
283
205
284
There are a few key differences with: [`tone()`](https://www.arduino.cc/reference/en/language/functions/advanced-io/tone/) library:
206
285
@@ -210,6 +289,8 @@ There are a few key differences with: [`tone()`](https://www.arduino.cc/referenc
210
289
211
290
- 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.
212
291
292
+
#### Primary Tone32 methods
293
+
213
294
Here are the key Tone32 methods:
214
295
215
296
{% highlight C %}
@@ -362,12 +443,10 @@ void loop() {
362
443
}
363
444
{% endhighlight C %}
364
445
365
-
##### Video of AnalogInputTone32
366
-
367
446
Here's a video demonstration of [AnalogInputTone32.ino](https://github.com/makeabilitylab/arduino/blob/master/ESP32/Tone/AnalogInputTone32/AnalogInputTone32.ino):
368
447
369
448
<iframewidth="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.
0 commit comments