GPIO0 (D3) not allow analogWrite - ESP8266 NodeMCU Dev. Kit

Hi Blynkers,

I have a very weird issue when I migrated my sketch to Blynk 2.0.

The code is a 4 channel led lamp, where with the PWM output trought the GPIO I can control the brightness of the leds.

This piece of code was working fine with the legacy Blynk, but with the new Blynk it looks like I can’t set the PWM output to GPIO0, but the other outputs works.

The code is too long, the I’ll show the main parts related to the fail:

//Declarations
#define BLYNK_TEMPLATE_ID "--------------------------"
#define BLYNK_DEVICE_NAME "Lampara SunnyReef"

#define BLYNK_FIRMWARE_VERSION        "0.2.0"

#define BLYNK_PRINT Serial
//#define BLYNK_DEBUG

#define APP_DEBUG

// Uncomment your board, or configure a custom board in Settings.h
//#define USE_SPARKFUN_BLYNK_BOARD
#define USE_NODE_MCU_BOARD
//#define USE_WITTY_CLOUD_BOARD
//#define USE_WEMOS_D1_MINI

#include "BlynkEdgent.h"


#include <TimeLib.h>
#include <SoftwareSerial.h>
#include <SimpleTimer.h>
#include <WidgetRTC.h>

int PWM_Digital_Canal[5] = { 16, 5, 4, 0, 2};

void setup()
{
  Serial.begin(115200); // See the connection status in Serial Monitor
  setSyncInterval(1);
  rtc.begin();

  Serial.println("Iniciando App");
  //Inicializa el controlador y sus variables
  analogWriteFreq(250);
  int i;
  for (i = 0; i < 4; i = i + 1)
  {
    pinMode(PWM_Digital_Canal[i], OUTPUT);
    analogWrite(PWM_Digital_Canal[i], potCanal[i]*proteccion_temp / 100);
  }
  pinMode(Relay, OUTPUT);

 BlynkEdgent.begin();
 
  actualizaLuzTemp();
  timer.setInterval(60000, actualizaLuzTemp);
  timer.setInterval(5000, leeTemperatura);
  setSyncInterval(10 * 60); // Sync interval in seconds (10 minutes)
}

void actualizaLuzTemp()
{
/* a lot of code not related with the fail */
int i;
  for (i = 0; i < 4; i = i + 1 )
  {
    analogWrite(PWM_Digital_Canal[i], potCanal[i]*proteccion_temp / 100); 
    luz_anterior[i] = potCanal[i];
    delay(10);
  }
  Blynk.virtualWrite(Virtual_Total_Azul, (potCanal[0] + potCanal[1] + potCanal[3]+ potCanal[2]*3/5)*proteccion_temp / 100);
  Blynk.virtualWrite(Virtual_Azul_Blanco, (potCanal[2])*2/5*proteccion_temp / 100);

  terminal.println(currentTime);

  Blynk.virtualWrite(GraphTemp_LED, temp_LED);

  terminal.println("LED " + String( temp_LED) + "°C ");

  terminal.println(Modo_txt[Modo]);
  for (i = 0; i < 4; i = i + 1 )
  {
    terminal.print("Canal ");
    terminal.print(String(i + 1));
    terminal.print(": ");
    terminal.print(String(potCanal[i]*proteccion_temp / 100));
    if ( Luz_Luna && ( potCanal[i] <= 10 ) && ( ( i == 0) || ( i == 3))) terminal.print(" Luna");
    terminal.println("");
  }
  terminal.println(F("-----------------"));
  terminal.flush();
}

The line:
analogWrite(PWM_Digital_Canal[i], potCanal[i]*proteccion_temp / 100);
does not work for GPIO0 only.
I’m using the last Arduino IDE, with all the upgraded libraries.

I know the code is doing right because I cann see at the Blynk app the messages in the terminal widget with the rigth values for each GPIO (channel).

Any ideas?

I highly recommend you to read this article

Thank for your advice, I already readed all about the GPIO used at the boot, But this code was working with the legacy Blynk.

For my lamp, the GPIO0 just produce a litlle flash at the boot with the old version of Blynk.

Any other ideas?

Look at the Settings.h tab of the sketch.
You’ll see that GPIO0 is already in use.

Pete.

Thanks Pete. As usual you are a great help.

Can I comment this line at Settings.h

#define BOARD_BUTTON_PIN 0

?

Not really. You can change to a different GPIO, or preferably start using a different GPIO for your PWM.
If you don’t have any spare GPIOs then stop using Edgent.

Pete.

or get a different board, the esp32 has more pins.

Thanks Pete,

Fortunatelly, on this project I have spares GPIO.

It worked now!