// Pin Definitions
#define LM35_PIN 34 // LM35 sensor analog input
#define IN1 32 // L298N IN1
#define IN2 33 // L298N IN2
#define ENA_PIN 25 // L298N ENA (PWM motor speed)
// PWM motor
int motorPWM = 128; // Set initial motor PWM value (range 0-255)
int SensorVal;
float temperature; // Temperature value (in Celsius)
void readTeamperatue(void);
void motor_directionspeed(bool in1_val, bool in2_val, int Ena_val);
void setup() {
// Start serial communication
Serial.begin(115200);
// Set motor pins as OUTPUT
pinMode(IN1, OUTPUT);
pinMode(IN2, OUTPUT);
pinMode(ENA_PIN, OUTPUT);
// Set PWM for motor driver
ledcAttachChannel(ENA_PIN, 30000, 8, 0);
ledcWrite(ENA_PIN, motorPWM); // Set initial motor speed (PWM value)
}
void loop() {
//LM35 read temp from sensor value
readTeamperatue();
//motor control
if(temperature > 300){
temperature = 300;
}
// else if(temperature >150){
// motorPWM = 255;
// }else{
// motorPWM = 200;
// }
motorPWM=map(temperature, 0, 300, 0, 225);
motor_directionspeed(1,0,motorPWM);
// wait
delay(500);
}
void readTeamperatue(void){
SensorVal = analogRead(LM35_PIN);
float Vout = (float) SensorVal * (3.3 / 4095.0);
temperature = Vout * 100;
//Print the temperature in Celsius
Serial.print("Temperature: ");
Serial.print(temperature);
Serial.print("\xC2\xB0"); // shows degree symbol
Serial.print("C \n");
}
void motor_directionspeed(bool in1_val, bool in2_val, int Ena_val){
ledcWrite(ENA_PIN, Ena_val); // Set initial motor speed (PWM value)
digitalWrite(IN1, in1_val);
digitalWrite(IN2, in2_val);
if( (in1_val == 0 && in2_val ==0) || Ena_val == 0){
Serial.print("Motor off");
}else if( (in1_val == 1 && in2_val ==1) || Ena_val == 0){
Serial.print("Motor off");
}else if( in1_val == 1 && in2_val ==0 && Ena_val> 0){
Serial.print("Motor on / clockwise direction | ");
int a = (int)(Ena_val*100)/255;
Serial.print("Speed: ");
Serial.print(a);
Serial.print("%\n");
}else if( in1_val == 0 && in2_val ==1 && Ena_val> 0){
Serial.print("Motor on / Anti-clockwise direction | ");
int a = (int)(Ena_val*100)/255;
Serial.print("Speed: ");
Serial.print(a);
Serial.print("%\n");
}else{
Serial.print("Processing");
}
}