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):
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; }
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.
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.
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?