Arduino + Servos

code for connecting servo90 to HC-SR04

Hey Pradeep,

What were you attempting to do with the Ultrasonic/Servo combo?

Give us a bit more detail and we’ll do our best to help out!

To just blurt out what you want is rude. Try a little tenderness and provide more info please.

i am trying to make a smart dustbin. It is one in which the lid opens(with 90 degree rotation of servo motor)once an object comes in front of hc-sr04 sensor. Please help out with the code. Thanks.

Hi Pradeep,

I have been working with servos and the HC-SR04. I assume you would be using an Arduino UNO.
This should get you started.
The Arduino Ultra Sonic function works and is quite accurate, but an object at 45 degrees to the sensor can give false readings. The sound waves simple bounce off at 45 degrees and are not reflected back to the sensor.

Cheers
Jim
(Core Electronics Customer)

//===========================================================================
//    UNO Dust Bin Lid V1.0
//===========================================================================
#include <Servo.h>

// Pin definitions
#define UTRIG        2              // ultra sonic trigger, output
#define UECHO        4              // ultra sonic echo, input
#define LIDTILT      6              // 

unsigned long millisCount = 0L;

// Create Servo instance
Servo LidTiltServo;
                        
//===========================================================================
void setup() {
// Setup Ultra Sonic sensor pins
  pinMode(UTRIG,OUTPUT);
  pinMode(UECHO,INPUT);

// Setup Servo pins
  LidTiltServo.attach(LIDTILT);

// Position Servos at close point
  LidTiltServo.write(0);                // 0 degrees is closed, 90 degrees is open

}
//===========================================================================
void loop() {

  if (CheckDistance() < 10) {             // something less than 10 cm from bin
    LidTiltServo.write(90);               // 0 degrees is closed, 90 degrees is open
  } else {
    LidTiltServo.write(0);                // 0 degrees is closed, 90 degrees is open
  }
  delay(1000);                            // check every second 
}
//===========================================================================
//===========================================================================
//    Send an Ultra Sonic pulse out, measure the time taken for the echo to return,
//    calculate the distance in centimeters and return this value.
//
//      This routine takes about 700 to 800 uSecs to process the return.
//      Actual time taken to complete routine depends on how long echo takes to return.
//      Measured: 169cm 10736us, 48cm 3552us, 5cm 976us.
//      If there is no return, pulseIn will take 1 second to timeout.
//===========================================================================
int CheckDistance(void) {

  int distance = 0;
  long duration = 0;

    digitalWrite(UTRIG, LOW);                   // Clear the trigPin
    delayMicroseconds(2);
    digitalWrite(UTRIG, HIGH);                  // Start the ultra sonic pulse train
    delayMicroseconds(10);
    digitalWrite(UTRIG, LOW);
    duration = pulseIn(UECHO, HIGH);            // waits for UECHO to go high then low 
                                                // returns the time taken in uSecs
                                                // using the timeout parameter causes motor pins to reset
    distance = (long)((duration * 0.034) / 2);  // speed of sound = 340 metres/second (0.034 cm/s)
                                                // divide by 2 because sound travels out and echo back
    millisDelay(2);
  return (distance);
}
//===========================================================================
void millisDelay (int d) {
  millisCount = millis() + d;
  while (millis() < millisCount) {}
  return;
}
//===========================================================================
//===========================================================================
3 Likes

Thanks a lot. I will try it out.