// After running the simulator, click on the DS18B20 chip to change the temperature
// Chip by bonnyr, source code: https://github.com/bonnyr/wokwi-ds1820-custom-chip/
#include <OneWire.h>
#include <DallasTemperature.h>
#include <LiquidCrystal_I2C.h>
#include "EncButton.h"
#include "PIDController.h"
#define LED1 8
#define LED2 9
#define LED3 11
#define BTN1 5
#define BTN2 6
#define BTN3 7
#define CLK 2
#define DT 3
#define SW 4
#define NTC_PIN A0
#define outputPin 10
#define mosfet_pin 12
#define __Kp 30
#define __Ki 0.7
#define __Kd 200
EncButton en(CLK, DT, SW);
OneWire oneWire(10);
DallasTemperature sensor(&oneWire);
LiquidCrystal_I2C lcd(0x3F,20,4);
const float BETA = 3950;
PIDController pid;
float readNTCHandler()
{
int analogValue = analogRead(NTC_PIN);
float celsius = 1 / (log(1 / (1023. / analogValue - 1)) / BETA + 1.0 / 298.15) - 273.15;
return celsius;
}
int set_temperature = 1;
int clockPin;
int clockPinState;
float debounce = 0;
int encoder_btn_count = 0;
float temperature_value_c = 0.0;
void setup() {
lcd.init();
pinMode(mosfet_pin, OUTPUT); // MOSFET output PIN
pid.begin(); // initialize the PID instance
pid.setpoint(150); // The "goal" the PID controller tries to "reach"
pid.tune(__Kp, __Ki,__Kd); // Tune the PID, arguments: kP, kI, kD
pid.limit(0, 255); // Limit the PID output between 0 and 255, this is important to get rid of integral windup!
en.setBtnLevel(HIGH);
en.setEncType(EB_STEP4_LOW);
}
void set_temp()
{
if (encoder_btn_count == 2) // check if the button is pressed twice and its in temperature set mode.
{
lcd.clear(); // clear the display
lcd.setCursor(16, 0); // set the diplay cursor
lcd.print("Set Temp."); // Print Set Temp. on the display
lcd.setCursor(45, 25); // set the cursor
lcd.print(set_temperature);// print the set temperature value on the display
}
}
void read_encoder() // In this function we read the encoder data and increment the counter if its rotaing clockwise and decrement the counter if its rotating counter clockwis
{
if (en.leftH()) {
Serial.println("left"); // if this condition is true then the encoder is rotaing counter clockwise and we decremetn the counter
set_temperature = set_temperature - 3; // decrmetn the counter.
if (set_temperature < 1 )set_temperature = 1; // if the counter value is less than 1 the set it back to 1
}
if(en.rightH()){
Serial.println("right");
set_temperature = set_temperature + 3;
if (set_temperature > 150 ) set_temperature = 150;
}
if (en.press()) //If we detect LOW signal, button is pressed
{
if ( millis() - debounce > 80) { //debounce delay
encoder_btn_count++; // Increment the values
if (encoder_btn_count > 2) encoder_btn_count = 1;
}
debounce = millis(); // update the time variable
}
}
void loop(void) {
read_encoder(); //Call The Read Encoder Function
set_temp();
sensor.requestTemperatures();
float temperature_value_c = sensor.getTempCByIndex(0);
int output = pid.compute(temperature_value_c);
analogWrite(mosfet_pin, output);
pid.setpoint(set_temperature);
lcd.setCursor(16, 0);
lcd.print("Cur Temp.");
lcd.setCursor(45, 25);
lcd.print(temperature_value_c);
lcd.display(); // Оновлення виводу на LCD дисплеї
Serial.print(temperature_value_c); // Виведення значення температури в консоль
Serial.print(" "); // Пропуск пустого місця
Serial.println(output); // Виведення обчисленого значення в консоль
delay(200);
}
Loading
ds18b20
ds18b20