Let's repeat another exercise from Chapter 6, PWM – Writing Analog Information to move a micro servo motor shaft to and fro. Connect the LED to P9_14
as shown in the diagram in Chapter 6. Type the following program in Cloud9, save it as microservo.py
and run. You should be able to see the motor shaft moving in 180 degrees to and fro:
#!/usr/bin/python import Adafruit_BBIO.PWM as PWM from time import sleep servo = "P9_14" duty_min = 3 PWM.start(servo, 0, 60) for loop in range(0, 10): for i in range(0, 180): ##move shaft from 0 to 180 degree PWM.set_duty_cycle(servo, (i*0.064) + duty_min) sleep(0.01) for i in range(0, 180): ##move shaft from 180 to 0 degree PWM.set_duty_cycle(servo, (180 - i)*0.064 + duty_min) sleep(0.01) PWM.stop(servo) PWM.cleanup()
This program is similar to previous LED fade-in and -out program. As explained in Chapter 6, PWM – Writing Analog Information, the micro servo motor needs to be set at 60Hz. The duty cycle needs to be set from 3 to 14.5 in order to move shaft from 0 to 180 degree. So, we set 60 as frequency parameter in the PWM.start()
function. Then we changed the duty cycle from 3 to 14.5 in 180 steps. This moves the shaft from 0 to 180 degrees. Then we reduced the duty cycle from 14.5 back to 3 in 180 steps. So, the shaft comes back to 0 degrees.
If you noticed, all the exercises work on different pin numbers. There is no common pin in any two exercises except the power and ground pin. That means we can have everything connected in all exercises and run all at a time on a single BeagleBone. The cover photo of this book gives an idea of how this would look like.
3.16.135.225