Let me start off my saying that this is my first project, so bare with me.
I am looking for advice / verification on my code and circuit. Everything works, but I am not sure if I may be missing something such as a diode, capacitor, extra resistance, etc.
My project is essentially turning a mini-freezer into a fridge/freezer using a digital temperature sensor (I was going to use a straight thermocouple, but I didn't want to design the instrumentation circuit).
I have attached my circuit diagram (sorry if it's difficult to read, never made one before), as well as my code. Please let me know if there's something I'm missing or need to improve.
Couple notes on this diagram: the temp sensor is digital, I just used a placement block to simulate it. The Vout is actually the digital signal output. And the motor is an AC pump, again a placement.
#include <OneWire.h>
#include <LiquidCrystal.h>
//pin declaration
int analogTempSet = A0; //temperature pot value
int DS18S20_Pin = 2; //DS18S20 Signal pin on D2
int relayPin = 3; //initiating the relay pin on D3
LiquidCrystal lcd(4, 5, 10, 11, 12, 13);
byte degree[8] = { //making the degree symbol
0b00110,
0b01001,
0b01001,
0b00110,
0b00000,
0b00000,
0b00000,
0b00000
};
//variable declaration
float tempValue = 0;
float setTemp = 0;
unsigned long currentSecs = 0; //time since the relay was last powered
unsigned long previousSecs = 0;
int relayPowerInterval = 10; //interval that the pump should take before turning on again (in seconds)
OneWire ds(DS18S20_Pin);
void setup() {
pinMode(relayPin,OUTPUT);
lcd.createChar(0, degree);
lcd.begin(16,2);
Serial.begin(9600);
}
void loop() {
currentSecs = millis() / 1000; //the time the relay has been active in seconds
//set temp reading (pot input)
tempValue = analogRead(analogTempSet); //read the current setting
//tempSet = map(tempValue,0,1023,-220,50) / 10.0; //map the pot to temp range
setTemp = map(tempValue,0,1023,150,340)/10.0; //TEST LINE
//curret temp reading (sensor return)
float currentTemp = getTemp();
if (currentTemp > setTemp ) {
//this is to prevent the pump from switching on and off too frequently
if ((unsigned long)(currentSecs - previousSecs) >= relayPowerInterval){
digitalWrite(relayPin, HIGH); //power the relay coil
previousSecs = currentSecs;
}
}
else{
digitalWrite(relayPin, LOW);
}
lcd.setCursor(0,0);
lcd.print("Set: ");
lcd.print(setTemp, 1);
lcd.write(byte(0));
lcd.print("C");
lcd.setCursor(0,1);
lcd.print("Current: ");
lcd.print(currentTemp, 1);
lcd.write(byte(0));
lcd.print("C");
Serial.println(setTemp);
Serial.println(currentTemp);
// Serial.println((currentSecs-previousSecs));
delay(1000); // delay in between reads for stability
}
//returns the temperature from one DS18S20 in DEG Celsius
float getTemp(){
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;
}