I was wondering if anyone could suggest a way of getting this to work.
I'm trying to get the rotary switch to work with the pi, but not having much luck. All I want to do is to be able to read up to 8 positions on a 12-way rotary switch through the pi with three GPIOs.
Then i found this:
https://bobrathbone.com/raspberrypi/doc ... 0Radio.pdf
The main thing that is confusing me is the section where he mentions diodes
tldr; here is the section I refer to:
Whichever way round the diodes are connected, I seem to get very strange results.Wire as follows:
• Leave switch position 1 with nothing connected to it, this has a binary value 0.
• Wire GPIO25 (pin 22) to switch position 2, this has a binary value 1.
• Wire GPIO8 (pin 24) to switch position 3, this has a binary value 2.
• Wire GPIO7 (pin 26) to switch position 5, this has a binary value 4.
• Wire two diodes from switch pin 4 to switch pins 2 and 3 (value 3)
• Wire two diodes from switch pin 6 to switch pins 2 and 5 (value 5)
• Wire two diodes from switch pin 7 to switch pins 3 and 5 (Value 6 - Optional - not used)
• Wire three diodes from switch pin 8 to switch pins 1,3 and 5 (Value 7 – Optional - not used)
• Wire the centre tap of the switch to 0 volts (GND)
The code he used for this is:
Code: Select all
#!/usr/bin/env python # # $Id: rotary_switch_class.py,v 1.2 2018/12/04 12:58:42 bob Exp $ # Raspberry Retro Pi Internet Radio # Retro radio rotary menu switch # This class allows a rotary switch (not encoder) to operate a simple menu for # the Vintage radio # # Author : Bob Rathbone # Site : http://www.bobrathbone.com # # License: GNU V3, See https://www.gnu.org/copyleft/gpl.html # import sys import RPi.GPIO as GPIO import time # Rotary switch class (Not rotary encoder) class RotarySwitch: # Status settings CLEAR = 0 VALUE1 = 1 VALUE2 = 2 VALUE4 = 4 switch1 = None switch2 = None switch4 = None def __init__(self, switch1,switch2,switch4,callback): self.switch1 = switch1 self.switch2 = switch2 self.switch4 = switch4 self.callback = callback # Set up switch lines GPIO.setmode(GPIO.BCM) GPIO.setwarnings(False) if self.switch1 > 0: GPIO.setup(self.switch1, GPIO.IN, pull_up_down=GPIO.PUD_UP) if self.switch2 > 0: GPIO.setup(self.switch2, GPIO.IN, pull_up_down=GPIO.PUD_UP) if self.switch4 > 0: GPIO.setup(self.switch4, GPIO.IN, pull_up_down=GPIO.PUD_UP) # Add event detection to the GPIO inputs switch = 0 try: switch = self.switch1 if self.switch1 > 0: GPIO.add_event_detect(self.switch1, GPIO.FALLING, \ callback=self.callback, bouncetime=100) switch = self.switch2 if self.switch2 > 0: GPIO.add_event_detect(self.switch2, GPIO.FALLING, \ callback=self.callback, bouncetime=100) switch = self.switch4 if self.switch4 > 0: GPIO.add_event_detect(self.switch4, GPIO.FALLING, \ callback=self.callback, bouncetime=100) except Exception as e: print "RotaryClass error setting GPIO " + str(switch) print e return # Get switch state def get(self): value = 0 if not GPIO.input(self.switch1): value += 1 if not GPIO.input(self.switch2): value += 2 if not GPIO.input(self.switch4): value += 4 return value if __name__ == "__main__": switch_value = 0 # Callback routine def rotary_switch_event(switch): global switch_value time.sleep(0.1) value = rotary_switch.get() if value != switch_value: print "Switch", switch,"Value =", value switch_value = value return rotary_switch = RotarySwitch(24,8,7,rotary_switch_event) switch_value = rotary_switch.get() while True: time.sleep(0.1) # End of class
The whole thing was very fiddly, soldering the diodes to the switch. I'm not even sure they are the right way round - the cathodes are pointing to the first pin listed - e.g. the two diodes from pin 4 to pins 2 and 3, the cathodes are both pointing at pin 4.
I hope someone could shed some light on this.