Anjali Shaw
Published © GPL3+

Servo motor + ultrasonic sensor 2

This is the continuation of previous project (servo motor + ultrasonic sensor ) with a little modification.

IntermediateFull instructions provided1 hour6,737
Servo motor + ultrasonic sensor 2

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
Buzzer
Buzzer
×1
SG90 Micro-servo motor
SG90 Micro-servo motor
×1
Male/Female Jumper Wires
Male/Female Jumper Wires
×1
Jumper wires (generic)
Jumper wires (generic)
×1

Software apps and online services

Arduino IDE
Arduino IDE

Story

Read more

Schematics

Circuit diagram

Carefully make the connections.

Code

The project code

Arduino
The code for project is given below.
you have to make the connections as per code.
attach the trig and echo pin of ultrasonic sensor to the pin 8 and 9 respectively.
and the connection for buzzer is by pin 10.
I would like to suggest you guys that you should write the code and must not just copy paste it.
#include<Servo.h>
int trig=8;
int echo=9;
int buzz=10;
int dt=10;
Servo servo;

//int distance,duration;
void setup() {
  // put your setup code here, to run once:
pinMode(trig,OUTPUT);
pinMode(buzz,OUTPUT);
pinMode(echo,INPUT);
Serial.begin(9600);
servo.attach(11);
}

void loop() {
  // put your main code here, to run repeatedly:
if (calc_dis()<15)
{
  digitalWrite(buzz,HIGH);
}
else
{
  digitalWrite(buzz,LOW);
}

if (calc_dis()<10)
{
  for (int i=0;i<=540;i++)
  {
    servo.write(i);
    delay(1);
  }
  delay(100);
  for (int i=540;i>=0;i--)
  {
    servo.write(i);
    delay(1);
  }
  delay(100);
}
}


int calc_dis()
{
  int duration,distance;
  digitalWrite(trig,HIGH);
  delay(dt);
  digitalWrite(trig,LOW);
  duration=pulseIn(echo,HIGH);
  distance = (duration/2) / 29.1;
  return distance;
}

Credits

Anjali Shaw

Anjali Shaw

10 projects • 7 followers

Comments