Project 1 - Controlling a DC motor using PWM and monitoring its Running status
Ans:
#include<LiquidCrystal.h> //lcd header files
LiquidCrystal lcd(12, 11, 5, 4, 3, 2); //pins of liquid crystals declared
#define potentiometer A0 //A0 pin declared for potentiometer
#define bt_F A1 //push button A1 declared
#define bt_S A2 //push button A2 declared
#define bt_B A3 //push button A3 declared
#define M1_34EN 10 //Pin 34EN and 10 connected
#define M1_3A 9 //Pin 3A and 9 connected
#define M1_4A 8 //Pin 4A and 8 connected
int read_ADC = 0; //read ADC integer value
int duty_cycle; //duty cycle value declared in int
int duty_cycle_lcd; //duty cycle lcd declared in lcd
int set = 0; //set integer is declared
void setup() {
Serial.begin(9600); //serial begins from 9600
pinMode(potentiometer, INPUT); //potentiometer is given as input
pinMode(bt_F, INPUT_PULLUP); //button F input
pinMode(bt_S, INPUT_PULLUP); //button S input
pinMode(bt_B, INPUT_PULLUP); //button B input
pinMode(M1_34EN, OUTPUT); //pin 34EN is output
pinMode(M1_3A, OUTPUT); //pin 3A is output
pinMode(M1_4A, OUTPUT); //pin 4A is output
lcd.begin(16, 2); //lcd pin 16 and 2
lcd.setCursor(0, 1); //lcd set value 0 and 1
lcd.print("Hi"); //display Hi
delay(2000); //delay time 2000 milli second
lcd.clear(); //clear lcd
}
void loop() {
read_ADC = analogRead(potentiometer); //read analog potentiometer
duty_cycle = map(read_ADC, 0, 1023, 0, 255); //duty cycle map declared
duty_cycle_lcd = map(read_ADC, 0, 1023, 0, 100); //duty cycle lcd map declared
analogWrite(M1_34EN, duty_cycle); //write the duty cycle in pin 34EN
lcd.setCursor(0, 0); //set cursor 0
lcd.print("Duty Cycle:"); //display duty cycle
lcd.print(duty_cycle_lcd); //display duty cycle in lcd
lcd.print("% "); //display
if (digitalRead (bt_F) == 0)(set = 1); //read push button set 1
if (digitalRead (bt_S) == 0)(set = 0); //read push button set 0
if (digitalRead (bt_B) == 0)(set = 2); //read push button set 2
lcd.setCursor(0, 1); //set cursor value 0,1
if (set == 0) {lcd.print(" Stop "); //display stop
digitalWrite(M1_3A, LOW); //write when 3A is low
digitalWrite(M1_4A, LOW); //write when 4A is low
}
if (set == 1) {lcd.print(" Clockwise "); //display clockwise
digitalWrite(M1_3A, HIGH); //write when 3A is high
digitalWrite(M1_4A, LOW); //write when 4A is low
}
if (set == 2) {lcd.print(" Anticlockwise "); //display anticlockwise
digitalWrite(M1_3A, LOW); //write when 3A is low
digitalWrite(M1_4A, HIGH); //write when 4A is high
}
delay(50); //delay time 50 milli sec
}