Changing to Servo instead of Stepper

Hi Everyone!

I am about to recreate this project - https://create.arduino.cc/projecthub/lbf20012001/sound-location-finder-92e6b0?ref=search&ref_id=sound%20sensor&offset=1

But I want to use my servo instead.

The Code Given by Him is -

const int stepsPerRevolution = 400;  // change this to fit the number of steps per revolution for your motor
const int numberOfSteps = stepsPerRevolution/8; //45 degree turns
const int dirPin=12; 
const int stepPin=13;
const int rightSensorPin=7;
const int leftSensorPin=8;
const int enablePin=5 ;
boolean rightVal = 0;
boolean leftVal = 0;

void setup()
{
  pinMode(leftSensorPin, INPUT); //Make pin 8 an input pin.
  pinMode(rightSensorPin, INPUT); //Make pin 7 an input pin.
  pinMode (stepPin, OUTPUT); //Make pin 13 an output pin.
  pinMode (dirPin, OUTPUT); //Make pin 12 an output pin.
  pinMode (enablePin, OUTPUT); //Make pin 5 an output pin.

  digitalWrite(enablePin, LOW); //Enable is active low
  Serial.begin (9600); // initialize the serial port:
}
  
void loop ()
{
  //poll inputs for signal
  rightVal =digitalRead(rightSensorPin);
  leftVal =digitalRead(leftSensorPin);
  
  // when the sensor detects a signal above the threshold value set on sensor, turn finder to the direction of sound
  if (leftVal==LOW && rightVal==HIGH)
  {
    Serial.println("Turning Right");
    digitalWrite(dirPin,LOW); //turn counter-clockwise
    
    //turn finder in the direction of the sound
    for(int steps = 0; steps < numberOfSteps; steps++)
    {
      //create pulse to turn motor one step at a time
      digitalWrite(stepPin,HIGH);
      delayMicroseconds(10000);
      digitalWrite(stepPin, LOW);
      delayMicroseconds(10000);
    }
    delayMicroseconds(100000);
    rightVal = 0;
    leftVal = 0;
  }
  else if (leftVal==HIGH && rightVal==LOW)
  {
    Serial.println("Turning Left");
    digitalWrite(dirPin,HIGH); //turn clockwise
      
    //turn finder in the direction of the sound
    for(int steps = 0; steps < numberOfSteps; steps++)
    {
      //create pulse to turn motor one step at a time
      digitalWrite(stepPin,HIGH);
      delayMicroseconds(10000);
      digitalWrite(stepPin, LOW);
      delayMicroseconds(10000);
    }
    delayMicroseconds(100000);
    rightVal = 0;
    leftVal = 0;
  }
  else 
  {
    //Do nothing
    rightVal = 0;
    leftVal = 0;
  }
}

SO, to change stepper motor to servo, we must use

servo.write(θ) // where theta is the angle required

Now I have changed to code to

#include <Servo.h>


const int rightSensorPin=7;
const int leftSensorPin=8;
boolean rightVal = 0;
boolean leftVal = 0;

int pos = 90;

Servo myservo;

void setup()
{
  myservo.attach(9);
  
  pinMode(leftSensorPin, INPUT); //Make pin 8 an input pin.
  pinMode(rightSensorPin, INPUT); //Make pin 7 an input pin.

  Serial.begin (9600); // initialize the serial port:
}
  
void loop ()
{
  //poll inputs for signal
  rightVal =digitalRead(rightSensorPin);
  leftVal =digitalRead(leftSensorPin);
  
  // when the sensor detects a signal above the threshold value set on sensor, turn finder to the direction of sound
  if (leftVal==LOW && rightVal==HIGH)
  {
    Serial.println("Turning Right");  
      for (pos = 90; pos <= 180; pos += 1) { // goes from 90 degrees to 180 degrees
    // in steps of 1 degree
    myservo.write(pos);              // tell servo to go to position in variable 'pos'
    delay(1000);
  }
 }
  else if (leftVal==HIGH && rightVal==LOW)
  {
    Serial.println("Turning Left");
    for (pos = 180; pos >= 90; pos -= 1) { // goes from 180 degrees to 90 degrees
    myservo.write(pos);              // tell servo to go to position in variable 'pos'
    delay(1000);
  }
 }
  else 
  {
    //Do nothing
    rightVal = 0;
    leftVal = 0;
  }
}

There is no error while compiling, but I don't wanna damage my arduino and sensors.. so could anyone tell me if there's a mistake cuz I am new to this....

Also, here's my schematic diagram -

And also, I want the servo to turn from 90 to 180 degree if the sound is received from left sensor and 90 to 0 degree if the sound is received from left sensor. And then return back to the initial position, which is 90 degree after a delay of 1000 ms.

Looking forward for your help!

Don't power the servo from the Arduino 5V and GND pins as it cannot provide enough current reliably if at all

Presumably the stepper was not originally powered from the Arduino

Do you want the servo to sweep all the way from 0 to 180 if the leftVal is LOW?

Sorry... actually i wanted the servo to turn from 90 to 180 degree if the sound is received from left sensor and 90 to 0 if the sound is received from left sensor

Then you could start with pos=90. The servo will step 1 deg every second until it reaches 180 degrees.
The rest of it looks good.

And, as per UKHeliBob, be sure to power the servo from its own power supply. But also be sure to connect the GNDs.

Thank you sir.. I will let u know once I do the proper connections and upload.. and may I know how to power the servo separately? I am a beginner. So i really didn't understand what that means..

You need a power supply that can provide the appropriate voltage and current for your particular servo - look up what those values are at the manufacturer's site.

For a typical hobby servo, it'll probably need 6V and at least 1A, preferably a bit more than 1.

Connect power and ground to the servo and ground also to the Arduino. Leave the signal wire as is. Don't run power through the breadboard, go direct to the servo.

so sir..., how can I power the sound sensor?

The ultrasonic sensors take little current so can be powered directly from the Arduino 5V and GND pins

sir, ik I am mean... but how can we connect both the sensors as well as a servo to the arduino directly without a breadboard?

It is OK to use a breadboard to connect the sensors but not the servo due to the current required

The Uno has several (4) GND connections and it is safe to use any of them to provide the GND connections to the sensors and servo

sorry, but the last question is, U want me to connect the 5v for the sensor and servo through a breadboard but give the ground connection directly? bcz I dont want to ruin my board due to my self confidence!

NO !

  • Connect an external 5V or 6V supply and its GND directly to the servo, not via the Arduino
  • Connect the GND of the servo to GND on the Arduino (via a breadboard if necessary, but it should not be)
  • Connect the signal wire of the servo directly to your chosen Arduino pin

The servo will get its power from the external power source and will be controlled by the Arduino

Use a breadboard to connect power and signal pins from the Arduino to the ultrasonic sensors

1 Like

got it... THANKS!

hello sir.. everything's fine but the servo is not moving left or right.. should I change my code?

Yes. Add Serial output to tell you the values of 'leftVal' and 'rightVal'. Maybe just under:

  rightVal = digitalRead(rightSensorPin);
  leftVal = digitalRead(leftSensorPin);

put

  Serial.print(leftVal);
  Serial.println(rightVal);

If Serial Monitor shows 00 or 11 then your servo is not supposed to turn and all is well. If Serial Monitor shows 10 or 01 and that is not followed by "Turning Left" or "Turning Right" then something is wrong in the code. If the "Turning Left" or "Turning Right" message appears and the servo doesn't move, there is more likely a hardware problem.

sir. idk what but my servo is not turning either side and in the serial port, I can see Turning Right text. Nothing else

will using a stepper be better than servo?

Does your servo work if you run the Sweep tutorial?

yes sir, the servo and sound sensor works fine when tested separately.