Let's start putting the S3's Hacker Port to use by connecting and controlling a Parallax Standard Servo. This tutorial will first demonstrate how to make a Standard Servo hold three different positions, and then how to approximate smooth, continuous motion with small, evenly-timed changes in position. With the S3’s Hacker Port, the physical connection is extremely easy and with BlocklyProp, coding is greatly simplified.
You will need the following accessory parts:
A hobby servo is a small device that controls the position of flaps, rudders, and steering in many radio-controlled toy planes, boats, and cars. The Parallax Standard Servo is a hobby servo that can also be useful in many robotics and animatronics projects. Since it can both move to and hold a position, it is ideal for tasks like rotating a distance sensor or controlling the fingers in a robotic hand.
For more information on Standard Servos and how they work, see: Propeller C Simple Devices - Standard Servo [2].
It helps to create some sort of angle marker on the turning part of the servo, the horn, to more easily indicate which position it is in. A jumper wire or twist-tie works well for this.
A Parallax Standard Servo is rated for a 4 to 6 V (volt) supply. The S3 User Guide notes that the S3 provides regulated 5V power, Ground (G) and access to I/O Pins 0 - 5 from the Hacker Port. Connections to 3.3 V power and Analog Pins 0 and 1 are also available from the Hacker Port.
All the info you need to properly connect the servo to your S3 is in the image below. Remember that it is printed right on the surface of your S3 directly adjacent to the Hacker Port. As you can see, there are 8 rows of pins arranged in 3 columns.
Make sure the power is off on your S3 and it is unplugged from your programming/charging cable. Connect the Standard Servo's black/red/white cable connector to the pin row marked P0. Orient the connector so that the black wire is connected to the Ground column, the red wire is connected to the middle (5V), and the white signal connector is on the pin column - all along the row marked P0. Be careful not to accidently connect it to one of the rows labeled A0 or A1.
Now, you can write the BlocklyProp Standard Servo test program to hold the three positions shown above. The code should move the servo to the three positions (0°, 90°, and 180° ) with three seconds to move between and hold each position. This code will also change the indicator LEDs based on the servo position. While the program is running, the servo will resist any attempt to move it out of position. When the program finishes, the servo will stop resisting.
In the ACTIONS > MOTORS category, you will find the rotate servo block and disable servo block. Other blocks will not be discussed, as they were used in previous tutorials.
With BlocklyProp, the code explains itself:
As the image above shows, the rotate servo block allows you to set two parameters: the pin the servo is connected to and the servo angle in degrees. For example, to make the servo connected to P0 turn to 90-degrees, the block is set to P0 and 90. Keep in mind that the rotate servo block does not wait for the servo to get into position - it is necessary to include a wait block for the servo to reach the desired position before setting a new angle. Finally, the disable servo block makes sure that pulses to the servo are ended.
Note: Standard Servos have a 180-degree range, plus or minus small differences between individual units. Be careful not to enter a servo angle greater than 180 degrees or you could damage the motor.
Special thanks to Parallax friend Whit Stodghill for his assistance in writing, editing, and testing material for these S3 tutorials.
Links
[1] https://www.parallax.com/product/900-00005
[2] https://learn.parallax.com/tutorials/language/propeller-c/propeller-c-simple-devices/standard-servo
[3] http://blockly.parallax.com/