I am new to Arduino and microcontrollers, I want to make a tone with my D1 mini and a LSM-50F speaker. But I don't get any output. Any idea what I am doing wrong? Tested it with two D1 mini boards.
This is how I wired everything: enter image description here
Specifications:
Impedance 45 Ohm
Power (Nominal) 0,2 W
Power (Maximum) 0,5 W
Resonance frequency 450 Hz
The D1 mini's GPIO pins have a maximum of 10 mA, so I am using a 330 Ohm resistor in series to the 45 Ohm speaker to avoid overloading.
I = U/R
I = 3.3 V / 330 Ohm + 45 Ohm
I = 3.3 V / 375 Ohm
I = 8.8 mA
My code:
int Speaker = 5;
void setup()
{
}
void loop()
{
tone(Speaker, 450);
delay(1000);
noTone(Speaker);
}
I also tried a piezo from an old computer in the same setup. Not sure if the piezo is active or passive, so I tried using:
digitalWrite(Speaker, HIGH); // Play a tone on an active piezo.
And
tone(Speaker, 450); // Play a tone on a passive piezo.
-
Yes! 3.3 V / 375 Ohm = 0.0088 A conversion to mALouis Eiden– Louis Eiden2023年10月26日 16:05:37 +00:00Commented Oct 26, 2023 at 16:05
1 Answer 1
You are plugged into D5 and have int Speaker = 5;
However, D5 and 5 are actually different things.
static const uint8_t D5 = 14;
The plain number refers to the GPIO number of the esp8266 chip. The "D5" type numbers that are silk screened are mapped onto those. So you need int Speaker = D5;
or alternately int Speaker = 14; /*D5 is GPIO14*/
or you need to move the wire to the actual GPIO5 which is labeled D1
.
You may find your speaker is very quiet, maybe inaudible. If you're going to try driving a speaker from a GPIO pin the piezo is a better choice. But really it would be better to have some kind of amplification. This is the sort of thing I've used with piezos before. You can test that you're affecting the pin at all with a meter. You may get a weird reading, but you should at least be able to tell the difference between that and a pin you aren't trying to use.