Last active
August 11, 2022 15:30
-
-
Save rc1/5900546 to your computer and use it in GitHub Desktop.
Reading a DIP switch on the RPI
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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