12v Solenoid valves with arduino

Hello, I am new to Arduino, I am trying to make a watering system with Arduino. I have 8 12v solenoid valves. I power them from an old PSU that is working. I have programmed temporarily my code using a soil moisture sensor, a DHT22 (is missing from the diagram) for temperature and humidity. The solenoids are normally closed. I bought a 12v relay board and connect them all as you can see in the picture below. When I turn it on all valves start working, either I program to
turn on or off the valve. I am thinking that I didn't find better relay module with voltage protection or any other kind, maybe because was a cheap it could be faulty, I don't know. Any help I would appreciate it!

Hello tousisd

Welcome to the worldbest Arduino forum ever.

What is the malfunction of the depicted system?

Many relays have active low inputs (relay turns on with low signal). An pin, when set to OUTPUT defaults to LOW so the relays turn on until the user issues a digitalWrite(pin, HIGH); to turn the relay off.

Are the solenoid valves DC loads? If so, why not just use a MOSFET driver for each? It is more efficient since the MOSFET only needs current to flow, briefly, when the MOSFET is turned on (to charge gate capacitance).

My problem is I want to open the water valves separately each one, like 1th opens 8minutes then 1th closes, 2th opens 8minutes then 2th closes, 3th ... etc till 8th valve. but when I turn it on they are all open at the same time. I am sorry I forgot to put the code:

=====


#include <DHT.h>

// DHT22 ASAIR AM2302
#define DHT22PIN 2 //Digital pin 2 for DHT22
#define DHTTYPE DHT22 // DHT22 (ASAIR AM2302)
DHT dht(DHT22PIN, DHTTYPE); // Initialize DHT22 sensor for normal 16MHz Arduino

}



// Variables for DHT22
int chk;
float humidity; // humidity value of DHT22
float tempair; // temperature air value of DHT22

int temp = 0;
// Variables for Soil Moisture Sensor
int soilsensor = 0;
int soilsensorpercent = 0;

const int AirValue = 520;
const int WaterValue = 215;

int solenoid1Pin = 3; // Solenoid Valve 1 on Pin 3
int solenoid2Pin = 4; // Solenoid Valve 2 on Pin 4
int solenoid3Pin = 5; // Solenoid Valve 3 on Pin 5
int solenoid4Pin = 6; // Solenoid Valve 4 on Pin 6
int solenoid5Pin = 7; // Solenoid Valve 5 on Pin 7
int solenoid6Pin = 8; // Solenoid Valve on Pin 8
int solenoid7Pin = 9; // Solenoid Valve on Pin 9
int solenoid8Pin = 10; // Solenoid Valve on Pin 10

int val1 = LOW; // Reads a Solenoid states
int val2 = LOW; // Reads a Solenoid states
int val3 = LOW; // Reads a Solenoid states
int val4 = LOW; // Reads a Solenoid states
int val5 = LOW; // Reads a Solenoid states
int val6 = LOW; // Reads a Solenoid states
int val7 = LOW; // Reads a Solenoid states
int val8 = LOW; // Reads a Solenoid states


void setup()
{

  pinMode(A0, INPUT);
  Serial.begin(9600);
  dht.begin();
  pinMode(solenoid1Pin, OUTPUT); // Set the pin as an output
  pinMode(solenoid2Pin, OUTPUT); // Set the pin as an output
  pinMode(solenoid3Pin, OUTPUT); // Set the pin as an output
  pinMode(solenoid4Pin, OUTPUT); // Set the pin as an output
  pinMode(solenoid5Pin, OUTPUT); // Set the pin as an output
  pinMode(solenoid6Pin, OUTPUT); // Set the pin as an output
  pinMode(solenoid7Pin, OUTPUT); // Set the pin as an output
  pinMode(solenoid8Pin, OUTPUT); // Set the pin as an output

//Initialize value for Solenoid 1
  val1 = digitalRead(solenoid1Pin);
  digitalRead(solenoid1Pin);
  val2 = digitalRead(solenoid2Pin);
  digitalRead(solenoid2Pin);
  val3 = digitalRead(solenoid3Pin);
  digitalRead(solenoid3Pin);
  val4 = digitalRead(solenoid4Pin);
  digitalRead(solenoid4Pin);
  val5 = digitalRead(solenoid5Pin);
  digitalRead(solenoid5Pin);
  val6 = digitalRead(solenoid6Pin);
  digitalRead(solenoid6Pin);
  val7 = digitalRead(solenoid7Pin);
  digitalRead(solenoid7Pin);
  val8 = digitalRead(solenoid8Pin);
  digitalRead(solenoid8Pin);
}

void loop()
{


  Serial.println("======================================================");
 
  //Read data and store it to variables for humidity and temperature air sensor
  humidity = dht.readHumidity();
  tempair = dht.readTemperature();
  if (isnan ( humidity) || isnan (tempair)) {
    Serial.println ("The DHT22 Sensor has a problem");
  }
  //Print humidity and temperature values
  Serial.print("\nAir Humidity: ");
  Serial.print(humidity);
  Serial.print(" % and Temperature: ");
  Serial.print(tempair);
  Serial.println(" Celcius");


  // do average value from Soil sensor!!
  soilsensor = analogRead(A0); // Reads Raw Value
  Serial.print("Soil Moisture Raw Value: ");
  Serial.print(soilsensor);
  delay(2000);
  soilsensor = analogRead(A0); // Reads Raw Value
  Serial.println("");
  Serial.print("Soil Moisture Raw Value: ");
  Serial.print(soilsensor);
  delay(2000);
  soilsensor = analogRead(A0); // Reads Raw Value
  Serial.println("");
  Serial.print("Soil Moisture Raw Value: ");
  Serial.print(soilsensor);
  delay(2000);
  soilsensor = analogRead(A0); // Reads Raw Value
  Serial.println("");
  Serial.print("Soil Moisture Raw Value: ");
  Serial.print(soilsensor);
  delay(2000);
  soilsensor = analogRead(A0); // Reads Raw Value
  Serial.println("");
  Serial.print("Soil Moisture Raw Value: ");
  Serial.print(soilsensor);
  delay(2000);
  Serial.println("");


  if (soilsensorpercent <= 40 || ((tempair <= 32) && (tempair >= 17))) {
    //Print humidity and temperature values
    Serial.print("\nAir Humidity: ");
    Serial.print(humidity);
    Serial.print(" % and Temperature: ");
    Serial.print(tempair);
    Serial.println(" Celcius");

    soilsensor = map(analogRead(A0), AirValue, WaterValue, 0, 100);

    //Print Soil Moisture level
    Serial.print("Soil Moisture Level: ");
    Serial.print(soilsensor);
    Serial.println("%");
    digitalWrite(solenoid1Pin, HIGH); // Switch Solenoid 1 ON
    Serial.println("Solenoid 1 is ON");
    //delay(5000); // add delay 5 secs
    delay(480000); // Watering for 8 minutes
    digitalWrite(solenoid1Pin, LOW); // Switch Solenoid 1 OFF
    Serial.println("Solenoid 1 is OFF");
    delay(5000); // add delay 5 secs
    //delay(480000); // Watering for 8 minutes
    digitalWrite(solenoid2Pin, HIGH); // Switch Solenoid 2 ON
    Serial.println("Solenoid 2 is ON");
    //delay(5000); // add delay 5 secs
    delay(480000); // Watering for 8 minutes
    digitalWrite(solenoid2Pin, LOW); // Switch Solenoid 2 OFF
    Serial.println("Solenoid 2 is OFF");
    delay(5000); // add delay 5 secs
    //delay(480000); // Watering for 8 minutes
    digitalWrite(solenoid3Pin, HIGH); // Switch Solenoid 3 ON
    Serial.println("Solenoid 3 is ON");
    //      delay(5000); // add delay 5 secs
    delay(480000); // Watering for 8 minutes
    digitalWrite(solenoid3Pin, LOW); // Switch Solenoid 3 OFF
    Serial.println("Solenoid 3 is OFF");
    delay(5000); // add delay 5 secs
    //delay(480000); // Watering for 8 minutes
    digitalWrite(solenoid4Pin, HIGH); // Switch Solenoid 4 ON
    Serial.println("Solenoid 4 is ON");
    //    delay(5000); // add delay 5 secs
    delay(480000); // Watering for 8 minutes
    digitalWrite(solenoid4Pin, LOW); // Switch Solenoid 4 OFF
    Serial.println("Solenoid 4 is OFF");
    delay(5000); // add delay 5 secs

    digitalWrite(solenoid5Pin, HIGH); // Switch Solenoid 5 ON
    Serial.println("Solenoid 5 is ON");
    //      delay(5000); // add delay 5 secs
    delay(480000); // Watering for 8 minutes
    digitalWrite(solenoid5Pin, LOW); // Switch Solenoid 5 OFF
    Serial.println("Solenoid 5 is OFF");
    delay(5000); // add delay 5 secs
    digitalWrite(solenoid6Pin, HIGH); // Switch Solenoid 6 ON
    Serial.println("Solenoid 6 is ON");
    //      delay(5000); // add delay 5 secs
    delay(480000); // Watering for 8 minutes
    digitalWrite(solenoid6Pin, LOW); // Switch Solenoid 6 OFF
    Serial.println("Solenoid 6 is OFF");
    delay(5000); // add delay 5 secs
    digitalWrite(solenoid7Pin, HIGH); // Switch Solenoid 7 ON
    Serial.println("Solenoid 7 is ON");
    //      delay(5000); // add delay 5 secs
    delay(480000); // Watering for 8 minutes
    digitalWrite(solenoid7Pin, LOW); // Switch Solenoid 7 OFF
    Serial.println("Solenoid 7 is OFF");
    delay(5000); // add delay 5 secs
    digitalWrite(solenoid8Pin, HIGH); // Switch Solenoid 8 ON
    Serial.println("Solenoid 8 is ON");
    //      delay(5000); // add delay 5 secs
    delay(480000); // Watering for 8 minutes
    digitalWrite(solenoid8Pin, LOW); // Switch Solenoid 8 OFF
    Serial.println("Solenoid 8 is OFF");
    delay(5000); // add delay 5 secs

    delay(1350000000); // add delay 90 minutes for watering
    delay(1350000000); // add delay 90 minutes for watering
    delay(1350000000); // add delay 90 minutes for watering
    delay(1350000000); // add delay 90 minutes for watering
    delay(1350000000); // add delay 90 minutes for watering
    delay(1350000000); // add delay 90 minutes for watering
    delay(1350000000); // add delay 90 minutes for watering
    delay(1350000000); // add delay 90 minutes for watering
    delay(1350000000); // add delay 90 minutes for watering
    delay(1350000000); // add delay 90 minutes for watering
    delay(1350000000); // add delay 90 minutes for watering
    delay(1350000000); // add delay 90 minutes for watering
    delay(1350000000); // add delay 90 minutes for watering
    delay(1350000000); // add delay 90 minutes for watering
    delay(1350000000); // add delay 90 minutes for watering
    delay(1350000000); // add delay 90 minutes for watering
  }

  if (soilsensorpercent >= 85 || ((tempair > 32) && (tempair < 17))) {

    //Print humidity and temperature values
    Serial.print("\nAir Humidity: ");
    Serial.print(humidity);
    Serial.print(" % and Temperature: ");
    Serial.print(tempair);
    Serial.println(" Celcius");
    soilsensor = map(analogRead(A0), AirValue, WaterValue, 0, 100);

    //Print Soil Moisture level
    Serial.print("Soil Moisture Level: ");
    Serial.print(soilsensor);
    Serial.println("%");
    // Stop all Solenoids
    Serial.println("Stop all Solenoids");
    digitalWrite(solenoid1Pin, LOW); // Switch Solenoid 1 OFF
    delay(5000); // add delay 2 secs
    digitalWrite(solenoid2Pin, LOW); // Switch Solenoid 2 OFF
    delay(5000); // add delay 5 secs
    digitalWrite(solenoid3Pin, LOW); // Switch Solenoid 3 OFF
    delay(5000); // add delay 5 secs
    digitalWrite(solenoid4Pin, LOW); // Switch Solenoid 4 OFF
    delay(5000); // add delay 5 secs
    digitalWrite(solenoid5Pin, LOW); // Switch Solenoid 5 OFF
    delay(5000); // add delay 5 secs
    digitalWrite(solenoid6Pin, LOW); // Switch Solenoid 6 OFF
    delay(5000); // add delay 5 secs
    digitalWrite(solenoid7Pin, LOW); // Switch Solenoid 7 OFF
    delay(5000); // add delay 5 secs
    digitalWrite(solenoid8Pin, LOW); // Switch Solenoid 8 OFF
    delay(5000); // add delay 5 secs
    Serial.println("All Solenoids are stopped");

    delay(1350000000); // add delay 90 minutes for watering
    delay(1350000000); // add delay 90 minutes for watering
    delay(1350000000); // add delay 90 minutes for watering
    delay(1350000000); // add delay 90 minutes for watering
    delay(1350000000); // add delay 90 minutes for watering
    delay(1350000000); // add delay 90 minutes for watering
    delay(1350000000); // add delay 90 minutes for watering
    delay(1350000000); // add delay 90 minutes for watering
    delay(1350000000); // add delay 90 minutes for watering
    delay(1350000000); // add delay 90 minutes for watering
    delay(1350000000); // add delay 90 minutes for watering
    delay(1350000000); // add delay 90 minutes for watering
    delay(1350000000); // add delay 90 minutes for watering
    delay(1350000000); // add delay 90 minutes for watering
    delay(1350000000); // add delay 90 minutes for watering
    delay(1350000000); // add delay 90 minutes for watering
  }

  if (soilsensorpercent > 100 || ((tempair > 32) && (tempair < 17))) {
    //Print humidity and temperature values
    Serial.print("\nAir Humidity: ");
    Serial.print(humidity);
    Serial.print(" % and Temperature: ");
    Serial.print(tempair);
    Serial.println(" Celcius");
    soilsensor = map(analogRead(A0), AirValue, WaterValue, 0, 100);

    //Print Soil Moisture level
    Serial.print("\nSoil Moisture Level: 100%"); // Max percentage is 100%

    Serial.println("Stop all Solenoids");

    digitalWrite(solenoid1Pin, LOW); // Switch Solenoid 1 OFF
    delay(5000); // add delay 2 secs
    digitalWrite(solenoid2Pin, LOW); // Switch Solenoid 2 OFF
    delay(5000); // add delay 5 secs
    digitalWrite(solenoid3Pin, LOW); // Switch Solenoid 3 OFF
    delay(5000); // add delay 5 secs
    digitalWrite(solenoid4Pin, LOW); // Switch Solenoid 4 OFF
    delay(5000); // add delay 5 secs
    digitalWrite(solenoid5Pin, LOW); // Switch Solenoid 5 OFF
    delay(5000); // add delay 5 secs
    digitalWrite(solenoid6Pin, LOW); // Switch Solenoid 6 OFF
    delay(5000); // add delay 2 secs
    digitalWrite(solenoid7Pin, LOW); // Switch Solenoid 7 OFF
    delay(5000); // add delay 5 secs
    digitalWrite(solenoid8Pin, LOW); // Switch Solenoid 8 OFF
    delay(5000); // add delay 2 secs
    Serial.println("All Solenoids are stopped");

    // 3hours
    delay(1350000000); // add delay 90 minutes for watering
    delay(1350000000); // add delay 90 minutes for watering

    delay(1350000000); // add delay 90 minutes for watering
    delay(1350000000); // add delay 90 minutes for watering

    delay(1350000000); // add delay 90 minutes for watering
    delay(1350000000); // add delay 90 minutes for watering

    delay(1350000000); // add delay 90 minutes for watering
    delay(1350000000); // add delay 90 minutes for watering

    delay(1350000000); // add delay 90 minutes for watering
    delay(1350000000); // add delay 90 minutes for watering

    delay(1350000000); // add delay 90 minutes for watering
    delay(1350000000); // add delay 90 minutes for watering

    delay(1350000000); // add delay 90 minutes for watering
    delay(1350000000); // add delay 90 minutes for watering
    // all 24 hours
    delay(1350000000); // add delay 90 minutes for watering
    delay(1350000000); // add delay 90 minutes for watering
  }

  delay(2500); // add delay 2,5 secs
}

Have a look at the code you posted, it's a mess. That is because the forum can't handle it without code tags. Please edit your post and place the code between code tags. The instructions for doing that are available in the reference threads.

Use the IDE autoformat tool (ctrl-t or Tools, Auto format) before posting code in a code block.

2 Likes

Hello tousisd

I´v made a small and fast code review.

In general - Arrays and structs are your friends.
Don't duplicate code in your sketch. Write code once - use it multiple times.

Do you have experience with programming in C++?

The task can easily be realised with an object.
A structured array contains all information, such as pin addresses for the I/O devices, as well as the information for the timing.
A single service takes care of this information and initiates the intended action.
The structured array makes the sketch scalable until all I/O pins are used up without having to adapt the code for the service.
It is cool stuff, isn´t it?

Have a nice day and enjoy coding in C++.

1 Like

Hi, @fj8
Welcome to the forum.

Have you developed your code in stages?
That is, do you have codes that just read you inputs, just activate your relays?
You need to prove that you can read and control each of your peripherals before writing a mass of code.

If you didn't then can I suggest you forget your main code for the time being and write some code that just operates the relays, to prove you know how to code for them.

Can you please post some images of your project?
So we can see your component layout.

What is your PSU?
What is y our PSU "adapter"?

Can you post link to the moisture sensor?

Thanks.. Tom.. :grinning: :+1: :coffee: :australia:

1 Like

I tried that you say too, like changing digitalWrite(pin, HIGH); at the beginning and when I want to open a valve put the pin to LOW, but all valves again working all together..

I am new to electronics and I don't know how to do it with a mofset driver for each one, I will look into the schematic you post

I know I keep it so simple, thanks for your advice though, I would look into it, but now my first priority is to keep my vegetables live :slight_smile:

We're more interested in looking into a schematic that you post. Also please comply with multiple requests to format your code correctly.

One if line wants tempair to be both larger then 32 and smaller then 17. That will never happen.

If they are depending on this sketch to live, you should post the details of your project before they die.

1 Like

How i must correct my if for tempair if it's range will be 17 - 32?

@fj8
On the relay board:
Remove the jumper on Vcc - JD-Vcc
Connect Vcc to arduino 5V
Connect JD-Vcc to 12V

Connect ALL grounds

A LOW will turn the relay ON

1 Like

@fj8
Once you make the hardware fixes outlined in post #15, then you can fix the code.
Hopefully you didn't burn out the arduino

1 Like

I am sorry again, I draw it quickly in paint, I also fix the code, I am new here

Thank you for your help, I will try that and change the LOW to HIGH and reverse. No it's not burned, I only burn one relay module :slight_smile:

Why not just

delay(1350000000 * 16);

?

1 Like

Don't forget the re-wiring or you will burn another.

1 Like