I want to create a system where the servo will make the ultrasonic sensor move and detect the area in front of it. I have made the program but when run the ultrasonic sensor can not detect as usual.

Does anyone have another program or why my program is not running as planned?

This is the source code :

#include <Servo.h> 

const int trigPin = 10;
const int echoPin = 9;
int lampu = 13 ;

// membuat nama objek servo untuk pengontrolan servo 
Servo myservo;          
// variable untuk menyimpan posisi servo 
int pos = 0;
long duration, inches, cm;
void setup() {
  pinMode(lampu, OUTPUT);

void loop() {
  pinMode(trigPin, OUTPUT);
  digitalWrite(trigPin, LOW);
  digitalWrite(trigPin, HIGH);
  digitalWrite(trigPin, LOW);
  pinMode(echoPin, INPUT);
  duration = pulseIn(echoPin, HIGH);
  inches = microsecondsKeInchi(duration);
  cm = microsecondsKeCenti(duration);
  Serial.print(" in, ");
  Serial.print(" cm");

  lampuNyala() ;

// start dari 0 derajar sampai 180 derajat 
long microsecondsKeInchi(long microseconds)
  return microseconds / 74 / 2;

long microsecondsKeCenti(long microseconds)
  return microseconds / 29 / 2;

void servo(){
  for(pos = 0; pos < 180; pos += 1)  
    // pada posisi 1 derajat
    // memberitahu servo untuk pergi ke posisi  'pos'
    // tunggu 15ms untuk pencapaian  posisi servo    
  // start dari 180 derajat ke 0 derajat 
  for(pos = 180; pos>=1; pos-=1)  
    // memberitahu servo untuk pergi ke posisi  'pos'                                
    // tunggu 15ms untuk pencapaian  posisi servo    

void lampuNyala(){
  if(cm > 0 && cm <=30){
    digitalWrite(lampu,HIGH) ;
    digitalWrite(lampu,LOW) ;
  • 1
    Comments in English would be helpful – Greenonline Jul 5 '18 at 17:10

Look at your code sequence. An ultrasonic measurement is only taken, when you use pulseIn(). At the end of the loop() function you call your servo() function. In it you tell the Arduino to first go from 0° to 180° and then back to 0°. AFTER THAT (!) the function exits and the loop() function continues. This means, that nothing else happens during the servo motion. The measurement only takes place between the motions, meaning about every 10s.

To solve this problem you need to make the servo code non-blocking. It is mostly a bad thing to use delay(), since you cannot do anything else during waiting. Think about how you are backing a pizza. You are not sitting in front of the oven the whole time, until the pizza is ready. Instead you are looking at your clock regularily and check, if it is time to take the pizza out of the oven.

You can do the same thing on an Arduino with the millis() function. It will return the milliseconds since the last reset/powerup of the Arduino. This has to be used as a clock. To do something after a specific time you first save the current time in a timestamp:

unsigned long timestamp=0;
timestamp = millis();

Then in the loop() function you are regularly (on every loop iteration) checking, if the difference between the timestamp and the current time is above your set interval:

if(millis() - timestamp > 30){
    // do something here, like moving a servo
    timestamp = millis(); //update timestamp for next event

The Arduino will not do the things simultaneously, but will switch between the different code parts so fast, that it will seem simultaneous. Also refer to the BlinkWithoutDelay example of the Arduino library.

Note: The current delay between two consecutive servo values is ok. If you want to go way faster with the servo, you have to watch out, that the rest of the code in loop() doesn't take to long. If this happens and you cannot make the other code parts non-blocking too, you can also do the servo part in an interrupt routine from a timer.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.