0% found this document useful (0 votes)
2 views2 pages

Arduino

The document contains an Arduino code that uses an ultrasonic sensor to measure distance and control a relay for a pump. If an object is detected within 10 cm, the pump is activated for 2 seconds. The code includes setup and loop functions to handle the sensor readings and relay control.

Uploaded by

muaw1704
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views2 pages

Arduino

The document contains an Arduino code that uses an ultrasonic sensor to measure distance and control a relay for a pump. If an object is detected within 10 cm, the pump is activated for 2 seconds. The code includes setup and loop functions to handle the sensor readings and relay control.

Uploaded by

muaw1704
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

#define trigPin 9

#define echoPin 10

#define relayPin 7

long duration;

int distance;

void setup() {

pinMode(trigPin, OUTPUT);

pinMode(echoPin, INPUT);

pinMode(relayPin, OUTPUT);

digitalWrite(relayPin, HIGH); // relay mati dulu (aktif LOW)

[Link](9600);

void loop() {

// kirim gelombang ultrasonik

digitalWrite(trigPin, LOW);

delayMicroseconds(2);

digitalWrite(trigPin, HIGH);

delayMicroseconds(10);

digitalWrite(trigPin, LOW);

// baca pantulan
duration = pulseIn(echoPin, HIGH);

distance = duration * 0.034 / 2;

[Link](distance);

// jika tangan dekat (<10 cm) pompa nyala

if (distance > 0 && distance < 10) {

digitalWrite(relayPin, LOW); // pompa ON

delay(2000); // nyala 2 detik

digitalWrite(relayPin, HIGH); // pompa OFF

delay(1000);

delay(200);

You might also like