Engineers Garage

  • Projects and Tutorials
    • Electronic Projects
      • 8051
      • Arduino
      • ARM
      • AVR
      • PIC
      • Raspberry pi
      • STM32
    • Tutorials
    • Circuit Design
    • Project Videos
    • Components
  • Articles
    • Tech Articles
    • Insight
    • Invention Stories
    • How to
    • What Is
  • News
    • Electronic Products News
    • DIY Reviews
    • Guest Post
  • Forums
    • EDABoard.com
    • Electro-Tech-Online
    • EG Forum Archive
  • Digi-Key Store
    • Cables, Wires
    • Connectors, Interconnect
    • Discrete
    • Electromechanical
    • Embedded Computers
    • Enclosures, Hardware, Office
    • Integrated Circuits (ICs)
    • Isolators
    • LED/Optoelectronics
    • Passive
    • Power, Circuit Protection
    • Programmers
    • RF, Wireless
    • Semiconductors
    • Sensors, Transducers
    • Test Products
    • Tools
  • EE Resources
    • DesignFast
    • LEAP Awards
    • Oscilloscope Product Finder
    • White Papers
    • Webinars
  • EE Learning Center
    • Design Guides
      • WiFi & the IOT Design Guide
      • Microcontrollers Design Guide
      • State of the Art Inductors Design Guide
  • Women in Engineering

Variable Frequency PWM(Pulse Width Modulation) signal generation using Timers of Arduino(Atmega328p Microcontroller)/Arduino Fast PWM

By EG Projects

In this post i am going to explain how to generate PWM of variable frequency using Atmega328 microcontroller? OR how to generate a variable frequency PWM signal using Arduino Uno? Arduino uno board contains an atmel Atmega328 microcontroller on it. I am going to generate a PWM of 50% duty cycle on frequencies between 10Hz to 100Hz. Frequency is changed using a potetiometer/variable resistor connected to an analog pin of the Arduino. Changing frequency value is displayed on 16×2 character lcd.

Atmega/Arduino Timers

First of all you need to understand about the timers of Atmega328 microcontroller and the registers associated with them. Atmega328 has 3 timers. Timer-0, Timer-1 and Timer-2. To generate a variable frequency we have to configure the registers associated with these timers.

Arduino Pins associated with its timers

We can generate variable frequency or output variable frequency pwm on Atmega328 microcontroller pins  (OC0A, OC0B, Oc1A, OC1B, OC2A, Oc2B). All these pins are associated with timers of Atmega328p. OCnX represents “Output Compare” match. When ever timer/counter value is matched with the predefined value in a register OCR1A or ICR1 the OCnx pins gets control and now you can do different things. With this “Output compare match” principle we can accomplish different tasks. Output a variable pwm (pulse width modulation) signal and so on. I am going to output a variable frequency pwm signal using this principle. Timers registers with associated timers and arduino pin numbers is shown below..
Arduino(Atmega328p) pins on variable frequency signal can be outputted

Arduino(Atmega328p) pins on variable frequency signal can be outputted

Arduino variable frequency signal output – Diy project

I am going to output a variable frequency pwm signal on Pin-9 of arduino board. The register associated with pin#9 is OC1A. OC1A means output compare using Timer-1. To output a variable frequency Pwm we have to set the timer in “Phase and Frequency correct PWM mode”. In this mode the output frequency formula is given in datasheet as below.
Arduino variable frequency generation formula

Arduino variable frequency generation formula

Where 

  • Fclk_IO=Input frequency to microcontroller(In my case Arduino Uno 16MHz)
  • N(Prescalar Value)=In my case 8
  • Top(ICR1)= Value in ICR1 Register
So to Output a variable frequency we have to calculate the value for ICR1 register. For example to generate a 100Hz frequency using 16MHz timer and prescaler of 8 the value for ICR1 comes out to be.
Arduino(Atmega328p) variable frequency pwm generation formula example

Arduino(Atmega328p) variable frequency pwm generation formula example

Now to output a PWM on variable frequency we have to configure OCR1A register. I am going to output a PWM of duty cycle 50%. Take the above generated frequency 100Hz as reference. To generate PWM of 50% duty cycle. We have to invert the signal output from low to high and high to low after every 5ms since the frequency in 100Hz.

For this to happen we have to set some bits in timer registers TCCR1A and TCCR1B. In TCCR1A set the mode of the timer. I set Fast PWM. By selecting bits COM1A1,COM1B1. Selecting these bits will make your pins OC1A and OC1B to output fast PWM. 

Arduino mode selection bits for Non-Inverted Mode

Arduino mode selection bits for Non-Inverted Mode

Now when mode is selected we have to do one more thing select the wave form. I selected PWM phase and frequency correction mode.
Arduino Wave Form Selection Bit

Arduino Wave Form Selection Bit

To study about the mode please consult the datasheet topic 16.9.5 Phase and Frequency Correct PWM Mode. I bet reading the topic “16.9.5 Phase and Frequency Correct PWM Mode” will clear you all about generating variable frequency PWM using Arduino(Atmega328p).
Important Update: Due to many questions on what is 0.0113 in code? Here is its explanation. Since i am generating variable frequency between 10 Hz to 100 Hz. For generating frequency of 10 Hz count(in code) or ICR1 value is 100000 and for 100Hz count or ICR1 value is 10000(You can calculate values in upper given formula). You know that analogread() function of arduino reads 1023 when voltage at corresponding analog pin is 5 volts. 0.0113 is increasing this 5v corresponding value to match the needed input for pwm function. For example
For generating 10Hz signal. You input 4.8 volts.

Code Flow
Pinput will be Pinput=1015;   // 4.8 volts at arduino analog pin reading
Pinput=(1015/0.0113);     —> Pinput=89823;
count= 10000+89823;
count=99823;                        //If we put this count value back in above formula we get 10.01 Hz frequency

The maximum analog channel can read is 1023 and Pinput value goes to 1023/0.0113=90530 or count=10000+90530= 100530. Which lower down the frequency to 9.9 Hz and since we want to remain in the bracket of 10 to 100Hz so we can not go more high. I hope 0.0113 makes sense now. Its just a value to remain in 10000 to 100000 bracket.  

Picture

Variable Frequency PWM(Pulse Width Modulation) signal generation using Timers of Arduino(Atmega328p Microcontroller)/Arduino Fast PWM

Download the project code, folder includes project simulation on proteaus and Arduino Project .ini file. If you have any questions regarding project code write them below in the projects section.

Watch the Project video here…

Project Code and Simulation


Filed Under: Knowledge Share, Microcontroller Projects

 

Questions related to this article?
👉Ask and discuss on Electro-Tech-Online.com and EDAboard.com forums.



Tell Us What You Think!! Cancel reply

You must be logged in to post a comment.

HAVE A QUESTION?

Have a technical question about an article or other engineering questions? Check out our engineering forums EDABoard.com and Electro-Tech-Online.com where you can get those questions asked and answered by your peers!


Featured Tutorials

  • Introduction to Brain Waves & its Types (Part 1/13)
  • Understanding NeuroSky EEG Chip in Detail (Part 2/13)
  • Performing Experiments with Brainwaves (Part 3/13)
  • Amplification of EEG Signal and Interfacing with Arduino (Part 4/13)
  • Controlling Led brightness using Meditation and attention level (Part 5/13)
  • Control Motor’s Speed using Meditation and Attention Level of Brain (Part 6/13)

Stay Up To Date

Newsletter Signup

Sign up and receive our weekly newsletter for latest Tech articles, Electronics Projects, Tutorial series and other insightful tech content.

EE Training Center Classrooms

EE Classrooms

Recent Articles

  • What are the battery-selection criteria for low-power design?
  • Key factors to optimize power consumption in an embedded device
  • EdgeLock A5000 Secure Authenticator
  • How to interface a DS18B20 temperature sensor with MicroPython’s Onewire driver
  • Introduction to Brain Waves & its Types (Part 1/13)

Most Popular

5G 555 timer circuit 8051 ai Arduino atmega16 automotive avr bluetooth dc motor display Electronic Part Electronic Parts Fujitsu ic infineontechnologies integratedcircuit Intel IoT ir lcd led maximintegratedproducts microchip microchiptechnology Microchip Technology microcontroller microcontrollers mosfet motor powermanagement Raspberry Pi remote renesaselectronics renesaselectronicscorporation Research samsung semiconductor sensor software STMicroelectronics switch Technology vishayintertechnology wireless

RSS EDABOARD.com Discussions

  • Advice for my disabled son please
  • Very low voltage/power Schmitt trigger?
  • 3D IC Design: Is it possible to stack CPU and FPGA?
  • dc to dc converter sparks when inserting fuse
  • 7 segment display connections

RSS Electro-Tech-Online.com Discussions

  • Pet Microchip scan
  • Disabled son needs advice please
  • Modify a digital clamp ammeter ?
  • Confirming whether this circuit will work
  • How does a blinky/flashing ball work?
Engineers Garage
  • Analog IC TIps
  • Connector Tips
  • DesignFast
  • EDABoard Forums
  • EE World Online
  • Electro-Tech-Online Forums
  • Microcontroller Tips
  • Power Electronic Tips
  • Sensor Tips
  • Test and Measurement Tips
  • 5G Technology World
  • About Us
  • Contact Us
  • Advertise

Copyright © 2022 WTWH Media LLC. All Rights Reserved. The material on this site may not be reproduced, distributed, transmitted, cached or otherwise used, except with the prior written permission of WTWH Media
Privacy Policy | Advertising | About Us

Search Engineers Garage

  • Projects and Tutorials
    • Electronic Projects
      • 8051
      • Arduino
      • ARM
      • AVR
      • PIC
      • Raspberry pi
      • STM32
    • Tutorials
    • Circuit Design
    • Project Videos
    • Components
  • Articles
    • Tech Articles
    • Insight
    • Invention Stories
    • How to
    • What Is
  • News
    • Electronic Products News
    • DIY Reviews
    • Guest Post
  • Forums
    • EDABoard.com
    • Electro-Tech-Online
    • EG Forum Archive
  • Digi-Key Store
    • Cables, Wires
    • Connectors, Interconnect
    • Discrete
    • Electromechanical
    • Embedded Computers
    • Enclosures, Hardware, Office
    • Integrated Circuits (ICs)
    • Isolators
    • LED/Optoelectronics
    • Passive
    • Power, Circuit Protection
    • Programmers
    • RF, Wireless
    • Semiconductors
    • Sensors, Transducers
    • Test Products
    • Tools
  • EE Resources
    • DesignFast
    • LEAP Awards
    • Oscilloscope Product Finder
    • White Papers
    • Webinars
  • EE Learning Center
    • Design Guides
      • WiFi & the IOT Design Guide
      • Microcontrollers Design Guide
      • State of the Art Inductors Design Guide
  • Women in Engineering