├── .gitignore ├── 01_hall_1.py ├── 01_hall_2.py ├── 01_hall_3.py ├── 02_rgb.py ├── 03_doubleColorLed.py ├── 04_shockSwitch.py ├── 05_knock.py ├── 06_Ir.py ├── 07_laser.py ├── 08_reedSwitch.py ├── 09_IR_receive.py ├── 10_analogTemp.py ├── 11_buzzer.py ├── 11_buzzer_passive.py ├── 12_button.py ├── 13_photo_interrput.py ├── 14_tiltSwitch.py ├── 15_mercurySwitch.py ├── 16_magicCup.py ├── 16_raindetection.py ├── 17_ds18b20.py ├── 18_rotaryEncoder.py ├── 19_autoFlashLed.py ├── 20_photoRes.py ├── 21_Humiture.py ├── 22_obstacleAvoidance.py ├── 23_Tracking.py ├── 24_microphone.py ├── 25_metalTouch.py ├── 26_flame.py ├── 27_relay.py ├── 29_expand01.py ├── 30_expand02.py ├── ADC0832.py ├── README.md ├── joystickPS2.py └── mq-2.py /.gitignore: -------------------------------------------------------------------------------- 1 | # Byte-compiled / optimized / DLL files 2 | __pycache__/ 3 | *.py[cod] 4 | 5 | # C extensions 6 | *.so 7 | 8 | # Distribution / packaging 9 | .Python 10 | env/ 11 | build/ 12 | develop-eggs/ 13 | dist/ 14 | downloads/ 15 | eggs/ 16 | .eggs/ 17 | lib/ 18 | lib64/ 19 | parts/ 20 | sdist/ 21 | var/ 22 | *.egg-info/ 23 | .installed.cfg 24 | *.egg 25 | 26 | # PyInstaller 27 | # Usually these files are written by a python script from a template 28 | # before PyInstaller builds the exe, so as to inject date/other infos into it. 29 | *.manifest 30 | *.spec 31 | 32 | # Installer logs 33 | pip-log.txt 34 | pip-delete-this-directory.txt 35 | 36 | # Unit test / coverage reports 37 | htmlcov/ 38 | .tox/ 39 | .coverage 40 | .coverage.* 41 | .cache 42 | nosetests.xml 43 | coverage.xml 44 | *,cover 45 | 46 | # Translations 47 | *.mo 48 | *.pot 49 | 50 | # Django stuff: 51 | *.log 52 | 53 | # Sphinx documentation 54 | docs/_build/ 55 | 56 | # PyBuilder 57 | target/ 58 | -------------------------------------------------------------------------------- /01_hall_1.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | import RPi.GPIO as GPIO 3 | 4 | #Hall sensor is connected to pin 11 (BOARD-Layout!) 5 | HALL = 11 6 | 7 | #LED is connected to pin 12 (BOARD-Layout!) 8 | LED = 12 9 | 10 | #Set pin-layout to BOARD 11 | GPIO.setmode(GPIO.BOARD) 12 | 13 | #Avoid error messages if GPIO was not shut down correctly before 14 | GPIO.setwarnings(False) 15 | 16 | #Set HALL-pin to input, use internal pull-up-resistor 17 | GPIO.setup(HALL,GPIO.IN, pull_up_down=GPIO.PUD_UP) 18 | 19 | #Set LED-pin to output. A resistor should be used here! 20 | GPIO.setup(LED, GPIO.OUT) 21 | 22 | #Turn LED off 23 | GPIO.output(LED, GPIO.LOW) 24 | 25 | #This function will be called if a change is detected 26 | def change_detected(channel): 27 | if GPIO.input(HALL) == GPIO.LOW: 28 | print 'Magnetic material detected: LED on' 29 | GPIO.output(LED, GPIO.HIGH) #LED on 30 | else: 31 | print 'No magnetic material: LED off' 32 | GPIO.output(LED, GPIO.LOW) # LED off 33 | 34 | # Register event-listener on falling and raising 35 | # edge on HALL-sensor input. Call "change_detected" as 36 | # callback 37 | GPIO.add_event_detect(HALL, GPIO.BOTH, change_detected, bouncetime=25) 38 | 39 | # The main-loop does nothing. All is done by the event-listener 40 | try: 41 | while True: 42 | pass 43 | 44 | # Quit on Ctrl-c 45 | except KeyboardInterrupt: 46 | print "Ctrl-C - quit" 47 | 48 | # Cleanup GPIO 49 | finally: 50 | GPIO.cleanup() 51 | 52 | -------------------------------------------------------------------------------- /01_hall_2.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | import ADC0832 3 | import time 4 | 5 | def init(): 6 | ADC0832.setup() 7 | 8 | def loop(): 9 | while True: 10 | res = ADC0832.getResult() 11 | print 'res = %d' % res 12 | time.sleep(0.2) 13 | 14 | if __name__ == '__main__': 15 | init() 16 | try: 17 | loop() 18 | except KeyboardInterrupt: 19 | ADC0832.destroy() 20 | print 'The end !' 21 | -------------------------------------------------------------------------------- /01_hall_3.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | import RPi.GPIO as GPIO 3 | import ADC0832 4 | import time 5 | 6 | ''' 7 | Example how to use the hall-sensor with digital and analog output 8 | While the loop is printing the analog values, an interrupt-method 9 | is waiting for changes on the digital pin. In case of any change 10 | the message "change detected" is displayed. 11 | 12 | Example output: 13 | res0 = 138, res1 = 0 14 | res0 = 133, res1 = 0 15 | Change detected 16 | res0 = 116, res1 = 0 17 | Change detected 18 | res0 = 132, res1 = 0 19 | res0 = 138, res1 = 0 20 | ''' 21 | 22 | # Digital out of sensor connected to 23 | # pin 16 (BOAR-Layout!) 24 | HALL = 16 # 25 | 26 | #The ADC is connected like described in the manual 27 | 28 | 29 | GPIO.setmode(GPIO.BOARD) # using BOARD layout 30 | 31 | GPIO.setwarnings(False) 32 | 33 | # Setup pins 34 | GPIO.setup(HALL,GPIO.IN, pull_up_down=GPIO.PUD_UP) 35 | 36 | # Callback if digital-out triggers 37 | def change_detected(channel): 38 | print 'Change detected' 39 | 40 | def init(): 41 | ADC0832.setup() 42 | 43 | # register event listener 44 | GPIO.add_event_detect(HALL, GPIO.BOTH, change_detected, bouncetime=25) 45 | 46 | def loop(): 47 | while True: 48 | res0 = ADC0832.getResult(0) 49 | res1 = ADC0832.getResult(1) 50 | print 'res0 = %d, res1 = %d' % (res0,res1) 51 | time.sleep(0.2) 52 | 53 | if __name__ == '__main__': 54 | init() 55 | try: 56 | loop() 57 | except KeyboardInterrupt: 58 | ADC0832.destroy() 59 | 60 | 61 | 62 | -------------------------------------------------------------------------------- /02_rgb.py: -------------------------------------------------------------------------------- 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 & 0xff0000) >> 16 26 | G_val = (col & 0x00ff00) >> 8 27 | B_val = (col & 0x0000ff) >> 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 | 50 | -------------------------------------------------------------------------------- /03_doubleColorLed.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | import RPi.GPIO as GPIO 3 | import time 4 | 5 | colors = [0xFF00, 0x00FF, 0x0FF0, 0xF00F] 6 | pins = {'pin_R':11, 'pin_G':12} # 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 | 16 | p_R.start(0) # Initial duty Cycle = 0(leds off) 17 | p_G.start(0) 18 | 19 | def map(x, in_min, in_max, out_min, out_max): 20 | return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min 21 | 22 | def setColor(col): # For example : col = 0x112233 23 | R_val = (col & 0x1100) >> 8 24 | G_val = (col & 0x0011) >> 0 25 | 26 | R_val = map(R_val, 0, 255, 0, 100) 27 | G_val = map(G_val, 0, 255, 0, 100) 28 | 29 | p_R.ChangeDutyCycle(R_val) # Change duty cycle 30 | p_G.ChangeDutyCycle(G_val) 31 | 32 | try: 33 | while True: 34 | for col in colors: 35 | setColor(col) 36 | time.sleep(0.5) 37 | except KeyboardInterrupt: 38 | p_R.stop() 39 | p_G.stop() 40 | for i in pins: 41 | GPIO.output(pins[i], GPIO.HIGH) # Turn off all leds 42 | GPIO.cleanup() 43 | 44 | -------------------------------------------------------------------------------- /04_shockSwitch.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | import RPi.GPIO as GPIO 3 | 4 | ShockPin = 11 5 | LedPin = 12 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(ShockPin, GPIO.IN, pull_up_down=GPIO.PUD_UP) 13 | GPIO.output(LedPin, GPIO.HIGH) # Set LedPin high(+3.3V) to off led 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 | print "led: " + ("on" if Led_status else "off") 20 | 21 | def loop(): 22 | GPIO.add_event_detect(ShockPin, GPIO.FALLING, callback=swLed, bouncetime=200) # wait for falling 23 | while True: 24 | pass # Don't do anything 25 | 26 | def destroy(): 27 | GPIO.output(LedPin, GPIO.LOW) # led off 28 | GPIO.cleanup() # Release resource 29 | 30 | if __name__ == '__main__': # Program start from here 31 | setup() 32 | try: 33 | loop() 34 | except KeyboardInterrupt: # When 'Ctrl+C' is pressed, the child program destroy() will be executed. 35 | destroy() 36 | 37 | -------------------------------------------------------------------------------- /05_knock.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | import RPi.GPIO as GPIO 3 | 4 | KnockPin = 11 5 | LedPin = 12 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(KnockPin, GPIO.IN, pull_up_down=GPIO.PUD_UP) 13 | GPIO.output(LedPin, GPIO.HIGH) # Set LedPin high(+3.3V) to off led 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 | print "LED: " + ("on" if Led_status else "off") 20 | 21 | def loop(): 22 | GPIO.add_event_detect(KnockPin, GPIO.FALLING, callback=swLed, bouncetime=200) # wait for falling 23 | while True: 24 | pass # Don't do anything 25 | 26 | def destroy(): 27 | GPIO.output(LedPin, GPIO.LOW) # led off 28 | GPIO.cleanup() # Release resource 29 | 30 | if __name__ == '__main__': # Program start from here 31 | setup() 32 | try: 33 | loop() 34 | except KeyboardInterrupt: # When 'Ctrl+C' is pressed, the child program destroy() will be executed. 35 | destroy() 36 | 37 | -------------------------------------------------------------------------------- /06_Ir.py: -------------------------------------------------------------------------------- 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 GPIOs by physical location 9 | GPIO.setup(LedPin, GPIO.OUT) # Set LedPin's mode is output 10 | GPIO.output(LedPin, GPIO.HIGH) # Set LedPin high(+3.3V) to off led 11 | 12 | def loop(): 13 | while True: 14 | print '...led on' 15 | GPIO.output(LedPin, GPIO.HIGH) # led on 16 | time.sleep(0.5) 17 | print 'led off...' 18 | GPIO.output(LedPin, GPIO.LOW) # led off 19 | time.sleep(0.5) 20 | 21 | def destroy(): 22 | GPIO.output(LedPin, GPIO.LOW) # 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 | 32 | -------------------------------------------------------------------------------- /07_laser.py: -------------------------------------------------------------------------------- 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 GPIOs by physical location 9 | GPIO.setup(LedPin, GPIO.OUT) # Set LedPin's mode is output 10 | GPIO.output(LedPin, GPIO.HIGH) # Set LedPin high(+3.3V) to off led 11 | 12 | def loop(): 13 | while True: 14 | print '...led on' 15 | GPIO.output(LedPin, GPIO.HIGH) # led on 16 | time.sleep(0.5) 17 | print 'led off...' 18 | GPIO.output(LedPin, GPIO.LOW) # led off 19 | time.sleep(0.5) 20 | 21 | def destroy(): 22 | GPIO.output(LedPin, GPIO.LOW) # 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 | 32 | -------------------------------------------------------------------------------- /08_reedSwitch.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | import RPi.GPIO as GPIO 3 | 4 | ReedPin = 11 5 | LedPin = 12 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(ReedPin, GPIO.IN, pull_up_down=GPIO.PUD_UP) 11 | GPIO.output(LedPin, GPIO.LOW) # Set LedPin high(+3.3V) to off led 12 | 13 | def switchLed(channel): 14 | if GPIO.input(11): 15 | print "Magnet detected - LED on!" 16 | GPIO.output(LedPin,GPIO.HIGH) 17 | else: 18 | print "No magnet detected - LED off!" 19 | GPIO.output(LedPin,GPIO.LOW) 20 | 21 | def loop(): 22 | GPIO.add_event_detect(ReedPin,GPIO.BOTH, callback=switchLed, bouncetime=20) 23 | while True: 24 | pass 25 | 26 | def destroy(): 27 | GPIO.output(LedPin, GPIO.LOW) # led off 28 | GPIO.cleanup() # Release resource 29 | 30 | if __name__ == '__main__': # Program start from here 31 | setup() 32 | try: 33 | loop() 34 | except KeyboardInterrupt: # When 'Ctrl+C' is pressed, the child program destroy() will be executed. 35 | destroy() 36 | 37 | 38 | -------------------------------------------------------------------------------- /09_IR_receive.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | import RPi.GPIO as GPIO 3 | 4 | IrPin = 11 5 | LedPin = 12 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(IrPin, GPIO.IN, pull_up_down=GPIO.PUD_UP) 13 | GPIO.output(LedPin, GPIO.LOW) # Set LedPin high(+3.3V) to off led 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 on...' 21 | else: 22 | print '...led off' 23 | 24 | def loop(): 25 | GPIO.add_event_detect(IrPin, 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.LOW) # 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 | 40 | -------------------------------------------------------------------------------- /10_analogTemp.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | import ADC0832 3 | import time 4 | import math 5 | 6 | def init(): 7 | ADC0832.setup() 8 | 9 | def loop(): 10 | while True: 11 | analogVal = ADC0832.getResult() 12 | Vr = 5 * float(analogVal) / 255 13 | Rt = 10000 * Vr / (5 - Vr) 14 | temp = 1/(((math.log(Rt / 10000)) / 3950) + (1 / (273.15+25))) 15 | temp = temp - 273.15 16 | print 'temperature = %d C' % temp 17 | time.sleep(0.2) 18 | 19 | if __name__ == '__main__': 20 | init() 21 | try: 22 | loop() 23 | except KeyboardInterrupt: 24 | ADC0832.destroy() 25 | print 'The end !' 26 | -------------------------------------------------------------------------------- /11_buzzer.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | import RPi.GPIO as GPIO 3 | import time 4 | 5 | BuzzerPin = 11 # pin11 6 | 7 | def setup(): 8 | GPIO.setmode(GPIO.BOARD) # Numbers GPIOs by physical location 9 | GPIO.setup(BuzzerPin, GPIO.OUT) 10 | GPIO.output(BuzzerPin, GPIO.LOW) 11 | 12 | def loop(): 13 | while True: 14 | GPIO.output(BuzzerPin, GPIO.HIGH) 15 | time.sleep(0.5) 16 | GPIO.output(BuzzerPin, GPIO.LOW) 17 | time.sleep(0.5) 18 | 19 | def destroy(): 20 | GPIO.output(BuzzerPin, GPIO.LOW) 21 | GPIO.cleanup() # Release resource 22 | 23 | if __name__ == '__main__': # Program start from here 24 | setup() 25 | try: 26 | loop() 27 | except KeyboardInterrupt: # When 'Ctrl+C' is pressed, the child program destroy() will be executed. 28 | destroy() 29 | 30 | -------------------------------------------------------------------------------- /11_buzzer_passive.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | import RPi.GPIO as GPIO 3 | import time 4 | 5 | BuzzerPin = 11 # pin11 6 | 7 | SPEED = 1 8 | 9 | # List of tone-names with frequency 10 | TONES = {"c6":1047, 11 | "b5":988, 12 | "a5":880, 13 | "g5":784, 14 | "f5":698, 15 | "e5":659, 16 | "eb5":622, 17 | "d5":587, 18 | "c5":523, 19 | "b4":494, 20 | "a4":440, 21 | "ab4":415, 22 | "g4":392, 23 | "f4":349, 24 | "e4":330, 25 | "d4":294, 26 | "c4":262} 27 | 28 | # Song is a list of tones with name and 1/duration. 16 means 1/16 29 | SONG = [ 30 | ["e5",16],["eb5",16], 31 | ["e5",16],["eb5",16],["e5",16],["b4",16],["d5",16],["c5",16], 32 | ["a4",8],["p",16],["c4",16],["e4",16],["a4",16], 33 | ["b4",8],["p",16],["e4",16],["ab4",16],["b4",16], 34 | ["c5",8],["p",16],["e4",16],["e5",16],["eb5",16], 35 | ["e5",16],["eb5",16],["e5",16],["b4",16],["d5",16],["c5",16], 36 | ["a4",8],["p",16],["c4",16],["e4",16],["a4",16], 37 | ["b4",8],["p",16],["e4",16],["c5",16],["b4",16],["a4",4] 38 | ] 39 | 40 | def setup(): 41 | GPIO.setmode(GPIO.BOARD) # Numbers GPIOs by physical location 42 | GPIO.setup(BuzzerPin, GPIO.OUT) 43 | 44 | def playTone(p,tone): 45 | # calculate duration based on speed and tone-length 46 | duration = (1./(tone[1]*0.25*SPEED)) 47 | 48 | if tone[0] == "p": # p => pause 49 | time.sleep(duration) 50 | else: # let's rock 51 | frequency = TONES[tone[0]] 52 | p.ChangeFrequency(frequency) 53 | p.start(0.5) 54 | time.sleep(duration) 55 | p.stop() 56 | 57 | def run(): 58 | p = GPIO.PWM(BuzzerPin, 440) 59 | p.start(0.5) 60 | for t in SONG: 61 | playTone(p,t) 62 | 63 | def destroy(): 64 | GPIO.output(BuzzerPin, GPIO.HIGH) 65 | GPIO.cleanup() # Release resource 66 | 67 | if __name__ == '__main__': # Program start from here 68 | setup() 69 | try: 70 | run() 71 | GPIO.cleanup() 72 | except KeyboardInterrupt: # When 'Ctrl+C' is pressed, the child program destroy() will be executed. 73 | destroy() 74 | 75 | -------------------------------------------------------------------------------- /12_button.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | import RPi.GPIO as GPIO 3 | 4 | BtnPin = 11 5 | LedPin = 12 6 | 7 | Led_status = 0 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.LOW) # Set LedPin low to off led 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 | print "LED: on " if Led_status else "LED: off" 20 | 21 | def loop(): 22 | GPIO.add_event_detect(BtnPin, GPIO.FALLING, callback=swLed, bouncetime=200) # wait for falling 23 | while True: 24 | pass # Don't do anything 25 | 26 | def destroy(): 27 | GPIO.output(LedPin, GPIO.LOW) # led off 28 | GPIO.cleanup() # Release resource 29 | 30 | if __name__ == '__main__': # Program start from here 31 | setup() 32 | try: 33 | loop() 34 | except KeyboardInterrupt: # When 'Ctrl+C' is pressed, the child program destroy() will be executed. 35 | destroy() 36 | 37 | -------------------------------------------------------------------------------- /13_photo_interrput.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | import RPi.GPIO as GPIO 3 | 4 | LightBreakPin = 11 5 | LedPin = 12 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(LightBreakPin, GPIO.IN, pull_up_down=GPIO.PUD_UP) 11 | GPIO.output(LedPin, GPIO.LOW) # Set LedPin low to off led 12 | 13 | def swLed(channel): 14 | status = GPIO.input(LightBreakPin) 15 | print "LED: on" if status else "LED: off" 16 | GPIO.output(LedPin,status) 17 | 18 | def loop(): 19 | GPIO.add_event_detect(LightBreakPin, GPIO.BOTH, callback=swLed) # wait for falling 20 | while True: 21 | pass 22 | def destroy(): 23 | GPIO.output(LedPin, GPIO.LOW) # 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 | 33 | -------------------------------------------------------------------------------- /14_tiltSwitch.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | import RPi.GPIO as GPIO 3 | 4 | TiltPin = 11 5 | LedPin = 12 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(TiltPin, GPIO.IN, pull_up_down=GPIO.PUD_UP) 13 | GPIO.output(LedPin, GPIO.LOW) # Set LedPin high(+3.3V) to off led 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 | print "LED: off" if Led_status else "LED: on" 20 | 21 | def loop(): 22 | GPIO.add_event_detect(TiltPin, GPIO.FALLING, callback=swLed, bouncetime=100) # wait for falling 23 | while True: 24 | pass # Don't do anything 25 | 26 | def destroy(): 27 | GPIO.output(LedPin, GPIO.LOW) # led off 28 | GPIO.cleanup() # Release resource 29 | 30 | if __name__ == '__main__': # Program start from here 31 | setup() 32 | try: 33 | loop() 34 | except KeyboardInterrupt: # When 'Ctrl+C' is pressed, the child program destroy() will be executed. 35 | destroy() 36 | 37 | -------------------------------------------------------------------------------- /15_mercurySwitch.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | import RPi.GPIO as GPIO 3 | 4 | MerPin = 11 5 | LedPin = 12 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(MerPin, GPIO.IN, pull_up_down=GPIO.PUD_UP) 13 | GPIO.output(LedPin, GPIO.LOW) # Set LedPin high(+3.3V) to off led 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 | print "LED: off" if Led_status else "LED: on" 20 | 21 | def loop(): 22 | GPIO.add_event_detect(MerPin, GPIO.FALLING, callback=swLed, bouncetime=100) # wait for falling 23 | while True: 24 | pass # Don't do anything 25 | 26 | def destroy(): 27 | GPIO.output(LedPin, GPIO.LOW) # led off 28 | GPIO.cleanup() # Release resource 29 | 30 | if __name__ == '__main__': # Program start from here 31 | setup() 32 | try: 33 | loop() 34 | except KeyboardInterrupt: # When 'Ctrl+C' is pressed, the child program destroy() will be executed. 35 | destroy() 36 | 37 | -------------------------------------------------------------------------------- /16_magicCup.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | 3 | import RPi.GPIO as GPIO 4 | import time 5 | 6 | MERCURY_SIG1 = 11 7 | LED1 = 12 8 | 9 | LED2 = 15 10 | 11 | 12 | def setup(): 13 | global p_LED1, p_LED2 14 | GPIO.setmode(GPIO.BOARD) 15 | GPIO.setup(MERCURY_SIG1, GPIO.IN) 16 | GPIO.setup(LED1, GPIO.OUT) 17 | GPIO.setup(LED2, GPIO.OUT) 18 | 19 | p_LED1 = GPIO.PWM(LED1, 200) 20 | p_LED2 = GPIO.PWM(LED2, 200) 21 | 22 | p_LED1.start(0) 23 | p_LED2.start(0) 24 | 25 | def loop(): 26 | led1_value = 0 27 | led2_value = 0 28 | while True: 29 | mercury_value = GPIO.input(MERCURY_SIG1) 30 | if mercury_value == 0: 31 | led1_value -= 1 32 | led2_value += 1 33 | if led1_value < 0: 34 | led1_value = 0 35 | if led2_value > 100: 36 | led2_value = 100 37 | 38 | if mercury_value == 1: 39 | led1_value += 1 40 | led2_value -= 1 41 | if led1_value > 100: 42 | led1_value = 100 43 | if led2_value < 0: 44 | led2_value = 0 45 | 46 | p_LED1.ChangeDutyCycle(led1_value) 47 | p_LED2.ChangeDutyCycle(led2_value) 48 | time.sleep(0.05) 49 | 50 | def destroy(): 51 | p_LED1.stop() 52 | p_LED2.stop() 53 | GPIO.cleanup() 54 | 55 | if __name__ == "__main__": 56 | try: 57 | setup() 58 | loop() 59 | except KeyboardInterrupt: 60 | destroy() 61 | 62 | 63 | 64 | 65 | -------------------------------------------------------------------------------- /16_raindetection.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | import ADC0832 3 | import time 4 | 5 | def init(): 6 | ADC0832.setup() 7 | 8 | def loop(): 9 | while True: 10 | res = ADC0832.getResult() - 80 11 | if res < 0: 12 | res = 0 13 | if res > 100: 14 | res = 100 15 | print 'res = %d' % res 16 | time.sleep(0.2) 17 | 18 | if __name__ == '__main__': 19 | init() 20 | try: 21 | loop() 22 | except KeyboardInterrupt: 23 | ADC0832.destroy() 24 | print 'The end !' 25 | -------------------------------------------------------------------------------- /17_ds18b20.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | import os 3 | import time 4 | 5 | #---------------------------------------------------------------- 6 | # Note: 7 | # ds18b20's data pin must be connected to pin7. 8 | #---------------------------------------------------------------- 9 | 10 | # Reads temperature from sensor and prints to stdout 11 | # id is the id of the sensor 12 | def readSensor(id): 13 | tfile = open("/sys/bus/w1/devices/"+id+"/w1_slave") 14 | text = tfile.read() 15 | tfile.close() 16 | secondline = text.split("\n")[1] 17 | temperaturedata = secondline.split(" ")[9] 18 | temperature = float(temperaturedata[2:]) 19 | temperature = temperature / 1000 20 | print "Sensor: " + id + " - Current temperature : %0.3f C" % temperature 21 | 22 | 23 | # Reads temperature from all sensors found in /sys/bus/w1/devices/ 24 | # starting with "28-... 25 | def readSensors(): 26 | count = 0 27 | sensor = "" 28 | for file in os.listdir("/sys/bus/w1/devices/"): 29 | if (file.startswith("28-")): 30 | readSensor(file) 31 | count+=1 32 | if (count == 0): 33 | print "No sensor found! Check connection" 34 | 35 | # read temperature every second for all connected sensors 36 | def loop(): 37 | while True: 38 | readSensors() 39 | time.sleep(1) 40 | 41 | # Nothing to cleanup 42 | def destroy(): 43 | pass 44 | 45 | # Main starts here 46 | if __name__ == "__main__": 47 | try: 48 | loop() 49 | except KeyboardInterrupt: 50 | destroy() 51 | 52 | -------------------------------------------------------------------------------- /18_rotaryEncoder.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | import RPi.GPIO as GPIO 3 | import time 4 | 5 | RoAPin = 11 # pin11 6 | RoBPin = 12 # pin12 7 | BtnPin = 13 # Button Pin 8 | 9 | globalCounter = 0 10 | 11 | flag = 0 12 | Last_RoB_Status = 0 13 | Current_RoB_Status = 0 14 | 15 | def setup(): 16 | GPIO.setmode(GPIO.BOARD) # Numbers GPIOs by physical location 17 | GPIO.setup(RoAPin, GPIO.IN) # input mode 18 | GPIO.setup(RoBPin, GPIO.IN) 19 | GPIO.setup(BtnPin, GPIO.IN, pull_up_down=GPIO.PUD_UP) 20 | GPIO.add_event_detect(BtnPin, GPIO.FALLING, callback=btnISR) 21 | 22 | def rotaryDeal(): 23 | global flag 24 | global Last_RoB_Status 25 | global Current_RoB_Status 26 | global globalCounter 27 | Last_RoB_Status = GPIO.input(RoBPin) 28 | while(not GPIO.input(RoAPin)): 29 | Current_RoB_Status = GPIO.input(RoBPin) 30 | flag = 1 31 | if flag == 1: 32 | flag = 0 33 | if (Last_RoB_Status == 0) and (Current_RoB_Status == 1): 34 | globalCounter = globalCounter - 1 35 | if (Last_RoB_Status == 1) and (Current_RoB_Status == 0): 36 | globalCounter = globalCounter + 1 37 | 38 | def btnISR(channel): 39 | global globalCounter 40 | globalCounter = 0 41 | 42 | def loop(): 43 | global globalCounter 44 | tmp = 0 # Rotary Temperary 45 | 46 | while True: 47 | rotaryDeal() 48 | if tmp != globalCounter: 49 | print 'globalCounter = %d' % globalCounter 50 | tmp = globalCounter 51 | 52 | def destroy(): 53 | GPIO.cleanup() # Release resource 54 | 55 | if __name__ == '__main__': # Program start from here 56 | setup() 57 | try: 58 | loop() 59 | except KeyboardInterrupt: # When 'Ctrl+C' is pressed, the child program destroy() will be executed. 60 | destroy() 61 | 62 | -------------------------------------------------------------------------------- /19_autoFlashLed.py: -------------------------------------------------------------------------------- 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 GPIOs by physical location 9 | GPIO.setup(LedPin, GPIO.OUT) # Set LedPin's mode is output 10 | GPIO.output(LedPin, GPIO.HIGH) # Set LedPin high(+3.3V) to off led 11 | 12 | def loop(): 13 | while True: 14 | GPIO.output(LedPin, GPIO.LOW) # led on 15 | time.sleep(0.5) 16 | GPIO.output(LedPin, GPIO.HIGH) # led off 17 | time.sleep(0.5) 18 | 19 | def destroy(): 20 | GPIO.output(LedPin, GPIO.HIGH) # led off 21 | GPIO.cleanup() # Release resource 22 | 23 | if __name__ == '__main__': # Program start from here 24 | setup() 25 | try: 26 | loop() 27 | except KeyboardInterrupt: # When 'Ctrl+C' is pressed, the child program destroy() will be executed. 28 | destroy() 29 | 30 | -------------------------------------------------------------------------------- /20_photoRes.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | import ADC0832 3 | import time 4 | 5 | def init(): 6 | ADC0832.setup() 7 | 8 | def loop(): 9 | while True: 10 | res = ADC0832.getResult() - 80 11 | if res < 0: 12 | res = 0 13 | if res > 100: 14 | res = 100 15 | print 'res = %d' % res 16 | time.sleep(0.2) 17 | 18 | if __name__ == '__main__': 19 | init() 20 | try: 21 | loop() 22 | except KeyboardInterrupt: 23 | ADC0832.destroy() 24 | print 'The end !' 25 | -------------------------------------------------------------------------------- /21_Humiture.py: -------------------------------------------------------------------------------- 1 | ''' 2 | ********************************************************************** 3 | * Filename : dht11.py 4 | * Description : test for SunFoudner DHT11 humiture & temperature module 5 | * Author : Dream 6 | * Brand : SunFounder 7 | * E-mail : service@sunfounder.com 8 | * Website : www.sunfounder.com 9 | * Update : Dream 2016-09-30 New release 10 | ********************************************************************** 11 | ''' 12 | import RPi.GPIO as GPIO 13 | import time 14 | 15 | DHTPIN = 17 16 | 17 | GPIO.setmode(GPIO.BCM) 18 | 19 | MAX_UNCHANGE_COUNT = 100 20 | 21 | STATE_INIT_PULL_DOWN = 1 22 | STATE_INIT_PULL_UP = 2 23 | STATE_DATA_FIRST_PULL_DOWN = 3 24 | STATE_DATA_PULL_UP = 4 25 | STATE_DATA_PULL_DOWN = 5 26 | 27 | def read_dht11_dat(): 28 | GPIO.setup(DHTPIN, GPIO.OUT) 29 | GPIO.output(DHTPIN, GPIO.HIGH) 30 | time.sleep(0.05) 31 | GPIO.output(DHTPIN, GPIO.LOW) 32 | time.sleep(0.02) 33 | GPIO.setup(DHTPIN, GPIO.IN, GPIO.PUD_UP) 34 | 35 | unchanged_count = 0 36 | last = -1 37 | data = [] 38 | while True: 39 | current = GPIO.input(DHTPIN) 40 | data.append(current) 41 | if last != current: 42 | unchanged_count = 0 43 | last = current 44 | else: 45 | unchanged_count += 1 46 | if unchanged_count > MAX_UNCHANGE_COUNT: 47 | break 48 | 49 | state = STATE_INIT_PULL_DOWN 50 | 51 | lengths = [] 52 | current_length = 0 53 | 54 | for current in data: 55 | current_length += 1 56 | 57 | if state == STATE_INIT_PULL_DOWN: 58 | if current == GPIO.LOW: 59 | state = STATE_INIT_PULL_UP 60 | else: 61 | continue 62 | if state == STATE_INIT_PULL_UP: 63 | if current == GPIO.HIGH: 64 | state = STATE_DATA_FIRST_PULL_DOWN 65 | else: 66 | continue 67 | if state == STATE_DATA_FIRST_PULL_DOWN: 68 | if current == GPIO.LOW: 69 | state = STATE_DATA_PULL_UP 70 | else: 71 | continue 72 | if state == STATE_DATA_PULL_UP: 73 | if current == GPIO.HIGH: 74 | current_length = 0 75 | state = STATE_DATA_PULL_DOWN 76 | else: 77 | continue 78 | if state == STATE_DATA_PULL_DOWN: 79 | if current == GPIO.LOW: 80 | lengths.append(current_length) 81 | state = STATE_DATA_PULL_UP 82 | else: 83 | continue 84 | if len(lengths) != 40: 85 | print "Data not good, skip" 86 | return False 87 | 88 | shortest_pull_up = min(lengths) 89 | longest_pull_up = max(lengths) 90 | halfway = (longest_pull_up + shortest_pull_up) / 2 91 | bits = [] 92 | the_bytes = [] 93 | byte = 0 94 | 95 | for length in lengths: 96 | bit = 0 97 | if length > halfway: 98 | bit = 1 99 | bits.append(bit) 100 | print "bits: %s, length: %d" % (bits, len(bits)) 101 | for i in range(0, len(bits)): 102 | byte = byte << 1 103 | if (bits[i]): 104 | byte = byte | 1 105 | else: 106 | byte = byte | 0 107 | if ((i + 1) % 8 == 0): 108 | the_bytes.append(byte) 109 | byte = 0 110 | print the_bytes 111 | checksum = (the_bytes[0] + the_bytes[1] + the_bytes[2] + the_bytes[3]) & 0xFF 112 | if the_bytes[4] != checksum: 113 | print "Data not good, skip" 114 | return False 115 | 116 | return the_bytes[0], the_bytes[2] 117 | 118 | def main(): 119 | print "Raspberry Pi wiringPi DHT11 Temperature test program\n" 120 | while True: 121 | result = read_dht11_dat() 122 | if result: 123 | humidity, temperature = result 124 | print "humidity: %s %%, Temperature: %s C`" % (humidity, temperature) 125 | time.sleep(1) 126 | 127 | def destroy(): 128 | GPIO.cleanup() 129 | 130 | if __name__ == '__main__': 131 | try: 132 | main() 133 | except KeyboardInterrupt: 134 | destroy() 135 | -------------------------------------------------------------------------------- /22_obstacleAvoidance.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | import RPi.GPIO as GPIO 3 | 4 | ObstaclePin = 11 5 | 6 | def setup(): 7 | GPIO.setmode(GPIO.BOARD) # Numbers GPIOs by physical location 8 | GPIO.setup(ObstaclePin, GPIO.IN, pull_up_down=GPIO.PUD_UP) 9 | 10 | def loop(): 11 | while True: 12 | if (0 == GPIO.input(ObstaclePin)): 13 | print "Barrier is detected !" 14 | 15 | 16 | def destroy(): 17 | GPIO.cleanup() # Release resource 18 | 19 | if __name__ == '__main__': # Program start from here 20 | setup() 21 | try: 22 | loop() 23 | except KeyboardInterrupt: # When 'Ctrl+C' is pressed, the child program destroy() will be executed. 24 | destroy() 25 | 26 | -------------------------------------------------------------------------------- /23_Tracking.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | import RPi.GPIO as GPIO 3 | 4 | TrackPin = 11 5 | LedPin = 12 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(TrackPin, GPIO.IN, pull_up_down=GPIO.PUD_UP) 11 | GPIO.output(LedPin, GPIO.HIGH) # Set LedPin high(+3.3V) to off led 12 | 13 | def loop(): 14 | while True: 15 | if GPIO.input(TrackPin) == GPIO.LOW: 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 | 33 | -------------------------------------------------------------------------------- /24_microphone.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | import RPi.GPIO as GPIO 3 | import ADC0832 4 | import time 5 | 6 | MIC_DO_PIN = 15 7 | 8 | def init(): 9 | GPIO.setmode(GPIO.BOARD) 10 | GPIO.setup(MIC_DO_PIN, GPIO.IN, pull_up_down=GPIO.PUD_UP) 11 | ADC0832.setup() 12 | 13 | def micISR(ev=None): 14 | print "voice in..." 15 | analogVal = ADC0832.getResult() 16 | print 'res = %d' % res 17 | 18 | def loop(): 19 | GPIO.add_event_detect(MIC_DO_PIN, GPIO.FALLING, callback=micISR) 20 | while True: 21 | pass 22 | 23 | if __name__ == '__main__': 24 | init() 25 | try: 26 | loop() 27 | except KeyboardInterrupt: 28 | ADC0832.destroy() 29 | print 'The end !' 30 | -------------------------------------------------------------------------------- /25_metalTouch.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | import RPi.GPIO as GPIO 3 | 4 | TouchPin = 11 5 | LedPin = 12 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(TouchPin, GPIO.IN, pull_up_down=GPIO.PUD_UP) 11 | GPIO.output(LedPin, GPIO.HIGH) # Set LedPin high(+3.3V) to off led 12 | 13 | def loop(): 14 | while True: 15 | if GPIO.input(TouchPin) == GPIO.LOW: 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 | 33 | -------------------------------------------------------------------------------- /26_flame.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | import RPi.GPIO as GPIO 3 | import time 4 | 5 | FlamePin = 11 6 | 7 | def init(): 8 | GPIO.setmode(GPIO.BOARD) 9 | GPIO.setup(FlamePin, GPIO.IN, pull_up_down=GPIO.PUD_UP) 10 | 11 | def myISR(ev=None): 12 | print "Flame is detected !" 13 | 14 | def loop(): 15 | GPIO.add_event_detect(FlamePin, GPIO.FALLING, callback=myISR) 16 | while True: 17 | pass 18 | 19 | if __name__ == '__main__': 20 | init() 21 | try: 22 | loop() 23 | except KeyboardInterrupt: 24 | print 'The end !' 25 | -------------------------------------------------------------------------------- /27_relay.py: -------------------------------------------------------------------------------- 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 GPIOs by physical location 9 | GPIO.setup(RelayPin, GPIO.OUT) 10 | GPIO.output(RelayPin, GPIO.HIGH) 11 | 12 | def loop(): 13 | while True: 14 | print '...relayd on' 15 | GPIO.output(RelayPin, GPIO.LOW) 16 | time.sleep(0.5) 17 | print 'relay off...' 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 | 32 | -------------------------------------------------------------------------------- /29_expand01.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | import RPi.GPIO as GPIO 3 | import time 4 | import os 5 | import sys 6 | 7 | # Define RGB LED pin 8 | LedRed = 11 9 | LedGreen = 12 10 | LedBlue = 13 11 | 12 | # Define Buzzer pin 13 | Buzzer = 15 14 | 15 | def beep(x): 16 | GPIO.output(Buzzer, 0) 17 | time.sleep(x) 18 | GPIO.output(Buzzer, 1) 19 | time.sleep(x) 20 | 21 | def tempRead(): 22 | global ds18b20 23 | address = '/sys/bus/w1/devices/' + ds18b20 + '/w1_slave' 24 | tfile = open(address) 25 | text = tfile.read() 26 | tfile.close() 27 | secondline = text.split("\n")[1] 28 | temperaturedata = secondline.split(" ")[9] 29 | temperature = float(temperaturedata[2:]) 30 | temperature = temperature / 1000 31 | return temperature 32 | 33 | def map(x, in_min, in_max, out_min, out_max): 34 | return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min 35 | 36 | def ds18b20Init(): 37 | global ds18b20 38 | for i in os.listdir('/sys/bus/w1/devices'): 39 | if i[:2] == '28': 40 | ds18b20 = i 41 | 42 | def setup(): 43 | GPIO.setmode(GPIO.BOARD) 44 | # Buzzer setup: 45 | GPIO.setup(Buzzer, GPIO.OUT) 46 | GPIO.output(Buzzer, 1) 47 | # RGB setup: 48 | GPIO.setup(LedRed, GPIO.OUT, initial=GPIO.LOW) 49 | GPIO.setup(LedGreen, GPIO.OUT, initial=GPIO.LOW) 50 | GPIO.setup(LedBlue, GPIO.OUT, initial=GPIO.LOW) 51 | # DS18B20 setup: 52 | ds18b20Init() 53 | 54 | def loop(): 55 | if len(sys.argv) != 3: 56 | print 'Usage:' 57 | print ' sudo python 29_expand01.py [temperature lower limit] [upper limit]' 58 | print 'For example: sudo python 29_expand01.py 29 31\n' 59 | destroy() 60 | quit() 61 | low = float(sys.argv[1]) 62 | high = float(sys.argv[2]) 63 | if low >= high: 64 | print 'Parameters error, lower limit should be less than upper limit' 65 | destroy() 66 | quit() 67 | print 'System is running...' 68 | print 'The lower limit of temperature:', low 69 | print 'The upper limit of temperature:', high 70 | 71 | while True: 72 | # Read temperature from ds18B20 73 | temp = tempRead() 74 | print 'Current temperature:', temp 75 | 76 | # Under/Over limit alarm: 77 | if temp < low: 78 | GPIO.output(LedBlue, 1); 79 | GPIO.output(LedRed, 0); 80 | GPIO.output(LedGreen, 0); 81 | for i in range(0, 4): 82 | beep(0.25) 83 | 84 | if temp >= low and temp < high: 85 | GPIO.output(LedBlue, 0); 86 | GPIO.output(LedRed, 0); 87 | GPIO.output(LedGreen, 1); 88 | time.sleep(1) 89 | 90 | if temp >= high: 91 | GPIO.output(LedBlue, 0); 92 | GPIO.output(LedRed, 1); 93 | GPIO.output(LedGreen, 0); 94 | for i in range(0, 8): 95 | beep(0.125) 96 | 97 | def destroy(): 98 | GPIO.output(LedRed, 0) 99 | GPIO.output(LedGreen, 0) 100 | GPIO.output(LedBlue, 0) 101 | GPIO.output(Buzzer, 1) 102 | GPIO.cleanup() 103 | 104 | if __name__ == '__main__': 105 | setup() 106 | try: 107 | loop() 108 | except KeyboardInterrupt: 109 | destroy() 110 | -------------------------------------------------------------------------------- /30_expand02.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | import RPi.GPIO as GPIO 3 | import ADC0832 4 | import time 5 | import os 6 | 7 | # Define RGB LED pin 8 | LedRed = 15 9 | LedGreen = 16 10 | LedBlue = 18 11 | 12 | # Define Joystick button pin 13 | btn = 22 14 | 15 | # Define Buzzer pin 16 | Buzzer = 3 17 | 18 | def beep(x): 19 | GPIO.output(Buzzer, 0) 20 | time.sleep(x) 21 | GPIO.output(Buzzer, 1) 22 | time.sleep(x) 23 | 24 | def tempRead(): 25 | global ds18b20 26 | address = '/sys/bus/w1/devices/' + ds18b20 + '/w1_slave' 27 | tfile = open(address) 28 | text = tfile.read() 29 | tfile.close() 30 | secondline = text.split("\n")[1] 31 | temperaturedata = secondline.split(" ")[9] 32 | temperature = float(temperaturedata[2:]) 33 | temperature = temperature / 1000 34 | return temperature 35 | 36 | def joystickbtn(ev=None): 37 | print 'Interrupt occur!' 38 | destroy() 39 | 40 | def joystickState(): 41 | if ADC0832.getResult1() == 0: 42 | return 1 #up 43 | if ADC0832.getResult1() == 255: 44 | return 2 #down 45 | if ADC0832.getResult() == 0: 46 | return 3 #left 47 | if ADC0832.getResult() == 255: 48 | return 4 #right 49 | 50 | def map(x, in_min, in_max, out_min, out_max): 51 | return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min 52 | 53 | def ds18b20Init(): 54 | global ds18b20 55 | for i in os.listdir('/sys/bus/w1/devices'): 56 | if i != 'w1-bus-master1': 57 | ds18b20 = i 58 | 59 | def setup(): 60 | GPIO.setmode(GPIO.BOARD) 61 | # Buzzer setup: 62 | GPIO.setup(Buzzer, GPIO.OUT) 63 | GPIO.output(Buzzer, 1) 64 | # RGB setup: 65 | GPIO.setup(LedRed, GPIO.OUT) 66 | GPIO.setup(LedGreen, GPIO.OUT) 67 | GPIO.setup(LedBlue, GPIO.OUT) 68 | GPIO.output(LedRed, 1) 69 | GPIO.output(LedGreen, 1) 70 | GPIO.output(LedBlue, 1) 71 | # DS18B20 setup: 72 | ds18b20Init() 73 | # Joystick setup: 74 | GPIO.setup(btn, GPIO.IN) 75 | GPIO.add_event_detect(btn, GPIO.FALLING, callback=joystickbtn, bouncetime=200) 76 | # ADC0832 setup: 77 | ADC0832.setup() 78 | 79 | def loop(): 80 | low = 28 81 | high = 32 82 | print 'System is running...' 83 | 84 | 85 | while True: 86 | # Change low/high limit with Joystick 87 | joystick = joystickState() 88 | print joystick 89 | 90 | if joystick == 1 and low < high - 1: 91 | low += 1 92 | if joystick == 2 and low > 0: 93 | low -= 1 94 | if joystick == 4 and high > low + 1: 95 | high -= 1 96 | if joystick == 3 and high < 125: 97 | high += 1 98 | 99 | print 'The lower limit of temperature:', low 100 | print 'The upper limit of temperature:', high 101 | 102 | # Read temperature from ds18B20 103 | temp = tempRead() 104 | print 'Current temperature:', temp 105 | 106 | # Under/Over limit alarm: 107 | if temp < low: 108 | GPIO.output(LedBlue, 0); 109 | GPIO.output(LedRed, 1); 110 | GPIO.output(LedGreen, 1); 111 | for i in range(0, 4): 112 | beep(0.25) 113 | 114 | if temp >= low and temp < high: 115 | GPIO.output(LedBlue, 1); 116 | GPIO.output(LedRed, 1); 117 | GPIO.output(LedGreen, 0); 118 | time.sleep(1) 119 | 120 | if temp >= high: 121 | GPIO.output(LedBlue, 1); 122 | GPIO.output(LedRed, 0); 123 | GPIO.output(LedGreen, 1); 124 | for i in range(0, 8): 125 | beep(0.125) 126 | 127 | def destroy(): 128 | GPIO.output(LedRed, 1) 129 | GPIO.output(LedGreen, 1) 130 | GPIO.output(LedBlue, 1) 131 | GPIO.output(Buzzer, 1) 132 | GPIO.cleanup() 133 | 134 | if __name__ == '__main__': 135 | setup() 136 | try: 137 | loop() 138 | except KeyboardInterrupt: 139 | destroy() 140 | -------------------------------------------------------------------------------- /ADC0832.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | #----------------------------------------------------- 3 | # 4 | # This is a program for all ADC Module. It 5 | # convert analog singnal to digital signal. 6 | # 7 | # This program is most analog signal modules' 8 | # dependency. Use it like this: 9 | # `import ADC0832` 10 | # `sig = ADC0832.getResult(chn)` 11 | # 12 | # *'chn' should be 0 or 1 represent for ch0 or ch1 13 | # on ADC0832 14 | # 15 | # ACD1302 Pi 16 | # CS ---------------- Pin 11 17 | # CLK --------------- Pin 12 18 | # DI ---------------- Pin 13 19 | 20 | # VCC ----------------- 3.3V 21 | # GND ------------------ GND 22 | # 23 | #----------------------------------------------------- 24 | import RPi.GPIO as GPIO 25 | import time 26 | 27 | ADC_CS = 11 28 | ADC_CLK = 12 29 | ADC_DIO = 13 30 | 31 | # using default pins for backwards compatibility 32 | def setup(cs=11,clk=12,dio=13): 33 | global ADC_CS, ADC_CLK, ADC_DIO 34 | ADC_CS=cs 35 | ADC_CLK=clk 36 | ADC_DIO=dio 37 | GPIO.setwarnings(False) 38 | GPIO.setmode(GPIO.BOARD) # Number GPIOs by its physical location 39 | GPIO.setup(ADC_CS, GPIO.OUT) # Set pins' mode is output 40 | GPIO.setup(ADC_CLK, GPIO.OUT) # Set pins' mode is output 41 | 42 | def destroy(): 43 | GPIO.cleanup() 44 | 45 | # using channel = 0 as default for backwards compatibility 46 | def getResult(channel=0): # Get ADC result, input channal 47 | GPIO.setup(ADC_DIO, GPIO.OUT) 48 | GPIO.output(ADC_CS, 0) 49 | 50 | GPIO.output(ADC_CLK, 0) 51 | GPIO.output(ADC_DIO, 1); time.sleep(0.000002) 52 | GPIO.output(ADC_CLK, 1); time.sleep(0.000002) 53 | GPIO.output(ADC_CLK, 0) 54 | 55 | GPIO.output(ADC_DIO, 1); time.sleep(0.000002) 56 | GPIO.output(ADC_CLK, 1); time.sleep(0.000002) 57 | GPIO.output(ADC_CLK, 0) 58 | 59 | GPIO.output(ADC_DIO, channel); time.sleep(0.000002) 60 | 61 | GPIO.output(ADC_CLK, 1) 62 | GPIO.output(ADC_DIO, 1); time.sleep(0.000002) 63 | GPIO.output(ADC_CLK, 0) 64 | GPIO.output(ADC_DIO, 1); time.sleep(0.000002) 65 | 66 | dat1 = 0 67 | for i in range(0, 8): 68 | GPIO.output(ADC_CLK, 1); time.sleep(0.000002) 69 | GPIO.output(ADC_CLK, 0); time.sleep(0.000002) 70 | GPIO.setup(ADC_DIO, GPIO.IN) 71 | dat1 = dat1 << 1 | GPIO.input(ADC_DIO) 72 | 73 | dat2 = 0 74 | for i in range(0, 8): 75 | dat2 = dat2 | GPIO.input(ADC_DIO) << i 76 | GPIO.output(ADC_CLK, 1); time.sleep(0.000002) 77 | GPIO.output(ADC_CLK, 0); time.sleep(0.000002) 78 | 79 | GPIO.output(ADC_CS, 1) 80 | GPIO.setup(ADC_DIO, GPIO.OUT) 81 | 82 | if dat1 == dat2: 83 | return dat1 84 | else: 85 | return 0 86 | 87 | def getResult1(): 88 | return getResult(1) 89 | 90 | 91 | def loop(): 92 | while True: 93 | res0 = getResult(0) 94 | res1 = getResult(1) 95 | print 'res0 = %d, res1 = %d' % (res0,res1) 96 | time.sleep(0.4) 97 | 98 | if __name__ == '__main__': # Program start from here 99 | setup() 100 | try: 101 | loop() 102 | except KeyboardInterrupt: # When 'Ctrl+C' is pressed, the child program destroy() will be executed. 103 | destroy() 104 | 105 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ## Sunfounder SensorKit Python code for Raspberry Pi 2 | 3 | This is Sunfounder sensor kit python code for Raspberry Pi Model B/B+ and Raspberry Pi 2 Model B. 4 | If you have any questions about this kit, please contact with us. Our website is www.sunfounder.com, and our support E-mail is support@sunfounder.com 5 | 6 | -------------------------------------------------------------------------------- /joystickPS2.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | #------------------------------------------------------ 3 | # 4 | # This is a program for JoystickPS2 Module. 5 | # 6 | # This program depend on ADC0832 ADC chip. Follow 7 | # the instruction book to connect the module and 8 | # ADC0832 to your Raspberry Pi. 9 | # 10 | #------------------------------------------------------ 11 | import ADC0832 12 | import RPi.GPIO as GPIO 13 | import time 14 | 15 | btn = 15 # Define button pin 16 | 17 | def setup(): 18 | ADC0832.setup() # Setup ADC0832 19 | GPIO.setmode(GPIO.BOARD) # Numbers GPIOs by physical location 20 | GPIO.setup(btn, GPIO.IN, pull_up_down=GPIO.PUD_UP) # Setup button pin as input an pull it up 21 | global state 22 | state = ['up', 'down', 'left', 'right'] 23 | 24 | def getResult(): #get joystick result 25 | if ADC0832.getResult1() == 0: 26 | return 1 #up 27 | if ADC0832.getResult1() == 255: 28 | return 2 #down 29 | 30 | if ADC0832.getResult() == 0: 31 | return 3 #left 32 | if ADC0832.getResult() == 255: 33 | return 4 #right 34 | 35 | if GPIO.input(btn) == 1: 36 | print 'Button is pressed!' # Button pressed 37 | 38 | def loop(): 39 | while True: 40 | tmp = getResult() 41 | if tmp != None: 42 | print state[tmp - 1] 43 | 44 | def destory(): 45 | GPIO.cleanup() # Release resource 46 | 47 | if __name__ == '__main__': # Program start from here 48 | setup() 49 | try: 50 | loop() 51 | except KeyboardInterrupt: # When 'Ctrl+C' is pressed, the child program destroy() will be executed. 52 | destory() 53 | -------------------------------------------------------------------------------- /mq-2.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | #----------------------------------------------------- 3 | # 4 | # This is a program for MQ-2 Gas Sensor Module. 5 | # It could detect danger gas and smokes. 6 | # 7 | # This program depend on ADC0832 ADC chip. Follow 8 | # the instruction book to connect the module and 9 | # ADC0832 to your Raspberry Pi. 10 | # 11 | #----------------------------------------------------- 12 | 13 | import RPi.GPIO as GPIO 14 | import ADC0832 15 | import time 16 | 17 | BEEP = 16 # Set buzzer pin 18 | TS = 100 # You can set the Threshold by yourself (0-255) 19 | 20 | def setup(): 21 | GPIO.setmode(GPIO.BOARD) # Numbers GPIOs by physical location 22 | GPIO.setup(BEEP, GPIO.OUT) # Set pins' mode is output 23 | ADC0832.setup() # Setup ADC0832 24 | 25 | def loop(): 26 | while True: 27 | tmp = ADC0832.getResult() # Get analog value from ADC0832 28 | print tmp # Print analog value 29 | if tmp > TS : # Beep when read value greater than threshold 30 | print ' ****************' 31 | print ' * !! DANGER !! *' 32 | print ' ****************' 33 | print '' 34 | 35 | GPIO.output(BEEP, GPIO.HIGH) # (0, means detect danger gas) 36 | time.sleep(0.25) 37 | GPIO.output(BEEP, GPIO.LOW) 38 | time.sleep(0.25) 39 | else : 40 | time.sleep(0.5) # Else delay printing. 41 | 42 | def destory(): 43 | GPIO.cleanup() # Release resource 44 | 45 | if __name__ == '__main__': # Program start from here 46 | setup() 47 | try: 48 | loop() 49 | except KeyboardInterrupt: # When 'Ctrl+C' is pressed, the child program destroy() will be executed. 50 | destory() 51 | --------------------------------------------------------------------------------