PWM pot controlled question

Hello everybody,

I am new with programming the Arduino Uno, so i am asking for a bit of assistance with my project.

I need the Arduino to work as 3 separated pwm's that are controlled by 3 pot resistor on 80 kHz with a duty cycle from between the 0 and 50%. It is meant to control 3 high power LED's separately.

What i found so far on the internet is:

#include <PWM.h>
int32_t frequency = 80000;
// Analog inputs connected to the variable resistors
const int knobPin1 = 1; // LED 1 control
const int knobPin2 = 2; // LED 2 control
const int knobPin3 = 3; // LED 3 control
// PWM outputs connected to LED driver circuits
const int drivePin1 = 9;// LED 1 drive
const int drivePin2 = 10;// LED 2 drive
const int drivePin3 = 11;// LED 3 drive
// initial value for the variable resistors
int knobValue1 = 0;
int knobValue2 = 0;
int knobValue3 = 0;
void setup() {
// set the drive pins as output:
pinMode(drivePin1, OUTPUT);
pinMode(drivePin2, OUTPUT);
pinMode(drivePin3, OUTPUT);
}
void loop() {
// read the variable resistors, convert it to 0 - 255
knobValue1 = analogRead(knobPin1) / 4;
knobValue2 = analogRead(knobPin2) / 4;
knobValue3 = analogRead(knobPin3) / 4;
// use the data to control the drive:
analogWrite(9, knobValue1);
analogWrite(10, knobValue2);
analogWrite(11, knobValue3);
}

I am sure it is incomplete because i just used what i had found and combined it.

Can somebody please tell me how to make this working as described above, and also explain what is doing what so i can learn how to program this my self in the future for other purposes.

The normal PWM frequency is 0.47 kHz, why such a high freq.?
What voltage and current do your LEDs require?

Yes, some datasheets for your LEDs and power supply would be helpful...

outsider:
The normal PWM frequency is 0.47 kHz, why such a high freq.?
What voltage and current do your LEDs require?

Yes, the normal frequency is very low for this project. I need the high frequency to boost the power from 12 Volt to 38 Volt. The booster circuit i have made already and that is functioning well. It are 100Watt 38Volt LED's 3.5 Amp.

The Mosfet i use works with 5volt on the gate.

Well you'll need to reprogram timer1 (pins 9, 10) and timer2 (pins 11, 3) to use divide-by-1 prescaler
and probably use a different mode to allow 0..199 fast-PWM count rather than 0.255..0 phase-correct
PWM.

Have you search these forums for "PWM frequency"?

And how do i do that?

Yes i have read a bit about this subject but i am not sure how to do this. Many forums write about it but for a newbee it is hard to understand.

If i put this behind void setup()

InitTimerSafe();
bool success = SetPinFrequencySafe(9,frequency);
bool success = SetPinFrequencySafe(10,frequency);
bool success = SetPinFrequencySafe(11,frequency);
bool success = SetPinFrequencySafe(3,frequency);

will that change the frequency for those pin's then?

This i had put allredy in the beginning:

#include <PWM.h>
int32_t frequency = 80000;

Would it work like this?

#include <PWM.h>
int32_t frequency = 80000;


 // Analog inputs connected to the variable resistors
    const int knobPin1 = 1; //LED 1 control 
    const int knobPin2 = 2; //LED 2 control 
    const int knobPin3 = 3; //LED 3 control 
    // PWM outputs connected to LED driver circuits
    const int drivePin1 = 9;//LED 1 drive
    const int drivePin2 = 10;//LED 2 drive
    const int drivePin3 = 11;//LED 3 drive
    // initial value for the variable resistors
    int knobValue1 = 0;
    int knobValue2 = 0;
    int knobValue3 = 0;
    void setup() {

 InitTimerSafe();
SetPinFrequencySafe(9,frequency);
SetPinFrequencySafe(10,frequency);
SetPinFrequencySafe(11,frequency);
SetPinFrequencySafe(3,frequency);

       // set the drive pins as output:
       pinMode(drivePin1, OUTPUT);
       pinMode(drivePin2, OUTPUT);
       pinMode(drivePin3, OUTPUT);
    }
    void loop() {
       // read the variable resistors, convert it to 0 - 255
       knobValue1 = analogRead(knobPin1) / 4;
       knobValue2 = analogRead(knobPin2) / 4;
       knobValue3 = analogRead(knobPin3) / 4;
       // use the data to control the drive:
       analogWrite(9, knobValue1);
       analogWrite(10, knobValue2);
       analogWrite(11, knobValue3);
    }

Hi,

Please read the first post in any forum entitled how to use this forum.
http://forum.arduino.cc/index.php/topic,148850.0.html
then look down to item #7 about how to post your code.
It will be formatted in a scrolling window that makes it easier to read.

Thanks...Tom.. :slight_smile:

Done :slight_smile: .... but it don't solve my question :-)))

Ok, i have understand that i have to formulate my question differently maybe to get the needed support, so let me try again:

I have a Arduino Uno Rev. 3 with a AT MEGA 328. I have IDE 1.7.0 with PWM.h from Arduino PWM Frequency Library v5 from googlegroups.com.

The meaning from the program is to get 3 pwm pin's on 80kHz with a duty cycle from 0 till 50%, this duty cycle will be controlled by 3 pot meters. So that i get 3 separated PWM's who are each controlled by 1 pot. So pot 1 is connected to A1, pot 2 is connected to A2 and pot 3 is connected to A3.

Pot 1 controls the duty cycle from D9 output, pot 2 controls the duty cycle from D10 output and pot 3 controls the duty cycle from D11 output.

The script i have so far comes from two sides:

The frequency part comes from: Arduino PWM Tutorial #2 - How to Set PWM Frequency Accurately - YouTube
And the PWM part comes from: http://www.electroschematics.com/9506/arduino-pwm-led-control/

I am not going to connect those small leds on it but 3 mosfets to control 3 times 100Watt LED's. I need the high frequency in the Arduino to boost the voltage from 12Volts to 38Volts. The boost circuit i have made allready and that is working fine.

The code i have so far is:

#include <PWM.h>
int32_t frequency = 80000;


 // Analog inputs connected to the variable resistors
    const int knobPin1 = 1; //LED 1 control 
    const int knobPin2 = 2; //LED 2 control 
    const int knobPin3 = 3; //LED 3 control 
    // PWM outputs connected to LED driver circuits
    const int drivePin1 = 9;//LED 1 drive
    const int drivePin2 = 10;//LED 2 drive
    const int drivePin3 = 11;//LED 3 drive
    // initial value for the variable resistors
    int knobValue1 = 0;
    int knobValue2 = 0;
    int knobValue3 = 0;
    void setup() {

 InitTimerSafe();
SetPinFrequencySafe(9,frequency);
SetPinFrequencySafe(10,frequency);
SetPinFrequencySafe(11,frequency);
SetPinFrequencySafe(3,frequency);

       // set the drive pins as output:
       pinMode(drivePin1, OUTPUT);
       pinMode(drivePin2, OUTPUT);
       pinMode(drivePin3, OUTPUT);
    }
    void loop() {
       // read the variable resistors, convert it to 0 - 255
       knobValue1 = analogRead(knobPin1) / 4;
       knobValue2 = analogRead(knobPin2) / 4;
       knobValue3 = analogRead(knobPin3) / 4;
       // use the data to control the drive:
       analogWrite(9, knobValue1);
       analogWrite(10, knobValue2);
       analogWrite(11, knobValue3);
    }

Now my question is, what must i change, and how so that the Arduino gives me the PWM signals that i need as described above?

Hi,

What does your sketch do now?
Do you have independent control of the duty cycle of the outputs?

Tom... :slight_smile:

TomGeorge:
Hi,

What does your sketch do now?
Do you have independent control of the duty cycle of the outputs?

Tom... :slight_smile:

To be honest....i have no idea, i didn't try it yet without some one's approval that it does not harm my Arduino.

Can i try it without risk?

I'm not sure of any way it would be possible to damage an Arduino via a "bad program". Only bad circuits destroy an Arduino.

I can only recommend using small 5mm LEDs (and resistors) like in the http://www.electroschematics.com/9506/arduino-pwm-led-control/ example you linked earlier. Your statement that you're using mosfets to PWM 100W LED driver circuits is something you need to provide more detail on.

ok, then i will try it today to run it like that.

Ow that is no problem, i can make a jpg from the circuit today, it is a easy circuit booster part that i first had tried with a 555 pwm control. But the pcb became very big for 3 channels. So this is why i try to figure out how the Arduino works. One's i have the feeling and some knowledge about this then i can use it for more projects. In the past i have learned how to program PLC's but that was in that time very expensive, so now i discovered this Arduino i really want to learn it.

Well, i got on pin 9 and 10 a good pwm on 30kHz, but on pin 11 i see 400Hz and not 30kHz. But the pots do work independent from each other. Here is the new script i had uploaded on the Arduino:

#include <PWM.h>
int32_t frequency = 30000;


 // Analog inputs connected to the variable resistors
    const int knobPin1 = 1; //LED 1 control 
    const int knobPin2 = 2; //LED 2 control 
    const int knobPin3 = 3; //LED 3 control 
    // PWM outputs connected to LED driver circuits
    const int drivePin1 = 9;//LED 1 drive
    const int drivePin2 = 10;//LED 2 drive
    const int drivePin3 = 11;//LED 3 drive
    // initial value for the variable resistors
    int knobValue1 = 0;
    int knobValue2 = 0;
    int knobValue3 = 0;
    void setup() {

 InitTimersSafe();
SetPinFrequencySafe(9,frequency);
SetPinFrequencySafe(10,frequency);
SetPinFrequencySafe(11,frequency);

       // set the drive pins as output:
       pinMode(drivePin1, OUTPUT);
       pinMode(drivePin2, OUTPUT);
       pinMode(drivePin3, OUTPUT);
    }
    void loop() {
       // read the variable resistors, convert it to 0 - 255
       knobValue1 = analogRead(knobPin1) / 4;
       knobValue2 = analogRead(knobPin2) / 4;
       knobValue3 = analogRead(knobPin3) / 4;
       // use the data to control the drive:
       analogWrite(9, knobValue1);
       analogWrite(10, knobValue2);
       analogWrite(11, knobValue3);
    }

This is the circuit i am using to boost the voltage for this LED.

Hi,
This page might help.

pins 9, 10 are timer 1
pins 3, 11 are timer 2

http://playground.arduino.cc/Code/PwmFrequency

also here

I think it refers to the UNO.

Hope it helps, Tom... :slight_smile:

Thanks.

It explains me a bit from how it works, but it does not explain to me why timer 1 did changed and timer 2 stayed on 400Hz in my script.

But if i have understand it right, then when i change divide by 8 instead of 4 in this line:

knobValue1 = analogRead(knobPin1) / 4;

Then the duty cycle would be not higher then 50%. Because 1024/8=128.

Is that correct?

I have try it, and it works on pin 9 and 10. With dividing throe 8 i get a duty cycle from 0 to 50%.

Just pin 11 does not give 30kHz and it looks that the duty cycle stay on 0 to 100%.