Things used in this project

Hardware components:
A000066 iso both
Arduino UNO & Genuino UNO
×1
51gzz5eu9pl. sx425
Ultrasonic Sensor - HC-SR04 (Generic)
×1
11026 02
Jumper wires (generic)
×1
09590 01
LED (generic)
×1
Software apps and online services:
Ide web
Arduino IDE

Schematics

Code

code programny digiital rulerArduino
const int trigPin = 10;
const int echoPin = 9;
int lampu = 13 ;

void setup() {
  Serial.begin(9600);
  pinMode(lampu, OUTPUT); 
}

long duration, inches, cm;
 
void loop()
{
  pinMode(trigPin, OUTPUT);
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);

  pinMode(echoPin, INPUT);
  duration = pulseIn(echoPin, HIGH);

  inches = microsecondsKeInchi(duration);
  cm = microsecondsKeCenti(duration);
  
  Serial.print(inches);
  Serial.print(" in, ");
  Serial.print(cm);
  Serial.print(" cm");
  Serial.println();

  lampuNyala() ; 
 
  
  delay(10);
}

long microsecondsKeInchi(long microseconds)
{
  return microseconds / 74 / 2;
}

long microsecondsKeCenti(long microseconds)
{
  return microseconds / 29 / 2;
}

void lampuNyala(){
   if(cm > 0 && cm <=30){
      digitalWrite(lampu,HIGH) ; 
    }
    else{
      digitalWrite(lampu,LOW) ; 
      }
  }

Credits

Fikri wibowo zclir8iqbw
Fikri Wibowo
1 project • 0 followers
"nothing impossible"
Contact

Replications

Did you replicate this project? Share it!

I made one

Love this project? Think it could be improved? Tell us what you think!

Give feedback

Comments

Add projectSign up / Login