Arduino Rotary Encoder Circuit Tutorial with Interrupts

preview_player
Показать описание
A better way for Arduino to read a rotary encoder including programming tips. This uses interrupts.
Рекомендации по теме
Комментарии
Автор

I've seen several different ways to connect a rotary encoder to an arduino on youtube.
I have tested many of them, but none have worked satisfactorily.
When I saw your solution, I immediately understood that this would work.
I bought the circuits and tested.
It works great, no missed pulses and no double pulses.
Thank you for sharing this elegant solution.

lrtum
Автор

Thank you for the excellent info. I am in the process of designing a DDS frequency source and wanted a way to make it independent of a PC. My solution was to use a cheap rotary encoder breakout board available from Amazon. Got 5 of them for 8.99. I had interfaced them as is without using interrupts by implementing a software de-bounce routine and was able to get them to work across the USB port. Your idea is more elegant using a hardware solution to interfacing the board. However, I did not have the exact parts on hand. I used a 74HCT74 and an 74LS14 instead. The only caveat was that the 74HCT74 device has a different pin out and logic levels for the preset and clear pins. They need to be pulled up to Vcc. Anyway, I put the circuit together with the changes and tested it with a couple of LEDs one for step and one for direction and also viewed the step output on my scope. It worked as advertised. What I would recommend is tying the unused inputs of the 7414 to ground to minimize chattering and avoid excess current draw. A low output draws 10 ma. The same goes for the dflop. I would tie the RS pin to Vcc to avoid the unused output chattering.

johnbrandolini
Автор

I used this to control my airbrush spray booth extratctor. The fan has a build in PWM controller, it just needs a pwm signal. Now I can control the fan using a rotary encoder and this circuit.
I did solder 0.15uF ceramic capacitors between Enc1 and GND and Enc2 and Gnd. There is no need for software debouncing tricks. Thanks for this circuit.

troncquoS
Автор

Should there be a resistor in series with the capacitor to the encoder pins to prevent the capacitor being shorted direct to ground?

brantwinter
Автор

Wonderful idea!
I decided to try and do it myself but with using just Arduino Micro as power source and two LED lights for output signals. I followed the circuit scheme (Figure 2 on the web page) and used my Arduino as the 5v and ground source. On "STEP" and "DIR" I connected LED lights first time to ground and second time to 5v.
When I checked it on the first time (LED connected to ground), it output nothing. When I checked it on the second time (LED connected to 5v), it outputs blank LED on "DIR" and flashing LED on "STEP" on every encoder "click" no matter the turn direction.
What am I doing wrong here?
What am I missing?
How can I fix this to receive the same "blink" outputs as you receive in the video?
Just to be more clear, I use my Arduino only for power and ground source and didn't uploaded (or used) any code into it and prefer not to use it as other than that.
Thanks.

ranpetel
Автор

If you are using a manually operated RE, Unless your sketch is very long and/or filled with "delay" or "while" statements, an Arduino is quite fast enough to poll the A and B inputs without the need for ISRs.

boldford
Автор

Why do you use a static variable instead of a volatile variable? I've read that sometimes it can be inaccurate if you use static instead of volatile.

alexsneedmiller
Автор

does using interrupts for the rotary encoder raise possible delay issues?

ClarkJohnsonGuys
Автор

One question please..If I want to make an encoder of 3 bits of resolution, how do I do it?

sergioisaacpuentesto
Автор

Might want to change your interrupt routine to:
counter -= digitalRead(DIR)*2-1 or
counter -=(digitalRead(DIR)<<1)--
not sure which would be faster but both are faster than an if else statement
PS: Luv your posts, keep it up. ;-)

wizardmerlin
Автор

Hello, I put a tread mill motor 2.65hp dc on a drill press, could the motor be controlled with this setup and read the rpm,

kocnn
Автор

I don’t really see why the 4013 is here. Wouldn’t it work just with the smitt triggers? You could then execute the interrupt either on a falling or rising edge wouldn’t matter.

davidbernfeld
Автор

Lewis, Thank you very much for this circuit. It answers one of two questions I have been working on, Direction? The other is quadrature output. I use a 2000 PPR Quadrature optical encoder and I want to feed my servo with the full 8000 PPR (step) from the encoder for higher resolution. You already took care of direction. Is there a circuit of all discrete components that can count transitions in A and B and output 2X and 4X pulses?

backlash
Автор

the bournes rotary encoder is so much more - limited by bournes themselves - lack of data - that of the increaseing binary , octal or hex to relate to the grey code that they constructed it with - perfect for the encoder to operate - pain in thearse if you want use it

notaclue
Автор

You're only monitoring your clock pulse with one leg of your encoder. These are quadrature encoders which four steps per cycle not two.

mosfet
Автор

very cool these days.. you can just find this kind of info on the internet.. Although it's good to have, you really only need debounce on the line you trig on, if reading A and B directly instad of via logic, because the line not trigged on will have stopped bouncing a long time ago.. .. So for example interrupt on A, debounce it somehow(disable int?). Then if B is high you're going one way, if low you're going the other way, so the logic circiuits are somewhat superfluous unless you're actually controlling some piece of hardware that need thoose signals, like a digital pot.. I'm more of a verilog/fpga dude theese days but I used to do stuff on avr a long time ago, when mega8 was popular.. :-D Cheers.

niklaswallin
welcome to shbcf.ru