Home »
Embedded Systems
AVR | Rotating DC Motor Clockwise and Anti-Clockwise
In this tutorial, we will learn how to rotate a simple DC motor clockwise and anticlockwise using AVR microcontroller ATmega16?
By Suryaveer Singh Last updated : May 12, 2023
It is also a very simple program, the Simple DC motor can be rotated clockwise when the input is given to the positive terminal and vice versa for Anti-clockwise rotation.
C Program to Rotate DC Motor Clockwise and Anti-Clockwise
#include <avr/io.h>
#define F_CPU 1000000
#include <util/delay.h>
int main(void)
{
DDRA = 0x81;
while(1)
{
PORTA = 0x01;
_delay_ms(1000);
PORTA = 0x00;
_delay_ms(500);
PORTA = 0x80;
_delay_ms(1000);
PORTA = 0x00;
_delay_ms(500);
}
}
Explanation
When you will connect the motor ATmega16 with the DC motor the terminals required would be PA0 and PA7.
PA7 PA6 PA5 PA4 PA4 PA3 PA2 PA1 PA0
1 0 0 0 0 0 0 0 1
The above representation can be represented in Hexadecimal as 0x81, the first four term i.e. 1000 represents 8 in binary and the last four term 0001 will represent 1 in binary. Therefore the input pins are defined as DDRA = 0x81.
Inside the while loop firstly we have given input to the PA0 i.e. (PORTA = 0x01) the motor will rotate in the clockwise direction.
Then for 500 milliseconds we have stopped our motor (PORTA = 0x00) & then again we have given our input to PA7 terminal which would move the Motor in anti-clockwise direction.
Again it is stopped and further the same cycle repeats.
Simulation
Simulation Explanation
-
Devices required:
- ATMEGA16
- Simple DC motor
- Arrange the setup as shown in the figure.
- Double click on the motor and click on edit properties and then change the voltage our motor to 3v or any small value, decreasing the voltage of our Motor will increase its speed of rotation.
- Put the HEX file in ATmega16 by double clicking it and selecting the path.
- Click on the play Button and your Simulation will run as we wanted.