Code
Code
Code
int Vo;
float R1 = 10000;
float logR2, R2;
int T; // T = temperature
float c1 = 1.009249522e-03, c2 = 2.378405444e-04, c3 = 2.019202697e-07;
#include <Adafruit_ADS1X15.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <stdio.h>
LiquidCrystal_I2C lcd(0x27, 16, 2); // Set the LCD address to 0x27 for a 16 chars
and 2 line display
void setup(void){
pinMode(FAN, OUTPUT);
pinMode(OUTPUT_PS, OUTPUT);
pinMode(RELAY, OUTPUT);
lcd.begin();
ads.begin();
// The ADC input range (or gain) can be changed via the following
// functions, but be careful never to exceed VDD +0.3V max, or to
// exceed the upper and lower limits if you adjust the input range!
// Setting these values incorrectly may destroy your ADC!
// ADS1015
ADS1115
// -------
-------
// ads.setGain(GAIN_TWOTHIRDS); // 2/3x gain +/- 6.144V 1 bit = 3mV
0.1875mV (default)
// ads.setGain(GAIN_ONE); // 1x gain +/- 4.096V 1 bit = 2mV
0.125mV
// ads.setGain(GAIN_TWO); // 2x gain +/- 2.048V 1 bit = 1mV
0.0625mV
// ads.setGain(GAIN_FOUR); // 4x gain +/- 1.024V 1 bit = 0.5mV
0.03125mV
ads.setGain(GAIN_EIGHT); // 8x gain +/- 0.512V 1 bit = 0.25mV
0.015625mV
// ads.setGain(GAIN_SIXTEEN); // 16x gain +/- 0.256V 1 bit = 0.125mV
0.0078125mV
//set all elements of arrays for the moving average filter to zero
for (int thisReading = 0; thisReading < numReadings; thisReading++) {
I_readings[thisReading] = 0;
U_readings[thisReading] = 0;
I_set_readings[thisReading] = 0;
}
void loop(void){
int16_t adc0, adc1, adc2;
float amps, volts, setcurrent;
//calculate temperature
Vo = analogRead(THERMISTOR_PIN);
R2 = R1 * (1023.0 / (float)Vo - 1.0);
logR2 = log(R2);
T = (1.0 / (c1 + c2*logR2 + c3*logR2*logR2*logR2));
T = T - 273.15;
//T = (T * 9.0)/ 5.0 + 32.0; //converting the tempereture reading to Fahrenheit
Fanspeed = map(T , TempThreshold, 50, 100, 255); //calculate current fan speed
//hysteresis, so that fans don't turn on/off repeatedly when crossing the
threshold
if (T < TempThreshold){
Fanspeed = 0;
TempThreshold = 31;
}
if (T >= TempThreshold){
TempThreshold=30;
}
//if it's hot drive fans at full speed
if (T > 50){
Fanspeed = 255;
}
//disable the output when it overheats
if(T > OVER_TEMPERATURE_PROTECTION){
digitalWrite(OUTPUT_PS, LOW);
}
else{
digitalWrite(OUTPUT_PS, HIGH);
}
lcd.setCursor(0, 0);
lcd.print(U_average); //display measured output voltage
lcd.print("V ");
lcd.setCursor(7, 0);
if (I_set_average >= 10){
lcd.print(I_set_average, 1); //display set output current with 1 decimal
place
} else lcd.print(I_set_average); //display set output current with 2 decimal
places
lcd.print("A ");
lcd.setCursor(13, 0);
lcd.print(T); //display measured teperature
//lcd.write(0);
lcd.print("C");
lcd.setCursor(0, 1);
if (I_average >= 10){
lcd.print(I_average); //display measured output current with 2 decimal
places
}else lcd.print(I_average,3); //display measured output current with 3 decimal
places
lcd.print("A");
lcd.setCursor(7, 1);
lcd.print(I_average * U_average, 1); //display output power with 1 decimal place
lcd.print("W ");
//hysteresis, so that the relay doesn't turn on/off repeatedly when crossing the
threshold
if(volts > VoltageThreshold){
digitalWrite(RELAY, HIGH);
VoltageThreshold = 13;
}
if(volts<VoltageThreshold){
digitalWrite(RELAY, LOW);
VoltageThreshold = 13.5;
}
analogWrite(FAN, Fanspeed);
delayMicroseconds(20);
}