4.6) DC Motor Interfacing with AVR (Atmega16)

posted by Hamid Sayyed • November 13, 2025 0 Comments

DC motors are electromechanical devices that convert direct current electrical energy into mechanical rotational motion. They are one of the most common actuators used in embedded and automation systems. In a DC motor, the direction of rotation depends on the polarity of the applied voltage. When current flows in one direction, the motor rotates clockwise, and reversing the current makes it rotate anticlockwise. The ATmega16 microcontroller can easily control a DC motor using its output ports, usually through a driver circuit like L293D or transistor switches. Understanding how to interface and control a DC motor helps students build projects such as robotic vehicles, conveyor belts, and automation systems. In this tutorial, we will study how a DC motor works, learn its interfacing method with ATmega16, and write a C program to rotate the motor in both directions. We will also visualize the wiring through an animated circuit diagram for better understanding.

Understanding DC Motor Structure

A DC motor mainly consists of two parts: the stator and the rotor (armature). The stator produces a fixed magnetic field, while the rotor carries windings that rotate under the influence of this field when current flows through them. The commutator and brushes ensure that the current direction in the rotor windings changes appropriately, maintaining continuous rotation. The speed of a DC motor is proportional to the applied voltage, and the torque depends on the current. In embedded systems, the microcontroller does not directly drive the motor, as it cannot supply the required current. Instead, a driver circuit like L293D or ULN2003 acts as an interface between the microcontroller and the motor.

Interfacing DC Motor with ATmega16

To control the direction of the motor, two control lines are used: one for clockwise rotation and another for anticlockwise rotation. By setting or clearing these control pins through program logic, we can control the rotation easily. For instance, when one control pin is HIGH and the other is LOW, the motor rotates in one direction. Reversing the logic causes the motor to rotate in the opposite direction. A delay can be introduced between direction changes to observe the switching effect clearly.

Note: Always use a driver IC like L293D between ATmega16 and the DC motor. The motor should not be directly connected to the I/O pins as it may draw higher current than the microcontroller can handle.

C Program for DC Motor Interfacing with ATmega16

The following code demonstrates how to control the direction of a DC motor connected to Port D of ATmega16. The motor rotates clockwise for one second, then anticlockwise for one second continuously.

#include <avr/io.h>
#include <util/delay.h>

int main(void)
{
    DDRD = 0x03;           // Set PD0 and PD1 as output pins
    while(1)
    {
        PORTD = 0x01;      // Rotate motor clockwise
        _delay_ms(1000);

        PORTD = 0x02;      // Rotate motor anticlockwise
        _delay_ms(1000);
    }
}
  

Animated Diagram of DC Motor Interfacing

The below animation shows the wiring connection between ATmega16 and a DC motor through a driver circuit. The motor alternates its rotation direction according to the program, representing forward and reverse control.

Explanation of Code

The DDRD register configures pins PD0 and PD1 as outputs for motor control. The motor driver’s input pins are connected to these microcontroller pins. Writing binary 01 to Port D rotates the motor in one direction, while binary 10 reverses it. Delays between changes allow visible alternation between forward and reverse motion.

Applications

  • Robotic vehicle motion control.
  • Industrial automation systems.
  • Conveyor and mechanical drive systems.
  • Smart home devices requiring mechanical actuation.

Conclusion

DC motor interfacing with ATmega16 is a fundamental step toward controlling mechanical motion using microcontrollers. It combines the concepts of I/O control, current amplification using drivers, and timing through delay functions. Once mastered, this knowledge can be extended to speed control, PWM-based direction control, and multi-motor robotic designs.

Comments

Post a Comment

Subscribe to Post Comments [Atom]