A few years ago I bought a Raspberry Pi but I never found time to experiment with the GPIO bus.
Recently I have bought a breadboard and some parts to experiment. I found a nice tutorial and this is the result.
The source code has been written in Python.
The red LED is used to test the digital output functionality.
The green LED is used to test the analog (Pulse Width Modulation) output functionality.
The yellow switch has been used to test the digtal input functionality.
# External module imports import RPi.GPIO as GPIO import time # PIN definitions # Digital ledRed = 23 # Broadcom pin 23 (P1 pin 16) button = 17 # Broadcom pin 17 (P1 pin 11) # Analog (Pulse Width Modulation) pwmGreen = 18 # Broadcom pin 18 (P1 pin 12) dc = 95 # Duty cycle (0-100) for PWM pin # Pin Setup: GPIO.setmode(GPIO.BCM) # Broadcom pin-numbering scheme GPIO.setup(ledRed, GPIO.OUT) # Set as output GPIO.setup(pwmGreen, GPIO.OUT) # Set as output pwm = GPIO.PWM(pwmGreen,100) # Initialize PWM on pwmGreen with 100Hz frequency GPIO.setup(button, GPIO.IN, pull_up_down=GPIO.PUD_UP) # Button pin set as input w/ pull-up # Initial state for LEDs: GPIO.output(ledRed, GPIO.LOW) pwm.start(dc) print("Here we go! Press CTRL+C to exit") try: while True: if GPIO.input(button): # button is released pwm.ChangeDutyCycle(dc) # Green led GPIO.output(ledRed, GPIO.LOW) # Red led else: # button is pressed: pwm.ChangeDutyCycle(100-dc) # Green led GPIO.output(ledRed, GPIO.HIGH) # Red led time.sleep(0.075) GPIO.output(ledRed, GPIO.LOW) # Red led time.sleep(0.075) except KeyboardInterrupt: # If CTRL+C is pressed, exit cleanly: pwm.stop() # stop PWM GPIO.cleanup() # cleanup all GPIO
More details can be found on the website of learn.sparkfun.com.