Skip to content

Instantly share code, notes, and snippets.

@rc1
Last active August 11, 2022 15:30
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save rc1/5900546 to your computer and use it in GitHub Desktop.
Save rc1/5900546 to your computer and use it in GitHub Desktop.
Reading a DIP switch on the RPI
#!/usr/bin/env python
import RPi.GPIO as GPIO
import time
# use P1 header pin numbering convention
GPIO.setmode(GPIO.BOARD)
# Config
pins = [3,5,7,8,10,11,12]
# GPIO Setup
for pin in pins:
GPIO.setup(pin, GPIO.IN, pull_up_down=GPIO.PUD_UP)
# Wait
time.sleep(0.03)
i = 1
# lazy not bit smashing
output = ''
for pin in pins:
# inverted
if not GPIO.input(pin):
output += '1'
else:
output += '0'
i += 1
# reverse the output string
output = output[::-1];
print int(output, 2)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment