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;
}
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.
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.
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.