/
boost_converter.ino
54 lines (44 loc) · 1.43 KB
/
boost_converter.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
/**
* Basic boost converter with PID loop for feedback control
* http://mcuhq.com/29/arduino-dc-dc-boost-converter-design-circuit-with-control-loop
* @author Justin Bauer
* @date June 3 2016
* @see http://mcuhq.com
*/
#include <PID_v1.h>
int pwmPin = 6; //PIN 5 IS DAMAGED!
int ledPin = 13;
int analogPin = 0;
int val = 0;
unsigned long previousMillis = 0; // will store last time
const long interval = 500; // interval at which to delay
double Setpoint, Input, Output;
double Kp=.2, Ki=.4, Kd=0;
PID myPID(&Input, &Output, &Setpoint, Kp, Ki, Kd, DIRECT);
void setup() {
pinMode(pwmPin,OUTPUT);
pinMode(ledPin, OUTPUT); // onboard LED
TCCR0B = (TCCR0B & 0b11111000) | 0x01; // 62KHz @see http://playground.arduino.cc/Main/TimerPWMCheatsheet
analogWrite(pwmPin, 120);
Serial.begin(9600);
//initialize the variables we're linked to
Input = analogRead(analogPin);
Setpoint = 46; // 46= 24v 92=45V 200=99V
//turn the PID on
myPID.SetMode(AUTOMATIC);
myPID.SetOutputLimits(0,220);
}
void loop() {
Input = analogRead(analogPin); // read the input pin
myPID.Compute();
analogWrite(pwmPin, Output);
// Blink the status LED
unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= interval) {
previousMillis = currentMillis;
digitalWrite(ledPin, !digitalRead(ledPin));
Serial.println(Input);
Serial.println(Output);
Serial.println("");
}
}