Rotary encoder for RPi + PI-DigiAmp+ with Moodeaudio

Hello team,

Hope you can assist,

I am adding the rotary encoder ADA377 volume control to the RPi4 + Pi-DigiAmp+ combination. Using moode audio release 8.3.1

Rotary encoder is connected to pins 14, 16, 18 on the DigiAmp 40 pin connector (middle pin on the encoder to pin 14 (Ground), and side pins on the encoder to pins 16, 18 (GPIO 23,24).

It seems to work, but it’s really unreliable, lots of skips, and sometimes the volume setting even goes backwards a couple steps when trying to increase, and sometimes volume setting goes up a couple steps when trying to decrease. Sounds like the GPIO pin state changes are not detected consistently. Wondering if I am missing something important - do I need to add 10K pull_up resistors between the encoder side pins and 3.3V pin 1 ?

Please let me know, thanks in advance,
Cheers,
-Victor

Hi Victor.
Sounds like you might have a switch debounce problem. Do you have any filtering or debounce in your program?
Here is a simple hardware filter that is easier than the program approach I think and works very well.

image
Courtesy of Bourns data sheet for their range of encoders.
Note the 10kΩ resistors are effectively pull up/down resistors and no other external (or internal) resistors should be used.
Cheers Bob

Thanks for this Bob.
Unfortunately I am not aware if moode audio software has any filtering or debounce built in.

Note the 10kΩ resistors are effectively pull up/down resistors and no other external (or internal) resistors should be used.
So basically what you are saying is that all I need is a couple of 10n capacitors from encoder side pins to GND?
Thanks for your help.
Cheers,
-Victor

Hi Victor

NO. Arrange the circuit EXACTLY as shown. Just putting caps from encoder pins to ground will do nothing. Just do not use any other pull up/down resistors. Just those.

From your description it doesn’t look like it Doesn’t matter if it does.

Just try it. It is only 4 resistors and 2 caps and it just might work. Always has for me and I have used it often.
Cheers Bob

Thanks Bob. I will give it a go.
Cheers,
-Victor

Hi Victor
Add on
If your RPi has 3.3V logic (which I think all RPi things have) substitute 3.3V for the 5V shown on that little circuit.
Cheers Bob

Yes thanks Bob. RPi does have 3.3V logic, I noted that. Also RPi has built in pull_up resistors.
So I was just thinking adding 10K + 10n pairs to the encoder side pins as per the diagram and test that. I am also planning to post the question to the moode audio forum.
Thanks for all your help so far.
Cheers,
-Victor

Hey Victor,

Looks like Bob might have given a great solution to your problem. Let us know how it goes as hopefully we can recommend this for similar issues in future as a known fix!

Cheers,
Blayden

Hi Victor
Don’t know much about RPi but with Arduino you have to enable this with the PULLUP statement. Disable this if you can. If it is enabled the effect will be dependant on what the value is. It means the pin will not pull completely to ground but will have a voltage divider formed by the pull up resistor and the 10kΩ. If the pull up is 10kΩ the pin would only come down to 1.65V which may not be enough.

As these pins are supposed to be GPIO (General Purpose IO) I can’t see the pin being permanently pulled up and should have to be declared. If this is the case remove it and have the pin declared as a straight input without the pull up. I don’t know how RPi handles this sort of thing.

This is easy enough to establish.
Disconnect everything external from the pin.
Connect the pin to ground with a 10kΩ resistor.
Measure the voltage on the pin.
If no pull up it should be 0V or very close to that.
If some value above 0V a pull up resistance will be present. Knowing the pull up is to 3.3V you should be able to calculate this value. If it is 1.65 or close the pull up value is 10kΩ.
Cheers Bob

Thanks again Bob. The pull_up resistors in the RPi are software-enabled (in my case moode audio software enables them). From what I know, RPi built-in pull_up resistors are 50K (confirmed this using your suggested method - when SW pulled up and 10K to the GND I read 0.55V which is expected with 3.3V on the rail.).
I am going to get a couple 10n capacitors and arrange the Bourns datasheet filter circuit to try.
Really appreciate your help.
Cheers,
-Victor

Hi Victor
Yes .55V is spot on with 50k pull up.
It might be that .55V is low enough for a reliable LOW in RPi. I just don’t know and you will just have to check. If it is not published I would try using something else. Remember I did say “reliable”.

It may be possible to remove this pull up and just declare as a normal input. I am not familiar with that software so you are on your own there.

You will not do any good just putting a cap from pin to ground as the R/C filter needs the R part or the cap will discharge immediately and do nothing. I recon these values have been optimised by Bourne to suit their particular switch bounce characteristics.
Cheers Bob

Hi Victor
The RPi documentation states a maximum voltage for a LOW is 0.9V so if you can’t disable the internal pull up you might just get away with .55v.

If you experience any trouble with 0.55V you could try doubling the cap value to about 18n or 22n (I think these are preferred values) and halving the resistors to 5k1Ω. This will be the same R/C time constant but will double the idle current through those networks. If your 3.3V is not being used for anything else that may not be a problem. This will reduce your “LOW” voltage to 0.395 (0.4) which will allow a bit more flexibility.

Hi Blayden
Yes I know from experimental usage that this filter does work. I think it is as I said, Bourns have settled for this time constant to suit their switch bounce characteristic. Other makes may require different values but as I seem to finish up with Bourns these values suit me.

Hint, Hint. It would be really useful if somebody produced a little board with these components fitted. Switch bounce is a problem with rotary encoders. Trying to decode without a filter is a nightmare, the result is all over the place. Something like Victor’s volume control.
Cheers Bob

Thanks guys, I have added these RC networks (10K resistor + 10n cap) - it improved things very significantly indeed. Not 100% reliable, but much better. I will do more testing tomorrow.
Thanks very much for your help.
Cheers,
-Victor

Hi Victor.
4 x resistors and 2 x caps (2 x res and 1 x cap each switch) I hope. Funny, Just decoding and printing in serial monitor in Arduino I found nearly 100% reliable. If you haven’t been able to disable the internal pull up this might be an issue.
Sounds like you are on the right track. Keep us posted.
Cheers Bob

Yes understand, thanks Bob. Looks reasonably OK now, with filters.
Interestingly, just got some info from the moOde SW developer - the driver they used was designed around the ALPS encoder:

Thanks for your help.
Cheers,
-Victor

Hi Victor
The Bourns data sheet I referred to was the PEC11R series which has the same bounce characteristic as the Alps one at Max 2mSec so that filter should be OK.

As a point of interest what encoder are you using. That linked one, ALPS EC12E series has a different indent positioning . The indent is on the edge of the B signal and could be on or off. The data sheet says “the position of the B signal cannot be determined”. I don’t know if this contributes to mis counting. Personally I prefer the indent positioning of the ALPS EC12D series which is the same as the Bourne ones I use. The signal is much more predictable as both A and B are known.

The bit that might cause change is the internal pull up. This possibly causes a reduction in time constant as instead of charging the cap to 3.3V then discharging to 0V it will only discharge to 0.55V. This may be your problem I don’t know but I suggest you try to get rid of it if you can. You will have to re define that input pin. Failing that you might try to increase the time constant by increasing the cap value to 12 or 15nF and see if that gets rid of your problem.
Cheers Bob

Thanks again Bob, you’ve been really helpful.
OK I ended up using this one:

With filters, it works more or less (I’d say 90% accuracy)
I also played around with the Bourns PEC11R unit:

Bourns unit worked markedly better (also with filters) and the reason I used one from Jaycar is because I already extended the shaft to the length I needed…
Next time I order something from Element14 or Mouser - will get ALPS EC12E and EC12D as well (they are really cheap) and play around.
Regarding time constant, I did try 20n (just because I had a few 10n caps on hand) - it didnt really change much.
Regarding redefining the pin - I am not sure it’s possible. With Raspberry OS, as far as I know, you can only define the GPIO pin as pull_up or pull_down. Also, moOde SW takes care of the pin init. Apart from this encoder saga, It actually works really well, here is the pointer just FYI:
https://moodeaudio.org/
Cheers,
-Victor

Hi Victor.
The link to the Core unit is a Bourns. I think it pays to use a known quality brand. I personally at the moment have 10 of the PEC11R series units attached to my flight simulator via a Leo Bodnar board. Admittedly the board looks after switch de bounce but they have never faltered once in the last year or so. The other difference is I am using 5V. I also use 5V and a Freetronics 11 (Arduino UNO R3) on my experimental set up and by using the filter described above to not have a bit of bother with mis reads. Without the filter the readings go all over the place in both directions.

The ALPS EC12D unit has the same indent arrangement as the PEC11R. I explained the difference above.

I don’t suppose the program itself is a bit flakey in this regard or the 5V versus 3.3V is the difference. As for myself I tend to favour 5V as it gives you that bit more headroom when switching Mosfets and dealing with little voltage drops here and there such as diode forward voltage drops etc which can add up in some circumstances.

I very much doubt that. Removes a lot of flexibility. Would have to check, perhaps someone with more RPi experience (would not have to be much as I know practically nothing about RPi, too iffy) could jump in and clarify.
Cheers Bob
PS: I note all the data sheets for these encoders indicate 5V. I don’t know if there might be a specific reason or not.

Hi Bob,
Thanks again for your help.
You are right - there is a way to disable internal pull-up via command line:
raspi-gpio set [GPIO] pn
I have not yet tested this, will need to install external pull-ups and try…
Must say my “jaycar encoder” doesn’t work very well after all. Better than without filters, but still unsatisfactory;-(
If external pull-ups do not improve the situation, will have to work on extending the shaft of the Bourns unit form core electronics.
Cheers,
-Victor