Sensors, actuators and the Raspberry PI using Python
-
Upload
derek-kiong -
Category
Devices & Hardware
-
view
822 -
download
1
Transcript of Sensors, actuators and the Raspberry PI using Python
Sensors, actuators and the Raspberry PI
Programming GPIO using Python
Raspberry PI vs Desktop PC
Smaller footprint Slower processor Less memory Non-standard peripherals
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
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
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
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)
Sensor Connections
Sensor board Raspberry PI
GND 6
OUT 11 (GPIO 17)
VCC 2
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"
AD/DA board connections
AD/DA board Raspberry PI
SDA 3
SCL 5
VCC 2
GND 6
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)