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;
pinMode(echoPin, INPUT);
distance =(duration/2)/29.1;

digitalWrite(waterpump, HIGH));

else if(distance>10)

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?