Relay Issues

I'm a complete newcomer to component level electronics, so bear with me. I'm building a fully scalable environmental control and irrigation system for hydroponic gardens that can be as small as a personal garden and as extensive as a multimillion-dollar industrial setup. Yes, I know this project is currently way over my head, but I have broken it down into phases and plan on taking my time learning bit by bit to bring this into reality as I learn.

I'm currently in Phase 1, which I have defined as making one self-watering plant pot. Simple enough, I have a circuit built that is currently just sending a signal to an LED when the pump would be running. The software and sensor are working properly and I am getting the LED to light for 1-second pulses every 5 seconds while the sensor is reporting low moisture and it stops when it hits the upper limit, cool. Now I need to hook up the relay module.

Knowing I was going to scale this up into a system that controls groups of plants I wanted to use a 16 channel relay module right from the beginning to make less work for me going forward. The relay I purchased (listed below) may have been a poor selection as the device to use. I can't figure out how to get it to trigger the relay.

I have the relay module powered through a wall wart 12v power supply. On the business end of the relay, I have a 9v battery and a 12v pump. The battery drives the pump just fine for testing purposes, and I don't have another power supply to use yet, yay Christmas strain on the mail system! So I am fairly confident everything in front of and behind the relay module is working just fine.

I have the signal wire coming from the Mega pin 12 into the breadboard with the indicator LED and another lead. The lead goes to the channel marked X1 on the module and the motor is connected to the K1 relay. When the LED illuminates I am getting no response from the pump.

I have a second 16ch relay module in the mail as well that has a better interface and is intended for use with Arduino, but if I can make this one work it will be a lot less expensive for the larger scaled end of the project. I saw in the listing for the module I am using it is a 5/12/24v 5mA board, but I don't see anything to select different voltages and the board came with absolutely nothing for documentation. One of the ports on this relay is labeled XM right next to DC+ and DC- on the input terminal. The rest are numbered X1 through X16. What is this XM used for?

My only theory is that the board's signal wire isn't supplying enough amperage to the relay module to trigger it, but I have no idea if that's even on the right track. I ordered a multimeter, which is also in the mail.

Elegoo Mega 2560 R3: https://www.amazon.com/gp/product/B01EWNUUUA/ref=ppx_yo_dt_b_asin_title_o09_s00?ie=UTF8&psc=1

16ch Relay Module: https://www.amazon.com/gp/product/B07T2MD1JS/ref=ppx_yo_dt_b_asin_title_o08_s00?ie=UTF8&psc=1

My best guess is that MX is for selecting whether the inputs X1 … X16 are active-high (connect XM to 0V) or active-low (XM to +5V), but it’s only a guess. (The Amazon entry states “Users can select the control level of the relay, which can be high level pull-in or low level pull-in”).

You could screw up your courage and try it (but don’t blame me if it damages the board!), try to reverse-engineer the board, or try Customer questions & answers on Amazon. Or try all three.

LEDs require current limiting resistors.

The pump should have a kickback diode across its terminals.

Those breadboard power supplies cannot provide enough power for a Mega.

Those relays require 24v for their coils.

“XM: Photoelectric isolation chip common terminal
If high level control is used: this signal should be connected to the negative terminal of the user control board power supply.
If low level control is used: this signal should be connected to the positive side of the user control board power supply.”

larryd:
XM: Photoelectric isolation chip common terminal

I guessed OK, then. Pretty obvious, I suppose.

larryd:
Those breadboard power supplies cannot provide enough power for a Mega.

They must be power-hungry. From one of the suppliers: "Output current: Maximum output current 700mA".

That did the trick, thank you so much! Luckily I had an old PC power supply laying around that I could convert into a benchtop power supply. Everything is working as intended now, you rock!

700mA would be quite iffy.

The wattage the regulator has to dissipate is (Vin - 5v ) * current taken.

As they are not on a heat sink, they can get very hot when Vin is high.

They are really meant to power small, low current ccts. on the breadboard itself.

johndg:
They must be power-hungry. From one of the suppliers: “Output current: Maximum output current 700mA”.

The supplier conveniently forgot to mention the conditions at which that is possible.
Only with <=6.5volt on the DC socket, and not continuously.

With 9volt on the DC socket, and a 700mA current draw from the 5volt pin, power dissipation in the regulator is (9-5)*0.7= 2.8watt. Enough to get the regulator in trouble in a matter of minutes.

Luckily the Mega only draws ~70mA, and the sensor and optos could add another 50mA to that.

But why use a breadboard supply. The Mega itself has the same parts on it (with the same limitations).
Leo…

I am currently building a self-watering pot that will later be integrated into an expandable irrigation system for hydroponic gardens. Right now I am having problems with the moisture sensor giving inaccurate and fluctuating readouts when the conditions it is monitoring remain unchanged. I don’t know if it is from using a cheap sensor, faulty code, or something I’m missing in the wiring.

Currently, the pump turns on upon initialization and doesn’t turn off until after the first low moisture reading comes across and resolves that ‘IF’ condition in the code. The sensor seems to cycle through moisture levels regardless of whether it is in the open air or in the moist soil so the water pump keeps getting triggered even when the soil is wet.

Circuit Diagram

Parts:

Arduino Nano
12v water pump
2x DC-DC Buck Down Converters (one for 5v, the second for 12v)
Red LED (using 100 Ohm resistor)
24v 16 Channel Relay board (using CH1 now, need additional channels for phase 2 of the project)
24v Power supply
Capacitive Soil Moisture Sensor v1.2 (cheap sensor off Amazon, a possible cause of my current hair loss)

int msensor = A1; //moisture sensor connectes to pin A1 on Mega
int pump = 12; //sets pump to pin 12
int msval; //moisture sensor value
int loopcount = 0; //declares a loop counting var
int err = 11; //sets err to pin 11
int led = 13; //onboard led uses pin 13's address
boolean flag = false; //error state indicator

void setup() {
  Serial.begin(9600); //opens serial at 9600 baud rate
  pinMode(msensor, INPUT); //Sets sensor as Input
  pinMode(pump, OUTPUT); //sets pump as output
  pinMode(err, OUTPUT); //sets err to output to LED warning light
  pinMode(led, OUTPUT); //Sets onboard LED as output
  }

void loop() {
 
   msval = analogRead(msensor); //sets msval to the value coming from the sensor

if (loopcount == 0) 
{
  Serial.println();
  Serial.println();
  Serial.println();
  Serial.println("***************************Begin Sequence***************************");
  Serial.println();
  Serial.println();
}

  Serial.println();
  Serial.print("++++++++ Loop Count = ");
  Serial.print(loopcount+1,DEC);
  Serial.println("++++++++");
  Serial.print("Moisture Reading: ");
  Serial.println(msval); //sends the output to the serial port for display when connected to pc

  if ( (msval >= 800) && (flag == false) && (loopcount <=9) ) //Moisture sensor wet = 0, dry = 1024
  {
    digitalWrite(led,HIGH); //turn on led
    digitalWrite(pump, LOW); //turn on pump
    Serial.println("pump on"); 
    delay(5000);
    digitalWrite(pump, HIGH); //turn off pump
    Serial.println("pump off");
    delay(5000);
 
    if (loopcount >= 5)  //if pump adds water more than 5 times, start low reservoir error sequence
    {
      digitalWrite(err,HIGH); //output to error light to indicate first step of error detection
      Serial.print("Error Count = ");
      Serial.println(loopcount-4,DEC);
      delay(loopcount*1000); //give the soil extra time to absorb and distribute water
    }

    if (loopcount >= 10) //if the pump adds water 10x and still is not reporting enough moisture, report error
    {
      Serial.println("Reservoir out of water");
      flag = true; //sets pot to error state and prevents running pump on empty reservoir 
    }
    
  }
 
  if (msval <= 500) //if water level is high enough, reset loop counter and return to periodic monitoring
  {
    digitalWrite(err, LOW); //turn off led
    loopcount = 0; //reset loop counter after water dispenses
    flag = false; //indicates to system water is not needed
  }

  delay(1000); //sets polling rate
 loopcount = loopcount+1; //increase loop counter

}

Here is what I am getting from the Serial Monitor when the sensor is not in the soil, should read continuous dry levels:

16:16:58.491 -> 
16:16:58.491 -> 
16:16:58.491 -> 
16:16:58.491 -> ***************************Begin Sequence***************************
16:16:58.557 -> 
16:16:58.557 -> 
16:16:58.557 -> 
16:16:58.557 -> ++++++++ Loop Count = 1++++++++
16:16:58.590 -> Moisture Reading: 129
16:16:59.567 -> 
16:16:59.567 -> ++++++++ Loop Count = 2++++++++
16:16:59.601 -> Moisture Reading: 614
16:17:00.572 -> 
16:17:00.572 -> ++++++++ Loop Count = 3++++++++
16:17:00.606 -> Moisture Reading: 494
16:17:01.546 -> 
16:17:01.546 -> ++++++++ Loop Count = 2++++++++
16:17:01.579 -> Moisture Reading: 466
16:17:02.553 -> 
16:17:02.553 -> ++++++++ Loop Count = 2++++++++
16:17:02.586 -> Moisture Reading: 502
16:17:03.553 -> 
16:17:03.553 -> ++++++++ Loop Count = 3++++++++
16:17:03.586 -> Moisture Reading: 554
16:17:04.564 -> 
16:17:04.564 -> ++++++++ Loop Count = 4++++++++
16:17:04.597 -> Moisture Reading: 588
16:17:05.568 -> 
16:17:05.568 -> ++++++++ Loop Count = 5++++++++
16:17:05.602 -> Moisture Reading: 918
16:17:05.602 -> pump on
16:17:10.536 -> pump off
16:17:16.546 -> 
16:17:16.546 -> ++++++++ Loop Count = 6++++++++
16:17:16.579 -> Moisture Reading: 127
16:17:17.533 -> 
16:17:17.533 -> ++++++++ Loop Count = 2++++++++
16:17:17.566 -> Moisture Reading: 65
16:17:18.541 -> 
16:17:18.541 -> ++++++++ Loop Count = 2++++++++
16:17:18.574 -> Moisture Reading: 90
16:17:19.554 -> 
16:17:19.554 -> ++++++++ Loop Count = 2++++++++
16:17:19.588 -> Moisture Reading: 0
16:17:20.533 -> 
16:17:20.533 -> ++++++++ Loop Count = 2++++++++
16:17:20.566 -> Moisture Reading: 264
16:17:21.545 -> 
16:17:21.545 -> ++++++++ Loop Count = 2++++++++
16:17:21.579 -> Moisture Reading: 0
16:17:22.556 -> 
16:17:22.556 -> ++++++++ Loop Count = 2++++++++
16:17:22.556 -> Moisture Reading: 0
16:17:23.529 -> 
16:17:23.529 -> ++++++++ Loop Count = 2++++++++
16:17:23.562 -> Moisture Reading: 0
16:17:24.538 -> 
16:17:24.538 -> ++++++++ Loop Count = 2++++++++
16:17:24.572 -> Moisture Reading: 1
16:17:25.551 -> 
16:17:25.551 -> ++++++++ Loop Count = 2++++++++
16:17:25.584 -> Moisture Reading: 0
16:17:26.529 -> 
16:17:26.529 -> ++++++++ Loop Count = 2++++++++
16:17:26.562 -> Moisture Reading: 9
16:17:27.540 -> 
16:17:27.540 -> ++++++++ Loop Count = 2++++++++
16:17:27.573 -> Moisture Reading: 0
16:17:28.542 -> 
16:17:28.542 -> ++++++++ Loop Count = 2++++++++
16:17:28.576 -> Moisture Reading: 14
16:17:29.555 -> 
16:17:29.555 -> ++++++++ Loop Count = 2++++++++
16:17:29.555 -> Moisture Reading: 13
16:17:30.534 -> 
16:17:30.534 -> ++++++++ Loop Count = 2++++++++
16:17:30.568 -> Moisture Reading: 16
16:17:31.547 -> 
16:17:31.547 -> ++++++++ Loop Count = 2++++++++
16:17:31.581 -> Moisture Reading: 0
16:17:32.555 -> 
16:17:32.555 -> ++++++++ Loop Count = 2++++++++
16:17:32.555 -> Moisture Reading: 32
16:17:33.531 -> 
16:17:33.531 -> ++++++++ Loop Count = 2++++++++
16:17:33.566 -> Moisture Reading: 105
16:17:34.543 -> 
16:17:34.543 -> ++++++++ Loop Count = 2++++++++
16:17:34.576 -> Moisture Reading: 190
16:17:35.522 -> 
16:17:35.522 -> ++++++++ Loop Count = 2++++++++
16:17:35.556 -> Moisture Reading: 291
16:17:36.537 -> 
16:17:36.537 -> ++++++++ Loop Count = 2++++++++
16:17:36.571 -> Moisture Reading: 376
16:17:37.521 -> 
16:17:37.521 -> ++++++++ Loop Count = 2++++++++
16:17:37.556 -> Moisture Reading: 490
16:17:38.533 -> 
16:17:38.533 -> ++++++++ Loop Count = 2++++++++
16:17:38.567 -> Moisture Reading: 539
16:17:39.553 -> 
16:17:39.553 -> ++++++++ Loop Count = 3++++++++
16:17:39.553 -> Moisture Reading: 652
16:17:40.532 -> 
16:17:40.532 -> ++++++++ Loop Count = 4++++++++
16:17:40.565 -> Moisture Reading: 708
16:17:41.544 -> 
16:17:41.544 -> ++++++++ Loop Count = 5++++++++
16:17:41.579 -> Moisture Reading: 742
16:17:42.522 -> 
16:17:42.522 -> ++++++++ Loop Count = 6++++++++
16:17:42.556 -> Moisture Reading: 815
16:17:42.556 -> pump on
16:17:47.520 -> pump off
16:17:52.507 -> Error Count = 1
16:17:58.521 -> 
16:17:58.521 -> ++++++++ Loop Count = 7++++++++
16:17:58.554 -> Moisture Reading: 158
16:17:59.501 -> 
16:17:59.501 -> ++++++++ Loop Count = 2++++++++
16:17:59.534 -> Moisture Reading: 329
16:18:00.505 -> 
16:18:00.505 -> ++++++++ Loop Count = 2++++++++
16:18:00.537 -> Moisture Reading: 473
16:18:01.512 -> 
16:18:01.512 -> ++++++++ Loop Count = 2++++++++
16:18:01.545 -> Moisture Reading: 656
16:18:02.519 -> 
16:18:02.519 -> ++++++++ Loop Count = 3++++++++
16:18:02.553 -> Moisture Reading: 780

What does the Serial.print show?

Serial.print tells Pump On and 5 seconds later Pump Off. Doesn't the pump behave like that?

Moisture readings look ok to me.

Railroader:
Serial.print tells Pump On and 5 seconds later Pump Off. Doesn't the pump behave like that?

Moisture readings look ok to me.

The moisture sensor reads from completely dry at 1024 to completely wet at 0. These readings were taken with the sensor not even in the dirt, and they were fluctuating from 0 all the way to wherever I set my upper limit to indicate the soil is too dry. Then the pump kicks on inline with the serial.print messages sometimes once, sometimes three times. Then the sensor starts to lower for a few cycles then rise back to the upper limit. All of this is happening as I would expect from a rapidly draining system, only the sensor is hanging in the air and the reservoir is disconnected while I troubleshoot.
Something is making the sensor report back changing conditions, even though the conditions are not changing.

Okey. I thought the sensor was down in the watered soil.

What is the currency capacity of the power supply, converters and how much current is used by the pump when it starts?

The sensor may have to be:

  • in the soil
  • calibrated

Some of these sensors have a design fault, where the 1Meg bleed resistor from it's output pin to ground is not connected, resulting in a very slow response of the sensor.
To fix, add a 1Megohm resistor externally between Arduino pin and Arduino ground.

It might also help if you power the Nano correctly.
Either 7-9volt on V-in, or 5volt directly on the 5volt pin.
Leo..

Starting a new thread for basically the same project is against forum rules.
I have asked a moderator to merge.
Leo..

aarg:
The sensor may have to be:

  • in the soil
  • calibrated

Even in the soil I get the same situation with the rolling values. There is no calibration screw on the sensor and I don't know how I would to that otherwise.

cyborggold:
Even in the soil I get the same situation with the rolling values. There is no calibration screw on the sensor and I don’t know how I would to that otherwise.

The calibration would be in software. You would test the actual readings vs. known moisture samples to produce an offset. Then you can use the offset to return a correct value from some operational reading.

Frequently, when circuits produce “wild readings”, it’s because of wiring errors or misunderstandings about wiring layout. So please post images of your actual hardware, and a wiring diagram (repost here please, links don’t cut it).

I do admit, I followed the link anyway and noticed that you are powering your Nano with 5V on the Vin pin. That is a horrible mistake. It needs to go directly to the 5V pin. Then you compound the error by trying to feed the relay module from the Nano 5V. Also the buck converter is usually noisy (if it’s a cheap, small one) so it may be leaking noise into the analog circuit both through the sensor and through the IC power pins.

Exactly how much are the readings “rolling”? There is always a little bit of noise in a reading.

Wawa:
Some of these sensors have a design fault, where the 1Meg bleed resistor from it's output pin to ground is not connected, resulting in a very slow response of the sensor.
To fix, add a 1Megohm resistor externally between Arduino pin and Arduino ground.

It might also help if you power the Nano correctly.
Either 7-9volt on V-in, or 5volt directly on the 5volt pin.
Leo..

I was curious about that, I'll move that connection from VIN to 5v and see if I get any better results. Are you saying to put the resistor between the wires connecting the sensor and Arduino similar to how the diode is wired for the pump?