RPi is a 3.3V device.
The GPIO pins are unbuffered and unprotected, so if you short it out, you can destroy the RPi.
GPIO I/O Example:
# Import the required module. import RPi.GPIO as GPIO
# Set the mode of numbering the pins. GPIO.setmode(GPIO.BOARD)
# GPIO pin 10 is the output. GPIO.setup(10, GPIO.OUT) GPIO pin 8 is the input. GPIO.setup(8, GPIO.IN)
# Initialise GPIO10 to high (true) so that the LED is off. GPIO.output(10, True) while 1: if GPIO.input(8): GPIO.output( 10, False) else:
# When the button switch is not pressed, turn off the LED. GPIO.output( 10, True)
I2C I/O Example: PCF8574A 8-bit I/O Expander for I2C BUS
# example2.py import smbus
# Access the i2c bus now. bus = smbus.SMBus(0)
# Now write 1 to the device with the address 56, turn off the LED by setting pin 0 to 1, and reset the switch by switching pin 1 to 0. bus.write_byte(56, 1) while 1:
# If the button is pressed, pin 1 will be 1 and the byte read from the device with address 56 will be 00000010 (2) or 0000000011 (3). if bus.read_byte(56) in (2,3):
# Write 00000000, setting pin 0 to 0, turning on the LED, and resetting the switch with pin 1 to 0. bus.write_byte(56, 0) else:
# Write 00000010, setting pin 0 to 1, turning off the LED, and pin 1 to 0 to reset the switch. bus.write_byte(56, 1)