ESP32 Feather and PWM scale tones (abcdefg)

Posts: 1
Joined: Tue Jan 30, 2018 11:20 pm

ESP32 Feather and PWM scale tones (abcdefg)

Postby gborges » Sun Mar 25, 2018 6:12 pm

Hi Guys,

Before all, thanks you for your time supporting my request!! I new using the ESP32 feather and here is my first post.

My Project: practice eChanter using ESP_32 + Touch Sensor CAP1188

I am using an IC ESP32 adafruit feather and I would to know if we could generate some frequency tones (music scale tones) on the same output PIN PWN changing the freq using the LedcWritetone!!! freq values one at the time, based on some input criteria’s. Normally ARDUINO have a tone library that provide a good solution for this type of projects but does not work yet on the ESP32!!! and I looking for a ESP32 work around to generated the scale music tones necessary for my project, using the PWM
So far I using as well a touch sensor “CAP1188” connected on I2C, as well and based on the touch sensor matrix SCAN via I2C I would to generate a different tones on the same output pin connect to my buzzer.( one tone at the time)

.-here is the expected scale tones to be generated in Hz (419,472.333,531,590,629,708,786,839,944)

At this moment, I already ensemble my practice eChanter and I can read the TouchSensor matrix report without problem on the ESP32 with a fast and clean response changes of the touchsensor as expected based on the sensitive touch settings etc.

Now to completed this project I need to know is if possible to change or generated the frequency output of my select GIOP PIN (26) that is connected to the buzzer with a resistor of 300ohm, using for example the channel 0, 8 bits, based on the touch changes!!!!

I already try the following test code with only 3 tones to be generated:

This is a library for the CAP1188 I2C/SPI 8-chan Capacitive Sensor

Designed specifically to work with the CAP1188 sensor from Adafruit

These sensors use I2C/SPI to communicate, 2+ pins are required to
Adafruit invests time and resources providing this open source code,
please support Adafruit and open-source hardware by purchasing
products from Adafruit!

Written by Limor Fried/Ladyada for Adafruit Industries.
BSD license, all text above must be included in any redistribution

#include <Wire.h>
#include <SPI.h>
#include <Adafruit_CAP1188.h>
#include <driver/ledc.h>

// 0 NULL 0.0001
// 1 LG 419.333
// 2 A 472.333
// 3 B 531
// 4 C 590
// 5 D 629.333
// 6 E 708
// 7 F 786.6667
// 8 G 839.111
// 9 HA 944

int freq = 0.0001;
int channel = 0;
int resolution = 8;

// Reset Pin is used for I2C or SPI
#define CAP1188_RESET 9

// CS pin is used for software or hardware SPI
#define CAP1188_CS 10

// These are defined for software SPI, for hardware SPI, check your
// board's SPI pins in the Arduino documentation
#define CAP1188_MOSI 11
#define CAP1188_MISO 12
#define CAP1188_CLK 13

// For I2C, connect SDA to your Arduino's SDA pin, SCL to SCL pin
// On UNO/Duemilanove/etc, SDA == Analog 4, SCL == Analog 5
// On Leonardo/Micro, SDA == Digital 2, SCL == Digital 3
// On Mega/ADK/Due, SDA == Digital 20, SCL == Digital 21

// Use I2C, no reset pin!
Adafruit_CAP1188 cap = Adafruit_CAP1188();

// Or...Use I2C, with reset pin
//Adafruit_CAP1188 cap = Adafruit_CAP1188(CAP1188_RESET);

// Or... Hardware SPI, CS pin & reset pin
// Adafruit_CAP1188 cap = Adafruit_CAP1188(CAP1188_CS, CAP1188_RESET);

// Or.. Software SPI: clock, miso, mosi, cs, reset
//Adafruit_CAP1188 cap = Adafruit_CAP1188(CAP1188_CLK, CAP1188_MISO, CAP1188_MOSI, CAP1188_CS, CAP1188_RESET);

void setup() {

delay(1000); // give me time to bring up serial monitor
Serial.println("ESP32 Touch Test");
Serial.println("CAP1188 test!");

// Initialize the sensor, if using i2c you can pass in the i2c address
// if (!cap.begin(0x28)) {
if (!cap.begin()) {
Serial.println("CAP1188 not found");
while (1);
Serial.println("CAP1188 found!");

// cap.writeRegister(0x1f, 0x3f); // sensibilidad 0x1f, 0x6F x2 7f es 1x y funciono bien 76 es otrpo
cap.writeRegister(0x24, 0x25); // Averaging and Sampling Configuration Register default 39 ,voy con31 no funciona 640us este esta optimo

ledcSetup(channel, freq, resolution);
ledcAttachPin(26, 0);


void loop() {

int touched = cap.touched();

Serial.print(touched, DEC);Serial.print("\t");

if (cap.touched()> 0 )
int touched =;
{switch (touched)

case 0:
// zero zero
Serial.println("case 99 ");
ledcWriteTone(channel, 0);

case 254:
// escale A
Serial.println("case 254 ");
ledcWriteTone(channel, 472);

case 252:
// escale B
Serial.println("case 252 ");
ledcWriteTone(channel, 531);

case 249:
// escale C
Serial.println("case 249 ");
ledcWriteTone(channel, 590);
// turn no tone:
Serial.println("case nulo ");
ledcWriteTone(channel, 0);



///////////////////////////end of code //////////////////

Thank you again for your support and point my in the right direction in order to be able to completed this project.



Who is online

Users browsing this forum: No registered users and 8 guests