garduino and fans

Hi, the following code is designed to turn on fans when my green house gets too hot. so far it works initially but then seems to fail and enter a resetting mode. Is it related to my use of delays or is it potentially related to the 4 relay being underpowered when three of the relays are on?

#include <OneWire.h>
#include <DallasTemperature.h>

#define ONE_WIRE_BUS 4

#define WATER_VALVE 6

#define FAN_OUT 5

#define FAN_IN 3


OneWire oneWire(ONE_WIRE_BUS);


DallasTemperature sensors(&oneWire);


DeviceAddress ambientThermometer = { 
0x28, 0x05, 0x15, 0x5C, 0x06, 0x00, 0x00, 0x40 };
DeviceAddress soilThermometer = { 
0x28, 0x43, 0x05, 0x5D, 0x06, 0x00, 0x00, 0x74 };

void setup(void)
{
  // start serial port
  Serial.begin(9600);
  // Start up the library
  sensors.begin();
  // set the resolution to 10 bit (good enough?)
  sensors.setResolution(ambientThermometer, 10);
  sensors.setResolution(soilThermometer, 10);
  //sensors.setResolution(dogHouseThermometer, 10);

  pinMode(WATER_VALVE, OUTPUT);
  pinMode(FAN_OUT, OUTPUT);
  pinMode(FAN_IN, OUTPUT);
  digitalWrite(FAN_OUT, LOW);
  digitalWrite(FAN_IN, LOW);
}

void printTemperature(DeviceAddress deviceAddress)
{
  float tempC = sensors.getTempC(deviceAddress);
  if (tempC == -127.00) {
    Serial.print("Error getting temperature");
  } else {
   // Serial.print("C: ");
   // Serial.print(tempC);
    Serial.print(" F: ");
    Serial.print(DallasTemperature::toFahrenheit(tempC));
  }
}

void loop(void)
{ 
  delay(2000);
  Serial.print("Getting temperatures...\n\r");
  sensors.requestTemperatures();
  
  Serial.print("ambient temperature is: ");
  printTemperature(ambientThermometer);
  Serial.print("\n\r");
  Serial.print("soil temperature is: ");
  printTemperature(soilThermometer);
  Serial.print("\n\r");
  Serial.print ("soil moisture ");
  Serial.println(analogRead(0));

//Read analog pin A0 and print it (prints newline)
  if(analogRead(0)>850)
//if soil humidity above - 550 on/off (change HIGH to LOW if needed) electrovalve

{

digitalWrite(WATER_VALVE, LOW);
delay(4000);

}

else

{

digitalWrite(WATER_VALVE, HIGH);
delay(4000);
}

  sensors.requestTemperatures();
  float ambientThermometerF = DallasTemperature::toFahrenheit(sensors.getTempC(ambientThermometer));
  float diff = ambientThermometerF;
 //Serial.print(diff);
 Serial.print("turn ");
 if ((diff) > 70) {
   Serial.println("fans on");
   digitalWrite(FAN_IN, HIGH);
   digitalWrite(FAN_OUT, HIGH);
   
 } else {
     Serial.println("fans off");
     digitalWrite(FAN_IN, LOW);
     digitalWrite(FAN_OUT, LOW);
 }
delay(6000);

}

Arduino resetting could be power related.

How do you power the Arduino, and how do you power the relays/valves/fans.
Leo..

arduino is powered by a 12v 2A wall plug and the fans are powered by 110 and 12v on sperate relays. the moisture sensor triggers a 12v water valve.

Is the 12volt supply plugged into the DC socket.
What else is powered from Arduino's 5volt supply.
How much current is that drawing.
Does the voltage regulator on the board get hot.
Leo..

Hi.

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?

Can you please post a copy of your sketch, using code tags?
They are made with the </> icon in the reply Menu.
See section 7 http://forum.arduino.cc/index.php/topic,148850.0.html

Tom.... :slight_smile:

As you probably surmised, power fluctuations is the single most common cause of a re-set.

if the relays are powered by the same 5v, there could be a dip. surprised no one offered that putting a cap on the input might help ride the dip.

another possibility is to use two power supplies, on for the Arduino logic and one for any peripheral that is powered by transistors and such. like a relay should be.

So i switched from the 12v wall adapter to an old usb phone charger which supplies power through the usb input on arduino and it seems to be working. Thanks.