Project 1 - Controlling a DC motor using PWM and monitoring its Running status
// This code will control the speed and direction of a DC motor using the L293 motor driver.
// The speed of the motor can be controlled by changing the value of the `pwm` variable.
The direction of the motor can be controlled by changing the value of the `direction` variable.
Define the pins that the motor is connected to.
const int motorPin1 = 10;
const int motorPin2 = 11;
// Define the pin that the LCD display is connected to.
const int lcdPin = 12;
// Initialize the motor driver.
L293 motorDriver(motorPin1, motorPin2);
// Initialize the LCD display.
LiquidCrystal lcd(lcdPin, 2, 1, 0, 4, 5, 6, 7);
void setup() {
// Set the motor driver to PWM mode.
motorDriver.setMode(PWM);
// Initialize the LCD display.
lcd.begin(16, 2);
}
void loop() {
// Read the value of the `pwm` variable.
int pwm = analogRead(A0);
// Set the motor speed to the value of the `pwm` variable.
motorDriver.setSpeed(pwm);
// Read the value of the `direction` variable.
int direction = digitalRead(A1);
// Set the motor direction to the value of the `direction` variable.
motorDriver.setDirection(direction);
// Print the current running status of the motor to the LCD display.
lcd.print("Motor is running");
if (direction == LOW) {
lcd.print(" in the forward direction");
} else {
lc
d.print(" in the reverse direction");
}
}