Home Arduino An Arduino With Two Servo Motors

An Arduino With Two Servo Motors

by Howard

In my last post I looked at using a servo motor with an Arduino and a couple of LED lights to indicate when it would rotate clockwise vs. counter-clockwise. In that post we were using a simple servo motor that had 180 degrees of rotation. We were using the write() function in the Servo library from Arduino.

Since then I have received a couple of continuous rotation servo motors (SpringRC SM-S4303R) that I ordered from Foxytronics.com. Today I wanted to look at the difference in how these servo motors are controlled.

When we were using the servo with 180 degrees rotation, our signal (using PWM) was directing the servo motor where to rotate in the 0 – 180 degree range. This is often referred to as setting the angle. The speed was fairly constant, as we did not vary the voltage or resistance to the servo. In the continuous rotation servo however, our signal does not actually set the angle of rotation, rather it sets the speed.

Not only does it set the speed, it determines whether the motor should rotate clockwise or counter-clockwise. It looks like this.

0 = full speed counter-clockwise

90 = stop

180 = full speed clockwise

In today’s test I have two servos running, motor1 on pin 9 and motor2 on pin 10, both PWM pins on the Arduino. The code is pretty simple. Motor 1 will run at full speed for 5 seconds going clockwise (180) and motor2 will run at full speed counter-clockwise (0). Then I will stop both servos using write(90). After a 5 second delay I set both of the motors to be 10 degrees from their stopping points. motor1.write(100) and motor2.write(80). This runs the servos at the same rotation, but much more slowly.


Code


// MakerDaddy.com - control two continuous motion servo motors.
// Precursor to moving a robot/hobby project.
#include 

Servo motor1; // We could have called it servoLeft, etc.
Servo motor2;

int motor1Speed = 180; // Full speed clockwise
int motor2Speed = 0; // Full speed counterclockwise

void setup(){
  motor1.attach(9);
  motor2.attach(10);
}

void loop(){
  motor1.write(motor1Speed);
  motor2.write(motor2Speed); 
  delay(5000); 
  motor1.write(90);
  motor2.write(90); 
  delay(5000); 
  motor1.write(100);
  motor2.write(80); 
  delay(5000);  
}

Related Articles

1 comment

Mikeb February 27, 2022 - 3:14 am

Need to add the library Servo.h at the #include statement

Reply

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More