본문 바로가기
정리, 공부/아두이노 & 회로구성

아두이노, 펠티어 ~ -40도 온도 PID컨트롤 + 액셀 데이터 키핑 + LCD스크린

by 쉬고 싶다 2022. 1. 29.
반응형

 

- 아두이노를 사용해

- 코드

- 회로

- 참고할 포스팅

 

 아두이노를 사용해

 

아두이노를 사용해 실험실에서 간단한 실험을 할 때, 테스트용으로 자동화를 하고자 했었습니다.

해당 내용을 담은 포스트 입니다 :)

이전까지 차근차근 단계를 밟아오며 단계별로 포스팅을 해두었기에,, 보고서 도움되겠다, 궁금하다 싶으다면 이전 포스팅을 참고 바랍니다 !.!

(페이지 하단에 관련 링크 달아두겠습니다.)

 

 코드

 

PID, PWM, LCD 라이브러리 사용

라이브러리 내용을 조금 포트/시스템에 맞게 수정

코드의 자세한 사항은 아래에 이전포스팅 링크를 달아두겠습니다 :)

코드를 짧게 이쁘게 짤 재주는 아직 없기에 깁니다 ..

스크롤 박스에 넣어두었으니 스크롤 내리며 참고 하시면 되겠습니다 :)

#include <PID_v1.h>
#include <PWM.h>
#include <LiquidCrystal_I2C.h>

//LCD
LiquidCrystal_I2C lcd(0x27, 16, 2);

//PID 
#define peltier 10
double Setpoint, Input, Output;
double Kp=5, Ki=10, Kd=4;
PID myPID(&Input, &Output, &Setpoint, Kp, Ki, Kd, REVERSE);
  //DIRECT: to increase the value which is decreasing automatically
  //REVERSE: to decrease the value which is increasing automatically
int start_Setpoint = -40;  
double start_Input = 5; 
double control = 0;

//PWM
//#define fan 9
int peltier_power = 0;
//int fan_power = 0;   //need one more PID setup or use mapping
int32_t frequency = 16000; //frequency (in Hz)

//Thermocouple
int Thermistor0 = 0;
int Thermistor1 = 1;
int Thermistor2 = 2;
int Thermistor3 = 3;
int V0, V1, V2, V3;

  // Static resistor or RT termistor resistance?
float SR = 100000;
float logR0, R0, T0, logR1, R1, T1, logR2, R2, T2, logR3, R3, T3;
double T_0, T_1, T_2, T_3;

  // ABC constant
float c1 = 1.028525291852400E-03 , c2 = 2.392327985577990E-04 , c3 = 1.562478971912460E-07;

void setup() {
  //PWM
  InitTimersSafe(); 
  bool success1 = SetPinFrequencySafe(peltier, frequency);
  //bool success2 = SetPinFrequencySafe(fan, frequency);
  
  #ifndef ESP8266
    while (!Serial);     // will pause Zero, Leonardo, etc until serial console opens
  #endif
  Serial.begin(9600);

  //LCD
  lcd.init();
  lcd.backlight();
  lcd.setCursor(0,0);

    //PWM: if the pin frequency was set successfully
  if(success1) {
    lcd.println("succeed freq1") ;  
  }
  /*if(success2) {
    lcd.print("succeed freq2") ;  
  }*/
  delay(1000);
  
  //PID
  Input = start_Input;
  Setpoint = start_Setpoint;
    //turn the PID on
  myPID.SetMode(AUTOMATIC);

  //Excel
  Serial.println("CLEARDATA");
  Serial.println("LABEL,Time,S-T3,PC-T2,PH(WB)-T1,PH-T0,peltier");
  //PH=Peltier Hot side, PC=Peltier Cold side, S=Sample Temperature
}

void loop() {

  // preparing the varialbes to calculate the temperature from volatage which get from thermistor
  V0 = analogRead(Thermistor0);
  V1 = analogRead(Thermistor1);
  V2 = analogRead(Thermistor2);
  V3 = analogRead(Thermistor3);
  R0 = SR * ((float)V0 / (1023.0-(float)V0));
  R1 = SR * ((float)V1 / (1023.0-(float)V1));
  R2 = SR * ((float)V2 / (1023.0-(float)V2));
  R3 = SR * ((float)V3 / (1023.0-(float)V3));
  logR0 = log(R0);
  logR1 = log(R1);
  logR2 = log(R2);
  logR3 = log(R3);

  // calculate the temperature from resistance which get from above
  T0 = (1.0 / (c1 + c2*logR0 + c3*logR0*logR0*logR0));
  T0 = T0 - 273.15; // K ==> C
  //T0 = (T0 * 9.0) / 5.0 + 32.0; // C --> F
  T1 = (1.0 / (c1 + c2*logR1 + c3*logR1*logR1*logR1));
  T1 = T1 - 273.15; // K ==> C
  //T1 = (T1 * 9.0) / 5.0 + 32.0; // C --> F
  T2 = (1.0 / (c1 + c2*logR2 + c3*logR2*logR2*logR2));
  T2 = T2 - 273.15; // K ==> C
  //T2 = (T2 * 9.0) / 5.0 + 32.0; // C --> F
  T3 = (1.0 / (c1 + c2*logR3 + c3*logR3*logR3*logR3));
  T3 = T3 - 273.15; // K ==> C
  //T3 = (T3 * 9.0) / 5.0 + 32.0; // C --> F
  
  //PID
  Input = T3;
  myPID.Compute();
  peltier_power = map(Output,0,255,0,200);
  //Driver input 0~150 has dramatic control value change
  //fan_power = map(peltier_power,0,255,150,255);
  //fan is start to rotate around 150
  
  //PWM
  pwmWrite(peltier, peltier_power);
  //pwmWrite(fan, fan_power);

  //Excel
  Serial.print("DATA,TIME,");
  Serial.print(T3);
  Serial.print(",");
  Serial.print(T2);
  Serial.print(",");
  /*Serial.print(Input);
  Serial.print(",");
  Serial.print(Setpoint);
  Serial.print(",");*/
  Serial.print(T1);
  Serial.print(",");
  Serial.print(T0);
  Serial.print(",");
  Serial.print(peltier_power);
  Serial.println(",");
  //Serial.print(fan_power);
  //Serial.println(",");
  
  //LCD
  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print(T3);
  lcd.print("/");
  lcd.print(T2); //holder(sample) temperature
  lcd.setCursor(0, 1);
  lcd.print(T1);
  lcd.print("/");
  lcd.print(T0);
  lcd.print(" C"); //temperature near peltier
  delay(200);
}

 

노이즈 제거 전, PID컨트롤로 -40도까지 도달하는 온도 그래프

 

 회로

 

회로도 이전에 써미스터, LCD 관련 회로는 포스팅해두었고, 링크는 아래에 있고,

팰티어 컨트롤을 위한 드라이버는 사용하시는 모터 드라이버 혹은 릴레이모듈의 데이터시트를 참고하셔야겠습니다 :)

 

<외부전원에 관한 포스팅>

2020.09.28 - [정리, 공부/아두이노 & 회로구성] - 아두이노, 외부전원, Barrel jack, 전선 처리/활용

 

아두이노, 외부전원, Barrel jack, 전선 처리/활용

- 아두이노에서 외부전원 사용 - 12V 10A 어댑터 전선 처리 - 5V 1A 어댑터 전선 처리  아두이노에서 외부전원 사용 2020/09/14 - [정리, 공부해요/아두이노] - 릴레이 모듈, 아두이노에 외부 전원(12V)이

setoo0922.tistory.com

해당 포스팅에 링크된 포스트 = 릴레이모듈 + 외부전원을 사용한 아두이노로 고전력 제품 제어

릴레이모듈에 연결된 방식을 참고하고 모터 드라이버도 해당 데이터시트를 참고하면 해당 회로를 연결할 만할 것입니다 !

 

<보통,, 모터드라이버에는>

1) 모터 드라이버에 시그널과 (최소한의)파워를 넣어줄 배선이 있고 (아두이노로 부터)

>> 이 사진에서 하나 헷갈리는게, 보통 제품에서 노란색이 시그널이고 빨간색이 +선인데,, 이 모터드라이버는 그 두 선이 반대였습니다 :( // 데이터시트에 나와있더군요 ..;

2) 모터 혹은 파워를 컨트롤할 제품(여기서는 팰티어)에 연결한 +, - 배선이 있으며

3) 고전력 제품(팰티어, 모터_BLCD, STEP 등)을 구동하는데 부족한 전력을 받을 외부전원 포트

 

가 있습니다 :)

 

 

 

참고할 포스팅

 

<써모커플, 써미스터, LCD디스플레이 ~ 코드,라이브러리 사용 & 회로>

2020.10.02 - [정리, 공부/아두이노 & 회로구성] - Thermocouple(열전대, 써모커플), 아두이노로 값 읽기, MAX31855 증폭기 사용

 

Thermocouple(열전대, 써모커플), 아두이노로 값 읽기, MAX31855 증폭기 사용

- Thermocouple (열전대) - Thermocouple amplifier (MAX31855, 왜 필요한지) - Thermocouple with arduino (회로 연결 & 코딩)  Thermocouple (열전대) 열전대에 관한 간단한 내용들은 아래 글을 참고하면 좋을..

setoo0922.tistory.com

2020.10.06 - [정리, 공부/아두이노 & 회로구성] - LCD display, 아두이노 우노, 기본연결/I2C/가변저항의 필요. 회로&코드

 

LCD display, 아두이노 우노, 기본연결/I2C/가변저항의 필요. 회로&코드

- LCD display - 기본연결 LCD (회로 & 코드) - I2C 탑재된 LCD (회로 & 코드) - 가변저항이 필요한 이유  LCD display address 찾기 LCD display를 사용하는 코드를 짜기 위해선 우선 우리가 가지고 있는 LCD가..

setoo0922.tistory.com

<액셀에 데이터 전달 & 저장 ~ PLX DAQ>

2020.10.12 - [정리, 공부/아두이노 & 회로구성] - Thermistor(써미스터) 아두이노 우노로 읽고, PLX DAQ 써서 엑셀에 실시간 기록/저장

 

Thermistor(써미스터) 아두이노 우노로 읽고, PLX DAQ 써서 엑셀에 실시간 기록/저장

- Thermistor - Thermistor, 얻은 전압에서 온도값 계산 - Multi Thermocouple - Arduino Uno - display on LCD - Thermistor로 읽은 값 실시간으로 Excel에 저장  Thermistor Thermistor 2020/09/10 - [정리, 공..

setoo0922.tistory.com

<PWM 코딩,라이브러리>

2020.10.14 - [정리, 공부/아두이노 & 회로구성] - 아두이노 우노, PWM 제어 라이브러리 활용

 

아두이노 우노, PWM 제어 라이브러리 활용

- PWM (Pulse Width Modulated) square wave - Arduino uno, PWM library download - PWM 제어 사용 코드 - led +, - 구분  PWM (Pulse Width Modulated) square wave 2020/09/24 - [정리, 공부해요/전기, 전자,..

setoo0922.tistory.com

<PID 코딩,라이브러리>

2020.10.18 - [정리, 공부/아두이노 & 회로구성] - Arduino uno, PID control, 아두이노 우노 PID 제어 라이브러리 활용

 

Arduino uno, PID control, 아두이노 우노 PID 제어 라이브러리 활용

- PID (proportional integral derivative control) control - PID 제어 라이브러리 다운 - PID 제어 코드 분석  PID (proportional integral derivative control) control 2020/09/20 - [정리, 공부해요/전기,..

setoo0922.tistory.com

 

 

 

반응형

댓글