Search icon CANCEL
Subscription
0
Cart icon
Cart
Close icon
You have no products in your basket yet
Save more on your purchases!
Savings automatically calculated. No voucher code required
Arrow left icon
All Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletters
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
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
1. Cloud9 IDE 2. Blinking Onboard LEDs 3. Blinking External LEDs 4. Controlling LED Using a Push Button 5. Reading from Analog Sensors 6. PWM – Writing Analog Information 7. Internet of Things with BeagleBone 8. Physical Computing in Python 9. UART, I2C, and SPI Programming 10. Internet of Things using Python GPIO Control in Bash BeagleBone Capes
Pinmux and the Device Tree Index

A Program to check light intensity


In Chapter 5, Reading from Analog Sensors, we used a LDR (Light Dependent Resistor) to measure light intensity. Let's write a Python program to do the same.

Connect the LDR to P9_38 as shown in the diagram in Chapter 5. It includes a voltage divider to reduce the high magnitude of output voltage. Type the following program in Cloud9, save it as LDR.py and run. It will print the output voltage. If the LDR is exposed to bright sunlight, you may get the output message – Looks like a bright sunny day. If this is not possible, point a torch on the LDR surface. You should see voltage has increased. In case it detects a high voltage, it will print that it is a sunny day.

#!/usr/bin/python
import Adafruit_BBIO.ADC as ADC
from time import sleep

LDR = "P9_38"

ADC.setup()

while True:
    volts = ADC.read(LDR)* 1.8
    print "Voltage at input pin = " + str(volts)
    if (volts > 1.6):
        print "Looks like bright sunny day"
    sleep(1)

Explanation

This program...

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}