How to control DC motor speed using PWM on Atmega32

Summary of How to control DC motor speed using PWM on Atmega32


This article explains generating Phase Correct PWM signals using the AVR Atmega32 microcontroller to control a DC motor's speed. It details the operation of Timer/Counter 0, where the counter oscillates between 0 and 0xFF, triggering output changes at specific compare values stored in the OCR0 register. The process involves configuring the TCCR0 register to enable inverted PWM mode and setting a prescaler value of 1024 for clock division.

Parts used in the AVR Atmega32 PWM Motor Control Project:

  • AVR Atmega32 microcontroller
  • Timer/Counter 0 module
  • DC motor
  • Output Compare Register (OCR0)
  • Timer Counter Register (TCNT0)
  • TCCR0 Register
  • System Clock

Using PWM (Pulse Width Modulation) to control a device is a common practice in embedded systems; for example, you can use it to control the light intensity of a LED or control the speed of a DC motor.
How to control DC motor speed using PWM on Atmega32
In this article, we will explain how to get a PWM from the AVR Atmega32 and we shalll apply the output PWM to a small DC motor to vary its speed.
In order to get the PWM from AVR, we need to use the timer/counter module of the AVR. This module can be used in several modes to generate different PWM signals of different characteristics; here we shall explain how to use the counter in the “Phase Correct PWM” mode. Atmega32 has 3 timer/counters and we are using timer/counter 0.
In “Phase Correct PWM” mode, the counter counts repeatedly from 0 to its maximum value (0xFF) and then back from the maximum to zero. The Output pin (OC0) is cleared when the counter reaches a certain value called the “Compare value” while up counting, and is set when the counter reaches the same value while down counting. This compare value is set by the software in a register called OCR0 (Output Compare Register), while the value of the counter itself is contained in a register called TCNT0.
When the value of TCNT0 matches the OCR0, it’s called a Compare Match.
You can also invert the output PWM by changing the values of bits (COM00 and COM01) in the TCCR register. In that case, the Output pin (OC0) is set when compare match occurs while up counting, and is cleared when compare match occurs while down counting, and the waveform will be as shown in the timing diagram below.
Let’s get first to configuring the PWM, this is done in 2 simple steps:

  1. Configure the TCCR0 register.
  2. Set the compare value in the OCR
  1. Configuring the TCCR0 Register

  1. Set bits WGM00 and WGM01 to 1 and 0 respectively. This enables the phase correct PWM mode.
  2. Set bits COM00 and COM01 to 0 and 1 respectively. This means that the generated PWM will be an inverted PWM.
  3. Set bits CS00 and CS01 and CS02 to 1, 0 and 1 respectively. This means that the counter will be clocked from the system clock divided by 1024.

How to control DC motor speed using PWM on Atmega32 Schematic
Read More: How to control DC motor speed using PWM on Atmega32

Quick Solutions to Questions related to AVR Atmega32 PWM Motor Control:

  • How can you generate a PWM signal from the AVR Atmega32?
    You must use the timer/counter module of the AVR in the Phase Correct PWM mode.
  • What happens when the TCNT0 value matches the OCR0 value?
    This event is called a Compare Match.
  • How do you configure the TCCR0 register for Phase Correct PWM?
    You set bits WGM00 and WGM01 to 1 and 0 respectively to enable the mode.
  • How can you invert the output PWM waveform?
    You change the values of bits COM00 and COM01 in the TCCR register.
  • Which timer/counter is used in this specific example?
    The project uses timer/counter 0.
  • What clock source configuration is used for the counter?
    The counter is clocked from the system clock divided by 1024.

About The Author

Ibrar Ayyub

I am an experienced technical writer holding a Master's degree in computer science from BZU Multan, Pakistan University. With a background spanning various industries, particularly in home automation and engineering, I have honed my skills in crafting clear and concise content. Proficient in leveraging infographics and diagrams, I strive to simplify complex concepts for readers. My strength lies in thorough research and presenting information in a structured and logical format.

Follow Us:
LinkedinTwitter
Scroll to Top