Project 1 - Controlling a DC motor using PWM and monitoring its Running status
In this project, I have implemented speed and direction control of a DC motor using a PWM signal. The project takes speed and direction inputs from the user and drives the motor accordingly. An oscilloscope is employed to monitor the PWM signals.

//CODE
#include
#include
int speed = 0;
String userInput;
LiquidCrystal_I2C lcd(32, 16, 2);
void setup(){
lcd.init();
lcd.backlight();
pinMode(3, OUTPUT);
pinMode(4, OUTPUT);
pinMode(9, OUTPUT);
Serial.begin(9600); // Initialize serial communication at 9600 baud
lcd.begin(16, 2); // Initialize the LCD with 16 columns and 2 rows
lcd.setCursor(0, 0);
}
void loop(){
Serial.println("Enter speed (0-255): ");
while (!Serial.available()) {
// Wait for user input
}
speed = Serial.parseInt();
speed = constrain(speed, 0, 255);
Serial.println("Enter F for forward and R for reverse ");
while (!Serial.available()) {
// Wait for user input
}
userInput = Serial.readString(); // Read the user input as a string
userInput.trim(); // Remove leading and trailing whitespaces
Serial.print("You entered: ");
Serial.println(userInput);
if(userInput == "F" || userInput == "f") {
delay(100);
digitalWrite(3, LOW);
digitalWrite(4, HIGH);
lcd.setCursor(0, 1);
lcd.print("Dir = Forward");
}
else if (userInput == "R" || userInput == "r") {
delay(100);
digitalWrite(3, HIGH);
digitalWrite(4, LOW);
lcd.setCursor(0, 1);
lcd.print("Dir = Reverse");
}
else {
Serial.println("Only enter F or R");
}
analogWrite(9, speed);
lcd.setCursor(0, 0);
lcd.print(" ");
lcd.setCursor(0, 0);
String displayText = "Speed = " + String(speed) + " RPM";
lcd.print(displayText);
delay(300);
}

THE LINK FOR THINKER CAD :
https://www.tinkercad.com/things/jXuE20PLmN4-pmw-project-1/editel?sharecode=QJsoX9_-uYj1Ulby4Wv-55JHJqnQFPvBBdxhiUeqWpI