Hi:
I'm trying to use PID control to control AC outputs of up to 220 volts. I found a youtube video where a fellow used pwm and a Mosfet circuit to control the voltage powering a light bulb. I built the circuit
and it will work fine. I wanted to control the power output, however, based on a setpoint of light in the room and I wanted to do it with PID control. The code below basically comes from another Youtube video by logMaker 360. When I run the test, however, the output from the myPID.Compute(); is always zero. Do you have any idea what might be causing this? Is the PID_v1.h no longer active for example? I've struggled with this for some time. Please help if you can.
Bob
/*
* This sketch starts with this video: https://youtube.com/watch?v=crw0Hcc67RY
* written by Moz for YouTube channel logMaker 360
* on 4-12-2017. I've changed the input to be A0 and the control output to be digital pin 9.
*/
#include<PID_v1.h>
double Setpoint; // This is the desired value
double Input; // This will be the input from the photoresistor
double Output; // This is the output from the PID controller that drives the load, we will use an LED initially
const int photores = A0; // Photo resistor is wired to pin A0
const int led = 9; // This will be the output pin to have the pwm output
//PID initial parameters from Moz are 0,10,0 for the LED control
double Kp = 0, Ki = 1, Kd = 0;
//create PID instance
PID myPID(&Input,&Output,&Setpoint,Kp,Ki,Kd,DIRECT);
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
//Hardcode the brightness value, DrBobEEPE will later put this on a pot
Setpoint = 75;
//Turn the PID on
myPID.SetMode(AUTOMATIC);
//Adjust PID Values
myPID.SetTunings(Kp,Ki,Kd);
}
void loop() {
// put your main code here, to run repeatedly:
//Read the value from the light sensor. Analog input: 0 t0 1024 and this should be mapped to 0 to 255 as it is used for our PWM function
Input = map(analogRead(photores),0,1024,0,255); // photoresistor is on pin A0
//Calculate PID output
Input = 78;
myPID.Compute();
//Write the output (process control output) as calculated by the PID function
analogWrite(led,Output); //LED for Moz is set to digital pin 9. This is a pwm pin.
Serial.println(Output);
//Serial data for plotting
Serial.print(Input);
Serial.print(" ");
Serial.println(Output);
Serial.print(" ");
Serial.println(Setpoint);
delay (1000);
}