Skip to content

Commit f0450fb

Browse files
committed
the first commit
1 parent 8799cc3 commit f0450fb

34 files changed

+1923
-2
lines changed

01_blinkingLed_1.py

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#!/usr/bin/env python
2+
3+
#-----------------------------------------------------------
4+
# File name : 01_blinkingLed_1.py
5+
# Description : make an led blinking.
6+
# Author : Jason
7+
8+
# Website : www.adeept.com
9+
# Date : 2015/06/12
10+
#-----------------------------------------------------------
11+
12+
import RPi.GPIO as GPIO
13+
import time
14+
15+
LedPin = 11 # pin11
16+
17+
GPIO.setmode(GPIO.BOARD) # Numbers pins by physical location
18+
GPIO.setup(LedPin, GPIO.OUT) # Set pin mode as output
19+
GPIO.output(LedPin, GPIO.HIGH) # Set pin to high(+3.3V) to off the led
20+
21+
try:
22+
while True:
23+
print '...led on'
24+
GPIO.output(LedPin, GPIO.LOW) # led on
25+
time.sleep(0.5)
26+
print 'led off...'
27+
GPIO.output(LedPin, GPIO.HIGH) # led off
28+
time.sleep(0.5)
29+
except KeyboardInterrupt: # When 'Ctrl+C' is pressed, the flowing code will be executed.
30+
GPIO.output(LedPin, GPIO.HIGH) # led off
31+
GPIO.cleanup() # Release resource
32+

01_blinkingLed_2.py

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#!/usr/bin/env python
2+
import RPi.GPIO as GPIO
3+
import time
4+
5+
LedPin = 11 # pin11
6+
7+
def setup():
8+
GPIO.setmode(GPIO.BOARD) # Numbers pins by physical location
9+
GPIO.setup(LedPin, GPIO.OUT) # Set pin mode as output
10+
GPIO.output(LedPin, GPIO.HIGH) # Set pin to high(+3.3V) to off the led
11+
12+
def loop():
13+
while True:
14+
print '...led on'
15+
GPIO.output(LedPin, GPIO.LOW) # led on
16+
time.sleep(0.5)
17+
print 'led off...'
18+
GPIO.output(LedPin, GPIO.HIGH) # led off
19+
time.sleep(0.5)
20+
21+
def destroy():
22+
GPIO.output(LedPin, GPIO.HIGH) # led off
23+
GPIO.cleanup() # Release resource
24+
25+
if __name__ == '__main__': # Program start from here
26+
setup()
27+
try:
28+
loop()
29+
except KeyboardInterrupt: # When 'Ctrl+C' is pressed, the child program destroy() will be executed.
30+
destroy()
31+

02_activeBuzzer.py

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#!/usr/bin/env python
2+
import RPi.GPIO as GPIO
3+
import time
4+
5+
BeepPin = 11 # pin11
6+
7+
def setup():
8+
GPIO.setmode(GPIO.BOARD) # Numbers pins by physical location
9+
GPIO.setup(BeepPin, GPIO.OUT) # Set pin mode as output
10+
GPIO.output(BeepPin, GPIO.HIGH) # Set pin to high(+3.3V) to off the beep
11+
12+
def loop():
13+
while True:
14+
GPIO.output(BeepPin, GPIO.LOW)
15+
time.sleep(0.1)
16+
GPIO.output(BeepPin, GPIO.HIGH)
17+
time.sleep(0.1)
18+
19+
def destroy():
20+
GPIO.output(BeepPin, GPIO.HIGH) # beep off
21+
GPIO.cleanup() # Release resource
22+
23+
if __name__ == '__main__': # Program start from here
24+
print 'Press Ctrl+C to end the program...'
25+
setup()
26+
try:
27+
loop()
28+
except KeyboardInterrupt: # When 'Ctrl+C' is pressed, the child program destroy() will be executed.
29+
destroy()
30+

03_passiveBuzzer.py

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#!/usr/bin/env python
2+
import RPi.GPIO as GPIO
3+
import time
4+
5+
BZRPin = 11
6+
7+
GPIO.setmode(GPIO.BOARD) # Numbers pins by physical location
8+
GPIO.setup(BZRPin, GPIO.OUT) # Set pin mode as output
9+
GPIO.output(BZRPin, GPIO.LOW)
10+
11+
p = GPIO.PWM(BZRPin, 50) # init frequency: 50HZ
12+
p.start(50) # Duty cycle: 50%
13+
14+
try:
15+
while True:
16+
for f in range(100, 2000, 100):
17+
p.ChangeFrequency(f)
18+
time.sleep(0.2)
19+
for f in range(2000, 100, -100):
20+
p.ChangeFrequency(f)
21+
time.sleep(0.2)
22+
except KeyboardInterrupt:
23+
p.stop()
24+
GPIO.cleanup()

04_btnAndLed_1.py

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#!/usr/bin/env python
2+
import RPi.GPIO as GPIO
3+
4+
LedPin = 11 # pin11 --- led
5+
BtnPin = 12 # pin12 --- button
6+
7+
def setup():
8+
GPIO.setmode(GPIO.BOARD) # Numbers GPIOs by physical location
9+
GPIO.setup(LedPin, GPIO.OUT) # Set LedPin's mode is output
10+
GPIO.setup(BtnPin, GPIO.IN, pull_up_down=GPIO.PUD_UP) # Set BtnPin's mode is input, and pull up to high level(3.3V)
11+
GPIO.output(LedPin, GPIO.HIGH) # Set LedPin high(+3.3V) to make led off
12+
13+
def loop():
14+
while True:
15+
if GPIO.input(BtnPin) == GPIO.LOW: # Check whether the button is pressed or not.
16+
print '...led on'
17+
GPIO.output(LedPin, GPIO.LOW) # led on
18+
else:
19+
print 'led off...'
20+
GPIO.output(LedPin, GPIO.HIGH) # led off
21+
22+
def destroy():
23+
GPIO.output(LedPin, GPIO.HIGH) # led off
24+
GPIO.cleanup() # Release resource
25+
26+
if __name__ == '__main__': # Program start from here
27+
setup()
28+
try:
29+
loop()
30+
except KeyboardInterrupt: # When 'Ctrl+C' is pressed, the child program destroy() will be executed.
31+
destroy()
32+

04_btnAndLed_2.py

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
#!/usr/bin/env python
2+
import RPi.GPIO as GPIO
3+
4+
LedPin = 11 # pin11 --- led
5+
BtnPin = 12 # pin12 --- button
6+
7+
Led_status = 1
8+
9+
def setup():
10+
GPIO.setmode(GPIO.BOARD) # Numbers GPIOs by physical location
11+
GPIO.setup(LedPin, GPIO.OUT) # Set LedPin's mode is output
12+
GPIO.setup(BtnPin, GPIO.IN, pull_up_down=GPIO.PUD_UP) # Set BtnPin's mode is input, and pull up to high level(3.3V)
13+
GPIO.output(LedPin, GPIO.HIGH) # Set LedPin high(+3.3V) to make led off
14+
15+
def swLed(ev=None):
16+
global Led_status
17+
Led_status = not Led_status
18+
GPIO.output(LedPin, Led_status) # switch led status(on-->off; off-->on)
19+
if Led_status == 1:
20+
print 'led off...'
21+
else:
22+
print '...led on'
23+
24+
def loop():
25+
GPIO.add_event_detect(BtnPin, GPIO.FALLING, callback=swLed) # wait for falling
26+
while True:
27+
pass # Don't do anything
28+
29+
def destroy():
30+
GPIO.output(LedPin, GPIO.HIGH) # led off
31+
GPIO.cleanup() # Release resource
32+
33+
if __name__ == '__main__': # Program start from here
34+
setup()
35+
try:
36+
loop()
37+
except KeyboardInterrupt: # When 'Ctrl+C' is pressed, the child program destroy() will be executed.
38+
destroy()
39+

05_relay.py

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#!/usr/bin/env python
2+
import RPi.GPIO as GPIO
3+
import time
4+
5+
RelayPin = 11 # pin11
6+
7+
def setup():
8+
GPIO.setmode(GPIO.BOARD) # Numbers pins by physical location
9+
GPIO.setup(RelayPin, GPIO.OUT) # Set pin mode as output
10+
GPIO.output(RelayPin, GPIO.HIGH)
11+
12+
def loop():
13+
while True:
14+
print '...clsoe'
15+
GPIO.output(RelayPin, GPIO.LOW)
16+
time.sleep(0.5)
17+
print 'open...'
18+
GPIO.output(RelayPin, GPIO.HIGH)
19+
time.sleep(0.5)
20+
21+
def destroy():
22+
GPIO.output(RelayPin, GPIO.HIGH)
23+
GPIO.cleanup() # Release resource
24+
25+
if __name__ == '__main__': # Program start from here
26+
setup()
27+
try:
28+
loop()
29+
except KeyboardInterrupt: # When 'Ctrl+C' is pressed, the child program destroy() will be executed.
30+
destroy()
31+

06_flowingLed.py

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#!/usr/bin/env python
2+
import RPi.GPIO as GPIO
3+
import time
4+
5+
pins = [11, 12, 13, 15, 16, 18, 22, 7]
6+
7+
def setup():
8+
GPIO.setmode(GPIO.BOARD) # Numbers GPIOs by physical location
9+
for pin in pins:
10+
GPIO.setup(pin, GPIO.OUT) # Set all pins' mode is output
11+
GPIO.output(pin, GPIO.HIGH) # Set all pins to high(+3.3V) to off led
12+
13+
def loop():
14+
while True:
15+
for pin in pins:
16+
GPIO.output(pin, GPIO.LOW)
17+
time.sleep(0.5)
18+
GPIO.output(pin, GPIO.HIGH)
19+
20+
def destroy():
21+
for pin in pins:
22+
GPIO.output(pin, GPIO.HIGH) # turn off all leds
23+
GPIO.cleanup() # Release resource
24+
25+
if __name__ == '__main__': # Program start from here
26+
setup()
27+
try:
28+
loop()
29+
except KeyboardInterrupt: # When 'Ctrl+C' is pressed, the child program destroy() will be executed.
30+
destroy()
31+

07_breathingLed.py

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#!/usr/bin/env python
2+
import RPi.GPIO as GPIO
3+
import time
4+
5+
LedPin = 12
6+
7+
GPIO.setmode(GPIO.BOARD) # Numbers pins by physical location
8+
GPIO.setup(LedPin, GPIO.OUT) # Set pin mode as output
9+
GPIO.output(LedPin, GPIO.LOW) # Set pin to low(0V)
10+
11+
p = GPIO.PWM(LedPin, 1000) # set Frequece to 1KHz
12+
p.start(0) # Start PWM output, Duty Cycle = 0
13+
14+
try:
15+
while True:
16+
for dc in range(0, 101, 4): # Increase duty cycle: 0~100
17+
p.ChangeDutyCycle(dc) # Change duty cycle
18+
time.sleep(0.05)
19+
time.sleep(1)
20+
for dc in range(100, -1, -4): # Decrease duty cycle: 100~0
21+
p.ChangeDutyCycle(dc)
22+
time.sleep(0.05)
23+
time.sleep(1)
24+
except KeyboardInterrupt:
25+
p.stop()
26+
GPIO.output(LedPin, GPIO.HIGH) # turn off all leds
27+
GPIO.cleanup()
28+

08_rgbLed.py

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
#!/usr/bin/env python
2+
import RPi.GPIO as GPIO
3+
import time
4+
5+
colors = [0xFF0000, 0x00FF00, 0x0000FF, 0xFFFF00, 0xFF00FF, 0x00FFFF]
6+
pins = {'pin_R':11, 'pin_G':12, 'pin_B':13} # pins is a dict
7+
8+
GPIO.setmode(GPIO.BOARD) # Numbers GPIOs by physical location
9+
for i in pins:
10+
GPIO.setup(pins[i], GPIO.OUT) # Set pins' mode is output
11+
GPIO.output(pins[i], GPIO.HIGH) # Set pins to high(+3.3V) to off led
12+
13+
p_R = GPIO.PWM(pins['pin_R'], 2000) # set Frequece to 2KHz
14+
p_G = GPIO.PWM(pins['pin_G'], 2000)
15+
p_B = GPIO.PWM(pins['pin_B'], 5000)
16+
17+
p_R.start(0) # Initial duty Cycle = 0(leds off)
18+
p_G.start(0)
19+
p_B.start(0)
20+
21+
def map(x, in_min, in_max, out_min, out_max):
22+
return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min
23+
24+
def setColor(col): # For example : col = 0x112233
25+
R_val = (col & 0x110000) >> 16
26+
G_val = (col & 0x001100) >> 8
27+
B_val = (col & 0x000011) >> 0
28+
29+
R_val = map(R_val, 0, 255, 0, 100)
30+
G_val = map(G_val, 0, 255, 0, 100)
31+
B_val = map(B_val, 0, 255, 0, 100)
32+
33+
p_R.ChangeDutyCycle(R_val) # Change duty cycle
34+
p_G.ChangeDutyCycle(G_val)
35+
p_B.ChangeDutyCycle(B_val)
36+
37+
try:
38+
while True:
39+
for col in colors:
40+
setColor(col)
41+
time.sleep(0.5)
42+
except KeyboardInterrupt:
43+
p_R.stop()
44+
p_G.stop()
45+
p_B.stop()
46+
for i in pins:
47+
GPIO.output(pins[i], GPIO.HIGH) # Turn off all leds
48+
GPIO.cleanup()
49+

09_segment.py

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
#!/usr/bin/env python
2+
import RPi.GPIO as GPIO
3+
import time
4+
5+
pins = [11,12,13,15,16,18,22,7]
6+
dats = [0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71,0x80]
7+
8+
def setup():
9+
GPIO.setmode(GPIO.BOARD)
10+
for pin in pins:
11+
GPIO.setup(pin, GPIO.OUT) # Set pin mode as output
12+
GPIO.output(pin, GPIO.LOW)
13+
14+
def writeOneByte(val):
15+
GPIO.output(11, val & (0x01 << 0))
16+
GPIO.output(12, val & (0x01 << 1))
17+
GPIO.output(13, val & (0x01 << 2))
18+
GPIO.output(15, val & (0x01 << 3))
19+
GPIO.output(16, val & (0x01 << 4))
20+
GPIO.output(18, val & (0x01 << 5))
21+
GPIO.output(22, val & (0x01 << 6))
22+
GPIO.output(7, val & (0x01 << 7))
23+
24+
def loop():
25+
while True:
26+
for dat in dats:
27+
writeOneByte(dat)
28+
time.sleep(0.5)
29+
30+
def destroy():
31+
for pin in pins:
32+
GPIO.output(pin, GPIO.LOW)
33+
GPIO.cleanup() # Release resource
34+
35+
if __name__ == '__main__': # Program start from here
36+
setup()
37+
try:
38+
loop()
39+
except KeyboardInterrupt: # When 'Ctrl+C' is pressed, the child program destroy() will be executed.
40+
destroy()

0 commit comments

Comments
 (0)