WATER HEATER DS18S20

Water heater DS18S20

The code can be downloaded at:

https://github.com/triantara/ARDUINO-TRIANTARA/blob/main/Water%20heater.zip

#include <OneWire.h>
#include <EEPROM.h>
//#include <LiquidCrystal.h>
int DS18S20_Pin = 4;
OneWire ds(DS18S20_Pin);
int pinIncrease = 2; //pin increase button
int pinDecrease = 3; //pin decrease button
int pinHeater = 11; // go to relay input
int pinIND = 6; // LED heating indicator
int pinSetting = 10; // setting temperature button key
int SettingSuhu = 0;
byte BatasSuhu;

#include <Wire.h>
#include <LCD.h>
#include <LiquidCrystal_I2C.h>

#define I2C_ADDR 0x27
#define BACKLIGHT_PIN 3
#define En_pin 2
#define Rw_pin 1
#define Rs_pin 0
#define D4_pin 4
#define D5_pin 5
#define D6_pin 6
#define D7_pin 7

LiquidCrystal_I2C lcd(I2C_ADDR,En_pin,Rw_pin,Rs_pin,D4_pin,D5_pin,D6_pin,D7_pin,BACKLIGHT_PIN,POSITIVE);
LCD *myLCD = &lcd;

void setup(void) {
Serial.begin(9600);
lcd.begin(16, 2);

pinMode(pinIncrease, INPUT);
pinMode(pinDecrease, INPUT);
pinMode(pinSetting, INPUT);
pinMode(pinHeater, OUTPUT);
pinMode(pinIND, OUTPUT);
digitalWrite(pinIncrease, HIGH);
digitalWrite(pinDecrease, HIGH);
}

void loop() {

if(digitalRead(pinSetting) == HIGH){
float temperature = getTemp();
SettingSuhu = constrain(SettingSuhu, 0, 255);
setting();
EEPROM.write(1, SettingSuhu);
BatasSuhu = EEPROM.read(1);
heater();
Serial.print(BatasSuhu);
Serial.print(” “);
Serial.println(temperature);
//Serial.print(” “);
//Serial.println(temp0);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print(“SETT :”);
lcd.setCursor(7, 0);
lcd.print(BatasSuhu);
lcd.setCursor(0, 1);
lcd.print(“TEMP :”);
lcd.setCursor(7, 1);
lcd.print(temperature);
delay(100);
}
else if(digitalRead(pinSetting) == LOW){
float temperature = getTemp();
BatasSuhu = EEPROM.read(1);
heater();
Serial.print(BatasSuhu);
Serial.print(” “);
Serial.println(temperature);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print(“SETT :”);
lcd.setCursor(7, 0);
lcd.print(BatasSuhu);
lcd.setCursor(0, 1);
lcd.print(“TEMP :”);
lcd.setCursor(7, 1);
lcd.print(temperature);
delay(100);
}
}

void setting(){
if(digitalRead(pinIncrease) == LOW){
SettingSuhu++;
}else if(digitalRead(pinDecrease) == LOW){
SettingSuhu–;
}
}
void heater(){
float temperature = getTemp();
if(temperature < BatasSuhu){
digitalWrite(pinHeater, HIGH);
digitalWrite(pinIND, HIGH);
}else{
digitalWrite(pinHeater, LOW);
digitalWrite(pinIND, LOW);
}
}
float getTemp(){
//returns the temperature from one DS18S20 in DEG Celsius
byte data[12];
byte addr[8];
if ( !ds.search(addr)) {
//no more sensors on chain, reset search
ds.reset_search();
return -1000;
}
if ( OneWire::crc8( addr, 7) != addr[7]) {
Serial.println(“CRC is not valid!”);
return -1000;
}
if ( addr[0] != 0x10 && addr[0] != 0x28) {
Serial.print(“Device is not recognized”);
return -1000;
}
ds.reset();
ds.select(addr);
ds.write(0x44,1); // start conversion, with parasite power on at the end
byte present = ds.reset();
ds.select(addr);
ds.write(0xBE); // Read Scratchpad

for (int i = 0; i < 9; i++) { // we need 9 bytes
data[i] = ds.read();
}

ds.reset_search();

byte MSB = data[1];
byte LSB = data[0];
float tempRead = ((MSB << 8) | LSB); //using two’s compliment
float TemperatureSum = tempRead / 16;

return TemperatureSum;

}