
CIRCUIT
DOWNLOAD SIMULATION FILES
PROGRAM
int p=1;
int pwmpin=9;
void setup()
{
setPwmFrequency(pwmpin, 1);
analogWrite(pwmpin,p);
//Serial.begin(9600);//SERIAL COMMUNICATION NOT //NEEDED
//Serial.println("HAII");
//delay(5000);delay(5000);
}
void loop()
{
int a=analogRead(A2);
delay(1);
//Serial.println(a);
//Serial.print("P=");
//Serial.println(p);
if(a>220)
{
p=p-1;
analogWrite(pwmpin,p);
if(p<0)p=0;
}
if (a<205 )//&& a>150
{
p=p+1;
if(p>220)p=220;
analogWrite(pwmpin,p);
}
}
void setPwmFrequency(int pin, int divisor) {
byte mode;
if(pin == 5 || pin == 6 || pin == 9 || pin == 10) {
switch(divisor) {
case 1: mode = 0x01; break;
case 8: mode = 0x02; break;
case 64: mode = 0x03; break;
case 256: mode = 0x04; break;
case 1024: mode = 0x05; break;
default: return;
}
if(pin == 5 || pin == 6) {
TCCR0B = TCCR0B & 0b11111000 | mode;
} else {
TCCR1B = TCCR1B & 0b11111000 | mode;
}
} else if(pin == 3 || pin == 11) {
switch(divisor) {
case 1: mode = 0x01; break;
case 8: mode = 0x02; break;
case 32: mode = 0x03; break;
case 64: mode = 0x04; break;
case 128: mode = 0x05; break;
case 256: mode = 0x06; break;
case 1024: mode = 0x7; break;
default: return;
}
TCCR2B = TCCR2B & 0b11111000 | mode;
}
}
/**
* Divides a given PWM pin frequency by a divisor.
*
* The resulting frequency is equal to the base frequency divided by
* the given divisor:
* - Base frequencies:
* o The base frequency for pins 3, 9, 10, and 11 is 31250 Hz.
* o The base frequency for pins 5 and 6 is 62500 Hz.
* - Divisors:
* o The divisors available on pins 5, 6, 9 and 10 are: 1, 8, 64,
* 256, and 1024.
* o The divisors available on pins 3 and 11 are: 1, 8, 32, 64,
* 128, 256, and 1024.
*
* PWM frequencies are tied together in pairs of pins. If one in a
* pair is changed, the other is also changed to match:
* - Pins 5 and 6 are paired on timer0
* - Pins 9 and 10 are paired on timer1
* - Pins 3 and 11 are paired on timer2
*
* Note that this function will have side effects on anything else
* that uses timers:
* - Changes on pins 3, 5, 6, or 11 may cause the delay() and
* millis() functions to stop working. Other timing-related
* functions may also be affected.
* - Changes on pins 9 or 10 will cause the Servo library to function
* incorrectly.
*
* Thanks to macegr of the Arduino forums for his documentation of the
* PWM frequency divisors. His post can be viewed at:
* http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1235060559/0#4
*
*/
How the Arduino Boost Converter Code Works
The program is a simple feedback loop. The Arduino switches the boost converter with a PWM signal on pin 9 and reads the output voltage on analog pin A2. The output has to be scaled down to the 0-5 V range of the analog input, usually with a resistor divider, before it reaches the Arduino.
- PWM frequency:
setPwmFrequency(pwmpin, 1)sets the divisor to 1 on timer 1. Pin 9 then runs at the base frequency of about 31.25 kHz instead of the default of roughly 490 Hz. A higher switching frequency lets the inductor and capacitor be smaller. - Duty cycle variable:
pis the value passed toanalogWrite(). It ranges from 0 to 255, where 255 means fully on. The code limits it to 220, which is about 86% duty, so the switch is never held on continuously. - Regulation: if the A2 reading rises above 220, the duty value drops by one step. If it falls below 205, the duty value rises by one step. Between 205 and 220 nothing changes, which gives a small dead band that stops the output from hunting up and down.
On a 5 V reference, the 10-bit ADC reads 220 at about 1.07 V and 205 at about 1.00 V. To set a different output voltage, change the two thresholds or change the divider ratio. The thresholds only make sense together with the divider, so adjust them as a pair.
The serial lines are commented out because the simulation does not need them. If you want to watch the duty value while tuning, uncomment them. Note that changing the timer 1 prescaler affects the Servo library, and changing timer 0 or timer 2 can break delay() and millis().
Is this the same as a buck-boost converter?
No. A boost converter only steps the voltage up, so the output is always higher than the input. A buck-boost converter can step the voltage either up or down, and it needs a different switching arrangement and a different control routine. This code will not work as a buck-boost controller without changes.