Kendali dan Monitor Suhu Heater PID Control serta Fitur Timer LCD OLED
Pada kesempatan kali ini saya akan menjelaskan mengenai bagaimana cara membuat sebuah alat yang dapat mengendalikan suhu panas heater dengan PID Control dan juga menggunakan modul dimmer AC karena menggunakan tegangan 220 Volt AC. alat ini juga memiliki fitur timer dan juga lcd Oled. untuk lebih jelasnya berikut adalah koding dan skemanya.
a. Skema Alat
b. Program Arduino IDE
#include <Wire.h>
#include <DallasTemperature.h>
#include <OneWire.h>
#include <RBDdimmer.h>//
#include "U8glib.h"
U8GLIB_SSD1306_128X64 u8g(U8G_I2C_OPT_NO_ACK);
#define DS3231_I2C_ADDRESS 0x68
float kp = 15.15;
float ki = 1.67;
float kd = 1.15;
#define outputPin 9
#define zerocross 2
dimmerLamp dimmer(outputPin);
int buzzer = 5;
int x;
char tmp_string1[8];
char tmp_string2[8];
char tmp_string3[8];
char tmp_string4[8];
float suhu;
int btdown = 3;
int btup = 4;
int btset = 10;
int btok = 11;
int btsetx;
int btupx;
int btdownx;
int btokx;
int menit;
int setpoint;
float p,i,d;
float error,errorx,sumerr;
int sp;
int pid;
#define ONE_WIRE_BUS 7
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);
byte second, minute, hour, dayOfWeek, dayOfMonth, month, year;
// Convert normal decimal numbers to binary coded decimal
byte decToBcd(byte val)
{
return( (val/10*16) + (val%10) );
}
// Convert binary coded decimal to normal decimal numbers
byte bcdToDec(byte val)
{
return( (val/16*10) + (val%16) );
}
void setup(void) {
Serial.begin(9600);
sensors.begin();
dimmer.begin(NORMAL_MODE, ON);
pinMode(9,OUTPUT);
pinMode(btset,INPUT_PULLUP);
pinMode(btup,INPUT_PULLUP);
pinMode(btdown,INPUT_PULLUP);
pinMode(btok,INPUT_PULLUP);
pinMode(buzzer,OUTPUT);
digitalWrite(buzzer,LOW);
Wire.begin();
// set the initial time here:
// DS3231 seconds, minutes, hours, day, date, month, year
setDS3231time(0,0,0,6,29,7,24);
if ( u8g.getMode() == U8G_MODE_R3G3B2 ) {
u8g.setColorIndex(255); // white
}
else if ( u8g.getMode() == U8G_MODE_GRAY2BIT ) {
u8g.setColorIndex(3); // max intensity
}
else if ( u8g.getMode() == U8G_MODE_BW ) {
u8g.setColorIndex(1); // pixel on
}
else if ( u8g.getMode() == U8G_MODE_HICOLOR ) {
u8g.setHiColorByRGB(255,255,255);
}
}
void setDS3231time(byte second, byte minute, byte hour, byte dayOfWeek, byte
dayOfMonth, byte month, byte year)
{
// sets time and date data to DS3231
Wire.beginTransmission(DS3231_I2C_ADDRESS);
Wire.write(0); // set next input to start at the seconds register
Wire.write(decToBcd(second)); // set seconds
Wire.write(decToBcd(minute)); // set minutes
Wire.write(decToBcd(hour)); // set hours
Wire.write(decToBcd(dayOfWeek)); // set day of week (1=Sunday, 7=Saturday)
Wire.write(decToBcd(dayOfMonth)); // set date (1 to 31)
Wire.write(decToBcd(month)); // set month
Wire.write(decToBcd(year)); // set year (0 to 99)
Wire.endTransmission();
}
void readDS3231time(byte *second,
byte *minute,
byte *hour,
byte *dayOfWeek,
byte *dayOfMonth,
byte *month,
byte *year)
{
Wire.beginTransmission(DS3231_I2C_ADDRESS);
Wire.write(0); // set DS3231 register pointer to 00h
Wire.endTransmission();
Wire.requestFrom(DS3231_I2C_ADDRESS, 7);
// request seven bytes of data from DS3231 starting from register 00h
*second = bcdToDec(Wire.read() & 0x7f);
*minute = bcdToDec(Wire.read());
*hour = bcdToDec(Wire.read() & 0x3f);
*dayOfWeek = bcdToDec(Wire.read());
*dayOfMonth = bcdToDec(Wire.read());
*month = bcdToDec(Wire.read());
*year = bcdToDec(Wire.read());
}
void displayTime()
{
// retrieve data from DS3231
readDS3231time(&second, &minute, &hour, &dayOfWeek, &dayOfMonth, &month,
&year);
// send it to the serial monitor
Serial.print(hour, DEC);
// convert the byte variable to a decimal number when displayed
Serial.print(":");
if (minute<10)
{
Serial.print("0");
}
Serial.print(minute, DEC);
Serial.print(":");
if (second<10)
{
Serial.print("0");
}
Serial.print(second, DEC);
Serial.println(" ");
}
void loop(void) {
displayTime();
error = setpoint - suhu;
p = error * kp;
sumerr = error + errorx;
i = ki * sumerr;
d = kd * (error - errorx) ;
pid = p + i + d;
Serial.println(pid);
dimmer.setPower(pid); // name.setPower(0%-100%)
btokx = digitalRead(btok);
btupx = digitalRead(btup);
btdownx = digitalRead(btdown);
btsetx = digitalRead(btset);
sensors.requestTemperatures();
suhu = sensors.getTempCByIndex(0);
// picture loop
u8g.firstPage();
do {
draw();
} while( u8g.nextPage() );
if(btupx == 0){
menit = menit + 5;
}
if(btdownx == 0){
menit = menit - 5;
}
if(menit > 15){
menit = 15;
}
if(menit < 5){
menit = 5;
}
if(btsetx == 0){
setpoint = setpoint + 1;
}
if(btokx == 0){
setpoint = setpoint - 1;
}
if(setpoint > 41){
setpoint = 41;
}
if(setpoint < 36){
setpoint = 36;
}
errorx = error;
// rebuild the picture after some delay
delay(200);
if(minute == menit){
setDS3231time(0,0,0,6,29,7,24);
u8g.firstPage();
do {
} while( u8g.nextPage() );
digitalWrite(buzzer,HIGH);
delay(3000);
digitalWrite(buzzer,LOW);
selesai();
}
}
void draw(void) {
//float dua angka dibelakang koma
dtostrf(suhu, 4, 2, tmp_string1);
dtostrf(menit, 4, 0, tmp_string2);
dtostrf(setpoint, 4, 0, tmp_string3);
// graphic commands to redraw the complete screen should be placed here
u8g.setFont(u8g_font_unifont);
//u8g.setFont(u8g_font_osb21);
u8g.drawStr(0, 22, "suhu= ");
u8g.drawStr(60, 22, tmp_string1);
u8g.drawStr(0, 42, "menit= ");
u8g.drawStr(60, 42, tmp_string2);
u8g.drawStr(0, 62, "SP= ");
u8g.drawStr(60, 62, tmp_string3);
}
void selesai(void){
u8g.setFont(u8g_font_unifont);
u8g.drawStr(0, 22, "selesai");
dimmer.begin(NORMAL_MODE, OFF);
dimmer.setPower(0);
selesai();
}
c. VIDEO HASILNYA
No comments:
Post a Comment