help in relay !

i make my arduino uno r3 detect the water level in my water tank, so now i want my pump work when the level is low so i buy yasterday one relay with driver, 5v * 220v 10A,
i know how to connect the pump to the relay,
but i dont know how i will connect it to the arduno
other thing i need help with the code, where i will add the code to make the pump work at 50 % and turn it off when the tank is 100% ?!
this is my code :slight_smile: you can share it :stuck_out_tongue:

#include <LiquidCrystal.h>

#define ECHOPIN 3                            // Pin to receive echo pulse
#define TRIGPIN 4                            // Pin to send trigger pulse

#define STATUSPIN 13                        // Use for troubleshooting

int highWater = 20;        // These values allow to calculate % of full
int lowWater = 100;        // SRF04 hangs above water (lower distance = more water)

byte symbol[8] = {         // Custom character for LCD display
  B00000, 
  B11111,
  B11111,
  B11111,
  B11111,
  B11111,
  B00000,
};

LiquidCrystal lcd(7, 8, 9, 10, 11, 12);    // Assign pins

// Utility function for flashing STATUSPIN
void flashLed(int pin, int times, int wait) {

  for (int i = 0; i < times; i++) {
    digitalWrite(pin, HIGH);
    delay(wait);
    digitalWrite(pin, LOW);

    if (i + 1 < times) {
      delay(wait);
    }
  }
}


void setup() {
  
  lcd.begin(16,2);
  lcd.print(" AQUA LEVEL ");
  lcd.createChar(0, symbol);
  pinMode(ECHOPIN, INPUT);
  pinMode(TRIGPIN, OUTPUT);

  delay(3000);       // Show application name for 3 seconds.

}

void loop() {
  // Measure distance
  digitalWrite(TRIGPIN, LOW);                   // Set the trigger pin to low for 2uS
  delayMicroseconds(2);
  digitalWrite(TRIGPIN, HIGH);                  // Send a 10uS high to trigger ranging
  delayMicroseconds(10);
  digitalWrite(TRIGPIN, LOW);                   // Send pin low again
  int distance = pulseIn(ECHOPIN, HIGH);        // Read in times pulse
  distance= distance/58;                        // divide by 58 gives cm.
  
  // Convert measured value to value between 0-16, to display on LCD
  // Use Arduino built-in map and constrain functions
  int scaledValue = map(constrain(distance, highWater, lowWater), lowWater, highWater, 0, 16);
  lcd.clear();
  lcd.print("Water Level:");
  lcd.setCursor(0,1);
  while (scaledValue > 0) {
     lcd.print((char)0);
     scaledValue--;
  }
      
   delay(2000);  // Wait 2 seconds before measuring again. We're in no hurry!
}

It looks like your variable distance has the number of cm to the water surface. Suppose it is 5cm when full and 18cm when half full.

Then you need a little code such as

if (distance >= 18) {
    digitalWrite(relayPin, HIGH); // assumes HIGH switches the relay on
}

if distance <= 5) {
   digitalWrite(relayPin, LOW);
}

You can’t connect an electo-mechanical relay direct to an Arduino I/O pin because it can’t supply enough current for the relay coil. You need to use the Arduino to control a transistor which controls the current in the relay coil. I reckon if you Google arduino relay you will get lots of circuit examples. Also, don’t draw power for the relay coil from the Arduino 5v pin - use a separate power source.

…R

i don't know what to say, just you are awesome :blush: i want ask other question :) i make other arduous with same code but i want it for two tanks so i add other ultrasonic sensor on port 5 and 6 if i want show level for two tanks ? how i can made that on the same diplay i will copy the same code to other sensor, but my problem with the display

for the relay i use this one :slight_smile: simple and i think i dont need to add anything on it,
i have a pump 15A for that i used other relay 24V*220V 20A AC , i have other power suply for the second relay , so i think that will work for me :slight_smile:
but not the same its take 5v *220v 10A
this is my relay

http://i01.i.aliimg.com/wsphoto/v0/1453630975_1/wholesales-100pcs-one-channel-Power-Relay-module-12V-high-lever-trigger-SRD-12VDC-SL-C-PCB.jpg

Why are you re-posting the same question?

Mark

you are right i will delete that one :)

narzan: how i can made that on the same diplay

Thank you for your kind comment, but now I have to disappoint you. I have little or no experience with LCD displays.

If you were just diplaying the data on the Serial Monitor I would do it with something like this

Serial.print("Tank A ");
Serial.print(tankOneDepth);
Serial.print("  Tank B ");
Serial.println(tankTwoDepth);

which would give something like Tank A 23 Tank B 34

...R