tinker_knight
Published © GPL3+

Ultra Sensor Distance Flag!

Learn how to make a servo-controlled flag that pops up when you get close to it.

BeginnerFull instructions provided221
Ultra Sensor Distance Flag!

Things used in this project

Hardware components

Arduino UNO
Arduino UNO
×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
Ultrasonic Sensor - HC-SR04 (Generic)
Ultrasonic Sensor - HC-SR04 (Generic)
×1
Popsicle Stick
×1
Cardboard
×1
Chopstick
×1
Paper
For your pop-up sign.
×1

Software apps and online services

Arduino IDE
Arduino IDE

Hand tools and fabrication machines

Hot glue gun (generic)
Hot glue gun (generic)

Story

Read more

Schematics

Build Schematic

Code

Code

C/C++
// ---------------------------------------------------------------- //
// Arduino Ultrasoninc Sensor HC-SR04
// Re-writed by Arbi Abdul Jabbaar
// Using Arduino IDE 1.8.7
// Using HC-SR04 Module
// Tested on 17 September 2019
// Modified by tinker_knight 7 of October 2021
// ---------------------------------------------------------------- //
#include <Servo.h> // includes the servo library

#define echoPin 2 // attach pin D2 Arduino to pin Echo of HC-SR04
#define trigPin 3 //attach pin D3 Arduino to pin Trig of HC-SR0

// defines variables
long duration; // variable for the duration of sound wave travel
int distance; // variable for the distance measurement
Servo myservo; // defines the Servo motor as "myservo"

void setup() {
  pinMode(trigPin, OUTPUT); // Sets the trigPin as an OUTPUT
  pinMode(echoPin, INPUT); // Sets the echoPin as an INPUT
  Serial.begin(9600); // // Serial Communication is starting with 9600 of baudrate speed
  Serial.println("Ultrasonic Sensor HC-SR04 Test"); // print some text in Serial Monitor
  Serial.println("with Arduino UNO R3");
  myservo.attach(7); // tells the arduino that the servo, myservo, is attached to pin 7
}
void loop() {
  // Clears the trigPin condition
  myservo.write(0); // sets the servo to 0 degrees
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  // Sets the trigPin HIGH (ACTIVE) for 10 microseconds
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);
  // Reads the echoPin, returns the sound wave travel time in microseconds
  duration = pulseIn(echoPin, HIGH);
  // Calculating the distance
  distance = duration * 0.034 / 2; // Speed of sound wave divided by 2 (go and back)
  // Displays the distance on the Serial Monitor
  Serial.print("Distance: ");
  Serial.print(distance);
  Serial.println(" cm");

  if (distance < 30) {
    myservo.write(90); // raises the servo to 90 degrees
    delay(3000); // keep this delay the same so the ultra sensor won't slow down!
    myservo.write(180); // lowers the servo back to normal
  }
}

Credits

tinker_knight
0 projects • 0 followers
Contact

Comments

Please log in or sign up to comment.