analogWrite with frequency on NodeMCU / ESP8266

Posted on
  • Hey folks,

    I have just received two NodeMCU boards and thanks to all the tutorials and great resources getting started with Espruino was a piece of cake - thank all of you for your efforts!

    I've managed to read IR codes sent out by various remotes using the IRReceiver module and I would like to replay them using the board and an IR LED.
    Unfortunately, however, (as it seems to me) I haven't been able to do so using analogWrite with a 38kHz frequency and digitalPulse. If I understand correctly, this is due to the lack of PWM support in Espruino for ESP8266, am I right? Although I've been using platforms like the Raspberry Pi etc. for several years, I'm quite new to the "low level stuff" and I'd just like to know, whether I got my head around this correctly.

    I guess due to Javascript's event loop nature and the required accuracy a pure JS implementation is not feasible, right?

    Possibly related (?): PWM? Anyone ever get it working? Even via peek/poke directly at control registers?

    Thanks in advance!

  • Yeah, the problem is that on ESP8266 there is no hardware PWM as far as I know. There is a hardware timer, but to make a pin change state you still need to do it using software. It means that realistically, reliable 38kHz soft PWM isn't an option right now.

    Someone could write some IR transmitter code in C, and then call it from JS like is done with the Neopixels, but it's a bit of a faff. Even with that, you can't transmit for too long or you'll interfere with WiFi/trigger the watchdog timer.

    The more sensible option is to use a microcontroller with hardware PWM on it, like the Espruino boards. I'll be releasing one with WiFi on soon now - I have hardware, there are just some software issues to sort out before I do a release.

  • Thanks for the quick reply, although that's a bit of a letdown, your announcement sounds promising ;)
    I'll definitely give it a shot, since I really like the JS programming approach.

  • Hey @Gordon, is there any update yet? AnalogWrite for 38Khz is feasible in ESP8266?


  • It looked like there was a way to call code from IRQ but nobody has put any work into it as far as I know.

    Also - on ESP8266, digitalPulse is a bit of a hack - it's synchronous, whereas the 'proper' one is async. Actually getting that working properly for ESP8266 could end up being a nightmare - as I guess you want to use this for IR.

    You may:

    • be able to use SPI to emulate the bit pattern
    • be able to specify a massive Float32Array of bit times to digitalPulse, which includes all 38kHz pulses.

  • Hi
    reading the above, I see I'm not the only one with such troubles ( .. )
    I just ordered 2 Espruino WiFi to support the Espruino project, meanwhile I still wanna figure out a way to make this work on the ESP8266 platforms ( see my latest post:­303540/ )

  • I have tried the IR receive stuff with the d1 mini (esp8266) and if my math is correct I am getting codes sometimes as long as 50ms. So if I wanted to simply retransmit using the tight loop digitalPulse() esp8266 port sw implementation it would take too long and the watchdog would reset the board before finishing. So your option 2 from above @Gordon doesn't seem feasible. Am I missing something there?

    On the other hand @Gordon's­ues/1175#issuecomment-318593010 comment gives hope in avoiding to go with some sort of ne555-based 38kHz oscillator or something. I wouldn't really think of it if it were just for the IR emission but it sounds like using the timer handling present in custom pwm code would make the esp8266 port way more usable compared to the current state. I personally would be very interested but I guess lack knowledge and instrumentation. Perhaps somebody could give it a shot? @Gordon?

  • Post a reply
    • Bold
    • Italics
    • Link
    • Image
    • List
    • Quote
    • code
    • Preview

analogWrite with frequency on NodeMCU / ESP8266

Posted by Avatar for davdev @davdev