I need some help

Hi I need you help
I just followed steps in this video How to make Smart Dustbin with Arduino | Arduino Project - YouTube
but the results my servo motor does not stop rotate it always rotate till I disconnect the power

Dustbin.ino (1.12 KB)

:slight_smile:
Can you try this code and let me know if this works.

#include <Servo.h> // Servo library

Servo myServo; // Change Name Accordingly

int trigPin = 2; // Change Pin Accordingly
int echoPin = 3; // Change Pin Accordingly
int servoPin = 4; // Change Pin Accordingly
int led = 5; // Change Pin Accordingly

long duration, distance;

void setup() {
  // put your setup code here, to run once:
  //Serial.begin(9600);   //For Troubleshooting
  myServo.attach(servoPin);

  pinMode(trigPin, OUTPUT); // Declare Trigger Pin as Output
  pinMode(echoPin, INPUT); // Declare Echo Pin as Input

  servo.write(0);
}

void loop() {
  // put your main code here, to run repeatedly:
  digitalWrite(led, HIGH);
  digitalWrite(trigPin, LOW);
  delayMicroseconds(5);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(20);
  digitalWrite(trigPin, LOW);
  duration = pulseIn(echoPin, HIGH);
  dist = duration * 0.034 / 2; //Measure Distance

  if (dist < 100) { // Change Distance Value Accordingly
    myServo.write(150); // Change Degree Value Accordingly
    delay(5000); // 5 sec Delay, //Change Accordingly
    myServo.write(0);
  }
  //Serial.println(dist);   //For Troubleshooting
}

What type of servo do you have ?
Is it by any chance a "continuous rotation servo" ?