Challenge

Project 1 - Controlling a DC motor using PWM and monitoring its Running status

Controlling a DC motor using PWM and monitoring its Running status
 
#include
#include "avr/interrupt.h"
#define F_CPU 16000000
#include
#define sw_dir (PIND&(1<<1))
#define sw_spd (PIND&(1<<2))
#define pw (PINC&(1<<3))
#define lcd_port PORTB
#define RS_high PORTD|=(1<<5)
#define RS_low PORTD&=~(1<<5)
#define EN_high PORTD|=(1<<6)
#define EN_low PORTD&=~(1<<6)
unsigned char i;
void lcd_cmd(char cmd)
{
lcd_port=(cmd&0xf0);
RS_low;
EN_low;
_delay_ms(1);
EN_high;
lcd_port=((cmd<<4)&(0xf0));
RS_low;
EN_low;
_delay_ms(1);
EN_high;
}
 
 
void lcd_data(char data){ // sending the data to lcd
 
lcd_port = (data & 0xf0);
RS_high;
EN_low;
_delay_ms(1);
EN_high;
lcd_port = ((data << 4)& (0xf0));
RS_high;
EN_low;
_delay_ms(1);
EN_high;
}
void lcd_init(){ // diff modes for lcd working
lcd_cmd(0x38);
_delay_ms(5);
lcd_cmd(0x02);
_delay_ms(5);
lcd_cmd(0x01);
_delay_ms(5);
lcd_cmd(0x0C);
_delay_ms(5);
lcd_cmd(0x06);
_delay_ms(5);
lcd_cmd(0x80);
_delay_ms(5);
}
void cursorpos(char x,char y){ // x for row y for columns
if((x<1 || x>2) || (y>16 || y<1 )){
x=1;
y=1;
}
if(x==1){
lcd_cmd(0x7F+y);
}
else{
lcd_cmd(0xBF+y);
}
 
}
void lcd_out(char x,char y, char *str){
cursorpos(x,y);
while(*str){
lcd_data(*str);
str++;
}
}
int main(void)
{
DDRB = 0xFF;
DDRA |= ((1<<0)|(1<<1));
DDRD &=~ (1<<1);
DDRD &=~ (1<<2);
DDRD |=(1<<6);
DDRC &=~ (1<<3);
PORTD |= (1<<2);
PORTC |= (1<<3);
 
i=64; //running the motor with 25% duty cycle.
TCCR0=0x61;
GICR=(1<
MCUCR=0X02;
sei(); //Enable global interrupt
lcd_init();
 
while(1){
while(pw==0){ //continue till the push button is released.
if(sw_dir == 0){ //if the direction control push button is released.
PORTA = 0b00000001;
lcd_out(1,1,"motor ON - cl"); //Running clock wise direction
}
else {
PORTA = 0b00000010; // 1 0 to respective 1A 2A pins to rotate in Anti-Clockwise direction.
lcd_out(1,1,"motor ON - acl");
 
}
 
OCR0=i; // increases in ISR, the corresponding value is to be loaded into OCR0.
 
 
}
PORTA = 0X00;
lcd_out(1,1,"motor is turnoff");
 
}
 
 
}
ISR(INT0_vect){ //interrupt service routine
i=i+20;
}
 

PWM stands for Pulse Width Modulation and it is a commonly used technique for controlling the speed of DC motors. In this technique, the power supply to the motor is turned on and off very rapidly, with the length of time that the power is on (known as the "duty cycle") determining the average voltage and thus the speed of the motor.

To control a DC motor using PWM, you will need a microcontroller or other circuit that can generate a PWM signal. This signal is then used to control a motor driver, which can be either an H-bridge or a MOSFET-based driver. The motor driver amplifies the PWM signal and uses it to control the power supply to the motor.

To monitor the running status of a DC motor, you can use various methods depending on the type of motor and its application. One common method is to use a current sensor to measure the current flowing through the motor. This can be used to determine whether the motor is running or not, as well as to detect any problems such as a stalled motor or a motor with too high or too low of a load.

Another method is to use an encoder or a tachometer to measure the speed of the motor. This can be used to detect any changes in the motor speed, which could indicate a problem such as a worn-out motor or a loss of power. Additionally, using an encoder or tachometer can allow for precise speed control of the motor, which can be important in applications such as robotics or industrial automation.

using PWM to control the speed of a DC motor and monitoring its running status can be a useful and effective way to ensure that the motor is operating as intended and to detect any issues before they become serious problems.

Your portfolio, next

Ship work like this
in your own course.

Talk to a career counsellor

Free 20-minute call. We'll map the right course to where you want to work.

By submitting you agree to our Terms and Privacy Policy.