gingerbill
Posts: 4
Joined: Sat Aug 18, 2012 2:20 pm

Need help to get a Rotary Encoder working

Sat Aug 18, 2012 2:26 pm

I'm pretty much a beginner when it comes to GPIO. I've found many tutorials online related to the arduino on how to work a rotary encoder but I cannot seem to get it work on a Raspberry pi. I have these rotary encoders:
Image
http://www.skpang.co.uk/catalog/rotary- ... p-435.html

User avatar
Gert van Loo
Posts: 2487
Joined: Tue Aug 02, 2011 7:27 am
Contact: Website

Re: Need help to get a Rotary Encoder working

Sat Aug 18, 2012 3:27 pm

gingerbill wrote:...but I cannot seem to get it work on a Raspberry pi. I have these rotary encoders:
....
"It don't work" does not leave much clues.
How about telling a bit more of what you tried and what did not work.

tech_monkey
Posts: 130
Joined: Fri Mar 09, 2012 6:12 pm

Re: Need help to get a Rotary Encoder working

Sat Aug 18, 2012 3:32 pm

And how you connected it up to the GPIO.
http://www.casatech.eu

User avatar
mahjongg
Forum Moderator
Forum Moderator
Posts: 13223
Joined: Sun Mar 11, 2012 12:19 am
Location: South Holland, The Netherlands

Re: Need help to get a Rotary Encoder working

Sat Aug 18, 2012 3:43 pm

The trick with rotary encoders is to sample all its pins simultaneously, otherwise you may end up with on pin changing state while you are sampling the pins, and that can truly spoil decoding.

gingerbill
Posts: 4
Joined: Sat Aug 18, 2012 2:20 pm

Re: Need help to get a Rotary Encoder working

Sat Aug 18, 2012 3:52 pm

Sorry that I wasn't clear but what I'm asking for is an example on how to do it. I only know the absolute basics of the GPIO as in making a LED blink and other basic things. I've followed the examples on Gordons Projects:https://projects.drogon.net/raspberry-pi/gpio-examples/.

User avatar
DexOS
Posts: 876
Joined: Wed May 16, 2012 6:32 pm
Contact: Website

Re: Need help to get a Rotary Encoder working

Sat Aug 18, 2012 4:08 pm

gingerbill wrote:Sorry that I wasn't clear but what I'm asking for is an example on how to do it. I only know the absolute basics of the GPIO as in making a LED blink and other basic things. I've followed the examples on Gordons Projects:https://projects.drogon.net/raspberry-pi/gpio-examples/.
The problem is unless someone has already written an example, you need to try and take Gordons gpio-examples and the arduino code and see if you can convert it, when it does not do what you want, you post the code here stating what you want to do and what it does.
That way you learn, and we feel you have at lest tried.
Batteries not included, Some assembly required.

gingerbill
Posts: 4
Joined: Sat Aug 18, 2012 2:20 pm

Re: Need help to get a Rotary Encoder working

Sat Aug 18, 2012 4:11 pm

DexOS wrote: The problem is unless someone has already written an example, you need to try and take Gordons gpio-examples and the arduino code and see if you can convert it, when it does not do what you want, you post the code here stating what you want to do and what it does.
That way you learn, and we feel you have at lest tried.
I'll just have to try and hopefully succeed. If I cannot get the code to work, I will post it here.

User avatar
Grumpy Mike
Posts: 936
Joined: Sat Sep 10, 2011 7:49 pm
Location: Manchester (England England)
Contact: Website

Re: Need help to get a Rotary Encoder working

Sat Aug 18, 2012 4:14 pm

The problem with a rotary encoder is that you have to be looking at it when it makes the transitions or at least between the transitions. It depends on your application. Can you afford to miss any pulses? What speed will these be produced at?

The main problem is that Linux will not allow you to poll the GPIO lines regularly enough to guarantee not to miss any pulses. I am not sure if any interrupts can be serviced fast enough either. On an arduino this is normally done with an interrupt but in Linux land interrupts are a very different thing. It is probably best using an external processor like an arduino or Gert board to off load the pulse gathering.

gingerbill
Posts: 4
Joined: Sat Aug 18, 2012 2:20 pm

Re: Need help to get a Rotary Encoder working

Sat Aug 18, 2012 5:08 pm

Grumpy Mike wrote:The problem with a rotary encoder is that you have to be looking at it when it makes the transitions or at least between the transitions. It depends on your application. Can you afford to miss any pulses? What speed will these be produced at?

The main problem is that Linux will not allow you to poll the GPIO lines regularly enough to guarantee not to miss any pulses. I am not sure if any interrupts can be serviced fast enough either. On an arduino this is normally done with an interrupt but in Linux land interrupts are a very different thing. It is probably best using an external processor like an arduino or Gert board to off load the pulse gathering.
I've just whipped up some quick code and I can see what you mean in that it misses pulses. Warning, this code is not well written; it's just to test.

Code: Select all

#include <cstdio>
#include <cstdlib>
#include <unistd.h>
#include <wiringPi.h>
#include <iostream>

int brightness = 120;
int fadeAmount = 10;
const int pin_A = 0;
const int pin_B = 1;
unsigned char encoder_A;
unsigned char encoder_B;
unsigned char encoder_A_prev = 0;

void setup();

void setup()
{
	if (geteuid() != 0)
	{
		fprintf(stderr, "Need to be root to run(sudo?)\n");
		exit(0);
	}
	if (wiringPiSetup() == -1)
		exit(1);

	pinMode(pin_A, INPUT);
	pinMode(pin_B, INPUT);
}

int main(int argc, const char **argv)
{
	setup();
	while(true)
	{
			encoder_A = digitalRead(pin_A); 		// Read Encoder Pins
			encoder_B = digitalRead(pin_B);
			if ((!encoder_A) && (encoder_A_prev))
			{
				if (encoder_B)
				{
					if (brightness + fadeAmount <= 255)
						brightness += fadeAmount;
				}
				else
				{
					if (brightness - fadeAmount >= 0)
						brightness -= fadeAmount;
				}
			}

			encoder_A_prev = encoder_A; 		// Store value of A for next time

			std::cout << "B: " << brightness << "\n";

			usleep(5000);
	}

	return 0;
}

User avatar
Grumpy Mike
Posts: 936
Joined: Sat Sep 10, 2011 7:49 pm
Location: Manchester (England England)
Contact: Website

Re: Need help to get a Rotary Encoder working

Sat Aug 18, 2012 5:27 pm

You can add hardware to count the signals and then read the counter with parallel GPIOs, I did a project years ago, the relevant part is here:-
http://www.thebox.myzen.co.uk/Workshop/Rotary_Max.html
If you do this note that the pin out of the 74LS75 can change as there are different versions of it so check what one you have.

You can also get chips that do this I believe.

User avatar
Grumpy Mike
Posts: 936
Joined: Sat Sep 10, 2011 7:49 pm
Location: Manchester (England England)
Contact: Website

Re: Need help to get a Rotary Encoder working

Sun Aug 19, 2012 9:18 pm

Found the chip that will do it for you.
The HCTL-2000, HCTL-2016, HCTL-2020 from Adgilent (Hewlett Packard)

User avatar
rurwin
Forum Moderator
Forum Moderator
Posts: 4257
Joined: Mon Jan 09, 2012 3:16 pm
Contact: Website

Re: Need help to get a Rotary Encoder working

Sun Aug 19, 2012 9:51 pm

That's rather expensive. A CD4029 should do the job for pennies, although you may need 2, and you may need some ancillary latches and so forth. A parallel-in/serial-out latch would keep the GPIO pin count down.

The trick is, you use one quadrature output as the clock input to a counter. The other quadrature output specifies the direction to count. Then all you have to do is to read the value often enough that the software can work out which direction the count is going.

davidmam
Posts: 101
Joined: Tue Dec 06, 2011 4:13 pm

Re: Need help to get a Rotary Encoder working

Sun Aug 19, 2012 10:23 pm

I've doen something similar on arduino where I use a simple binary counter to sample the pulses (in my case it is a magnet on a drum passing a hall effect sensor). The counter is then read via a PISO. This requires that you poll at least once every full cycle of the counter and that you don't need to know when every pulse occurs but you do need to know how many occurred in a time window.

You then poll the GPIOs that you use to talk to the PISO (latch, clock and data) at your leisure though what you get is knowing how many events occurred at the point you latched the PISO.

User avatar
Grumpy Mike
Posts: 936
Joined: Sat Sep 10, 2011 7:49 pm
Location: Manchester (England England)
Contact: Website

Re: Need help to get a Rotary Encoder working

Mon Aug 20, 2012 9:41 pm

rurwin wrote:Then all you have to do is to read the value often enough that the software can work out which direction the count is going.
Yes that is ALL you have to do, but this is Linux so you can't.

User avatar
rurwin
Forum Moderator
Forum Moderator
Posts: 4257
Joined: Mon Jan 09, 2012 3:16 pm
Contact: Website

Re: Need help to get a Rotary Encoder working

Mon Aug 20, 2012 10:11 pm

I beg your pardon, my previous remarks were regarding quadrature encoders, of which this isn't one.
Grumpy Mike wrote:
rurwin wrote:Then all you have to do is to read the value often enough that the software can work out which direction the count is going.
Yes that is ALL you have to do, but this is Linux so you can't.
Using external logic as I was suggesting, that is not an issue. However that is a different scenario, and it isn't necessary.

With a big heavy knob on it, you might be able to turn it at ten revolutions per second if you tried hard. You need two readings per revolution to work out which way it's going. 20Hz is achievable in Linux. There are some interrupt routines that currently take 100ms, but they are at this moment being tracked down and re-coded. Even so it would affect only the fastest knob twirlers, and all you would get is a stutter -- forward 5, back 1, forward 4 instead of forward 10. When it mattered, you'd be turning the knob slowly.

Now if you were trying to encode a rotating shaft for, say, a robot wheel, then you'd have to re-do that maths, but the chances are good that it would still work in many cases.

User avatar
Grumpy Mike
Posts: 936
Joined: Sat Sep 10, 2011 7:49 pm
Location: Manchester (England England)
Contact: Website

Re: Need help to get a Rotary Encoder working

Tue Aug 21, 2012 10:19 am

rurwin wrote:I beg your pardon, my previous remarks were regarding quadrature encoders, of which this isn't one.
Sorry, misunderstood what you meant.

However have you tried that sachem? It would not work with TTL up/Down counters because the flip flops inside the counters were not edge triggered master / slave D type flip flops but simple level triggered D type flip flops. This meant that while the clock was active any change in the up/down line would cause a count and thus not work like you would hope on a quadrature signal.
The data sheet for the TI version of the CD4029 says:-
The up/down control can be changed at any count. The only restriction on changing the up/down control is that the clock input to the first counting stage must be high.
Which sounds like it is the same as the TTL versions I have tried.

User avatar
rurwin
Forum Moderator
Forum Moderator
Posts: 4257
Joined: Mon Jan 09, 2012 3:16 pm
Contact: Website

Re: Need help to get a Rotary Encoder working

Tue Aug 21, 2012 11:07 am

That's the only manufacturer who mentions it, and they all specify edge triggering. *sigh*

And to answer your question, no, but that's the algorithm that we implement in software with a 1024-line encoder at 8000 RPM. (No we don't use Linux, we use a DSP ;-) )

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