You are on page 1of 23

Arduino 6.

5 –
Controlling Speed and
Position
Mr. McBrien
TEJ2O
References

 https://www.arduino.cc/en/Reference/Servo
 https://www.arduino.cc/en/Reference/Libraries
 https://learn.parallax.com/tutorials/robot/shield-bot/rob
otics-board-education-shield-arduino/chapter-2-shield-ligh
ts-servo-11
Yesterday

 Motors
Today

 Controlling the position of a device


 Controlling the speed of a motor
 Calling an external library
Standard Motors are Great for
Propulsion
 Wheels

 Elevators
 Propellers

As long as you want a motor to turn and keep turning,


standard motors are great.
What about positioning a device?

 Aim
 Left/right
 Open/closed
 Up/down
 Grab/release

Any time we want to move an object a small distance,


it’s good to be able to know where it is.
Where Are We?

 Standard motors are the ultimate “dumb” device


 They know they are spinning, but not where they are at
any given time.
 To know the position of the device, we have to use
sensors.
Feedback Loop

 Motor + Sensor:
 Motor moves slowly
 Sensor
monitors situation and specifies stop
when objective is achieved.

In some cases, an implementation like this is needed...


For Many Implementations, There’s a
Better Way…
Servos Control Position

 Servos are like a sensor and a motor combined in one


device.
 Simple electronics inside the device allow it to control
rotation from 0 to 180 degrees
Controlling Servos with Arduinos

 The Arduino is designed to work with standard servos


without specialised coding
 A single line of code puts the servo in the position that
you desire.
 Don’t forget to put a delay in place after your command
to give it time to get in position!
Step 1 – Declare Servo

 Set pin number, and initial position.


Step 2 – Specify Position

 To position the servo, give the servo name and the


position you require.
Step 3 – Be Patient. Key Pitfall
 Weird things can happen if you start the next process
before your device is in position.
What does the “#include servo.h” mean?

 This is a call to an external library


 Additional commands will then be available to the Arduino

There are many additional libraries for various


functions. We call them when needed.
Activity

 Write three sketches (submit #2 and #3):


 1. Move a servo motor back and forth to full extension as if it’s
waving a flag. (do not submit)
 2. Move a servo back and forth ½ of its range of motion.
 3. Move a servo slowly back and forth to full extension. The
servo should take 4 seconds to complete an iteration. As your
extension here, modify in some logical way to link to LEDs.

Don’t forget the delay statement at the end of the command!


Continuous Rotation Servos

 The servos we’ve seen so far work with position as


discussed.
 There is another kind of servo that can rotate indefinitely.
 Continuous Rotation servos control speed instead of
location.
 Like the positional servos, these motors can run forwards
or backwards based on our commands.
Parallax Motors

 Our parallax motors are compatible with the Arduino servo


library.
 These continuous rotation servos are designed to allow for
control of speed and direction by the Arduino.
Calibrating the Motor

 The first time that you set up the CR servos, you need to
calibrate using a Philips screwdriver.
 Set the output for both motors to 1500 ms.
 Turn the screw until the wheel does not turn.
 Your sketch will send 1700 ms for full speed forward
rotation, 1300 for full speed backward.
 Any numbers between these values will give different
speeds.
Advantages of CR Servos

 No H-Bridge!
 We can have forward and reverse rotation without using
transistors
 No flyback diode!
 Voltage spike is dealt with by the motor itself.

The use of servo motors dramatically reduces the


complexity of our implementations.
Activity
 Obtain at least one CR servo.
 Connect the appropriate pins.
 Upload the “stationary” sketch.
 Calibrate the motor(s) for stationary position.
 Modify the code for a fast right turn. Save the sketch.
 Modify the code for a fast left rotation of the robot. Save
the sketch.

What is the difference between the sketches?


Key Points
 With standard servos, we can control the position of motors,
not just whether they turn, and how fast.
 With Continuous Rotation servos, we can control the speed
and direction of motors without an H-Bridge.
 The Arduino has specific commands for servos when you
enable the servo library
 Don’t forget to incorporate delays in your code so that the
device has time to move into position.
Homework

 Upload your sketches to Classroom.

You might also like