Worthless rotary encoders - Page 1
Forum section: electronics / beginners

author: Jan Audio
author_rank: Frequent Contributor
timestamp: April 24, 2021, 02:18:28 pm
content: Why would they sell rotary encoders that give pulses on both pins ?The pins are always high volt.Am i being ripped off ?Normally those china encoders work for me,this time i ordered 100, placed them away for later use, and just found out they are not usable.

author: mikerj
author_rank: Super Contributor
timestamp: April 24, 2021, 02:24:49 pm
content: Quote from: Jan Audio on April 24, 2021, 02:18:28 pmAll quadrature encoders give pulses on the two output pins. You said you are seeing pulses on both pins, but then you say both pins remain at high voltage so I'm not sure what the actual problem is?

author: MikeK
author_rank: Super Contributor
timestamp: April 24, 2021, 02:56:17 pm
content: Post a scope trace.

author: MarkF
author_rank: Super Contributor
timestamp: April 24, 2021, 03:08:08 pm
content: The Bourns PEC11 encoders are that way.- They have pulses on both lines WHILE being turned.- With no movement, both lines are high.

author: Jan Audio
author_rank: Frequent Contributor
timestamp: April 24, 2021, 03:11:36 pm
content: Hmz, how do i get the movement then ?It always goes up with my code now.

author: Jan Audio
author_rank: Frequent Contributor
timestamp: April 24, 2021, 03:13:34 pm
content: Quote from: mikerj on April 24, 2021, 02:24:49 pmNegative pulses, sorry.

author: HB9EVI
author_rank: Frequent Contributor
timestamp: April 24, 2021, 03:17:14 pm
content: either you change your code to do the decoding or hardware-decode with a 74HC74; then a rotary encoder behaves like up/down buttons

author: Jan Audio
author_rank: Frequent Contributor
timestamp: April 24, 2021, 03:32:16 pm
content: Well, i have my code in the main loop, no interrupts.Hardware no, it is a finished project, i just need some new encoders.So they are still usable with interrupts, intresting to test in a simple project soon.Thanks.Those alps are so expensive i cant afford them.And mouser has so many options i stopped searching after opening the site.Just going to order those alps for around 2 euro each, i make very expensive gear now.Did i miss something when ordering ebay encoders ?Could i have found out ?I did not know there are so many options.

author: Jan Audio
author_rank: Frequent Contributor
timestamp: April 24, 2021, 03:42:59 pm
content: Quote from: MikeK on April 24, 2021, 02:56:17 pmI have a single channel analog scope.Waiting for a modern scope to come out anytime soon with better specs.

author: HB9EVI
author_rank: Frequent Contributor
timestamp: April 24, 2021, 03:49:53 pm
content: personally I don't know any encoders which don't do gray code output, so I'm a bit surprised that you struggle with it now.never mind whether I used magnetic, optical or mechanical encoders, they all worked the same way, with same code on mcu sidemost of my projects run with those cheap china encoders, never had any unexpected behaviour with them

author: Jan Audio
author_rank: Frequent Contributor
timestamp: April 24, 2021, 03:53:32 pm
content: I have these from ebay :50PCS Rotary encoder code switch EC11 audio digital potentiometer with switch 5pITEM PRICE:US $22.20Glad i dont have 100.What does that EC11 means then ?Same as the bourns ?

author: Domagoj T
author_rank: Frequent Contributor
timestamp: April 24, 2021, 03:55:10 pm
content: Are you sure your code is correct?

author: Jan Audio
author_rank: Frequent Contributor
timestamp: April 24, 2021, 03:59:24 pm
content: I dont know, what do you mean ?There is no further info :https://www.ebay.com/itm/293687768027?hash=item446127b3db:g:V24AAOSwtbpeg2vb

author: Domagoj T
author_rank: Frequent Contributor
timestamp: April 24, 2021, 04:23:54 pm
content: You are using some software to monitor the encoder. Are you sure the software is correct?Show us the code.

author: madires
author_rank: Super Contributor
timestamp: April 24, 2021, 04:44:06 pm
content: If "EC11" refers to being compatible with ALPS EC11 series then the rotary encoders should have two Gray code steps per detent. However, most cheap rotary encoders have four Gray code steps (complete Gray code sequence) per detent. You can check that also with an inexpensive USB-based logic analyzer, if you don't have a multi-channel scope.

author: wizard69
author_rank: Super Contributor
timestamp: April 24, 2021, 05:09:34 pm
content: Look up the manufacture and part number on google and down load the data sheet. Right now you have people guessing at what might be the encoders behavior which is not all that useful if the actual device family is not known.If the encoder doesn't have suitable markings nor the packaging, then I'd call this a lesson learned. Most likely it isn't the encoder you think it is.Quote from: Jan Audio on April 24, 2021, 03:53:32 pm

author: rhodges
author_rank: Frequent Contributor
timestamp: April 24, 2021, 09:58:43 pm
content: I have some ALPS rotary encoders, and here are my notes from my library:Code:The ALPS rotary encoder switch has 30 detent positions, each one spaced 12degrees apart. The three (encoder) pins are A (left), Common, B (right).Pins A and B should be pulled up to Vcc; 10K resistors are recommended tokeep current under 1mA. Here, the input pins have internal pull-ups ofabout 40K.Both A and B are stable at detent position, and both A and B will havethe same output. When the encoder is rotated clockwise, B will changefirst, then A. For counterclockwise, A will change first, then B.Instead of using Interrupt On Change, poll at a rate often enough tocatch the fastest transitions. One full revolution in half a secondmight be a good starting point, which gives 60 pin changes per second,or one every 16 milliseconds. Oversampling by 4 would give one pollevery 4 milliseconds, which is probably already a timer interrupt.0. Read pins A and B, save starting status.Polling procedure:1. Are pins A and B the same?NO: Which changed from saved status? Set direction flag. Exit.YES: Continue to 2.2. Are A and B equal to last saved status?YES: Encoder at same detent position. Ignore. Exit.NO: Save new status. Eval direction flag and indicate rotation.You can see the source code here (it's pretty short):https://github.com/unfrozen/stm8_libs/blob/master/lib_rotary.c

author: wizard69
author_rank: Super Contributor
timestamp: April 25, 2021, 09:24:23 am
content: I don't have any use for the code right now but I must say I like you commenting style and easy to read code.Quote from: rhodges on April 24, 2021, 09:58:43 pm

author: Nusa
author_rank: Super Contributor
timestamp: April 25, 2021, 12:53:06 pm
content: Quote from: Jan Audio on April 24, 2021, 03:59:24 pmI followed your link. What passes for a datasheet is in the 2nd image (I could read it in full screen or zoom modes). With a blowup of the mechanics in the 3rd image.You can always rig up a test circuit with led's for both outputs. If you turn the shaft very slowly, you should be able to see the expected waveform. If you turn it faster, you should be able to see them both blinking.

author: rhodges
author_rank: Frequent Contributor
timestamp: April 25, 2021, 02:33:45 pm
content: I can send you some ALPS encoders. Send me PM if interested.

author: Jan Audio
author_rank: Frequent Contributor
timestamp: April 25, 2021, 03:09:34 pm
content: LOL, thanks.I will find out, i go solder one of these green/blue ones to a working project.The total blue ones i always had are good.

author: Jan Audio
author_rank: Frequent Contributor
timestamp: April 25, 2021, 03:14:48 pm
content: Quote from: Domagoj T on April 24, 2021, 04:23:54 pmhttps://www.eevblog.com/forum/microcontrollers/how-to-properly-code-rotary-encoder/msg2875434/#msg2875434

author: tszaboo
author_rank: Super Contributor
timestamp: April 25, 2021, 03:17:24 pm
content: Quote from: Jan Audio on April 24, 2021, 03:32:16 pmIf you use a library, you have to call the rotary encoder handler quite often.Really often.If you have a display or handle anything slightly demanding, like writing to serial port, then you might already fail to call the handler often enough. Then it skips or it wont work properly. Also, pull up resistors.

author: Jan Audio
author_rank: Frequent Contributor
timestamp: April 25, 2021, 03:23:28 pm
content: Maybe i should try interrupts then.Yes i have a display, my other project also works.Need to order those blue ones i think.btw : the display dont halt the code, it polls the timer, even for each enable state.

author: MarkF
author_rank: Super Contributor
timestamp: April 25, 2021, 03:28:25 pm
content: Quote from: Jan Audio on April 25, 2021, 03:23:28 pmInterrupts are for doing a fixed sample rate.(i.e. NOT Interrupt-On-Change of the pins as I've seen so many do.)Interrupt-On-Change can overload your processor.I've found that you pretty much need to sample at 500Hz or faster to avoid missing pulses.

author: Jan Audio
author_rank: Frequent Contributor
timestamp: April 25, 2021, 03:34:02 pm
content: I have to measure a pin with my multimeter to see if i am around that 500KHz.

author: madires
author_rank: Super Contributor
timestamp: April 25, 2021, 04:48:46 pm
content: Quote from: MarkF on April 25, 2021, 03:28:25 pmAnd I'd recommend to design your algorithm to be able to cope with bad Gray code steps because the cheap encoders don't create nice and clean pulses as good encoders do. There can be a huge difference in timing and bouncing behavior.

author: xavier60
author_rank: Super Contributor
timestamp: April 26, 2021, 01:08:56 am
content: The transitions can be very close together so I poll at 1mS.On each change, I Left shift the A and B bits into a 6 bit register then test for 0x12 or 0x21 for up/down.I use RC filtering and apply a drop of PAO oil to the encoder to stop it from becoming scratchy with time.It also helps for the input ports to be anything but TTL level.Putting capacitors directly across the encoder contacts without series resistors can cause discharge spikes resulting in miscounts.

author: Jan Audio
author_rank: Frequent Contributor
timestamp: April 26, 2021, 01:22:32 pm
content: I sample at 35KHz.Cant have 500KHz in a 8-bit PIC.

author: tszaboo
author_rank: Super Contributor
timestamp: April 26, 2021, 01:48:02 pm
content: Quote from: Jan Audio on April 26, 2021, 01:22:32 pmIt was 500 Hz, not KHZ.35KHz should be enough to catch all pulses IMHO. I mean with a really good confidence, with not superhuman scrolling speeds.You get usually surprises, when using Arduino with some badly designed blocking libraries.

author: Jan Audio
author_rank: Frequent Contributor
timestamp: April 26, 2021, 01:58:21 pm
content: Ah ok thanks, i readed it to fast.All my code is custom without blocking, fastest non-asm code with free mode in the west.

author: szszoke
author_rank: Regular Contributor
timestamp: April 26, 2021, 03:37:18 pm
content: I am using rotary encoders for my project that always give pulses when it is being turned.I debounce the signal with a couple of 10k resistors and 100nF capacitors.Both pins (let's call them A and B) pulse when the encoder is moved by one detent.Depending on the direction, either A or B will be pulsed first.Because of this behavior, I can essentially use one pin as a "clock" which would trigger a pin change interrupt, and inside that interrupt I can read the state of the other pin. The state of that other pin will give me the direction.I tried the polling method as well but it kept not working and kept missing pulses when I turned my encoder faster.I am not missing steps this way, and the hardware debouncing ensures that I have clean pulses.If nothing else works, maybe you can give this a try.

author: tooki
author_rank: Super Contributor
timestamp: April 26, 2021, 06:53:38 pm
content: Quote from: HB9EVI on April 24, 2021, 03:49:53 pmQuote from: madires on April 24, 2021, 04:44:06 pmIncremental encoders like EC11 are not Gray code, but quadrature. Actual Gray code is used in absolute encoders.

author: jesuscf
author_rank: Frequent Contributor
timestamp: April 26, 2021, 09:16:14 pm
content: Quote from: tooki on April 26, 2021, 06:53:38 pmQuadrature encoders produce 2-bit Gray code:00 -> 01 -> 11 -> 10 -> 00 etc.

author: xavier60
author_rank: Super Contributor
timestamp: April 27, 2021, 12:04:08 am
content: All cheap mechanical rotary encoders that I have purchased on ebay have been the type where A and B momentarily turn on between dents, always off at each dent.This sort of thing,https://www.ebay.com.au/itm/311947024355?hash=item48a17dd7e3:g:wbAAAOSw7ZlZoZDq

author: xavier60
author_rank: Super Contributor
timestamp: April 27, 2021, 01:44:32 am
content: After a lot of searching, this is the only mention I have found about the 2 types,https://forum.arduino.cc/t/strange-rotary-encoder-problem/483892/4

author: madires
author_rank: Super Contributor
timestamp: April 27, 2021, 11:39:05 am
content: Quote from: xavier60 on April 27, 2021, 12:04:08 amThose create four Gray code steps per detent (see the diagram posted by jesuscf). The number of steps is important for encoders with detents because you want to align the change of some value with the detents for proper user feedback. For encoders without detents, which turn freely, it's not that important.

author: xavier60
author_rank: Super Contributor
timestamp: April 27, 2021, 12:00:56 pm
content: Many timing diagrams don't show the dent positions, causing ambiguity.This example shows the 2 dent positions,https://tech.alpsalpine.com/prod/e/html/encoder/incremental/ec11/ec11_common.html

author: Jan Audio
author_rank: Frequent Contributor
timestamp: September 13, 2021, 01:41:44 pm
content: Ok they do work.I was tired of this problematic hobby and quitted a while.They are ok i still need to find some problem, no impulses on 1 line, cable is good.

author: rstofer
author_rank: Super Contributor
timestamp: September 13, 2021, 03:30:12 pm
content: Quote from: MarkF on April 25, 2021, 03:28:25 pmInterrupt on change is very fast when implemented as a state machine and table driven. It also takes care of bouncing (generally) by looking only for expected edges for the interrupt on change.Here is some very credible code that is nearly identical to what I wrote years ago.https://github.com/buxtronix/arduino/blob/master/libraries/Rotary/Rotary.cppSampling burns a lot of CPU cycles for nothing when the encoder isn't being used and the sample rate needs to be quite high.With interrupt on change, no cycles are used unless the encoder is actually changing positions. It's pretty clear that if we just did an interrupt on change for the falling edge of a signal, we need to change the interrupt direction to interrupt on rising edge.Google for 'rotary encoder interrupt driven state table'

author: rstofer
author_rank: Super Contributor
timestamp: September 13, 2021, 03:34:12 pm
content: I found my code written for some variant of the ATmega128. This was written for wheel encoders on a small robot so the signals are changing pretty fast. It also counts +4 for a complete transition. It counts each transition as + or - 1 depending on direction. This will need to be rethought for panel encoders but the idea is right. The interrupt handlers are pretty short!Code:#include <avr/io.h>#include <avr/interrupt.h>#include <stdio.h>#include <ctype.h>#include "defines.h"volatile int8_t RightWheelState = 0;volatile int32_t RightWheelCount = 0;volatile int8_t LeftWheelState = 0;volatile int32_t LeftWheelCount = 0;volatile uint8_t urx;volatile uint8_t urx_recv;/*Quadrature Encoder Scheme**There are two pairs of quadrature signals, one for each wheel.* The right wheel is on EXINT4 (PE4) and EXINT5 (PE5)* The left wheel is on EXINT6 (PE6) and EXINT7 (PE7)* Each encoder has two outputs A & B. For convenience, A is considered the MSB*while B is considered the LSB.*/const int32_t EncoderB[] = {+1,-1,-1,+1};const int32_t EncoderA[] = {-1,+1,+1,-1};SIGNAL(SIG_INTERRUPT4)// encoder B for right wheel{EICRB ^= 0x01;// toggle the edge bitRightWheelState ^= 0x01;RightWheelCount += EncoderB;}SIGNAL(SIG_INTERRUPT5)// encoder A for right wheel{EICRB ^= 0x04;RightWheelState ^= 0x02;RightWheelCount += EncoderA;}SIGNAL(SIG_INTERRUPT6)// encoder B for left wheel{EICRB ^= 0x10;LeftWheelState ^= 0x01;LeftWheelCount += EncoderB;}SIGNAL(SIG_INTERRUPT7)// encoder A for left wheel{EICRB ^= 0x40;LeftWheelState ^= 0x02;LeftWheelCount += EncoderA;}void IOInit(void){UBRR0H = 0;UBRR0L = BAUD;UCSR0B = _BV(RXEN) | _BV(TXEN) | _BV(RXCIE);}static int uart_putchar(char c, FILE * stream){if (c == '\n')uart_putchar('\r', stream);while (! (UCSR0A & _BV(UDRE)));UDR0 = c;return c;}SIGNAL(SIG_UART0_RECV){uint8_t s;s = UCSR0A;urx = UDR0;if (bit_is_clear(s, FE))urx_recv = 1;}void QuadInit(void){LeftWheelState = (PORTE & 0xC0) >> 6;RightWheelState = (PORTE & 0x30) >> 4;EICRB = 0xff; // enable rising edge triggering on INT4..7EICRB ^= (LeftWheelState & 0x02) << 5;// if a signal is already setEICRB ^= (LeftWheelState & 0x01) << 4;// set the interrupt to falling edgeEICRB ^= (RightWheelState & 0x02) << 1;EICRB ^= (RightWheelState & 0x01);EIMSK = 0xf0; // enable these 4 interrupts}int main(void){IOInit();QuadInit();sei();fdevopen(uart_putchar, NULL);printf("Hello World\n");while (1) {printf("LWS =%2d LWC =%6ld RWS =%2d RWC =%6ld\n",LeftWheelState, LeftWheelCount, RightWheelState, RightWheelCount);}}

author: Old Printer
author_rank: Frequent Contributor
timestamp: September 13, 2021, 04:26:19 pm
content: Quote from: Jan Audio on April 24, 2021, 03:42:59 pmI assume you are kidding??