Home
JAQForum Ver 24.01
Log In or Join  
Active Topics
Local Time 06:56 02 Aug 2025 Privacy Policy
Jump to

Notice. New forum software under development. It's going to miss a few functions and look a bit ugly for a while, but I'm working on it full time now as the old forum was too unstable. Couple days, all good. If you notice any issues, please contact me.

Forum Index : Microcontroller and PC projects : MKII using encoders

Author Message
viscomjim
Guru

Joined: 08/01/2014
Location: United States
Posts: 925
Posted: 02:59pm 28 Oct 2014
Copy link to clipboard 
Print this post

Hello all. I have not yet received my 170s yet from mouser, so I can't try this yet. Can anyone verify if using the new pull up pin configuration combined with a 100nf cap as shown in the manual will work well with an encoder as opposed to using the 10k pullups shown in the manual also.

Thank you!!!
 
atmega8

Guru

Joined: 19/11/2013
Location: Germany
Posts: 724
Posted: 04:40am 29 Oct 2014
Copy link to clipboard 
Print this post

Yes, i can ;-), why not?

There is no difference between an internal or external resistor.
Both follow Mr. OHM's Law.

It's working.
 
TassyJim

Guru

Joined: 07/08/2011
Location: Australia
Posts: 6283
Posted: 09:26pm 01 Nov 2014
Copy link to clipboard 
Print this post

I have tried 0.1uF with the internal pullups to give a rise time of ~3mS
0.01uF gave a rise time of ~300uS

I am using 0.1uF for push buttons and 0.01uF on a rotary encoder.
The rotary encoder is an ultra-cheap unit and with the capacitor, clockwise is very good and anticlockwise gives the occasional extra kick. In my application, that is not a problem.

Jim

VK7JH
MMedit
 
viscomjim
Guru

Joined: 08/01/2014
Location: United States
Posts: 925
Posted: 02:03pm 02 Nov 2014
Copy link to clipboard 
Print this post

Thanks Jim, I will give the internals a try with the switch and encoder.
 
matherp
Guru

Joined: 11/12/2012
Location: United Kingdom
Posts: 10315
Posted: 09:23am 03 Nov 2014
Copy link to clipboard 
Print this post

I've found the secret to the cheap rotary encoders is to look at all the edges and then use software to decide what is valid. The only state which guarantees that a real transition has occurred is when both inputs are active. The software then needs to establish which went active first to determine direction.

The attached code is a port of something I did on picaxe. It isn't perfect enough for absolute positioning (can edges be lost during things like "print" statements?), but is pretty good and even with the very cheap mechanical encoders does not require any hardware filtering.

Hope it may be of some use.

Peter


option default integer
const false=0
const true=1
const in0=3 'encoder pins pulled high so should be reading high in detent positions, centre pin connected to ground
const in1=2 'swap the pin definitions as required to get the correct sense of rotation
'
init:
setpin in0,intB,rint 'enable interrupts on both edges of both pins
setpin in1,intB,rint
rotatedright=false
rotatedleft=false
resetenc=true
value=0
lastvalue=0
print value
'
MAIN:
Do
if value <> lastvalue then
print value
lastvalue=value
endif
Loop
end
'
rint:
pin0=NOT pin(in0) 'reverse sense to get positive logic
pin1=NOT pin(in1)
if pin0 then
if pin1 and not resetenc then 'in0 and in1 both active
if rotatedleft and not rotatedright then
value=value-1
rotatedleft=false
endif
if rotatedright and not rotatedleft then
value=value+1
rotatedright=false
endif
else 'only in0 active
if resetenc and not rotatedleft then
rotatedright=true
resetenc=false
endif
endif
else
if pin1 then 'only in1 active
if resetenc and not rotatedright then
rotatedleft=true
resetenc=false
endif
else 'both off so reset
rotatedleft=false
rotatedright=false
resetenc=true
endif
endif
if pin(in0)=pin0 or pin(in1)=pin1 then goto rint 're-enter if another change has happened almost immediately
ireturn
Edited by matherp 2014-11-04
 
viscomjim
Guru

Joined: 08/01/2014
Location: United States
Posts: 925
Posted: 03:09pm 03 Nov 2014
Copy link to clipboard 
Print this post

Hi Matherp, welcome to the forum and thanks for the code. I will give this a whirl once I get a board back together with encoder on it. Looks interesting...
 
viscomjim
Guru

Joined: 08/01/2014
Location: United States
Posts: 925
Posted: 04:02pm 19 Nov 2014
Copy link to clipboard 
Print this post

Hi Matherp, this code works very very well with the el cheapo encoders. Thanks for the contribution!!!
 
drkl

Senior Member

Joined: 18/10/2015
Location: Hungary
Posts: 102
Posted: 11:53pm 20 Dec 2015
Copy link to clipboard 
Print this post

Dear Mathers,
You do not have a rotary encoder management software cfunction code? It works perfectly, congratulations.
Have two functions, one would return the value: Rencval(), and the other only in the direction +1: clockwise, -1: anti clockwise 0: no movement.

drkl
 
Print this page


To reply to this topic, you need to log in.

The Back Shed's forum code is written, and hosted, in Australia.
© JAQ Software 2025