Touch Input Piano on ESP32, Passive Buzzer with strange output when using Serial print

Hello!

First of all I hope it's fine that this is related to an ESP32 project and not an Arduino board. I am really new to all of this so I don't really know if this is a competitor thing or not.

Quick introduction, I'm in an school based IT apprenticeship and we currently are working with an ESP32 in electronics.

While fiddling around at home I've set my goal on creating a touch based "piano" basically, using a passive buzzer and some thumbtacks as touch based inputs.

I have been able to get it to work but ran into some - for me at least - strange issues that I couldn't find any help for while googling or searching these forums specifically.

Therefor I'm hoping someone could help me out around here - but now to the issue!

So, I'm attaching a picture of my setup:

And here is my code:

#include "pitches.h"

void setup() {
  //pinMode(16, OUTPUT);
  Serial.begin(115200);
}

void loop() {
  Serial.println(touchRead(4));
  while (touchRead(15) > 40 && touchRead(2) > 40 && touchRead(4) > 50 && touchRead(12) > 50 && touchRead(14) > 20 && touchRead(27) > 40 && touchRead(33) > 50 && touchRead(32) > 50) {
    noTone(16);
  }
  playTone(15,NOTE_C4);
  playTone(2,NOTE_D4);
  playTone(4,NOTE_E4);
  playTone(12,NOTE_F4);
  playTone(14,NOTE_G4);
  playTone(27,NOTE_A4);
  playTone(33,NOTE_B4);
  playTone(32,NOTE_C5);
}

void playTone(int read, int note){
if (touchRead(read) < 10) {
    tone(16, note);
  }
}

(notes are defined in another file which I don't need to post reall, just to clarify)

While learning all kinds of stuff about how a buzzer works, it being a passive buzzer and how to utilize tone(), I've read up on how to get some touch input working and worked my way towards some thresholds that I am utilizing in my code.

Now, this code does work - When i start it up I can play all of the defined touch buttons - but - the buzzer sounds.. How would I phrase this.. cranky? Like it's slightly vibrating? It definitely does not sound like it's supposed to.

I found out through some trial and error that apparently the Serial.println() is the perpetrator here, since it has a builtin wait time or similar - so I removed it and then the whole code simply does not remotely work anymore. Sounds only are played basically for the fraction of a second when letting go of the touch button, and not at all while touching it. But the sounds play correctly and not "cranky".

Since I don't know why this would be the case and I can't see if it might "just" be that the threshold for the touch input has changed after removing the Serial output, I've tried around but to no luck in the end. I just cannot get it to work without using the Serial.println in there.

Since I'm a complete beginner at this, this might be a very easy question, or I might very well be just doing things the wrong way.. So I'm looking for some guidance on if this issue is solvable even, or what I could do to achieve what I'm looking for.

Any help is really appreciated!

Welcome. That is a good try at providing the information but it's missing a schematic.

The speaker looks like the electromagnetic type, you can not drive those directly from an Arduino pin (especially an ESP which has a lower current spec).

I'm not saying it is the direct cause of your problem but it's good to eliminate low hanging fruit at the start...

I don't see the function 'touchRead()' defined anywhere. Please post the entire sketch.

Your problem seems complex and unlikely to produce a quick "aha" from anyone. So you need to flesh out the details considerably.

Hey, thanks for the reply.

Uhm, the touchRead() is a built in function? I have not defined that one. See ESP32 Capacitive Touch Sensor Pins with Arduino IDE | Random Nerd Tutorials which I used as reference while learning about the touch possibilities.

Also, this is the entire sketch aside from the note declarations which are just variables referring to the frequencies used by the buzzer.

The buzzer itself I do not know any specifications of, I just know it's a passive buzzer that can be given a frequency to change the pitch of the tone it produces. Without the Serial.println it works like it's supposed to when it comes to the tone and how it's supposed to sound - but my code does not provide what it's supposed to when I'm removing the Serial.println line.

I'll add a scematic tomorrow, sorry for missing that one!

That's encouraging, but if the pin is overloaded, it can work today, and burn up tomorrow. Do you have a DMM that you can use to measure the speaker resistance? If it's a piezo it will read infinite ohms, if it's electromagnetic you will read some low resistance value across the terminals. But, even a piezo should have a current limiting resistor between itself and the MCU pin.

Also, excess current spikes from non-compliant hardware can sneak into the MCU power supply and wreak havoc, sometimes creating a false impression that the code is bad. Changing things in the code changes the timing and other dependencies, which may cause a hardware based behaviour like that to appear and disappear.

You should not be using a tutorial as a reference. Although, considering the state of Espressif documentation, in this case you have my sympathy. But it's worth a look at least...

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.