Hi all,
I know my way around basic coding and electronics stuff but my current project is getting a bit too complex for me. Before doing something stupid it would be great to get some feedback on my problem.
The basic need is to heat a liquid (about 200ml) to a certain temperature with a precision as close to ±0,1° as possible.
I included the following PID Library but am having troubles with the wiring/ schematics.
My idea is to use a Mosfet (IRLZ34N) and pwm to control the voltage from a desktop power supply to the heating element.
Would it be smarter to use it as a relay (on/off) instead?
Could anybody please have a look at the wiring if I am missing something?
Any help is highly appreciated!
I have also attached the current code:
#include <AutoPID.h>
#include <DallasTemperature.h>
#include <OneWire.h>
#define TEMP_PROBE_PIN 13
#define Heater_PIN A5
#define TEMP_READ_DELAY 800 //can only read digital temp sensor every ~750ms
//pid settings and gains
#define OUTPUT_MIN 0
#define OUTPUT_MAX 255
#define KP 50
#define KI 0
#define KD 0
//Dallas Tempsensor
#define TEMPERATURE_PRECISION 12
unsigned long current = 0;
unsigned long last = 0;
int mode;
double temperature, setPoint = 75, outputVal;
OneWire oneWire(TEMP_PROBE_PIN);
DallasTemperature temperatureSensors(&oneWire);
//input/output variables passed by reference, so they are updated automatically
AutoPID myPID(&temperature, &setPoint, &outputVal, OUTPUT_MIN, OUTPUT_MAX, KP, KI, KD);
unsigned long lastTempUpdate; //tracks clock time of last temp update
//call repeatedly in loop, only updates after a certain time interval
//returns true if update happened
bool updateTemperature() {
if ((millis() - lastTempUpdate) > TEMP_READ_DELAY) {
temperature = temperatureSensors.getTempFByIndex(0); //get temp reading
lastTempUpdate = millis();
temperatureSensors.requestTemperatures(); //request reading for next time
return true;
}
return false;
}
void setup() {
pinMode(Heater_PIN,OUTPUT);
Serial.begin(9600);
temperatureSensors.begin();
temperatureSensors.requestTemperatures();
while (!updateTemperature()) {} //wait until temp sensor updated
//if temperature is more than 4 degrees below or above setpoint, OUTPUT will be set to min or max respectively
myPID.setBangBang(4);
//set PID update interval to 4000ms
myPID.setTimeStep(4000);
last = millis();
}
void loop() {
updateTemperature();
myPID.run(); //call every loop, updates automatically at certain time interval
analogWrite(Heater_PIN, outputVal);
Serial.print ("tmp");
Serial.println( temperature);
Serial.print ("pwm");
Serial.println(outputVal);
}