SOLVED power sucking code

Hi,

Have this water cooler project where I want to keep water at the T° of the tap with minimum usage of water.

Setup is reading two T°and controlling a valve through a relay depending on the difference between these T°. Quite simple.

Problem; no power out of arduino! code working perfectly, even 5v at the output pin when it comes HIGH! But no amperage! (that’s my guess).

here are the steps:

start
open valve to purge
close valve
set ref. T°
wait (5 s)
if not in purge interval, monitor temperature (40 seconds)
if in purge interval, purge(10 seconds)

all goodl except from power issue.

here’s the code

code based on thermistor code from Ladyadas'  site http://www.ladyada.net/learn/sensors/thermistor.html
//analog pin
#define THERMISTORPIN2 A0
#define THERMISTORPIN3 A1
// resistance at 25 *C
#define THERMISTORNOMINAL 10000
// temp. for nom. resistance
#define TEMPERATURENOMINAL 25 
#define NUMSAMPLES 5
//beta coefficient of thermistor 
#define BCOEFFICIENT 3950
//value of 'other' resistor
#define SERIESRESISTOR 10000

#include <LiquidCrystal.h>

LiquidCrystal lcd(11, 12, 16, 17, 18, 19);

int ledPin[] = {2, 3, 4, 5};       
int ledState[] = {2, 3, 4, 5};    
int buttonPin[] = {6, 7, 8, 9};  
int relayPin[] = {10};
float temp[] = {2, 3};      // [0] is static [1] is live
float steinhart[] = {2, 3}; //temp = steinhart, didn't want to change Ladyadas' code for futur reference
float tempGauge;           //after purge, T° stored in this variable.
float diff;                     //T° difference temp[0] and  "tempGauge"
int samples2[NUMSAMPLES];
int samples3[NUMSAMPLES];

unsigned long previousTemper2Millis  =  0;
unsigned long previousTemper3Millis  =  0;
unsigned long previousPurgeMillis      =  0;
unsigned long previousCycleMillis       =  0;
unsigned long previousMonitorMillis   =  0;
long previousMillis  = 15000;

long purgeInterval    = 10000;
long cycleInterval     = 40000;
long interval           =  1000;
long intervalTemper =    10;

void setup(void) {
  lcd.begin(16, 2);
  lcd.setCursor(0,0);
  Serial.begin(9600);
  analogReference(EXTERNAL);

  pinMode(relayPin[0], OUTPUT);

  for(int f = 0; f < 4; f++){
   pinMode(buttonPin[f], INPUT);
  }

  for(int g = 0; g < 4; g++){
   pinMode(ledState[g], LOW);
  }
  for(int h = 0; h < 4; h++){
   pinMode(ledPin[h], OUTPUT);
  }
}

void temper2(){                         //thermistor code from ladyada (minor changes)
  unsigned long currentTemper2Millis = millis();
  uint8_t i;
  float average2;
  for (i=0; i< NUMSAMPLES; i++) {               // take N samples in a row, with a slight delay
   samples2[i] = analogRead(THERMISTORPIN2);
   if(currentTemper2Millis - previousTemper2Millis > intervalTemper) { //changed delay() to millis()
     previousTemper2Millis = currentTemper2Millis;
   }
  }
  average2 = 0;
  for (i=0; i< NUMSAMPLES; i++) {
     average2 += samples2[i];
  }
  average2 /= NUMSAMPLES;                    //Serial.print("Average2 analog reading "); Serial.println(average2);
  average2 = 1023 / average2 - 1;            // convert the value to resistance
  average2 = SERIESRESISTOR / average2;  //Serial.print("Thermistor resistance2 "); Serial.println(average2);
  steinhart[0] = average2 / THERMISTORNOMINAL;   // (R/Ro)
  steinhart[0] = log(steinhart[0]);                    // ln(R/Ro)
  steinhart[0] /= BCOEFFICIENT;                      // 1/B * ln(R/Ro)
  steinhart[0] += 1.0 / (TEMPERATURENOMINAL + 273.15); // + (1/To)
  steinhart[0] = 1.0 / steinhart[0];                 // Invert
  steinhart[0] -= 273.15;                              // convert to *C

  temp[0] = steinhart[0];                              //temp[0] = pin 2 fresh water in, becomes static reference
}

void temper3(){
  unsigned long currentTemper3Millis = millis();
  uint8_t j;
  float average3;
  for (j=0; j< NUMSAMPLES; j++)
  {
   samples3[j] = analogRead(THERMISTORPIN3);
   if(currentTemper3Millis - previousTemper3Millis > intervalTemper) {
     previousTemper3Millis = currentTemper3Millis;
   }
  }

  average3 = 0;
  for (j=0; j< NUMSAMPLES; j++) {
   average3 += samples3[j];
  }
  average3 /= NUMSAMPLES;
  average3 = 1023 / average3 - 1;
  average3 = SERIESRESISTOR / average3;
  steinhart[1] = average3 / THERMISTORNOMINAL;
  steinhart[1] = log(steinhart[1]);
  steinhart[1] /= BCOEFFICIENT;
  steinhart[1] += 1.0 / (TEMPERATURENOMINAL + 273.15);
  steinhart[1] = 1.0 / steinhart[1];
  steinhart[1] -= 273.15;

  temp[1] = steinhart[1];               //temp[1] = pin 3 reservoir, live
}

void flow(void){                    //if temperature differential  > 0, relay activated 
  if(diff > 0) {                                       
   Serial.println("Cooling process!");
      lcd.clear();
      lcd.print("Cooling "); lcd.print("Re="); lcd.print(temp[1]);
      lcd.setCursor(0,1);
      lcd.print("Process ");  lcd.print("Ga="); lcd.print(tempGauge);
      digitalWrite(relayPin[0], HIGH);
 
  }
  if(diff <= 0){                  //if temperature differential  < 0, relay -de-activated 
   Serial.println("Temperature ok");
     lcd.clear();
     lcd.print("Temp    "); lcd.print("Re="); lcd.print(temp[1]);
     lcd.setCursor(0,1);
     lcd.print("ok      ");  lcd.print("Ga="); lcd.print(tempGauge);
     digitalWrite(relayPin[0], LOW);
  }
}

void tempgauge(){
  tempGauge = temp[0];              // tempGauge becomes the T° reference
}

void differential(void){                 // temperature difference after purge
  diff = temp[1] - tempGauge;      // temp[1] = live thermistor
}

void loop(void) {
  lcd.setCursor(0, 0);
  temper2();  //(reference)
  temper3();  //(live)

  unsigned long currentMillis = millis();
  unsigned long currentFirstPurgeMillis = millis();
  unsigned long allMillis = millis();

  if(allMillis < 15000){                                                                              //time window for start purge
   if((currentFirstPurgeMillis >= 5000) && (currentFirstPurgeMillis <= 5001)){    //currentFirstPurgeMillis == 5000 wasn't working
     differential();                                                                                  
     if((currentFirstPurgeMillis >= 5000)&&(currentFirstPurgeMillis < 10000)){    
     digitalWrite(relayPin[0], HIGH);                                             
     Serial.println("purge session #1");                                        
     lcd.clear();                                                               
     lcd.print("purge session #1");                                               
     }                                                                            
   }                                                                             
 
   if((currentFirstPurgeMillis >= 10000) && (currentFirstPurgeMillis <= 10001)){  //currentFirstPurgeMillis == 10000 wasn't working 
     if((currentFirstPurgeMillis >= 10000)&&(currentFirstPurgeMillis < 15000)){
     digitalWrite(relayPin[0], LOW);
     Serial.println("purge session #1 is over");
     lcd.clear();
     lcd.print("purge session #1");
     lcd.setCursor(0,1);
     lcd.print("is over");
     tempgauge();                         //purge is over, time to set the reference
    }
   }
  }

  if(allMillis > 15000){                                                 //start purge is over, begin monitoring
   if(currentMillis >= previousCycleMillis + cycleInterval){   //cycle purge start
    previousCycleMillis   += (cycleInterval + purgeInterval);
    previousMonitorMillis += (cycleInterval + purgeInterval);
    digitalWrite(relayPin[0], HIGH);
    Serial.println("purge session");
    lcd.clear();
    lcd.print("purge session");
   }
 
   if(currentMillis >= (previousPurgeMillis + cycleInterval + purgeInterval)){   //cycle purge ends and reference T° set
    previousPurgeMillis += (cycleInterval + purgeInterval);
    tempgauge();
    Serial.print("///////Reference T* Set/////// "); Serial.print(temp[0]); Serial.println(" *C");
    digitalWrite(relayPin[0], LOW);
    Serial.println("Purge is over");
    lcd.clear();
   }
 
   if(currentMillis >= previousMillis + interval){ 
    differential();
    previousMillis += interval;
    Serial.print("                                          Reservoir (T3) "); Serial.print(temp[1]);   Serial.println(" *C");
    Serial.print("                                             Entree (T2) "); Serial.print(temp[0]);   Serial.println(" *C");
    Serial.print("   Fresh water comparative temperature after purge (T2s) "); Serial.print(tempGauge); Serial.println(" *C"); 
    Serial.print("                   differential: reservoir - fresh water "); Serial.print(diff)     ; Serial.println(" *C");   Serial.println();
    if((currentMillis > previousMonitorMillis) && (currentMillis < (previousMonitorMillis + cycleInterval))){
     flow();          // go set the valve open or closed depending on the T° differential
    }
   }
  }
}

already checked:

connect relay to Arduino, upload “blink” code, relay blinks, no problem.
connect an LED to Arduino, upload my “cooler” code, very dim glow.
strip the code from all the Serial communication and lcd.print, problem remains.

Ideas?

But no amperage!

Any milliamperage?
What are you driving?

BTW,buttonPin has only four entries.
You may wish to check ledState too.

I connected an LED to the output pin, that dimly glows, and my multimeter doesn't read anything (it's quite cheap though).

buttonPin, ledPin, ledState are declared but not in use yet, but you're right, I corrected them.

int relayPin[] = {10};

A one element array? Why?

I connected an LED to the output pin, that dimly glows, and my multimeter doesn't read anything (it's quite cheap though).

Would that be pin 10? Do you know that pin 10 is HIGH when you perform this reading? Do you have a current limiting resistor with that LED, or have you burnt out the pin?

the one element array is because I plan to add elements later on, it's just preparation.

And yes it's pin 10, sorry about the lack of info but I had to cut out all of my comments because of the character limitation of the forum :wink:

for the current limiting resistor, I have a 500ohm resistor in series with the led.

the pin is ok, I just uploaded the blink sketch to see, it's all good.

You mentioned a relay.
In my experience, these do not 'blink', so how are you driving this?

You mentioned a relay.
In my experience, these do not 'blink', so how are you driving this?

when pin 10 is HIGH, I can hear the contact inside the relay click

when pin 10 is LOW, I can hear the contact inside the relay unclick

I also have a test circuit with an LED being turned on and off by the relay, everything working good.

this way I know the board has no problem either with the pin or with the relay

so yes, it't not really blinking, we could say it's ticking. but the LED driven by the relay is blinking though...

Does the relay coil have a snubber diode?

If its not onboard the relay, it doesn't, I drive it directly

here is the datasheet for the relayhttp://www.alliedelec.com/Images/Products/Datasheets/BM/OMRON_ELECTRONICS_LLC/821-0379.PDF

But, as I said, I have been clicking it for a while with the blink code, same pin, and it was ok, but I will go study those snubber and install one.

Could a different code give a different result that would require a snubber?

So, for the snubber diode,

from what I get is that, for inductive loads, It needs to be installed in parallel to the relay so that the current can flow back to the source as in fig.A to dissipate the spikes created when the circuit is switched.

I have 3 kind of diodes at hand, but I don't know how to calculate which one is best, any help on that as well???

they are :1N4001, 1N4148, 1N914

the 1n4001 can handle 1 amp, 50V, that seems nice!?

hmmm weird,

played around with the diode(1n4001),

thing started working,

took the diode away, continued to work...

Now it's working...

thanks I Guess ; )