Relay controlling DC 12V computer fan switching on/off randomly

I am controlling 2 computer fans controlled by 2 relays to provide airflow to a garden project I have. Both fans are activated at the same time, just by 2 separate relays. My problem is the relays switching on and off an average of 5 times a minute for milliseconds each switch. I haven’t been able to find any solutions online which is why I’m coming here. Thank you.

Here is my code the bulk of it is controlling an RTC clock

#include "Wire.h"
#include "ds3231.h"
#include "rtc_ds3231.h"
#include "LiquidCrystal_I2C.h"
#include "dht.h"

dht DHT;

int in1 = 4;
int in2 = 5;
int in3 = 6;
int in4 = 7;

  LiquidCrystal_I2C lcd(0x3F, 20, 4);

#define DHT22_PIN 8
#define Moisture A0
#define BUFF_MAX 128

  uint8_t time[8];
  char recv[BUFF_MAX];
  unsigned int recv_size = 0;
  unsigned long prev, interval = 5000;

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////

void setup()
{

  pinMode(in1, OUTPUT);
  digitalWrite(in1, HIGH);
  pinMode(in2, OUTPUT);
  digitalWrite(in2, HIGH);
  pinMode(in3, OUTPUT);
  digitalWrite(in3, HIGH);
  pinMode(in4, OUTPUT);
  digitalWrite(in4, HIGH);

  Serial.begin(9600);
  Wire.begin();
  DS3231_init(DS3231_INTCN);
  memset(recv, 0, BUFF_MAX);
  Serial.println("GET time");
  lcd.init();                      // initialize the lcd 
  lcd.backlight();
  //Serial.println("Setting time");
  //parse_cmd("T300714422122016",16);
}

void loop()
{
  int chk = DHT.read22(DHT22_PIN);

  int Moisture = analogRead(A0);

       Wire.begin();
       lcd.setCursor(0,1);
       lcd.print("Moisture     =   ");
       lcd.println(Moisture);  
       lcd.setCursor (0,0);        
       lcd.print("Temperature  = ");
       lcd.println(DHT.temperature);
       lcd.setCursor (0,2);        
       lcd.print("Humidity     = ");
       lcd.println(DHT.humidity);

   if (DHT.humidity < 50.00) 
       {
       digitalWrite(in1, LOW);
       digitalWrite(in2, LOW);
       } 
       else 
       {
       digitalWrite(in1, HIGH);
       digitalWrite(in2, HIGH);
       }

   if (Moisture < 800) 
       {
       digitalWrite(in3, LOW);
       digitalWrite(in4, HIGH);
       } 
       else 
       {
       digitalWrite(in3, HIGH);
       digitalWrite(in4, LOW);
       }         

  char in;
  char buff[BUFF_MAX];
  unsigned long now = millis();
  struct ts t;

      // show time once in a while
  if ((now - prev > interval) && (Serial.available() <= 0)) {
      DS3231_get(&t); //Get time
      
      lcd.setCursor(12,3); //Go to second line of the LCD Screen
      lcd.print(t.hour);
      lcd.print(":");
      if(t.min<10)
      {
        lcd.print("0");
      }
      lcd.print(t.min);
      lcd.print(":");
      if(t.sec<10)
      {
        lcd.print("0");
      }
      lcd.print(t.sec);
      
      prev = now;
  }

  if (Serial.available() > 0) {
      in = Serial.read();

      if ((in == 10 || in == 13) && (recv_size > 0)) {
          parse_cmd(recv, recv_size);
          recv_size = 0;
          recv[0] = 0;
      } else if (in < 48 || in > 122) {;       // ignore ~[0-9A-Za-z]
      } else if (recv_size > BUFF_MAX - 2) {   // drop lines that are too long
          // drop
          recv_size = 0;
          recv[0] = 0;
      } else if (recv_size < BUFF_MAX - 2) {
          recv[recv_size] = in;
          recv[recv_size + 1] = 0;
          recv_size += 1;
      }

  }
}

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////

void parse_cmd(char *cmd, int cmdsize)
{
  uint8_t i;
  uint8_t reg_val;
  char buff[BUFF_MAX];
  struct ts t;

  //snprintf(buff, BUFF_MAX, "cmd was '%s' %d\n", cmd, cmdsize);
  //Serial.print(buff);

  // TssmmhhWDDMMYYYY aka set time
  if (cmd[0] == 84 && cmdsize == 16) {
      //T355720619112011
      t.sec = inp2toi(cmd, 1);
      t.min = inp2toi(cmd, 3);
      t.hour = inp2toi(cmd, 5);
      t.wday = inp2toi(cmd, 7);
      t.mday = inp2toi(cmd, 8);
      t.mon = inp2toi(cmd, 10);
      t.year = inp2toi(cmd, 12) * 100 + inp2toi(cmd, 14);
      DS3231_set(t);
      Serial.println("OK");
  } else if (cmd[0] == 49 && cmdsize == 1) {  // "1" get alarm 1
      DS3231_get_a1(&buff[0], 59);
      Serial.println(buff);
  } else if (cmd[0] == 50 && cmdsize == 1) {  // "2" get alarm 1
      DS3231_get_a2(&buff[0], 59);
      Serial.println(buff);
  } else if (cmd[0] == 51 && cmdsize == 1) {  // "3" get aging register
      Serial.print("aging reg is ");
      Serial.println(DS3231_get_aging(), DEC);
  } else if (cmd[0] == 65 && cmdsize == 9) {  // "A" set alarm 1
      DS3231_set_creg(DS3231_INTCN | DS3231_A1IE);
      //ASSMMHHDD
      for (i = 0; i < 4; i++) {
          time = (cmd[2 * i + 1] - 48) * 10 + cmd[2 * i + 2] - 48; // ss, mm, hh, dd
      }
      uint8_t flags[5] = { 0, 0, 0, 0, 0 };
      DS3231_set_a1(time[0], time[1], time[2], time[3], flags);
      DS3231_get_a1(&buff[0], 59);
      Serial.println(buff);
  } else if (cmd[0] == 66 && cmdsize == 7) {  // "B" Set Alarm 2
      DS3231_set_creg(DS3231_INTCN | DS3231_A2IE);
      //BMMHHDD
      for (i = 0; i < 4; i++) {
          time = (cmd[2 * i + 1] - 48) * 10 + cmd[2 * i + 2] - 48; // mm, hh, dd
      }
      uint8_t flags[5] = { 0, 0, 0, 0 };
      DS3231_set_a2(time[0], time[1], time[2], flags);
      DS3231_get_a2(&buff[0], 59);
      Serial.println(buff);
  } else if (cmd[0] == 67 && cmdsize == 1) {  // "C" - get temperature register
      Serial.print("temperature reg is ");
      Serial.println(DS3231_get_treg(), DEC);
  } else if (cmd[0] == 68 && cmdsize == 1) {  // "D" - reset status register alarm flags
      reg_val = DS3231_get_sreg();
      reg_val &= B11111100;
      DS3231_set_sreg(reg_val);
  } else if (cmd[0] == 70 && cmdsize == 1) {  // "F" - custom fct
      reg_val = DS3231_get_addr(0x5);
      Serial.print("orig ");
      Serial.print(reg_val,DEC);
      Serial.print("month is ");
      Serial.println(bcdtodec(reg_val & 0x1F),DEC);
  } else if (cmd[0] == 71 && cmdsize == 1) {  // "G" - set aging status register
      DS3231_set_aging(0);
  } else if (cmd[0] == 83 && cmdsize == 1) {  // "S" - get status register
      Serial.print("status reg is ");
      Serial.println(DS3231_get_sreg(), DEC);
  } else {
      Serial.print("unknown command prefix ");
      Serial.println(cmd[0]);
      Serial.println(cmd[0], DEC);
  }
}

Welcome to the forum!

Please edit your post and place your code within the code tags (</>) so it is much easier to read.

You may also get more help if you post your circuit diagram- hand-drawn is fine. It may be that you dont have enough power to properly drive your relays.

Why are all your pins labeled "inx" meaning they are input? But some you set to output. Meaningful names are the first aid in debugging.

Paul

rw950431:
Welcome to the forum!

Please edit your post and place your code within the code tags (</>) so it is much easier to read.

You may also get more help if you post your circuit diagram- hand-drawn is fine. It may be that you dont have enough power to properly drive your relays.

Thanks for responding. Here is a blueprint I made as part of my project, sorry the fans are not represented, Fritzing doesn’t have any. I am using a Seeed Studio Relay Shield V3.0 and the relays are connected through pins 4-7. The problem exists in the code since I switched the inputs for the LEDs and Fans to check if it was the relays, but the problem persisted.

What IO pins are connected to the relay coils? Your wiring diagram doesnt seem to match your code- where are the connections to IO pins 4-7?

How is this project powered.
How are the relay coils powered.
12volt (fan supply) on the DC socket or V-in is too much for the Uno's onboard 5volt regulator if it has to power the LCD and two relay coils.
The Arduino could reboot when the regulator overheats internally (if you're lucky).
Leo..

Hi,

Can you please post a copy of your circuit, a hand drawn circuit in jpg, png will be good.

Also a picture of your project so we can see your component layout.

Thanks.. Tom... :slight_smile: