How to write such a program?

I`m new in programming and sorry for my english.
I want to write a program in which: they will be measure the different power values ​​that are changing by pwm. Assuming that the pwm value decreases by 1%, the program reads the power value every 1% pwm and writes it when all power values ​​are read. The program selects the highest power and corresponds to its % pwm. All this with the help of arduino uno v3, I do not know what functions to use or how to take a part of the program at all when choosing the highest value of all measured. If someone could tell me something I would be very grateful :wink:

What will be producing the PWM values that you want to measure?
What is the PWM frequency?

In general the output of PWM is either 1 or 0 - it is the duration of each of the parts that gives the average power.


It`s like this: photovoltaic panel> pwm> heater and arduino controls pwm to find the maximum power point. The best way to find that point is to change the load and measure the current and voltage, then multiply it to get the power, select the highest power and set the pwm as it was when it measured the highest power value.
My main problem is the skeleton itself, I do not know how to take it. When the program chooses from all measurements the highest power value and sets the pwm to the value it had when this measurement was made.

and arduino controls pwm to find the maximum power point.

Now that I understand what you are trying to do I see that I probably misinterpreted your Original Post. It is always a good idea to describe the project so that questions can be understood in the proper context.

I assume you want a program that can change the PWM and then measure the corresponding voltage and current at the load. Of course if it is a fixed resistive load then it is only necessary to measure the voltage as the current can be calculated.

I think you need to make a drawing of the proposed electrical circuit and post a photo of the drawing.

Are you using a buck-boost circuit to match the solar panel load to the heater load? What exactly is the Arduino controlling?


PV panels produce a constant power, so the moment you start modulating it you lose power (you effectively switch off the panels for short periods of time - they don't store any power so it's lost - and I'd worry about my panels if such a load is imposed on them, doesn't sound good for them). Normally a PV panel is connected to a charge controller, which in turn is connected to a battery. The charge controller manages the charging of the battery and the power draw by any loads you put on it, such as your heater.

Measuring voltage of a PWM power supply is also not that straightforward as it's not a constant voltage. Peak voltage is always the maximum or 0. The same for the current, maximum or 0.

For a heater, I'd say the maximum power it can output will be when supplied with the maximum rated voltage. I don't understand what you're trying to regulate here.

Besides these comments I have no clue what you're really trying to accomplish. Some images may work.

The schematic looks like this, data about voltage and current measurements are sent to arduino so it can control pwm.
I will describe how it works:

  1. Pwm value set to 20% and voltage and current measurements taken to calculate power, power value is stored and assigned to the pwm value that was set during measurement.
  2. Pwm value increased by, for example, 2%.
  3. Wait a few milliseconds.
  4. Read the measurements and calculate power, assign power values ​​to pwm.
    5 When all measurements are taken (suppose 40) the program selects the highest power from all stored and corresponding pwm values ​​that was measured during this measurement.
    6 Program waits 120 seconds and the cycle repeats itself.
    In response to your questions:
  5. The pwm frequency is 500k Hz so i think the appropriate capacitor at the output of the panels will smooth out any variables in the current consumption.
  6. There are a few methods of finding mppt, ie the maximum power point for panels but the one I have described is the most accurate.
  7. Why do not I just plug the panels into the heater?
    If it is the middle of the day and the sun shines brightly, my panels will give 2kW for a 2kW heater and everything is great. But on a more cloudy day, the current on the panels will be half the size, and as the heater has its resistance and the voltage will be appropriate, it will want to charge its nominal current. This will result in that the voltage on the panels will drop very heavily, if I have pwm and mppt will select respectively less load the panels will produce the same current at higher voltage. In fact, when the sun goes down on clouds with mppt it will be 1000W and without 300W it is a big difference.

My main problem is:
I do not know how to take a part where the program has saved all the values from the measurements and reads the maximum power value and related pwm set to this value and sets it.
Would anyone have any idea?

The schematic.

Arrays are part of the answer. Save each reading to an element of an array (readings[40]), Another array could store the PWM values (values[40]). Take the readings, calculate power. save the power value and the PWM value to their respective array. Use an index to increment through the array. When all the readings are taken, look through the readings array and find the max. The PWM value for that power value will be at the same index.

Image from Reply #6 so we don’t have to download it. See this Image Guide



What size is the capacitor and what is the PWM frequency?


I'm still curious: why heating the hardest when the sun shines brightest? I thought that usually goes the other way around.

The pwm frequency is 500kHz and i don`t know yet what capacitor i will use. I'm most worried about the code.
Wvmarle I do not understand your question, the stronger the sun, the more energy the photovoltaic panel will produce.

I don't know enough about the behaviour of capacitors but I suspect your voltage and current readings will be different depending on whether the reading is taken with the PWM on or off. And I don't know which reading would be most appropriate or if you need to average them.

I suspect the size of the capacitor will be important - I can see how you could have one that is too small but I don't know if there could be such a thing as too big.

It might be cheaper just to get a bigger solar panel and waste some energy :slight_smile:


Switching on / off takes 2 microseconds, it's really fast and i think there should be no difference in voltage or current measured, such systems exist and work very well but their construction is guarded by companies that produce such controllers.

Switching on / off takes 2 microseconds, it’s really fast and i think there should be no difference in voltage or current measured,

I don’t have the expertise you need for this but my sense of it is that if there were no difference there would be no point in switching it on an off. you really need an oscilloscope to help with this so that you can see what the Arduino can see - or what you want the Arduino to see.


I don't think think you can run 500kHz PWM with 256-levels on an arduino - that would imply a clock of 128MHz!

Ghoose a much lower frequency - a few kHz would be plenty for this application


The pwm frequency is 500kHz

So it's not the Arduino producing the PWM? or how will you reach that frequency?

Wvmarle I do not understand your question, the stronger the sun, the more energy the photovoltaic panel will produce.

You're using solar power to heat something - and you heat it most when the sun is at it's brightest (hottest) and not at all when the sun is gone. That's odd, using sunlight directly would normally be more efficient and simpler to build.

Google MPPT Arduino

This will give you the basics of what you want, Maximum Power Point Tracker.

Tom.... :slight_smile:
PS Please be very careful with any instructables project.

Google MPPT Arduino

This will give you the basics of what you want, Maximum Power Point Tracker.

But do the maths first. It may be more economical (and a lot less trouble) just to get a bigger solar panel.


What are the specs of your PV panels?
What voltage do you want the system to operate at?
What is the Load?

A 2kW PV system will not produce 2kW unless under laboratory conditions.

Tom.... :slight_smile: