# Raspberry pi GPIO
> [TOC]
###### tags: `Raspberry pi`
## LED CODE
```
import RPi.GPIO as GPIO # import RPi.GPIO module
from time import sleep # lets us have a delay
GPIO.setmode(GPIO.BCM) # choose BCM or BOARD
GPIO.setup(24, GPIO.OUT) # set GPIO24 as an output
try:
while True:
GPIO.output(24, 1) # set GPIO24 to 1/GPIO.HIGH/True
sleep(0.5) # wait half a second
GPIO.output(24, 0) # set GPIO24 to 0/GPIO.LOW/False
sleep(0.5) # wait half a second
except KeyboardInterrupt: # trap a CTRL+C keyboard interrupt
GPIO.cleanup() # resets all GPIO ports used by this program
```
```
import RPi.GPIO as GPIO # import RPi.GPIO module
from time import sleep # lets us have a delay
GPIO.setmode(GPIO.BCM) # choose BCM or BOARD
GPIO.setup(24, GPIO.OUT) # set GPIO24 as an output
try:
while True:
GPIO.output(24, 1) # set GPIO24 to 1/GPIO.HIGH/True
sleep(0.5) # wait half a second
if GPIO.input(24):
print("LED just about to switch off")
GPIO.output(24, 0) # set GPIO24 to 0/GPIO.LOW/False
sleep(0.5) # wait half a second
if not GPIO.input(24):
print("LED just about to switch on")
except KeyboardInterrupt: # trap a CTRL+C keyboard interrupt
GPIO.cleanup() # resets all GPIO ports used by this program
```
## Button
```
import RPi.GPIO as GPIO
import time
GPIO.setmode(GPIO.BCM)
GPIO.setup(18, GPIO.IN, pull_up_down=GPIO.PUD_UP)
while True:
input_state = GPIO.input(18)
if input_state == False:
print('Button Pressed')
time.sleep(0.2)
```
```
import RPi.GPIO as GPIO
import time
GPIO.setmode(GPIO.BCM)
GPIO.setup(23, GPIO.IN, pull_up_down=GPIO.PUD_UP)#Button to GPIO23
GPIO.setup(24, GPIO.OUT) #LED to GPIO24
try:
while True:
button_state = GPIO.input(23)
if button_state == False:
GPIO.output(24, True)
print('Button Pressed...')
time.sleep(0.2)
else:
GPIO.output(24, False)
except:
GPIO.cleanup()
```
## gpio zero
### LED
```
from gpiozero import LED
led = LED(17)
led.on()
```
### LED API
* led.on()
* led.off()
* led.toggle()
* led.blink()
### PIR
```
from gpiozero import MotionSensor
pir = MotionSensor(4)
while True:
if pir.motion_detected:
print("You moved")
```
### PIR API
* pir.wait_for_motion()
* pir.wait_for_no_motion()
* pir.motion_detected
* pir.when_motion = motor.forward
* pir.when_no_motion = motor.backward
### Moter
```
from gpiozero import Motor
motor = Motor(forward=17, backward=18)
motor.forward()
```
### Motor API
* motor.forward()
* motor.backward()
* motor.stop()
* motor.reverse()
### UART
```
import serial
import os
import time
port = "COM5"
Packet = 115200
ser = serial.Serial(port,Packet,timeout =1)
print(ser) # print serial config
while True:
ser.write("Hello".encode('utf-8'))
time.sleep(1)
print(ser.read(1).decode('utf-8'))
time.sleep(1)
```
### HC-SR04
```
import RPi.GPIO as GPIO
import time
import signal
import sys
# use Raspberry Pi board pin numbers
GPIO.setmode(GPIO.BCM)
# set GPIO Pins
pinTrigger = 18
pinEcho = 24
# set GPIO input and output channels
GPIO.setup(pinTrigger, GPIO.OUT)
GPIO.setup(pinEcho, GPIO.IN)
def close(signal, frame):
print("\nTurning off ultrasonic distance detection...\n")
GPIO.cleanup()
sys.exit(0)
signal.signal(signal.SIGINT, close)
def main():
while True:
# set Trigger to HIGH
GPIO.output(pinTrigger, True)
# set Trigger after 0.01ms to LOW
time.sleep(0.00001)
GPIO.output(pinTrigger, False)
startTime = time.time()
stopTime = time.time()
# save start time
while 0 == GPIO.input(pinEcho):
startTime = time.time()
# save time of arrival
while 1 == GPIO.input(pinEcho):
stopTime = time.time()
# time difference between start and arrival
TimeElapsed = stopTime - startTime
# multiply with the sonic speed (34300 cm/s)
# and divide by 2, because there and back
distance = (TimeElapsed * 34300) / 2
#print ("Distance: %.1f cm" % distance)
#time.sleep(1)
if distance >=0 and distance <= 100:
print("小")
print ("Distance: %.1f cm" % distance)
time.sleep(1)
elif distance <=100 and distance >500:
print("中")
print ("Distance: %.1f cm" % distance)
time.sleep(1)
elif distance <=500 and distance >1000:
print("大")
print ("Distance: %.1f cm" % distance)
time.sleep(1)
if __name__ == '__main__':
main()
```