sensors, actuators and the raspberry pi using python

25
Sensors, actuators and the Raspberry PI Programming GPIO using Python

Upload: derek-kiong

Post on 21-Apr-2017

829 views

Category:

Devices & Hardware


1 download

TRANSCRIPT

Page 1: Sensors, actuators and the Raspberry PI using Python

Sensors, actuators and the Raspberry PI

Programming GPIO using Python

Page 2: Sensors, actuators and the Raspberry PI using Python

Raspberry PI vs Desktop PC

Smaller footprint Slower processor Less memory Non-standard peripherals

Page 3: Sensors, actuators and the Raspberry PI using Python
Page 4: Sensors, actuators and the Raspberry PI using Python
Page 5: Sensors, actuators and the Raspberry PI using Python
Page 6: Sensors, actuators and the Raspberry PI using Python

Using GPIO – output mode

import RPi.GPIO as GPIO GPIO.setmode(GPIO.BCM) # refer to GPIO pinsGPIO.setwarnings(False) # ignore warnings

GPIO.setup(pin, GPIO.OUT) # make GPIO pin for output

GPIO.output(pin, GPIO.HIGH) # turn ON GPIO pin

GPIO.output(pin, GPIO.LOW) # turn OFF GPIO pin

Page 7: Sensors, actuators and the Raspberry PI using Python

Using GPIO – input mode

import RPi.GPIO as GPIO GPIO.setmode(GPIO.BCM) # refer to GPIO pinsGPIO.setwarnings(False) # ignore warnings

GPIO.setup(pin, GPIO.IN) # make GPIO pin for input

val = GPIO.input(pin) # read GPIO pin

Page 8: Sensors, actuators and the Raspberry PI using Python
Page 9: Sensors, actuators and the Raspberry PI using Python
Page 10: Sensors, actuators and the Raspberry PI using Python
Page 11: Sensors, actuators and the Raspberry PI using Python

Relay Connections

Relay board Raspberry PI

GND 6

IN1 15 (GPIO 22)

IN2 16 (GPIO 23)

IN3 18 (GPIO 24)

IN4 22 (GPIO 25)

VCC 2

Page 12: Sensors, actuators and the Raspberry PI using Python
Page 13: Sensors, actuators and the Raspberry PI using Python

Relay controller sample code

import RPi.GPIO as GPIOimport time

pins = [22,23,24,25]

for p in pins: GPIO.setup(p, GPIO.OUT)

while True: for q in pins: GPIO.output(q, GPIO.HIGH) time.sleep(5) for q in pins: GPIO.output(q, GPIO.LOW) time.sleep(5)

Page 14: Sensors, actuators and the Raspberry PI using Python
Page 15: Sensors, actuators and the Raspberry PI using Python
Page 16: Sensors, actuators and the Raspberry PI using Python
Page 17: Sensors, actuators and the Raspberry PI using Python

Sensor Connections

Sensor board Raspberry PI

GND 6

OUT 11 (GPIO 17)

VCC 2

Page 18: Sensors, actuators and the Raspberry PI using Python

Sensor sample code

pin = 17GPIO.setup(pin, GPIO.IN) # make GPIO pin for input

state = GPIO.input(pin) # read GPIO pinwhile True: time.sleep(1) r = GPIO.input(pin) if (r != state): state = r print "state changed"

Page 19: Sensors, actuators and the Raspberry PI using Python
Page 20: Sensors, actuators and the Raspberry PI using Python
Page 21: Sensors, actuators and the Raspberry PI using Python

AD/DA board connections

AD/DA board Raspberry PI

SDA 3

SCL 5

VCC 2

GND 6

Page 22: Sensors, actuators and the Raspberry PI using Python

Convertor sample codeimport smbus

def read(a): bus.read_byte_data(0x48, a) return bus.read_data(0x48)

bus = smbus.SMBus(1)

control = read(0)light = read(1)temperature = read(2)custom = read(3)

bus.write_byte(self.addr, 99)

Page 23: Sensors, actuators and the Raspberry PI using Python
Page 24: Sensors, actuators and the Raspberry PI using Python
Page 25: Sensors, actuators and the Raspberry PI using Python