Download presentation
Presentation is loading. Please wait.
1
Controlling LED with PWM
# A6 anode+ long lead # A4 cathode- short lead # B6 to B12 Resistor # Brown-Red-Red 1.2KOhm # C4 to GND # B12 to GPIO 12 . Copyright (c) 2017 Dr. E. Horvath
2
Controlling LED with PWM
import RPi.GPIO as GPIO from time import sleep def main(): GPIO.setmode(GPIO.BCM) GPIO.setup(12, GPIO.OUT) pwm_green = GPIO.PWM(12, 100) # GPIO=12 period=1/100Hz pwm_green.start(0) try: while True: for value in range(0, 101, 5): pwm_green.ChangeDutyCycle(value) sleep(0.3) Copyright (c) 2017 Dr. E. Horvath
3
Controlling LED with PWM
for value in range(100, -1, -5): pwm_green.ChangeDutyCycle(vakye) sleep(0.3) except KeyboardInterrupt: print("Exiting") finally: pwm_green.stop() GPIO.cleanup() main() . Copyright (c) 2017 Dr. E. Horvath
4
Copyright (c) 2017 Dr. E. Horvath
LED and Button Circuit Copyright (c) 2017 Dr. E. Horvath
5
Handling Button Presses
# Button same side of button, across the divider of the board # Button F25 # Button E25 # G21 to G27 10KOhm # D27 to GND (Pin 39) # H21 to GPIO 4 # Brown-Black-Orange 10KOhm resisto Copyright (c) 2017 Dr. E. Horvath
6
Handling Button Presses
# Button same side of button, across the divider of the board # Button F25 # Button E25 # G21 to G27 10KOhm # D27 to GND (Pin 39) # H21 to GPIO 4 # Brown-Black-Orange 10KOhm resistor # It is very important to use a large resistance, since a low resistance will fry your Pi. Copyright (c) 2017 Dr. E. Horvath
7
Handling Button Presses
def main(): GPIO.setmode(GPIO.BCM) button_pin = 4 GPIO.setup(button_pin, GPIO.IN, pull_up_down=GPIO.PUD_UP) # The GPIO is pulled high by default. The other side of the button is # connected to GND, when the button is pressed, it is pulled down to # ground. Copyright (c) 2017 Dr. E. Horvath
8
Handling Button Presses
try: while True: value = GPIO.input(button_pin) print("value " + str(value)) if value == 0: print("Button pressed") sleep(0.05) except KeyboardInterrupt: print("Exiting") finally: p.stop() GPIO.cleanup() main() Copyright (c) 2017 Dr. E. Horvath
9
Copyright (c) 2017 Dr. E. Horvath
Speaker Leaf #Used to play low level sounds. # Red-Red-Brown 220 Ohm # Speaker leaf s+ to F15 # Speaker leaf s- to F17 # G15 to G17 to 220Ohm # H10 to GPIO pin 18 # I17 to GND pint 39 Copyright (c) 2017 Dr. E. Horvath
10
Copyright (c) 2017 Dr. E. Horvath
Speaker Leaf import RPi.GPIO as GPIO from time import sleep def playsound(): print("Setup pins") GPIO.setmode(GPIO.BCM) pwm_pin = 18 GPIO.setup(pwm_pin,GPIO.OUT) pwm_sound = GPIO.PWM(pwm_pin, 1000) for j in range(0,len(melody)): melody[j] = j*10 note_durations = [0.3] * len(melody) Copyright (c) 2017 Dr. E. Horvath
11
Copyright (c) 2017 Dr. E. Horvath
Speaker Leaf melody = [200,200,200,0,200,200,200,0,200,320,200,360,300] note_durations = [0.3] * len(melody) print("Trying to play") try: for i in range(0,len(melody)): f = melody[i] # time between the pulses print(str(f)) if f != 0: pwm_sound = GPIO.PWM(pwm_pin, f) pwm_sound.start(0.6) Copyright (c) 2017 Dr. E. Horvath
12
Copyright (c) 2017 Dr. E. Horvath
Speaker Leaf sleep(note_durations[i]) except KeyboardInterrupt: print("Exiting...") finally: print("Cleanup") GPIO.cleanup() sleep(0.5) print("Done") playsound() Copyright (c) 2017 Dr. E. Horvath
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.