유은제gledel
Published

Servo Motor & Ultrasonic Sensor

Control the servo motor by using the ultrasonic sensor.

IntermediateShowcase (no instructions)133,452
Servo Motor & Ultrasonic Sensor

Things used in this project

Hardware components

Arduino UNO
Arduino UNO
×1
Breadboard (generic)
Breadboard (generic)
×1
Ultrasonic Sensor - HC-SR04 (Generic)
Ultrasonic Sensor - HC-SR04 (Generic)
×1
servo motor
×1
Jumper wires (generic)
Jumper wires (generic)
×9

Software apps and online services

Arduino IDE
Arduino IDE

Story

Read more

Schematics

schemetics of servo motor and ultra sonic sensor

Code

code of servo motor and ultra sonic sensor

EJS
#include <Servo.h>
Servo servo1;
int trigPin = 9;
int echoPin = 8;
long distance;
long duration;
 
void setup() 
{
servo1.attach(7); 
 pinMode(trigPin, OUTPUT);
 pinMode(echoPin, INPUT);// put your setup code here, to run once:
}
 
void loop() {
  ultra();
  servo1.write(0);
  if(distance <= 10){
  servo1.write(90);
  }
}
 
void ultra(){
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);
  duration = pulseIn(echoPin, HIGH);
  distance = duration*0.034/2;
  }

Credits

유은제

유은제

15 projects • 13 followers
Hello :-) I'm a high school student in South Korea.
gledel

gledel

100 projects • 115 followers
Looking back on my childhood, I was happy when I was making something and I was proud of myself. "Making is instinct!"

Comments