Sensor + Servo Machine (NEED HELP)

I'm trying to make a machine using sensors and a servo. Everything's perfectly fine but my servo isn't moving. Can anyone tell me why?

#include <LiquidCrystal.h> // includes the LiquidCrystal Library
#include <Servo.h>
LiquidCrystal lcd(1, 2, 4, 5, 6, 7); // Creates an LCD object. Parameters: (rs, enable, d4, d5, d6, d7)
Servo myservo;
const int led = 0;
const int trigPin = 9;
const int echoPin = 10;
long duration;
int distanceCm, distanceInch;
int noteduration = 500;
void setup() {
lcd.begin(16,2); // Initializes the interface to the LCD screen, and specifies the dimensions (width and height) of the display
pinMode(trigPin, OUTPUT);
pinMode(led, OUTPUT);
pinMode(echoPin, INPUT);
void loop() {
digitalWrite(trigPin, LOW);
digitalWrite(trigPin, HIGH);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distanceCm= duration*0.034/2;
distanceInch = duration*0.0133/2;
lcd.setCursor(0,0); // Sets the location at which subsequent text written to the LCD will be displayed
lcd.print("Put Hand Under "); // Prints string "Distance" on the LCD
lcd.print("Arrow ");
if (distanceInch<5)
  digitalWrite(led, HIGH);
else {
    digitalWrite(led, LOW);

The delay in your loop is too short to see the servo working.

1. You need to declare the following global variables as floats:

float distanceCm, distanceInch;

2. Try the above quoted code with the following parameter and see if Servo moves.


3. If the Servo still does not move, try by putting the following code above the if()...

distnaceInch = 4.15;    //test purpose
if (distanceInch<5.0)
  digitalWrite(led, HIGH);

That does not make sense. Angle values are limited to 0...180.
Please provide a link to the servo you are using. Especially digital servos ignore pulse values out of their limits.

What are your sensors?
If you are using the ultrasonic sensor, try using NewPing library to do your ultrasonic sensor work/calcs.
Also in the IDE press CTRL and T keys to auto format your code to make it easier to read.

Thanks.. Tom.. :smiley: :+1: :coffee: :australia:

apparently my servos are just broken. Thanks for the help though!

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.