Search code examples
arduinoesp8266nodemcu

Weird behavior when using monitor and analogWrite (ESP32)


I'm currently working on a project where I want to vary the PWM to a motor given an input from a potentiometer. However, I cant seem to use the serial monitor and an analogWrite statement in the same program.

When I run this code I get this output from the serial monitor:

void setup()
{
  Serial.begin(115200);

  Serial.println();
  Serial.println();
  Serial.print("WORKING");

  //analogWrite(1, 255);
}

void loop()
{
  
}
12:09:48.314 -> 
12:09:48.314 -> WORKING

However, when I run this code (the only difference is that the analogWrite() line is no longer commented out), the serial monitor outputs garbage.

void setup()
{
  Serial.begin(115200);

  Serial.println();
  Serial.println();
  Serial.print("WORKING");

  analogWrite(1, 255);
}

void loop()
{
  
}
{l$ܞ⸮⸮$⸮|⸮⸮l⸮#|⸮⸮⸮⸮{⸮#⸮"⸮⸮on⸮lon⸮⸮⸮#p⸮⸮bl`{lp⸮o⸮⸮l⸮⸮bo⸮|⸮⸮⸮#⸮⸮nN⸮$⸮⸮$`⸮on⸮{lor⸮⸮⸮⸮{r⸮ p⸮o⸮s⸮ܜ⸮⸮⸮#o⸮|⸮B⸮⸮on⸮⸮l ⸮No⸮{lor⸮⸮⸮Nrd`{⸮⸮N{d`⸮⸮⸮⸮⸮⸮$`⸮⸮N⸮l
⸮

Does anyone know why this is happening and how to fix it?

Not sure if it helps but im using an ESP8266 NodeMCU purchased from this amazing link: https://www.amazon.com/gp/product/B081CSJV2V/ref=ppx_yo_dt_b_asin_title_o07_s00?ie=UTF8&th=1


Solution

  • analogWrite(1, 255) is writting to GPIO1. If you look at the pin-out for the ESP8266 NodeMCU, GPIO1 is the TXD line for serial port 0 (it's labeled TXD0). I don't have a NodeMCU schematic handy, but port 0 is typically used for the serial monitor and wired to the USB-UART converter for that purpose. So by writing to GPIO1, you are interfering with the monitor output.

    Try doing an analogWrite to a different GPIO, for example, GPIO5.