Arduino Uno with HC-SR04 ultrasonic sensor

Hi, I’m a beginner of this arduino Projects but its for Moving Up, to get a valid Research. As you can see I am having hard of this HC-SR04, Arduino and its code. The output of this prodcut is a mini submersible waterpump. Whenever I put my hand in front of the sensor, the waterpump would’nt work. I dont know if this is a right code, please correct me if I’m wrong.

const int trigPin = 10; // Trigger Pin of Ultrasonic Sensor
const int echoPin = 9; // Echo Pin of Ultrasonic Sensor
const int waterpump = 13;

void setup() {
Serial.begin(9600); // Starting Serial Terminal
pinMode(waterpump, OUTPUT);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
// put your setup code here, to run once:

}

void loop() {
long duration, distance,cm;
digitalWrite(trigPin,HIGH);
delayMicroseconds(1000);
digitalWrite(trigPin,LOW);
pinMode(echoPin, INPUT);
duration=pulseIn(echoPin,HIGH);
distance =(duration/2)/29.1;
Serial.print(distance);
Serial.println(“CM”);
delay(10);

if((distance<=10))
(
digitalWrite(waterpump, HIGH));

else if(distance>10)
{
digitalWrite(waterpump,LOW);
}
}

sketch_jan20a.ino (796 Bytes)

Do the debug prints show that the range values are sensible?

I set up my Uno with an HCSR04 rangefinder and an LED to simulate the pump and loaded your code. The LED turns on and off at 10 cm so the code "works".

How is the pump wired to the Uno? You do know that you will need a driver for the pump as the Uno will not output enough current to run a motor?