1MHz PWM signal on Arduino Uno

can anyone tell me how can i generate a 1 mhz pwm signal on arduino uno

See if this link helps, especially the title:
Fast PWM Mode
https://docs.arduino.cc/tutorials/generic/secrets-of-arduino-pwm/

1 Like

Someone might be able to tell you if you can, first. I'd suggest you can't, based on a quick perusal of

'328 Datasheet
Page 74 and onwards.

But, given we know nothing of your application, that may be premature. What PWM resolution do you require?

Your initial post is a bit thin for an initial post in "Project Guidance".

1 Like

You need to divide 16MHz by 16. You can set some TCCRnx registers to divide the 16MHz clock by 1, use a WGM mode with a top of 15, and for PWM duty cycle, vary the OCRnx register for your chosen pin.

Maybe the TimerOne library will do that for you.

Here is the sketch for you:

#include <TimerOne.h>
//UNO only

void setup()
{
  pinMode(9, OUTPUT);
  Timer1.initialize(1);  // Frequency, 1us = 1 MHz
  Timer1.pwm(9, 50);      // 50% DC on pin 9

  //Timer1.pwm(10,255);    // 25% DC on pin 10
}

void loop() //regulating duty cycle sing Pot at A0-pin of UNO
{
  Timer1.pwm(9, map(analogRead(A0), 0, 1023, 0, 100));
  delay(1000);
}
1 Like

Why do you want a 1 MHz PWM signal?

The results are very crude.
The PWM generated only has 9 distinct values of duty cycle.

I had to modify GolamMostafa's code to get it to work correctly.

#include <TimerOne.h>
//UNO only

void setup()
{
  pinMode(9, OUTPUT);
  Timer1.initialize(1);   // Period 1µs, Frequency 1MHz
  Timer1.pwm(9, 512);     // 50% DC on pin 9
}

void loop() //regulating duty cycle using Pot at A0-pin of UNO
{
  Timer1.pwm(9, analogRead(A0) + 1);
  delay(100);
}

Here is a video showing the PWM produced.

The yellow trace is the voltage on pin A0, ramping up and down.
The blue trace is the resulting PWM.

2 Likes

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.