Challenge

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

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

Ans:

Code:

#include //provides access to I/O (Input/Output) port and pin definitions for the AVR microcontroller.
#include //provides access to delay functions for generating time delays in your code.

#define LCD_DATA_PORT PORTD //to write data to the LCD data lines
#define LCD_CONTROL_PORT PORTB //for controlling various aspects of the LCD module, such as the RS (Register Select), RW (Read/Write), and EN (Enable) control lines
//to control the behavior of the LCD module
#define LCD_RS PB0
#define LCD_RW PB1
#define LCD_EN PB2

#define L293_ENABLE PB3 //represents the enable pin of the L293 motor driver.
#define L293_INPUT1 PB4 //represents one of the input pins for controlling the direction of the motor connected to the L293 driver
#define L293_INPUT2 PB5 //represents another input pin for controlling the direction of the motor connected to the L293 driver

void LCD_init(); //initialize the LCD module.
void LCD_command(uint8_t command); //sends a command to the LCD module.
void LCD_writeChar(char character); //to write a single character to the LCD
void LCD_writeString(const char *string); //to write a string of characters to the LCD.
void LCD_setCursor(uint8_t row, uint8_t col); //sets the cursor position on the LCD display based on the row and column provided.

void initMotor(); //initializes the motor control circuit.
void controlMotor(int speed); //control the motor's speed and direction.

intmain(void)
{
// Initialize LCD and motor
LCD_init();
initMotor();
LCD_setCursor(0, 0);

while(1)
{
// Update LCD display
LCD_setCursor(1, 0);
}
return0;
}

voidLCD_init()
{
 
DDRD |= (1 << PD0>

// Set control pins (RS, RW, EN) as outputs
LCD_CONTROL_PORT |= (1 << LCD>

_delay_ms(15);
LCD_command(0x38); // Initialize 2 lines, 5x8 matrix
LCD_command(0x0E); // Display on, cursor blinking

LCD_command(0x06); // Entry mode: increment cursor, no shift
LCD_command(0x01); // Clear display
_delay_ms(2);
}


voidLCD_command(uint8_tcommand)
{
LCD_DATA_PORT = command;
LCD_CONTROL_PORT &= ~(1 << LCD>
LCD_CONTROL_PORT &= ~(1 << LCD>
LCD_CONTROL_PORT |= (1 << LCD>
_delay_us(1);
LCD_CONTROL_PORT &= ~(1 << LCD>
_delay_us(100);
}

voidLCD_writeChar(charcharacter)
{
LCD_DATA_PORT = character;
LCD_CONTROL_PORT |= (1 << LCD>
LCD_CONTROL_PORT &= ~(1 << LCD>
LCD_CONTROL_PORT |= (1 << LCD>
_delay_us(1);
LCD_CONTROL_PORT &= ~(1 << LCD>
_delay_us(100);
}

voidLCD_writeString(constchar*string)
{
while(*string)
{
LCD_writeChar(*string);
string++;
}
}

voidLCD_setCursor(uint8_trow, uint8_tcol)
{
uint8_t address = 0x80; // Base address for first row
if(row == 1)
{
address |= 0x40; // Second row offset
}
address |= col;
LCD_command(address | 0x80); // Set DDRAM address
}
voidinitMotor()
{
// Set L293 control pins as outputs
DDRB |= (1 << L293>
// Set L293 enable pin as output
DDRB |= (1 << L293>
// Set PWM pins as output
// Initialize PWM for motor speed control
TCCR1A |= (1 << COM1B1>
TCCR1B |= (1 << WGM12>
DDRB |= (1 << PB4>
controlMotor(0); // Stop the motor initially
}
voidcontrolMotor(intspeed)
{
//if (speed > 500)
{
// Set L293 direction
PORTB |= (1 << L293>
PORTB &= ~(1 << L293>
// Enable the motor using L293
PORTB |= (1 << L293>
speed = -speed;
LCD_writeString("Running forward ");
_delay_ms(10000);
LCD_command(0x01); // Clear display
}
 
//else if (speed < 500>
{
// Set L293 direction
PORTB &= ~(1 << L293>
PORTB |= (1 << L293>
// Enable the motor using L293
PORTB |= (1 << L293>
// Convert speed to positive for L293 control
speed = -speed;
LCD_writeString("Running backward");
_delay_ms(10000);
LCD_command(0x01); // Clear display
}
 
{
// Disable the motor using L293
PORTB &= ~(1 << L293>
}
 
OCR1B = speed;
}
 

Overview of the Project: 

1. Initialization:
- Initializes the LCD module and motor control circuit.
- Sets up the necessary pins for data and control lines of the LCD.
- Defines pins for controlling the L293 motor driver.

2. LCD Display:
- Uses LCD functions to write characters, strings, and control the cursor position.
- Demonstrates scenarios by updating the LCD display messages

3. Motor Control:
- Initializes the L293 motor driver's control pins.
- Utilizes PWM for motor speed control.
- Sets direction and speed based on user input.

4. Main Loop:
- Runs an infinite loop to showcase different motor scenarios.
- Updates the LCD display to reflect the motor's status.

Goals:

1. Initialize and control an LCD display to show motor status and messages.
2. Configure motor control pins and use PWM to control motor speed using the L293 motor driver.
3. Illustrate various motor scenarios (forward, backward, stopped) on the LCD display.
4. Integrate potentiometer input to adjust motor speed dynamically.
5. Demonstrate motor direction and speed control using the AVR ATmega328 microcontroller.

Key Highlights of the Project:

1. LCD Initialization: The code initializes the LCD module to display messages using custom functions like `LCD_init`, `LCD_command`, `LCD_writeChar`, `LCD_writeString`, and `LCD_setCursor`.

2. Motor Control: The L293 motor driver is used to control the motor's speed and direction. The `initMotor` function sets up the necessary pins, and the `controlMotor` function adjusts the motor's behavior based on speed input.

3. ADC Input: The code reads analog input from a potentiometer using the ADC module to control the motor's speed. The potentiometer value is converted to a motor speed value.

4. LCD Display: The LCD displays status messages indicating whether the motor is running forward, backward, or stopped. The display updates dynamically based on motor behavior.

5. PWM for Motor Speed: The code employs PWM (Pulse Width Modulation) to control the motor's speed using Timer1's output compare register OC1B.

6. Scenario Demonstration: The code demonstrates various scenarios, such as running the motor forward, running it backward, and stopping it, accompanied by relevant LCD display messages.

7. Clearing LCD: The `LCD_command(0x01);` line is used to clear the LCD screen after displaying messages, providing a clean display for the next scenario.

8. Pin Configurations: The code defines clear pin assignments for the LCD control lines, L293 motor driver control pins, and ADC input, enhancing readability and configurability.

9. Infinite Loop: The program runs in an infinite loop, continuously demonstrating motor scenarios and updating the LCD display.

10. Code Readability: The code utilizes meaningful function names, clear comments, and organized sections, making it easy to understand and modify.

11. Effective Use of Delay: Delays are used appropriately with `_delay_ms` and `_delay_us` to control timing within the program.

12. Efficient Memory Usage: The code efficiently uses memory by declaring necessary variables and functions while avoiding unnecessary overhead.

Deliverables of this project:

1. Motor Control and Direction: The code provides motor speed control and direction change using the L293 motor driver. It demonstrates the ability to run the motor both forward and backward.

2. LCD Display: The code utilizes an LCD display to show the motor's status and actions. It displays messages like "Running forward" and "Running backward" to indicate the motor's direction.

3. Initialization: The code initializes the LCD, motor control circuit, and PWM for motor speed control, creating a functional setup.

4. Loop and Scenarios: The main loop continuously updates the LCD with different motor scenarios, showcasing how the motor reacts to speed changes and direction reversals.

5. User Interaction: Users can observe the motor's behavior and direction changes on the LCD display in response to the potentiometer input.

Expected Outcomes: 

  1. The LCD will be initialized and the display cleared.
  2. The motor will run forward, and the LCD will display "Running forward" for 10 seconds.
  3. The motor will run backward, and the LCD will display "Running backward" for 10 seconds.
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.