Infrared tone emitter not working on Nodemcu, but works on Nano or Micro

Hi, does anyone have experience with the tone function on Nodemcu? I'm trying to do the retro reflective sensor described here:

I had to power the emitter down a "little bit" because I only need it to work for 1.5meters. I removed the transistor and added a 680ohm resistor to the IR led by trial and error.

It worked perfectly on a Nano and a Micro, but I really wanted it to be networked, so I moved to a Nodemcu, which is 3.3v.
The IR receiver seems to work fine because I tried with a IR led connected to a Nano.

But the emitter does not work. I can see the LED on a phone camera and it looks ok. I thought there was too little power so I removed the resistor, still nothing. Then I added the 2N2222 transistor and the LED is VERY bright on the phone camera. But sill the receiver only picks it up from 20cm, instead of 2m...

I simplified the code as much as possible:

void setup() {
pinMode(D3, INPUT);
pinMode(D7, OUTPUT);

void loop() {
// put your main code here, to run repeatedly:
if (digitalRead(D3) == HIGH) {
digitalWrite(D7, HIGH);

else {
digitalWrite(D7, LOW);

So, I'm thinking that maybe one of these is the problem, can you help?

Is it possible that the tone does not work well on Nodemu?

Does the 3.3v instead of 5v mean more that I think? I don't really have much experience with electronics, I've done stuff with modules, but not low level components.

I'm clearly overpowering the LED right now at 3.3v with a transistor, maybe it looses the frequency somehow?

Thank you for any help, I'm really out of ideas.

tone(D8,38000);... D8 is not declared as output as I can see.

Thanks, I had it and removed it because of another example, didn't seem to matter. I can check the IR led with a phone camera to see that it is lighting up... And it is :wink:

Thank you,

So it is time to add resistors to the base of your transistor and to limit the current to the LED to what it says in the data sheet to stop it burning out.


I did that, and then removed it because I thought it wasn't powerful enough, then I added a transistor and connected it to the 5v power supply to make it even more powerful, so I'm sure that's not the problem..

What I don't think I have is a good frequency, and would like a way to generate the 38Khz with interrupts or an internal timer, because there is also a web sockets server running on the board...

Thank you,

so I'm sure that's not the problem..

You are asking questions here so by definition you don’t know everything. Without a base transistor you are over stressing the current rating on the pin of the Arduino, that will damage it.
By not having a current limiting resistor in the LED you are over stressing the LED and this will lead to burnout of the LED and a severe reduction in the brightness half life of the LED in the meantime.

So I know this is a problem even if you don’t.

would like a way to generate the 38Khz with interrupts or an internal timer,

How do you think the tone function works if not with a timer interrupt?

It is a waste of time telling you anything else because you won’t believe me will you?

Hi :wink:

Thank you for your reply, like I said, initially I had a transistor and a resistor on the LED, of course, but since it wasn't working I started removing components to simplify as much as possible, with the risk of breaking them. I intend to put them back in, of-course. It's not that I don't believe you, I do.

I also think that tone works with interrupts, but it does not appear to give out a good frequency on Nodemcu, since the same code worked on other boards.

Since the 2 boards where it worked were 5v I thought that the problem was the voltage and decreased and then removed the resistor, but the LED still lights up a lot on the camera.

So the advice would be to add the transistor and resistor and the frequency will be better because now the LED is too heated up and messes the frequency? (in the past I played with the power and even it it was full power and hot it still triggered the sensor).

Or because I'm pulling too much current from the pin I'm screwing with the frequency? The pin still works, so I figured I can test things a bit on it, I wasn't aware it could be damaged and still work...

Thank you very much for your reply, I'm not trying to be difficult, just understand more stuff!

Sorry man, I was just looking for an answer, not for some frustrated asshole to talk down to me :wink:

Thanks for setting me straight, I guess your superiority is crystal clear now, lesson learned!
Have a nice day.

void tone(uint8_t _pin, unsigned int frequency, unsigned long duration) {
pinMode (_pin, OUTPUT );

Use the ESP8266 Tone API: core_esp8266_waveform.h, you can look it up.

Lots of tone thingies by using your favorite search engine and the words "esp8266 tone."