Serial monitor not showing anything

I have combined both code of TCS3200 sensor and mosfet to control pwm signal using analog. Each sensor works when checked individually and serial monitor too but problem arises when combined both code.


#define S0_PIN 25
#define S1_PIN 26
#define S2_PIN 27
#define S3_PIN 13
#define OUT_PIN  14


int motorPin = 32; // for ESP32 microcontroller

void setup() {

  Serial.begin(9600);

  
  pinMode(motorPin, OUTPUT);
  
   pinMode(S0_PIN, OUTPUT);
  pinMode(S1_PIN, OUTPUT);
  pinMode(S2_PIN, OUTPUT);
  pinMode(S3_PIN, OUTPUT);
  //Set OUT_PIN as Input
  pinMode(OUT_PIN, INPUT);
  // Set Pulse Width scaling to 20%
  digitalWrite(S0_PIN, HIGH);
  digitalWrite(S1_PIN, LOW);
  // Enabl UART for Debugging
  
}

void loop() {
  
  
   int r, g, b;
  r = process_red_value();
  //delay(200);
  g = process_green_value();
  //delay(200);
  b = process_blue_value();
  //delay(200);
  Serial.print("r = ");
  Serial.print(r);
  Serial.print(" ");
  Serial.print("g = ");
  Serial.print(g);
  Serial.print(" ");
  Serial.print("b = ");
  Serial.print(b);
  Serial.print(" ");
  Serial.println();



 

    for (int speed = 0; speed<= 1023; speed=35) {
    analogWrite(motorPin, speed);  
    }
}

int process_red_value()
{
  digitalWrite(S2_PIN, LOW);
  digitalWrite(S3_PIN, LOW);
  int pulse_length = pulseIn(OUT_PIN, LOW);
  return pulse_length;
}
int process_green_value()
{
  digitalWrite(S2_PIN, HIGH);
  digitalWrite(S3_PIN, HIGH);
  int pulse_length = pulseIn(OUT_PIN, LOW);
  return pulse_length;
}
int process_blue_value()
{
  digitalWrite(S2_PIN, LOW);
  digitalWrite(S3_PIN, HIGH);
  int pulse_length = pulseIn(OUT_PIN, LOW);
  return pulse_length;
}

Is the serial monitor baudrate set to 9600?

YES! Its 9600 on serial monitor too!

What does the serial monitor display?

Can you try 115200 for the baudrate?

Nothing, its blank! I tried with 115200 too in code and on serial monitor, it shows nothing!

Surprisingly, TCS3200 work alone and show output on serial monitor.

Can you do a schematic of your wiring?

Actually, mosfet is driving a small dc motor! I'm controlling speed using PWM signal through analog pin 32 on ESP32. TCS3200 color sensor uses 5 pins in total and work perfect fine alone.

Does the motor circuit work by itself?

Yes, it does work fine! I can control it using pwm!

Add a couple of serial prints inside setup, just to make sure the code is properly started, like:

void setup() {
  Serial.begin(9600);
  Serial.println("Setup started...");
  pinMode(motorPin, OUTPUT);
  pinMode(S0_PIN, OUTPUT);
  pinMode(S1_PIN, OUTPUT);
  pinMode(S2_PIN, OUTPUT);
  pinMode(S3_PIN, OUTPUT);
  //Set OUT_PIN as Input
  pinMode(OUT_PIN, INPUT);
  // Set Pulse Width scaling to 20%
  digitalWrite(S0_PIN, HIGH);
  digitalWrite(S1_PIN, LOW);
  Serial.println("Setup ends!");
}

Can you see those messages or not? If yes, you can go on. If not, There's a problem with your USB serial connection.

Then, please show us the two single codes you tried to "combine", just to see what you intended to do.

I added serial prints yet it doesn't show anything on serial monitor.

//CODE FOR TCS3200 WITH ESP32

#define S0_PIN 25
#define S1_PIN 26
#define S2_PIN 27
#define S3_PIN 13
#define OUT_PIN  14
void setup()
{
  // Set the S0, S1, S2, S3 Pins as Output
  pinMode(S0_PIN, OUTPUT);
  pinMode(S1_PIN, OUTPUT);
  pinMode(S2_PIN, OUTPUT);
  pinMode(S3_PIN, OUTPUT);
  //Set OUT_PIN as Input
  pinMode(OUT_PIN, INPUT);
  // Set Pulse Width scaling to 20%
  digitalWrite(S0_PIN, HIGH);
  digitalWrite(S1_PIN, LOW);
  // Enabl UART for Debugging
  Serial.begin(9600);
}
void loop()
{
  int r, g, b;
  r = process_red_value();
  delay(200);
  g = process_green_value();
  delay(200);
  b = process_blue_value();
  delay(200);
  Serial.print("r = ");
  Serial.print(r);
  Serial.print(" ");
  Serial.print("g = ");
  Serial.print(g);
  Serial.print(" ");
  Serial.print("b = ");
  Serial.print(b);
  Serial.print(" ");
  Serial.println();
  if (r < 42)
  {
    Serial.println("Colour Pink");
  }
  else if (g < 63)
  {
    Serial.println("Colour Green");
  }
  else if (r < 64)
  {
    Serial.println("Colour Red");
  }
}
int process_red_value()
{
  digitalWrite(S2_PIN, LOW);
  digitalWrite(S3_PIN, LOW);
  int pulse_length = pulseIn(OUT_PIN, LOW);
  return pulse_length;
}
int process_green_value()
{
  digitalWrite(S2_PIN, HIGH);
  digitalWrite(S3_PIN, HIGH);
  int pulse_length = pulseIn(OUT_PIN, LOW);
  return pulse_length;
}
int process_blue_value()
{
  digitalWrite(S2_PIN, LOW);
  digitalWrite(S3_PIN, HIGH);
  int pulse_length = pulseIn(OUT_PIN, LOW);
  return pulse_length;
}
//CODE FOR DC MOTOR CONTROLLED VIA ESP32 PWM

int motorPin = 32; // for ESP32 microcontroller

void setup() {
  pinMode(motorPin, OUTPUT);
}

void loop() {
    for (int speed = 0; speed<= 1023; speed=35) {
    analogWrite(motorPin, speed);
    //delay(100);
    }
}

That's weird, if you can't see anything not even the "Setup started", you have some kind of problem with the serial connection. And I don't have any ESP32 here to physically test your code.

If you are sure it works with the first sketch of you last reply (does it?), it's not a matter of USB/serial, so start adding some serial prints to the second sketch and see if it prints out something:

//CODE FOR DC MOTOR CONTROLLED VIA ESP32 PWM

int motorPin = 32; // for ESP32 microcontroller

void setup() {
  Serial.begin(9600);
  Serial.println("Setup started...");
  pinMode(motorPin, OUTPUT);
  Serial.println("Setup ends!");
}

void loop() {
  Serial.print("Running...");
  for (int speed = 0; speed<= 1023; speed=35) {
    analogWrite(motorPin, speed);
    delay(100);
  }
  Serial.print(" STOP.");
  delay(2000);
}

If it works, we can then go ahead trying to integrate the two codes.
If not, try changing the motorPin GPIO number from 32 to another one (e.g. to 34 or 35) but don't connect the motor at all and see what happens. If this time the code is working, connect the motor to the new GPIO and run again the code and see what happens.

Unfortunately, its not printing anything on serial monitor when uploaded above motor sketch. Serial monitor is working with TCS3200.

And then?... I think I have said what to do next...

Please note this line is wrong!!!

 for (int speed = 0; speed<= 1023; speed=35) {

The for() becomes an endless loop because "speed" will always contain 35!
Change it into this to make 35 as the speed increment:

 for (int speed = 0; speed<= 1023; speed += 35) {

PS: I still doubt the code I posted onPort #12 can't give you any serial data, are you sure you uploaded exactly that code?

1 Like

Yes sir! I upload same code as you posted here!

I found something interesting that when serial monitor is on and when I press BOOT button on esp32 it prints Setup started, Setup ends etc etc

Looks like something somehow related to your specific ESP32 kit version and/or programmed behaviour! In this case, I suspect you just need to press BOOT to start the code you have just uploaded (with the "speed=35" correction also).
I haven't any ESP32 to make tests on, so I suggest you to google around (with our exact ESP32 kit version) to find out why you need to press that button.

It seems that pulseIn() is waiting for one or more transitions to happen so it can complete. Note that this is a blocking function.

True, but that doesn't prevent the output of a Serial.print() put in setup() function as a first statement after Serial.begin()...

Can you try putting a delay after Serial.begin like the.

Serial.begin(9600);
delay(3000);

For some reason some boards need a delay to work.

Also can you try uploading the sketch with the motor and mosfet disconnected.