Project Name: Distance LED Bar

Aim of the project:  How to Program a Ultrasonic to move noob of servo

Component:

  • Arduino
  • Ultrasonic
  • Servo Motor
  • Jumper Wire

Circuit Diagram:

Code:

#include <Servo.h>

#include <NewPing.h>

const int ServoPin = 10;
const int TriggerPin = 6;
const int EchoPin = 7;

// 100 = maxDistance
NewPing sonar (TriggerPin, EchoPin, 100);
Servo servo;

void setup() {
  Serial.begin(9600);
  servo.attach(ServoPin);
}

void loop() {
  int cm = sonar.ping_cm();
  Serial.println(cm);

  int angle = map(cm, 2, 15, 15, 100);
  servo.write(angle);

  delay(50);
}

Video: