The slide that explains how to make circuits that connected to Raspberry PI GPIO and several sensors.
This slides show how to program using Python language.
5. http://blog.xcoda.net
Raspberry-Pi
Introduction
Raspberry Pi 3
ARMv8 CPU 1.2GHz 64bit quad-core
802.11n Wireless LAN
Bluetooth 4.1
Bluetooth Low Energy(BLE)
1GB RAM
4 USB ports
40 GPIO Pins
Full HDMI port
Ethernet port
3.5mm audio
Camera interface(CSI)
Display interface(DSI)
Micro SD card slot
VideoCore IV 3D graphics core
69. http://blog.xcoda.net
Raspberry-Pi
Digital Input
PIR 殊 Code
import RPi.GPIO as GPIO
import time
from datetime import datetime
pri_pin = 18
try:
GPIO.setmode(GPIO.BCM)
GPIO.setup(pri_pin, GPIO.IN)
val = -1
while True:
read = GPIO.input(pri_pin)
if val != read:
val = read
if val== 0:
print str(datetime.now()), "No intruder"
elif val == 1:
print str(datetime.now()), "Intruder dectected"
time.sleep(0.5)
finally:
print 'clean up'
GPIO.cleanup()
76. http://blog.xcoda.net
Raspberry-Pi
Analog Output PWM
LED Fade Code
import RPi.GPIO as GPIO
import time
try:
pin = 26
GPIO.setmode(GPIO.BCM)
GPIO.setup(pin, GPIO.OUT)
pwm = GPIO.PWM(pin, 100)
pwm.start(0)
while True:
for i in range(0, 101):
pwm.ChangeDutyCycle(i)
time.sleep(0.05)
for i in range(100, -1, -1):
pwm.ChangeDutyCycle(i)
time.sleep(0.05)
finally:
pwm.stop()
GPIO.cleanup()
86. http://blog.xcoda.net
Raspberry-Pi
Sensor Modules
DHT-11 Source
Rpi.GPIO襦 蟲
https://github.com/netikras/r-pi_DHT11/blob/master/dht11.py
def pullData():
global data
global effectiveData
global pin
data = []
effectiveData = []
GPIO.setup(pin,GPIO.OUT)
GPIO.output(pin,GPIO.HIGH)
time.sleep(0.025)
GPIO.output(pin,GPIO.LOW)
time.sleep(0.14)
GPIO.setup(pin, GPIO.IN, pull_up_down=GPIO.PUD_UP)
for i in range(0,1000):
data.append(GPIO.input(pin))
import RPi.GPIO as GPIO
import time
import sys
def bin2dec(string_num):
return str(int(string_num, 2))
data = []
effectiveData = []
bits_min=999;
bits_max=0;
HumidityBit = ""
TemperatureBit = ""
crc = ""
crc_OK = False;
Humidity = 0
Temperature = 0
pin=4
GPIO.setmode(GPIO.BCM)
87. http://blog.xcoda.net
Raspberry-Pi
Sensor Modules
DHT-11 Source
Rpi.GPIO襦 蟲 <螻>
for i in range(0, 40):
buffer = "";
while(seek < len(data) and data[seek] == 0):
seek+=1;
while(seek < len(data) and data[seek] == 1):
seek+=1;
buffer += "1";
if (len(buffer) < bits_min):
bits_min = len(buffer)
if (len(buffer) > bits_max):
bits_max = len(buffer)
effectiveData.append(buffer);
def analyzeData():
seek=0;
bits_min=9999;
bits_max=0;
global HumidityBit
global TemperatureBit
global crc
global Humidity
global Temperature
HumidityBit = ""
TemperatureBit = ""
crc = ""
while(seek < len(data) and data[seek] == 0):
seek+=1;
while(seek < len(data) and data[seek] == 1):
seek+=1;
88. http://blog.xcoda.net
Raspberry-Pi
Sensor Modules
DHT-11 Source
Rpi.GPIO襦 蟲 <螻>
def isDataValid():
global Humidity
global Temperature
global crc
print "isDataValid(): H=%d, T=%d, crc=%d"% (int(Humidity),
int(Temperature), int(bin2dec(crc)))
if int(Humidity) + int(Temperature) == int(bin2dec(crc)):
return True;
else:
return False;
def printData():
global Humidity
global Temperature
print "H: "+Humidity
print "T: "+Temperature
for i in range(0, len(effectiveData)):
if (len(effectiveData[i]) < ((bits_max + bits_min)/2)):
effectiveData[i] = "0";
else:
effectiveData[i] = "1";
for i in range(0, 8):
HumidityBit += str(effectiveData[i]);
for i in range(16, 24):
TemperatureBit += str(effectiveData[i]);
for i in range(32, 40):
crc += str(effectiveData[i]);
Humidity = bin2dec(HumidityBit)
Temperature = bin2dec(TemperatureBit)
90. http://blog.xcoda.net
Raspberry-Pi
Sensor Modules
DHT-11 Source
Adafruit DHT 覈
https://learn.adafruit.com/dht/overview
import Adafruit_DHT
sensor = Adafruit_DHT.DHT11
pin = 7
while True:
humidity, temperature = Adafruit_DHT.read_retry(sensor, pin)
if humidity is not None and temperature is not None:
print "Temp={0:0.1f}*C Humidity={1:0.1f}%".format(temperature, humidity)
else:
print "Failed to get reading."
git clone https://github.com/adafruit/Adafruit_Python_DHT.git
110. http://blog.xcoda.net
Raspberry-Pi
Camera
Pycamera Code
import time
import picamera
with picamera.PiCamera() as camera:
try:
camera.start_preview()
while True:
shutter = input('insert key when you are ready to take photo. [photo:1, video:2] ')
now_str = time.strftime("%Y%m%d-%H%M%S")
if shutter == 1:
camera.capture('/home/pi/demo/camera/photo%s.gif' %now_str)
elif shutter == 2:
camera.start_recording('/home/pi/demo/camera/video%s.h264' %now_str)
raw_input('insert key when you want to stop recoding.')
camera.stop_recording()
finally:
camera.stop_preview()