User avatar
MortySmith
Posts: 16
Joined: Tue Aug 13, 2019 3:45 pm
Location: Cambridge, UK

Rotary switch (not encoder) problems [SOLVED]

Tue Aug 13, 2019 3:59 pm

Hi

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:
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)
Whichever way round the diodes are connected, I seem to get very strange results.

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
with all 9 diodes, I don't seem to get the numbers 1, or 2, or I get very erratic results.
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.

Thanks
Last edited by MortySmith on Wed Aug 21, 2019 3:37 pm, edited 1 time in total.
"Uncertainty is inherently unsustainable. Eventually, everything either is or isn't" - Rick Sanchez

pcmanbob
Posts: 6619
Joined: Fri May 31, 2013 9:28 pm
Location: Mansfield UK

Re: Rotary switch (not encoder) problems

Wed Aug 14, 2019 8:28 am

Hi.

The first this I would suggest is to remove the switch and all the wiring/diodes and then test the switch to see if it works in the same way as the one in the tutorial.

So you need to find the common connection and then test using a multimeter set on ohms between the common and each terminal on the switch noting the if there is a closed or open connection, do this for each switch position.

Reading the tutorial the switch used only connected one switch terminal to common for each switch position,

doing this should also confirm which switch position, switches which terminal on.

once you have confirmed your switch works in the same way and you know which terminals are which switch positions, then you need to wire it up like this

Image

the black line on your diodes should match the black line on the diode symbol

Image

I have left out the diodes for switch positions 7 & 8 as they are not used.
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

User avatar
MortySmith
Posts: 16
Joined: Tue Aug 13, 2019 3:45 pm
Location: Cambridge, UK

Re: Rotary switch (not encoder) problems

Wed Aug 14, 2019 2:44 pm

Wow.
Thanks ever so much for that, pcmanbob.

I'll do what you suggested and let you know how I get on.

Your diagram makes a lot more sense than the description in the original project too.

btw. what program did you use for that schematic?

Kind regards

M
"Uncertainty is inherently unsustainable. Eventually, everything either is or isn't" - Rick Sanchez

pcmanbob
Posts: 6619
Joined: Fri May 31, 2013 9:28 pm
Location: Mansfield UK

Re: Rotary switch (not encoder) problems

Wed Aug 14, 2019 3:58 pm

I use tinycad to produce my circuit diagrams https://www.tinycad.net/ it only runs on windows x86 pc
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

User avatar
MortySmith
Posts: 16
Joined: Tue Aug 13, 2019 3:45 pm
Location: Cambridge, UK

Re: Rotary switch (not encoder) problems

Wed Aug 14, 2019 7:46 pm

Hi pcmanbob

After some probing of the switch with a multimeter on continuity mode, I can confirm that it works the same way as the one in Bob's tutorial.

I've just designed a circuit on stripboard based on your schematic, but I still seem to be getting weird problems with the code provided on Bob's website.
I changed the pin numbers in the code to match your diagram, but it still outputs very weird results - Sometimes it doesn't even register changes in the switch position - it'll go from 0 to 2, rotate back, and it jumps about.
I've attached my stripboard layout I did in diylc, after I'd actually built the circuit. I double-checked it to make sure the diodes were in the right places (the two diodes standing upright have the line pointing toward the bottom edge of the stripboard).
The switch terminals are connected to the stripboard with dupont connectors, and the four (inc. GND) connections to the RPi are the same.

I'm going to try just running a stripped-down python script that just checks each of the GPIOs separately in a loop.

Is there anything else you could think of trying?
Attachments
rotary.png
rotary.png (25.68 KiB) Viewed 1364 times
Last edited by MortySmith on Sat Aug 17, 2019 5:25 pm, edited 1 time in total.
"Uncertainty is inherently unsustainable. Eventually, everything either is or isn't" - Rick Sanchez

pcmanbob
Posts: 6619
Joined: Fri May 31, 2013 9:28 pm
Location: Mansfield UK

Re: Rotary switch (not encoder) problems

Thu Aug 15, 2019 11:59 am

As I don't have access to your hardware or a rotary switch of the same type I can't do any testing to try and figure out what's happening,

Have you tried contacting the person who wrote the tutorial you are following may be they would have some insight in to your problem.
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

User avatar
MortySmith
Posts: 16
Joined: Tue Aug 13, 2019 3:45 pm
Location: Cambridge, UK

Re: Rotary switch (not encoder) problems

Thu Aug 15, 2019 4:59 pm

Thanks.
I took your advice, and have just used the feedback form on his website to e-mail him.

I'd also like to thank you for your help, especially the schematic. It has given me the encouragement to continue experimenting and try and get this to work..

What I want this for is as part of a retro-radio project, similar to bob rathbone's, but integrating a few extra features in addition to the ones his provides.

Kind regards

M
"Uncertainty is inherently unsustainable. Eventually, everything either is or isn't" - Rick Sanchez

User avatar
MortySmith
Posts: 16
Joined: Tue Aug 13, 2019 3:45 pm
Location: Cambridge, UK

Re: Rotary switch (not encoder) problems

Sat Aug 17, 2019 5:30 pm

If anyone's still following this thread..
With Bob Rathbone's help I've been making some progress with the switch.
This is now fixed, and I'm marking this thread as solved.

M
"Uncertainty is inherently unsustainable. Eventually, everything either is or isn't" - Rick Sanchez

Return to “Interfacing (DSI, CSI, I2C, etc.)”