ESP32 serial monitor not working properly

198 views Asked by At

I want to connect an ESP32 module to a TMC2209 driver. I'm new in this, so I want to try easy stuff first. I'm using the TMC2209 library https://github.com/janelia-arduino/TMC2209/tree/main

I'm using one of their examples to test the baud rates, but I can't see a thing in the serial monitor in VScode with PlatformIO.

This is the PlatformIO configuration and code that I'm using:

[env:nodemcu-32s]
platform = espressif32
board = nodemcu-32s
framework = arduino
lib_deps = janelia-arduino/TMC2209@^9.0.6
monitor_speed = 115200

--

#include <Arduino.h>
#include <TMC2209.h>

// This example will not work on Arduino boards without HardwareSerial ports,
// such as the Uno, Nano, and Mini.
//
// See this reference for more details:
// https://www.arduino.cc/reference/en/language/functions/communication/serial/

HardwareSerial & serial_stream = Serial1;

const long SERIAL_BAUD_RATE = 115200;
const long SERIAL1_BAUD_RATE_COUNT = 10;
const long SERIAL1_BAUD_RATES[SERIAL1_BAUD_RATE_COUNT] =
{
  500000,
  250000,
  115200,
  57600,
  38400,
  31250,
  28800,
  19200,
  14400,
  9600
};
const uint8_t SUCCESSIVE_OPERATION_COUNT = 3;
const int DELAY = 2000;

// Instantiate TMC2209
TMC2209 stepper_driver;
uint8_t serial1_baud_rate_index = 0;

const int RX_PIN = 3;
const int TX_PIN = 1;


void setup()
{
  Serial.begin(SERIAL_BAUD_RATE);
}

void loop()
{
  long serial1_baud_rate = SERIAL1_BAUD_RATES[serial1_baud_rate_index++];
  //stepper_driver.setup(serial_stream, SERIAL_BAUD_RATE, TMC2209::SERIAL_ADDRESS_0, RX_PIN, TX_PIN);
  //stepper_driver.setup(serial_stream,serial1_baud_rate);
  stepper_driver.setup(serial_stream, serial1_baud_rate, TMC2209::SERIAL_ADDRESS_1, RX_PIN, TX_PIN);
  if (serial1_baud_rate_index == SERIAL1_BAUD_RATE_COUNT)
  {
    serial1_baud_rate_index = 0;
  }

  bool test_further = false;

  Serial.println("*************************");
  Serial.print("serial1_baud_rate = ");
  Serial.println(serial1_baud_rate);

  if (stepper_driver.isSetupAndCommunicating())
  {
    Serial.println("Stepper driver setup and communicating!");
    test_further = true;
  }
  else
  {
    Serial.println("Stepper driver not setup and communicating!");
  }

  if (test_further)
  {
    uint32_t microstep_sum = 0;
    for (uint8_t i=0; i<SUCCESSIVE_OPERATION_COUNT; ++i)
    {
      microstep_sum += stepper_driver.getMicrostepsPerStep();
    }
    if (microstep_sum > 0)
    {
      Serial.println("Successive read test passed!");
    }
    else
    {
      Serial.println("Successive read test failed!");
    }
    uint8_t itc_begin = stepper_driver.getInterfaceTransmissionCounter();
    for (uint8_t i=0; i<SUCCESSIVE_OPERATION_COUNT; ++i)
    {
      stepper_driver.disable();
    }
    uint8_t itc_end = stepper_driver.getInterfaceTransmissionCounter();
    if (itc_begin != itc_end)
    {
      Serial.println("Successive write test passed!");
    }
    else
    {
      Serial.println("Successive write test failed!");
    }
  }

  Serial.println("*************************");
  Serial.println();
  delay(DELAY);
}

and this is what I see in the Serial Monitor enter image description here

1

There are 1 answers

4
romkey On

Your code is using 1 and 3 as the TX and RX pins for Serial1. These pins are already used as TX and RX for Serial and are wired to the board's USB/UART chip. You're seeing garbage because you're sending garbage. Choose a different pair of pins that aren't already in use and change your wiring and software to use that pair of pins.

An easy way to debug this is to write a simple program that does nothing but output Hello World to Serial - eliminate everything else from your program and disconnect anything you have from pins 1 and 3. That will work correctly. When you see that work correctly it will be clear that the fault lies in the rest of your program.

Edit:

You can configure a second hardware serial port like this:

Serial1.begin(SERIAL1_BAUD_RATE_COUNT, SERIAL_8N1, RX_PIN, TX_PIN);

You would also need to change the #define lines to use the correct new pin numbers for RX_PIN and TX_PIN.

The rest of your code is already set up to use Serial1.