Stuck on activating relay when limit achieve

Hi I am currently working on simple automate hydroponic system. I use sensor tds meter, ph meter, and ultrasonic jsn sr04t. I use 4 relay to activate 2 peristaltic pump for ph, 1 peristaltic pump for tds and 1 solenoid pump. When I execute the code that i make, the pump can't turn on like the limitation that i've already made.

#include <Wire.h>
#include <GravityTDS.h>
#include <DFRobot_PH.h>
#include <NewPing.h>


#define TRIGGER_PIN 5
#define ECHO_PIN 6

NewPing sonar(TRIGGER_PIN, ECHO_PIN);
const int tdsSensorPin = A1;
const int pHMeterPin = A2;

const int waterRelayPin = 7; 
const int pHUpRelayPin = 8; 
const int pHDownRelayPin = 9; 
const int abMixRelayPin = 10; 

void setup() {
  Serial.begin(115200);
  pinMode(tdsSensorPin, INPUT);
  pinMode(pHMeterPin, INPUT);
  pinMode(TRIGGER_PIN,OUTPUT);
  pinMode(ECHO_PIN,INPUT);
  digitalWrite(TRIGGER_PIN, LOW);

  //relay
  pinMode(waterRelayPin, OUTPUT);
  pinMode(pHUpRelayPin, OUTPUT);
  pinMode(pHDownRelayPin, OUTPUT);
  pinMode(abMixRelayPin, OUTPUT);
  
  digitalWrite(waterRelayPin, LOW);   
  digitalWrite(pHUpRelayPin, LOW);    
  digitalWrite(pHDownRelayPin, LOW); 
  digitalWrite(abMixRelayPin, LOW); 
}
void loop() {
  long duration, distance;
  int percentage;
  
  // Read nilai TDS Sensor
  int tdsValue = analogRead(tdsSensorPin);

  // Read nilai PH Sensor
  float pHValue = analogRead(pHMeterPin);
  float pH = (pHValue-256.08)/20.337;
  
  //Ultrasonic HC SR04
  digitalWrite(TRIGGER_PIN, HIGH);
  delayMicroseconds(11);
  digitalWrite(TRIGGER_PIN, LOW);
  digitalWrite(ECHO_PIN, HIGH);
  
  duration = pulseIn(ECHO_PIN, HIGH);
  distance = duration * 0.034/2;
  
  //Water Height Solenoid
  if(distance < 30){
    Serial.println("Water Pump : ON");
    digitalWrite(waterRelayPin, LOW);
  }else{
    Serial.println("Water Pump : OFF");
    digitalWrite(waterRelayPin, HIGH);
  }
  
  //pH UP & Down 
  if (pH < 5.5) {
  Serial.println("Pump PH Up : ON");
  Serial.println("Pump PH Down : OFF");
    digitalWrite(pHUpRelayPin, LOW);  // Turn On relay "PH Up"
    digitalWrite(pHDownRelayPin, HIGH);  // Turn Off relay "PH Down"
  } else if(pH > 6.5) {
    Serial.println("Pump PH Up : OFF");
    Serial.println("Pump PH Down : ON");
    digitalWrite(pHUpRelayPin, HIGH);  // Turn Off relay "PH Up"
    digitalWrite(pHDownRelayPin, LOW);  // Turn On relay "PH Down"
  }else {
  Serial.println("Pump PH Up : OFF");
  Serial.println("Pump PH Down : OFF");
  digitalWrite(pHUpRelayPin, HIGH);  // Turn Off relay "PH Up"
  digitalWrite(pHDownRelayPin, HIGH); // Turn Off relay "PH Down"
  }

  //AB Mix
  if (tdsValue < 1000) {
  Serial.println("ABMix : ON");
    digitalWrite(abMixRelayPin, LOW);  // Turn On relay "ABMix"
  } else {
  Serial.println("ABMix : OFF");
    digitalWrite(abMixRelayPin, HIGH); // Turn Off relay "ABMix"
  }
  
  //TDS
  Serial.print("TDS: ");
  Serial.print(tdsValue);
  Serial.println("ppm");
  
  //PH Meter
  Serial.print("pH: ");
  Serial.print(pH);
  
  //Ultrasonic
  Serial.print("% Distance: ");
  Serial.print(distance);
  Serial.println("cm");

}

At first, I try to execute it just using relay and it doing well. But when I try it with a complete series (sensor, relay and actuator) the actuator does not run as output sensor. Thank you

what does the serial monitor display? You are printing your sensor values - are they what you expect?

Can you post your schematic, showing all parts and how you've connected them, with particular attention to how power is supplied and routed to the components that need power?

Can you try running your system without any relays or solenoids, just using the printing to confirm the logic?

a7

1 Like


This is the output from the serial monitor just to check my logic. So I put a fixed value.

And I try to run it in my schematic but it turns out the relay cant turn on but the output on the serial monitor notify that the relay is turn on

We need you annotated schematic as to exactly how you have wired it. It sounds like a hardware problem however since you did not follow the forum guidelines your code is not readable.

You should post the serial output as a code-section too

best regards Stefan

1 Like

post a datasheet or an url from exact your 4-relay-board

What are you using as the power-supply?
What exact type of microcontroller are you using?
The onboard-power of an Arduino-Uno (and almost any other microcontroller) might be able to supply a single relay but has way too less power to supply multiple relays and pumps.

You need an external power-supply that can deliver enough ampere to drive it all plus 20% extra just to make sure that it can deliver enough current.

best regards Stefan

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.