0% found this document useful (0 votes)
95 views

Boost Code For Blog Pid

This document provides instructions for using a PID controller with an Arduino to control the output of a boost converter. It defines variables for the PID controller, sets the PID parameters, and initializes the controller in the setup function. The main loop calls the PidUpdate function at least every 10ms to update the PID and set the PWM duty cycle output based on the voltage reading and target voltage setpoint.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
95 views

Boost Code For Blog Pid

This document provides instructions for using a PID controller with an Arduino to control the output of a boost converter. It defines variables for the PID controller, sets the PID parameters, and initializes the controller in the setup function. The main loop calls the PidUpdate function at least every 10ms to update the PID and set the PWM duty cycle output based on the voltage reading and target voltage setpoint.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

/* boost converter guide at reibot.

org
feedback connected to analog 1.
pwm pin is on digital 11
V 1 for the arduino pid library
*/

#include <PID_v1.h>
//Define Variables we'll be connecting to
double Setpoint, Input, Output;
//Specify the links and initial tuning parameters
PID myPID(&Input, &Output, &Setpoint, .8 , .1 , .01

, DIRECT);

int targetVoltage = 100; //up to 200v if you used same parts from the guide
void setup(){
Serial.begin(115200);
pwmSetup(); //set the pwm to 31khz
myPID.SetMode(AUTOMATIC);
myPID.SetOutputLimits(0, 100);
//how often you want the pid to update in ms
myPID.SetSampleTime(10); //changing this will require pid to be retuned

void loop(){
PidUpdate();
delay(10);
//call PidUpdate(); at least every 10ms.
//Otherwise set OCR2A = 0; while the arduino is doing something time consuming
}

void PidUpdate(){
//using resistive voltage divider, same R1 and R2 on wikipedia "voltage divider"
//R1 is connected to high voltage
//R2 is connected to ground
double R2 = 200000;
double R1 = 10000000;
double voltage = (analogRead(A1)/1023.)*4.9*(R2+R1)/R2;
Input = voltage;
Setpoint = targetVoltage;

myPID.Compute();
if(voltage > targetVoltage + 10){
OCR2A = 0;
}else if(voltage <= targetVoltage){
OCR2A = Output;
}

//have it output the voltage


/*
Serial.print("V:");
Serial.print(voltage);
Serial.print("\tADC voltage:");
Serial.print((analogRead(A1)/1023.)*4.9);
Serial.print("\tduty:");
Serial.println((int)OCR2A);
delay(10);
*/

void pwmSetup(){//just run once at setup


pinMode(3, OUTPUT); //OCR2B 3 and 11 are pwm channels
pinMode(11, OUTPUT); //OCR2A

TCCR2A = _BV(COM2A1) | _BV(COM2B1) | _BV(WGM20); //phase correct pwm 31250hz

TCCR2B = _BV(CS20);//change this as datasheet says to mainly get different pwm frequencies
OCR2A = 0;
OCR2B = 0;

You might also like