Search icon
Arrow left icon
All Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletters
Free Learning
Arrow right icon
Programming the BeagleBone

You're reading from  Programming the BeagleBone

Product type Book
Published in Jan 2016
Publisher
ISBN-13 9781784390013
Pages 180 pages
Edition 1st Edition
Languages

Table of Contents (21) Chapters

Programming the BeagleBone
Credits
About the Author
Acknowledgment
About the Reviewers
www.PacktPub.com
Preface
Cloud9 IDE Blinking Onboard LEDs Blinking External LEDs Controlling LED Using a Push Button Reading from Analog Sensors PWM – Writing Analog Information Internet of Things with BeagleBone Physical Computing in Python UART, I2C, and SPI Programming Internet of Things using Python GPIO Control in Bash BeagleBone Capes
Pinmux and the Device Tree Index

Program to control micro servo motor


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()

Explanation

This program is similar to previous LED fade-in and -out program. As explained in Chapter 6, PWM – Writing Analog Information, the micro...

lock icon The rest of the chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime}