Changing analogWrite() function frequency

Hi, I am trying to implement very basic MPPT algorithm so I will measure input power and output power of a buck converter and adjust the duty cycle of my PWM for MOSFET according to that. Problem is 1kHz is not enough for me I want to increase the switching frequency of my PWM output. But I heard that playing with timers and default settings may disturb the other algorithms or sensor readings. Is it true ? and if yes hat should I do?

I will use ATmega328 Arduino Nano

Before we can help, we need to know what processor you are talking about.
After all, we are very good at helping, but we are very poor at guessing.

I will use ATmega328 Arduino Nano

The source code for the analogWrite function is on your computer. You are free to modify the timer settings as you wish.

Or, implement your custom timer code and do not use analogWrite.

1 Like

This page talks about fast pwm on the leonardo, but is just as useful for a nano (I've tried it)

You can simply modify the prescale ratio of a timer while maintaining the analogWrite() values. Changing the TOP value instead also affects (reduces or extends) the duty cycle range.

Don't touch Timer0 because it's used for system timing.

See if it helps you.

The Nano Every is the same foot print as the 328 Nano but has many additional useful features as it uses the Atmega 4809 as the processor.

MegaCoreX is the best platform to use with the Nano Every, and it contains a function
analogWriteFrequency( ) which will work for 1(default), 4, 8, 16, or 32 KHz.

What you need to know can be found at

Instructions on how to load the core through the board manager is here

Changing the frequency on the Nano PWM output is a question of two lines of code:

// Pins D5 and D6 - 31.4 kHz
TCCR0B = 0b00000001; // x1
TCCR0A = 0b00000001; // phase correct

After then you could use the analogWrite() function as before, but with 31 KHz frequency

More info

Only timer0 is used by the core arduino functions (for maintaining the millis() and micros() times (including delay() and similar.).) Timer0 controls pwm on pins 5 and 6, I think. You should be able to change the other 4 pwm pin frequencies by manipulating the other two timers, without bothering any of the core functions.

That is true. But watch out for other libraries that use other timers. It is not always easy to spot what hardware resources are used, especial third party ones. They often have poor documentation.

So if you have no other library loaded fell free to use either timer 1 or timer 2. But if later down the line with your project you might have to change the timer you used for the PWM speed. Or edit and change the other library.

If you don't want to maipulate registers yourself, you can try out my FastPwmPin library. I made it when I needed high frequency PWM. It supports various popular Atmel processors, including the ATmega328.

(See the readme for details such as used timers, supported pins, etc.)

Okay so, I am doing a very basic circuit to be honest but I will use INA226 (a power sensor) which communicate with I2C so SDA SCL pins (A4, A5) I also need to have a PWM output to drive a logic mosfet (I am planning to drive it directly from the D9 pin since it is a low power mosfet) around 20 kHz (there is no specific requirement). I am afraid that playing with timer1 might (I have no idea) create problems with INA226.

Okay so, I am doing a very basic circuit to be honest but I will use INA226 (a power sensor) which communicate with I2C so SDA SCL pins (A4, A5) I also need to have a PWM output to drive a logic mosfet (I am planning to drive it directly from the D9 pin since it is a low power mosfet) around 20 kHz (there is no specific requirement). I am afraid that playing with timer1 might (I have no idea) create problems with INA226.

@kmesne

There is no need to post two identical answers to two different people.

Just start your reply with an @ "who ever you want to address" to more than one people. In your specific case use an @westfw and @Grumpy_Mike.

So what is this circuit, please supply a real schematic, hand drawn is fine but fritzing like physical layout diagrams are not.

Why is this?

So what other libraries will be used to drive the INA226? If the answer is no other libraries then it will not be an issue.

GitHub - Zanduino/INA: Combined Arduino library for reading multiple INA2xx power monitor devices this library for INA

I will construct a buck converter with low side mosfet (logic level mosfet so it won't need high gate current therefore Arduino should be enough to turn it on/off and it will be simpler)

Required energy depends on the switching frequency, with all MOSFET circuits.

While tweaking the timers to have high frequency pwm is there any negative effect of using 20 kHz instead of 10kHz? Should I go lower if possible or it won't make a difference ?

While tweaking the timers to have high frequency pwm is there any negative effect of using 20 kHz instead of 10kHz? Should I go lower if possible or it won't make a difference ?

What is your targey PWM frequency?