Peltier + Relays + Temperature Sensor

Ok, so I’m a newbie here, and this is my longest code so far. Please excuse me any conceptual or vocabulary mistakes.
I don’t know what I’m doing wrong, but it doesn’t work. I’ve got two peltiers (with cooling fans and heatsinks), a temperature sensor (DS18B20), and a RGB LED that indicates the state of the system.

The relays control the peltiers. The peltier fans’ negative pole is attached to pins 11 and 12 , so that if any of the fans stops working, the peltiers are turned off to protect them from overheating.

I want to read the temperature from the sensor, and depending on that data, to turn both peltiers on, to turn just one on, or to turn both off. Also, if any of the fans stops, both peltiers are turned off. Let’s say I want to clasify into four states: Cooling (Blue LED, Both Peltiers on), Cool (Green LED, One peltier on), Cooled (Green LED, No peltiers on), STOP (RED Led, Peltiers off).

I tried to do this with some if statements, and also tried with while loops, but all it does is to go from one state to other constantly (from cooling to Stop). All the components work fine, I tried them separately and with independent code, but I just can’t get it to work… Sure there are easier ways to program this, but just can’t figure out. I attach the both codes. Let me know what you think.

//Febrero 2016
//Edurojolavanda

#include <OneWire.h> //Se importan las librerías
#include <DallasTemperature.h>

#define Pin 2 //Se declara el pin donde se conectará la DATA
const int rele1 = 3; //Declarar la salida del rele 1
const int rele2 = 4; //Declarar la salida del rele2
const int Fan1 = 11; //Declarar la entrada del ventilador de la placa 1
const int Fan2 = 12; //Declarar la entrada del ventilador de la placa 2
const int Rojo = 5;
const int Verde = 6;
const int Azul = 7;
int temperatura; //Crear la variable temperatura

OneWire ourWire(Pin); //Se establece el pin declarado como bus para la comunicación OneWire

DallasTemperature sensors(&ourWire); //Se instancia la librería DallasTemperature

void setup() {
  
pinMode(rele1, OUTPUT); //Declarar el pin del rele 1 como salida
pinMode(rele2, OUTPUT); //Declarar el pin del rele 2 como salida
pinMode(Fan1, INPUT);   //Declara el pin del ventilador 1 como entrada
pinMode(Fan2, INPUT);   //Declara el pin del ventilador 2 como entrada
pinMode(Rojo, OUTPUT);
pinMode(Verde, OUTPUT);
pinMode(Azul, OUTPUT);
Serial.begin(9600);
sensors.begin(); //Se inician los sensores

}

void loop() {
  
sensors.requestTemperatures(); //Prepara el sensor para la lectura
temperatura = sensors.getTempCByIndex(0); //Declarar la variable temperatura

if(Fan1 || Fan2 == LOW){
  digitalWrite(rele1, LOW);
  digitalWrite(rele2, LOW);
  digitalWrite(Verde, LOW);
  digitalWrite(Azul, LOW);
  digitalWrite(Rojo, HIGH);
}  
delay(1000);
if(temperatura > 5){            //Cuando temperatura sea mas que 5, Peltiers encendidos, y LED Azul
  digitalWrite(rele1, HIGH);
  digitalWrite(rele2, HIGH);
  digitalWrite(Verde, LOW);
  digitalWrite(Azul, HIGH);
  digitalWrite(Rojo, LOW);
}
if(temperatura <= 5){          //Cuando llegue a 5 o menos, apagar un peltier, y LED Verde
  digitalWrite(rele1, LOW);
  digitalWrite(rele2, HIGH);
  digitalWrite(Verde, HIGH);
  digitalWrite(Azul, LOW);
  digitalWrite(Rojo, LOW);}
if(temperatura <= 0){          //Cuando llegue a 0, apagar los dos peltier, y LED Verde
  digitalWrite(rele1, LOW);
  digitalWrite(rele2, LOW);
  digitalWrite(Verde, HIGH);
  digitalWrite(Azul, LOW);
  digitalWrite(Rojo, LOW);
}
  
Serial.print(temperatura); //Se lee e imprime la temperatura en grados Celsius
Serial.println(" grados Centigrados");
Serial.print(digitalRead(Fan1));
Serial.println("Fan1");
Serial.print(digitalRead(Fan2));
Serial.println("Fan2");
delay(250); //Se provoca un lapso de 5 segundos antes de la próxima lectura

}

and the “while” version:

//Febrero 2016
//Edurojolavanda

#include <OneWire.h> //Se importan las librerías
#include <DallasTemperature.h>

#define Pin 2 //Se declara el pin donde se conectará la DATA
const int rele1 = 3; //Declarar la salida del rele 1
const int rele2 = 4; //Declarar la salida del rele2
const int Fan1 = 11; //Declarar la entrada del ventilador de la placa 1
const int Fan2 = 12; //Declarar la entrada del ventilador de la placa 2
const int Rojo = 5;
const int Verde = 6;
const int Azul = 7;
int temperatura; //Crear la variable temperatura

OneWire ourWire(Pin); //Se establece el pin declarado como bus para la comunicación OneWire

DallasTemperature sensors(&ourWire); //Se instancia la librería DallasTemperature

void setup() {
  
pinMode(rele1, OUTPUT); //Declarar el pin del rele 1 como salida
pinMode(rele2, OUTPUT); //Declarar el pin del rele 2 como salida
pinMode(Fan1, INPUT);   //Declara el pin del ventilador 1 como entrada
pinMode(Fan2, INPUT);   //Declara el pin del ventilador 2 como entrada
pinMode(Rojo, OUTPUT);
pinMode(Verde, OUTPUT);
pinMode(Azul, OUTPUT);
Serial.begin(9600);
sensors.begin(); //Se inician los sensores

}

void loop() {
  
sensors.requestTemperatures(); //Prepara el sensor para la lectura
temperatura = sensors.getTempCByIndex(0); //Declarar la variable temperatura

while(Fan1 && Fan2 == LOW){
cool();
}

  digitalWrite(rele1, LOW);
  digitalWrite(rele2, LOW);
  digitalWrite(Verde, LOW);
  digitalWrite(Azul, LOW);
  digitalWrite(Rojo, HIGH);

Serial.print(temperatura); //Se lee e imprime la temperatura en grados Celsius
Serial.println(" grados Centigrados");
Serial.print(digitalRead(Fan1));
Serial.println("Fan1");
Serial.print(digitalRead(Fan2));
Serial.println("Fan2");
delay(1000); //Se provoca un lapso de 5 segundos antes de la próxima lectura

}
void cool(){
if(temperatura > 5){            //Cuando temperatura sea mas que 5, Peltiers encendidos, y LED Azul
  digitalWrite(rele1, HIGH);
  digitalWrite(rele2, HIGH);
  digitalWrite(Verde, LOW);
  digitalWrite(Azul, HIGH);
  digitalWrite(Rojo, LOW);
}
if(temperatura <= 5){          //Cuando llegue a 5 o menos, apagar un peltier, y LED Verde
  digitalWrite(rele1, LOW);
  digitalWrite(rele2, HIGH);
  digitalWrite(Verde, HIGH);
  digitalWrite(Azul, LOW);
  digitalWrite(Rojo, LOW);}
if(temperatura <= 0){          //Cuando llegue a 0, apagar los dos peltier, y LED Verde
  digitalWrite(rele1, LOW);
  digitalWrite(rele2, LOW);
  digitalWrite(Verde, HIGH);
  digitalWrite(Azul, LOW);
  digitalWrite(Rojo, LOW);
}
}

Please post a hand-drawn (not Fritzing) schematic diagram of the circuitry. I have no idea what this means:

The peltier fans' negative pole is attached to pins 11 and 12

The Arduino pins cannot be used to directly power motors or relays.

Actually, I have a relay module, with transistors and LEDs that indicate if active. I’ve just realize that the way I’m trying to detect if any fan stops, is wrong… Sorry for the messy drawing…

51gnjY2HceL.jpg

There are a lot of problems with that drawing. The temperature sensor can't be powered by 12V.

Without a separate sensor there is no way to know if the fans are actually working.

You could use a current Sensor to detect if the fan is working or not.
This sensor gives you an analog signal …

Ok, the fan thing is solved, I just replaced them for CPU cooling Fans (which include a sensor that reads the rpm). The problem now is the code, it works when the Fans are stopped, but the system doesn’t go back to the “normal” state:

//HUMO BAJO V1.0 Febrero 2016
//Edurojolavanda

#include <OneWire.h> //Se importan las librerías
#include <DallasTemperature.h>

#define Pin 7 //Se declara el pin donde se conectará la DATA
const int rele1 = 4; //Declarar la salida del rele 1
const int rele2 = 5; //Declarar la salida del rele2
const int Rojo = 9;
const int Verde = 10;
const int Azul = 11;
int temperatura; //Crear la variable temperatura

OneWire ourWire(Pin); //Se establece el pin declarado como bus para la comunicación OneWire

DallasTemperature sensors(&ourWire); //Se instancia la librería DallasTemperature
 
//Varibles used for calculations
int NbTopsFan; int Calc;
 
//The pin location of the sensor
int hallsensor = 2; typedef struct{
 
//Defines the structure for multiple fans and 
//their dividers 
char fantype;
unsigned int fandiv; }fanspec;
 
//Definitions of the fans
//This is the varible used to select the fan and it's divider,
//set 1 for unipole hall effect sensor
//and 2 for bipole hall effect sensor
fanspec fanspace[3]={{0,1},{1,2},{2,8}}; char fan = 1;
 
void rpm ()
//This is the function that the interupt calls
{ NbTopsFan++; }

void setup() {
pinMode(hallsensor, INPUT);
pinMode(rele1, OUTPUT); //Declarar el pin del rele 1 como salida
pinMode(rele2, OUTPUT); //Declarar el pin del rele 2 como salida
pinMode(Rojo, OUTPUT);
pinMode(Verde, OUTPUT);
pinMode(Azul, OUTPUT);
Serial.begin(9600);
sensors.begin(); //Se inician los sensores
attachInterrupt(0, rpm, RISING);

}

void loop() {
  
sensors.requestTemperatures(); //Prepara el sensor para la lectura
temperatura = sensors.getTempCByIndex(0); //Declarar la variable temperatura
//Set NbTops to 0 ready for calculations
NbTopsFan = 0;
 
//Enables interrupts
sei();
 
//Wait 1 second
delay (1000);
 
//Disable interrupts
cli();
 
//Times NbTopsFan (which is apprioxiamately the fequency the fan 
//is spinning at) by 60 seconds before dividing by the fan's divider
Calc = ((NbTopsFan * 60)/fanspace[fan].fandiv);
 
//Prints the number calculated above
Serial.print (Calc, DEC);
 
//Prints " rpm" and a new line
Serial.print (" rpm\r\n");

while(Calc > 0){
cool();
}

  digitalWrite(rele1, HIGH);
  digitalWrite(rele2, HIGH);
  digitalWrite(Verde, LOW);
  digitalWrite(Azul, LOW);
  digitalWrite(Rojo, HIGH);

Serial.print(temperatura); //Se lee e imprime la temperatura en grados Celsius
Serial.println(" grados Centigrados");
delay(1000); //Se provoca un lapso de 5 segundos antes de la próxima lectura

}
void cool(){
if(temperatura > 5){            //Cuando temperatura sea mas que 5, Peltiers encendidos, y LED Azul
  digitalWrite(rele1, LOW);
  digitalWrite(rele2, LOW);
  digitalWrite(Verde, LOW);
  digitalWrite(Azul, HIGH);
  digitalWrite(Rojo, LOW);
}
if(temperatura <= 5){          //Cuando llegue a 5 o menos, apagar un peltier, y LED Verde
  digitalWrite(rele1, HIGH);
  digitalWrite(rele2, LOW);
  digitalWrite(Verde, HIGH);
  digitalWrite(Azul, LOW);
  digitalWrite(Rojo, LOW);}
if(temperatura <= 0){          //Cuando llegue a 0, apagar los dos peltier, y LED Verde
  digitalWrite(rele1, HIGH);
  digitalWrite(rele2, HIGH);
  digitalWrite(Verde, HIGH);
  digitalWrite(Azul, LOW);
  digitalWrite(Rojo, LOW);
}
void loop();
}

I changed the code to the While function.