Change PWM frequency but variable duty cycle

Hi everyone,

I'm a new learner to Arduino, and I've been enjoying programming. Although I haven't worked with other programming languages before, I'm currently working on a project an actuator (which I believe contains a motor). Here's the challenge: I've found that the actuator runs at a frequency of 190-200 Hz with a 10V supply and a duty cycle ranging from 15% to 90% with my PWM signal generator. Now, I'd like to fix the frequency at 200 Hz while allowing variable duty cycles using a potentiometer within the void loop(). If anyone has insights or suggestions on how to achieve this, I'd greatly appreciate your help!

Unfortunately, I don't have an oscilloscope for testing; I can only test directly with the actuator.

Below is the code I've made, but it doesn't work with my actuator (even though it works fine with my signal generator):

const unsigned TOP = 9999; 
const int POT_PIN = A0;     // Potentiometer
const int PWM_PIN = 9;      // PWM output pin

void setup() {
  pinMode(PWM_PIN, OUTPUT);

  // 타이머1 
  TCCR1A = 0;
  TCCR1B = 0;
  TCCR1C = 0;
  TIMSK1 = 0; 
  TIFR1 = 0;   

  TCCR1A = (1 << COM1A1) | (1 << COM1B1) | (1 << WGM11);
  TCCR1B = (1 << WGM13) | (1 << WGM12) | (1 << CS11);

  ICR1 = TOP;
}

void loop() {

  int potValue = analogRead(POT_PIN);

  unsigned int dutyCycle = map(potValue, 0, 1023, 0, TOP);

  // Set the duty cycle for the appropriate pin (9 or 10)
  OCR1A = dutyCycle;
 OCR1B = dutyCycle;  // For pin 10

  delay(100); 
}

Maybe something for you here
https://www.oxgadgets.com/2011/04/creating-a-variable-frequency-pwm-output-on-arduino-uno.html

Maybe this helps:

#include <Arduino.h>
#include <Streaming.h>
#include <util/atomic.h>
Print &cout {Serial};

constexpr uint8_t PWM_OUT {9};
constexpr uint8_t ANALOG_IN {A0};
constexpr uint16_t HERTZ {200};   // Min is 1 / Max is 65535!

// Dont change anything below ------ PSC = Prescaler Timer 1 ----------------------------------------------------------
constexpr uint32_t PSC_INDEX() {
  return (F_CPU / (1 * HERTZ)) <= 65535U      ? 0
         : (F_CPU / (8 * HERTZ)) <= 65535U    ? 1
         : (F_CPU / (64 * HERTZ)) <= 65535U   ? 2
         : (F_CPU / (256 * HERTZ)) <= 65535U  ? 3
         : (F_CPU / (1024 * HERTZ)) <= 65535U ? 4
                                              : 5;
}
constexpr uint16_t PSC[6] {1, 8, 64, 256, 1024, 0};
constexpr uint8_t SET_PSC[6] {_BV(CS10), _BV(CS11), (_BV(CS11) | _BV(CS10)), _BV(CS12), (_BV(CS12) | _BV(CS10)), 0};
constexpr uint16_t ICR1_TOP {(F_CPU / (2UL * PSC[PSC_INDEX()] * HERTZ)) - 1};
// --------------------------------------------------------------------------------------------------------------------

void setup() {
  Serial.begin(115200);
 
  pinMode(PWM_OUT, OUTPUT);
  ATOMIC_BLOCK(ATOMIC_RESTORESTATE) {
    TCCR1B = 0;   // Stop Timer first, for safety
    TCCR1A = 0;
    TCCR1B = _BV(WGM13) | SET_PSC[PSC_INDEX()];   // Prescaler
    TCCR1A = _BV(COM1A1) | _BV(WGM11);            // phase correct, clear ocr1a on compare match
    ICR1 = ICR1_TOP;                              // TOP Value 
    OCR1A = (ICR1_TOP / 2) + 1;                   // 50% Duty Cycle
  }
}

void loop() {
  static int16_t prevResult {1025};
  int16_t result = analogRead(A0);
  if (abs(result - prevResult) > 4) {
    prevResult = result;
    uint16_t duty = map(result, 0, 1023, 0, ICR1_TOP);
    cout << "Duty: " << _WIDTH(duty,5) << " ICR TOP: " << _WIDTH(ICR1_TOP,5) << " Duty %:  "  << _WIDTH(duty * 100UL / ICR1_TOP,3)<< "%" << endl;
    ATOMIC_BLOCK(ATOMIC_RESTORESTATE) { OCR1A = duty; }
  }
  delay(200);
}

The frequency is defined via the constant “HERTZ”. The duty cycle is variable. In this example, it depends on the voltage at analog pin A0. It is set by the line ATOMIC_BLOCK(ATOMIC_RESTORESTATE) { OCR1A = duty; }

The code was written for Atmegaxx8 controllers.

Thanks ZX80, I have a quick question. what is the difference between Fast PWM ICR1 Top mode and PWM, Phase and Frequency Corrected ICR1 Top mode ? I am a pretty new and your expertise will be helpful to me.

You mean the actuator only runs if the PWM is 190-200Hz? And it will not work at the normal Arduino PWM frequency of 500-1000Hz?

Thanks Kai,

I forgot to mention that I am using Atmega328 Nano tho. I am trying to understand the code you gave me but it's not that easy to get them. Following your code, how can I tell what is the value of TCCR1A and TCCR1B?

No, it does not work at another frequency. The actuator is a part of my car, and apparently, it has its own circuit inside. I just don’t want to cut it open to see inside because it costs about 450 bucks per actuator haha. I checked with my signal generator from 1hz to 100khz. It only works at 195-205hz

I don't quite understand the question. TCCR1A and TCCR1B are set in the setup() function. The code is designed to provide a phase-correct PWM signal. The prescaler for the desired frequency is calculated based on the set CPU frequency and also applied to TCCR1B in the setup() function.

The whole thing works up to a maximum of 65535Hz.

Thanks Kai,

I really appreciate your help. I now have a better understanding of how timers work and control pulse signals in different modes. As far as I understand, a timer counts from 0 to 255 (or 65535 for Timer 2), and once it reaches the top value (Top), it either resets to 0 or counts back down from the top, depending on whether it’s Fast PWM or Phase-Correct PWM. Signal high and low times can be adjusted using ICR# or OCR#A (which allows changing the duty cycle). This what my current understanding is. Please correct me if I’m mistaken.

I still have some questions

How do OCR# and ICR# work?
In your code, ICR1 is used as the top value (period). In this case, OCR1A determines the duty cycle. If OCR1A were used as the top with a different mode, ICR1 would automatically determine the duty cycle?

Why do you add 1 to ICR1 to get OCR1A?
Similar to here,
ICR1_TOP = Clock Frequency / 2 / Pre-scaler / Desired Frequency - 1
(dividing by 2 means is not Fast PWM mode? correct me if I'm wrong)
The “-1” is for what and why?

Thanks,

Your first question is probably best answered if you read through sections 16.9.4 / 16.9.5 in the linked data sheet.

ATmega48A, ATmega48PA, ATmega88A, ATmega88PA, ATmega168A, ATmega1688PA, ATmega328, ATmega328P datasheet (microchip.com)

To the second question: 1 is subtracted because the counter starts counting at 0 and not at one.