Problem with PWM and IRremote on ESP8266

Hello,

I have encountered a problem in my code for ESP8266 (NodeMCU), it's a code that uses IR transmitter to control a diode at the desired PWM level.

void setup() {
pinMode(D1, OUTPUT); //LED
pinMode(D2, INPUT); // TSOP1738
}

void loop() {
if (IrReceiver.decode()) {
switch (IrReceiver.decodedIRData.decodedRawData) {

case 1935591058:
x +=y;
analogWrite(D1, x);
break;

case 7520486194:
x -=y;
analogWrite(D1, x);
break;

}
IrReceiver.resume();
}
Serial.println(x);
delay(100);
}

The code is actually much longer and this is just a truncated snippet, but independent of this fact, the problem is that in order for the IR signals to be received, a PWM signal must not be generated. (Nowhere must the analogWrite(); function be run).

Once I comment out all analogWrite(); functions, the program accepts all signals and the value of x changes depending on the signal received. If the analogWrite(); function is present, IR signals are received until the analogWrite(); function is run.

I tried connecting the diode to all PWM pins, it behaves the same everywhere. I have also tried TSOP1738 on A0. No change.

On the AVR the same code works without problems. Why don't I use the AVR? Because it doesn't have PWM with 10bit resolution.

I have absolutely no idea how to solve this problem. Do you have a solution?

Second question, if this problem can't be solved with the conclusion that you simply can't receive IR signals and generate PWM at the same time on the ESP8266 board while you can on the AVR, will an I2C expander (e.g. PCA9685) solve this problem ?

Thank you!

Hint: PWM is not a hardware function but is generated by the imbedded OS (Operating System). You can try writing a digital '0' to it, not sure it will help as I have never tired it. For me interrupt functions caused timing changes on the PWM outputs. This with a Wemos R1D1 board.

I also tried replacing all analogWrite(); functions with digitalWrite(); and it helped and the device receives IR signals (because PWM signal is not generated anywhere), but I really need to generate PWM signal, I need to control the brightness of the diode.

You can do this:

  1. Set up timer 2 to operate in PWM mode with the internal clock. And enable CH1 to be the PWM output channel.
  2. Set the ARR value to the maximum 65535 for example, so the frequency should be 1098Hz.
  3. Control the duty cycle by writing to the CCR1 register.
  4. Make Duty Cycle sweep from 0% up to 100% back and forth.

or the easy way.
pinMode(#, OUTPUT);

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