Things used in this project

Hardware components:
51gzz5eu9pl. sx425
Ultrasonic Sensor - HC-SR04 (Generic)
×1
SG90 9g Micro Digital Servo Motor
×1
A000066 iso both
Arduino UNO & Genuino UNO
×1
826 04
Male/Female Jumper Wires
×1
Software apps and online services:
Ide web
Arduino IDE
Hand tools and fabrication machines:
Screwdriver-Jackly-32-in-1-Magnetic-Screwdriver-Set-Repair-Tool-Kit Screwdriver-Jackly-32-in-1-Magnetic-Screwdriver-Set-Repair-Tool-Kit Screwdriver-Jackly-32-in-1-Magnetic-Screwdriver-Set-Repair-Tool-Kit Screwdriver-Jackly-32-in-1-Magnetic-Screwdriver

Schematics

connection
describing connection
Ready upload lismm4kv8h

Code

Arduino codeArduino
code
#include<Servo.h>
int trigPin=2;
int echoPin=3;

long duration;
int distance;
Servo servo;

void setup()
{
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
  Serial.begin(9600);
  servo.attach(9);
}

void loop()
{
  for(int i=15;i<=165;i++)
  {
    servo.write(i);
    delay(100);
    distance=calculateDistance();
    
    Serial.print(i);
    Serial.print(",");
    Serial.print(distance);
    Serial.print(".");
  }
  for(int i=165;i>15;i--)
  {
    servo.write(i);
    delay(100);
    distance=calculateDistance();
    
    Serial.print(i);
    Serial.print(",");
    Serial.print(distance);
    Serial.print(".");
  }
}

int calculateDistance()
{
  digitalWrite(trigPin,LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin,HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin,LOW);
  
  duration=pulseIn(echoPin,HIGH);
  distance=duration*0.034/2;
  return distance;
}

Credits

Untitled 1 copy xrzurtxfgo
Nekhil ravi
4 projects • 11 followers
HARD CORE TINKERER
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

Sign up / LoginProjectsPlatformsTopicsContestsLiveAppsBetaBlog