Skip to content

Instantly share code, notes, and snippets.

@githubnemo
Created February 8, 2016 07:08
Show Gist options
  • Save githubnemo/78e34f0880a1a886124a to your computer and use it in GitHub Desktop.
Save githubnemo/78e34f0880a1a886124a to your computer and use it in GitHub Desktop.
/*
Code adapted from http://reibot.org/2011/08/07/intro-to-boost-converter/.
Tested with an Arduino Pro Mini.
*/
const int pwm_pin = 0;
const int fb_pin = 24;
const int manual = 0;
void setup(){
DDRD |= (1<<PD6);
TCNT0 = 0;
TCCR0A=0;
TCCR0B=0;
TCCR0A |= (1<<COM0A1); // Timer0 in toggle mode Table 11-2
TCCR0A |= (1 << WGM02) | (1 << WGM00); // phase-correct PWM (Table 15-8)
TCCR0B |= (1 << CS00); // undivised clock
OCR0A=104; // Default compare value for Timer 0
}
void loop(){
if (manual) {
double multiplier = analogRead(fb_pin) / 1023.;
OCR0A = (255. * multiplier);
} else {
/*
The setup is this:
Vin ---R1---o---R2---GND
|
fb_pin
R1/R2 is chosen so that voltage at fb_pin
does not exceed 5V. Vin is voltage from boost conv.
*/
double targetVoltage = 34;
double R2 = 98.8e3;
double R1 = 1070e3;
double voltage = (analogRead(fb_pin)/1023.)*4.9*(R2+R1)/R2;
if(voltage < targetVoltage){
// Tune this value depending on your inductor.
// Use manual mode to estimate the right PWM frequency
// that yields the best results, then use it here.
OCR0A = 100; // turn transistor mostly off
}
if(voltage > targetVoltage){
OCR0A = 0; // turn transistor always on
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment