Parts Required 1 Servomotor Jumper wires 1 Three-pin male/male header or plug directly into Board of Education servo slot | Three-pin male/male header | Building the Circuit - Turn off the Board of Education
- Place the three-pin header in the board, as indicated in figure below
- Connect the servo to the other side of the header
- Servomotors have three wires. The black wire is connected to the ground (Vss) of your BS2, the red wire to the positive voltage (Vdd), and the white wire to the signal source (P12).
| Servo motor using 3 pin connector into BS2 breadboad. | | schematic of the pin connections to the BS2 and back to the servo |
Pulsout, For…Next Loops In this lesson, you will learn to use the Pbasic code word PULSOUT and you will use FOR…NEXT LOOPS in order to control the servomotor speed, direction, and position. PULSOUT is code that creates PWM at a certain frequency. A servomotor is rotated by using the micro controller to send very brief high and low signals over and over again every 20 ms. The high signal’s duration is between 1 and 2 ms. Here is the command syntax for PULSOUT: PULSOUT Pin, Duration Pin is the specified output pin you want the pulse to come from. Duration creates a high signal in two –millionths-of-a-second (ms) time intervals. | A microsecond (ms) is one millionth of a second. | | Servo motor with horn used to attach mechanics |
The first program we will explore is one called Simple Servo, and it allows you to control the movement of a servomotor through a full swing of the servo. Enter the code below ‘Simple Servo program ' {$STAMP BS2} ' {$PBASIC 2.5}
Time VAR Word 'create a variable named 'time' to store FOR...NEXT cycle Move: 'start the program at a label named 'move' FOR time= 1 TO 100 'start a repeating cycle that repeats 100 times PULSOUT 12, 500 'send information to servo to move to a position PAUSE 10 'pause for 10 ms to allow servo to process info NEXT PAUSE 100 'pause for a short time in this position FOR time= 1 TO 100 'start a repeating cycle that repeats 100 times PULSOUT 12,1000 'send information to servo to move to a different position PAUSE 10 'pause for 10 ms to allow servo to process info NEXT PAUSE 100 'pause for a short time in this position GOTO move 'start again at label 'move' Notice that it moves fast and that if you hold the horn a bit, it will fight you to get to the place the feedback unit is instructing it to travel to. This next program has two separate parts, the servo will step through a number of small rotations and then move backwards rapidly to its original position and start the cycle again. In the first program, the position of the servo was provided in the code by the numbers 500 and 1000. In this program the servo positions will also be numbers, but given representationally in the variable named ‘forward’. The first movements are accomplished with a clever application of the FOR…NEXT command, which starts with 500 and counts to 1000 by hundreds each time it cycles. Large numbers need to be used in this loop because servos only accept numbers between 500 and 1000. Whenever the program sees the variable ‘forward’, it will use whatever number the FOR…NEXT loop is currently providing as the position information for the PULSOUT command. Here, the FOR…NEXT loop acts both as a programming loop and as a numerical generator for the servo’s position information. In the last part of the program, the command PULSOUT 12, 500 is used to return the servo to its starting position. Enter the code below ‘Start/Stop program ' {$STAMP BS2} ' {$PBASIC 2.5}
Forward VAR Word 'variable 'forward' acts here as a loop counter and as ‘servo position information Getback VAR Word 'variable 'getback' acts here as a loop counter Start: 'start the program at a label named 'start' FOR forward = 500 TO 1000 STEP 100 'start a repeating cycle that counts up from 500 to 1000 ‘by adding 100 each cycle PULSOUT 12, forward 'send loop counter info stored in 'forward' to servo ‘Information for position information PAUSE 10 'pausing for 10 allows time for servo to ‘receive info from stamp PAUSE 1000 'pause for 1 second at each new position NEXT FOR getback= 1 TO 100 'this FOR...NEXT loop returns the servo to a starting ‘position PULSOUT 12, 500 'here, 200 is the position info sent to the servo PAUSE 10 NEXT GOTO start 'start again at label 'start' The next program is designed to move the motor very slowly. This is controlled by creating a new variable called ‘time’, which is counted step-by-step, as the FOR…NEXT loop advances. Enter the code below ' {$STAMP BS2} ' {$PBASIC 2.5}
Time VAR Word 'variable 'time' is loop counter for servo position Slow: 'start the program at a label named 'slow' FOR time= 500 TO 1000 'start a counter that starts at 500 and counts to 1000 one by one PULSOUT 12,time 'send a pulse that varies from 2 ms to 1000 ms. PAUSE 20 'pausing for 20 milliseconds NEXT FOR time= 1000 TO 500 'start a counter that counts down from 1000 to 500 PULSOUT 12,time ‘pulsout to servo motor the varying time variable PAUSE 20 'pausing for 20 milliseconds NEXT GOTO slow 'start again at label 'slow' Now try changing the pause lengths and see what happens. Remember, a pause below ten will not give the servo enough time to respond, but go ahead try it out to see what happens.
|