ESP8266 NodeMCU Backdoor uPWM Hack for IR signals

We received our ESP8266 NodeMCU several months ago it has been difficult to find a working example of Hardware PWM to generate crisp Infrared carrier signals. Most posts we have read suggest that there is no PWM readily available on any of the supported platforms. As we have already shown with Arduinos and the Particle Photon we figured it would be possible to use a spare UART pin on the ESP8266 NodeMCU to achieve our goal.  After some initial success we encountered some watchdog timeouts/resets and it seemed like the uPWM hack would not be possible on the ESP8266 Node MCU platform. For a few weeks we ‘parked’  the effort and today we decided to try again with all of the latest and updated firmware available from the ESP8266 community via the Arduino IDE. This time we were successful and the ESP8266 NodeMCU Backdoor uPWM Hack for IR signals using works!

ESP8266 NodeMCU uPWM Inverted IR Circuit
ESP8266 NodeMCU uPWM Inverted IR Circuit

UPDATE: an improved non-inverted version is now available here.

The uPWM Circuit diagram above for ESP8266 NodeMCU using UART shows the IR out signal on pin D4 . This  signal is inverted and Q1 serves to invert it back to normal. Q2 is the main IR  LED driver and the IR current can be adjusted by varying R4 to suit. Note that the IR LED D1 is connected to VUSB(or +5V) and R2 is connected to the 3V3 pin on the ESP8266 NodeMCU device.

The Hack
Quite simple really – just set the baud rate to 10 times the desired Infrared carrier frequency and send a ‘magic’ 8 bit character to achieve the desired duty cycle. Of course we need to take the 1-start bit and 1-stop bit into account plus the 8 bits in each character. Remember that the UART sends the data inverted, so this needs to be taken into account with the characters sent and also in the IR LED driver circuit above, which required 2 transistors instead of the usual one. In summary, sending the following characters will generate the associated duty cycle, as follows:

  • 0xF0 – 50%
  • 0xF8 – 40%
  • 0xFC – 30%
  • 0xFE – 20%
  • 0xFF – 10%

This is more than sufficient for most, if not all IR systems, which would typically use 50% or more commonly 33%. We defaulted the value in the sketch to 40% without problems. It should of course be possible to generate a duty cycle up to 90% by extending the code, which may be of interest for PWM in non-infrared applications.

ESP8266 NodeMCU Backdoor uPWM Hack for IR signals using UART

The Code
I will leave it up to the reader to review the code and figure the rest out. Suffice to say we took the softPWM code in part 3 of the series , our series on this uPWM hack and updated the PWM initialisation, mark & space routines. Using this approach it should be relatively simple to fully port libraries like IRremote & IRLib to ESP8266 NodeMCU and get some awesome IR projects going at last! Similarly, it will now be possible to incorporate this hack into your own IR projects to generate crisp Infrared signals at the target carrier frequency & duty cycle.  This enhanced accuracy will improve performance and range vs other approaches. If you need any advice contact us via the website above or post on our IRforum.

One of the real benefits of this approach is that there is no interference with any of the Photon’s “WiFi foo” or internals. That is provided the watchdog timer is serviced regularly. We have placed lots of calls to service the watchdog timer throughout the example code. (Note our example code does not use WiFi directly)

ESP8266 NodeMCU uPWM AnalysIR Screenshot
ESP8266 NodeMCU uPWM AnalysIR Screenshot

To test the uPWM, we wired up the ESP8266 NodeMCU to the circuit at the top of this post and used our soon to be released LearnIR connected to AnalysIR to record the signals sent from the firmware provided. As you can see from the screenshot of AnalysIR above all test signals were received, decoded and at the correct carrier frequencies of 30, 33, 36,38,40 & 56 kHz.

ESP8266 NodeMCU uPWM NEC IR Signal
ESP8266 NodeMCU uPWM NEC IR Signal

The image above shows an NEC IR signal as transmitted using uPWM from the ESP8266 NodeMCU and the firmware provided in this post. You will notice here that the output from pin D4 is inverted as mentioned above.

ESP8266 NodeMCU uPWM IR Signal 40kHz 40% Duty Cycle
ESP8266 NodeMCU uPWM IR Signal 40kHz 40% Duty Cycle

The image above shows an IR signal as transmitted at a carrier frequency of 40kHz and a duty cycle of 40% using uPWM from the ESP8266 NodeMCU and the firmware provided in this post. The shorter NEC repeat signal is to the right of the main signal. You will notice here that the output from pin D4 is inverted as mentioned above. Hence it ‘may’ appear as a duty cycle of 60% at first glance, but it is in fact an inverted 40% duty cycle.

The Circuit
The circuit diagram above takes the ‘inverted‘ IR signal output on the UART’s TX pin on the ESP8266 NodeMCU and inverts it back to normal with the first transistor stage, Q1. The second transistor stage, Q2,  drives the IR signal at 170 mA using a 10R resistor. You should be able to get circa 300mA by replacing R4 with a 5R resistor and so on. There is also a superior constant current circuit available on our blog, but you will have to design in the inverting stage, or take it from the circuit above.

ESP8266 NodeMCU uPWM IR current
ESP8266 NodeMCU uPWM IR current

The image above, captured from the oscilloscope shows the voltage over R4 (10R) and represents a voltage drop of 1.7 volts peak. Thus the current passing through the resistor and hence the IR LED D1 is 170mA. IMPORTANT: When pushing the current higher, you may have to use a better power supply if the device resets or if your USB connection cannot provide the peak current required. Another way of increasing/doubling the IR power, almost for free, is to put a second IR LED in series with D1. Similarly with 2 IR LEDs and R4 at 5R, you can get almost 4x the IR Power of the circuit shown above, subject t any power supply constraints.

If you find an interesting  use for this firmware or embed it into a library then do let us know via the contact form linked above or our IRforum.

UPDATE: an improved non-inverted version is now available here.

You can download a copy of the Firmware for the ESP8266 NodeMCU uPWM here.

Get your own copy of AnalysIR here

Leave a Reply