changing duty cycle in arduino

Dear All in Arduino.cc

I am planning to make dc/dc boost converter working in 5kHz , can anyone help me how to change the duty cycle from 5% to 90% . can you help me with the code in IDE or in MATLAB or even in simulink.

I did it before by using the code:

void setup()
{
pinMode(13,OUTPUT);
}
void loop()
{
digitalWrite(13,HIGH);
delay(0.1);
digitalWrite(13,LOW);
delay(0.1);
}

but the problem that I found it will change the frequency not to be 5kHZ

anyone can suggest what to do

thaks

The analogWrite function sets the duty cycle of a PWM output. The default PWM frequencies are around 500 and 1000 Hz. Changing the PWM frequency is a subject often covered in the forum, so a search should find how its done.

Not directly related.

delay(0.1);

delay() can't have a decimal place.
If you want a higher resolution, use delayMicroseconds()
Leo..

Thank you guys
I did it

for example

void setup()
{
pinMode(13, OUTPUT);
}

void loop()
{
digitalWrite(13, HIGH);

delayMicroseconds(60); // Approximately 60% duty cycle @ 10KHz

digitalWrite(13, LOW);

delayMicroseconds(100-60);
}

but does anyone know How to make it in Matlab simulink with using repeating sequence with comparator <= and all

I made it before but the frequency was changing to be 1Khz and I want it to be 5kHz with variable duty cycle

any help pleaseā€¦

Maybe go to a Matlab forum?

delayMicroseconds(100-60);
That also isn't allowed.

Study the reference page on the learning tab here, or in the help>reference tab of your IDE.
Leo..

Wawa:
delayMicroseconds(100-60);
That also isn't allowed.

Study the reference page on the learning tab here, or in the help>reference tab of your IDE.
Leo..

Now I'm confused (and I did read the reference page :wink: )

delayMicroseconds takes an unsigned int; both 100 and 60 fit in an unsigned int; the result also fits in there.
The maximum value for an accurate delay is 16383, so that requirement is also met.

As said, I'm confused.

Ahh, duh.
100-60 as a calculation.
Leo..

Wawa:
Ahh, duh.
100-60 as a calculation.
Leo..

What did you think it was :wink:

sterretje:
What did you think it was :wink:

Just looked at it with a blank mind.
I think Alzheimer's is starting to kick in.
Leo..

I am still working on it , actually I need it in matlab to use a fuzzy logic controller to control the boost converter by changing the duty cycle. the working frequency is 5kHz.

I you suggest any Idea , I will be gratitude.

Thanks for your support , Take care

Not only is it a calculation, but it is a constant calculation and will be done at compile time, so the function only ever sees 40.

Welcome again..

I want to make a square wave in arduino mega with 5Khz and with different duty cycle by changing the

resistor in the input pin A0

can any one help me to have this code? , any Idea ?

Please I want your help.

Thanks

You can start with the analog input example that comes with the IDE; should give you a pretty good idea.