Wiring Forum
Wiring Hardware >> Tangible computing >> Encoder Library issue

Message started by roypardi on 08/10/07 at 16:10:50

Title: Encoder Library issue
Post by roypardi on 08/10/07 at 16:10:50
I'm trying to use a Maxon Tacho encoder with the Wiring Encoder Library. It's this one here:

The spec says 100 counts per turn. I think I have it wired up correctly and I am able to see values printed to the serial monitor.

There are two problems though - I am getting way more than 100 counts per turn and the accuracy of the encoder seems way off: I can turn it by hand back and forth between two positions and the reported positions seem to drift considerably.

The barebones code I am using to test this is below. I have the encoder powered + grounded off the 5v taps on the Wiring board, am using pins 2/3, and have pull-up resistors (3.3k) on the channel pins. So.... is there anything I am missing?



Encoder myEncoder;
long val;
long lastVal;

void setup(){

 myEncoder.attach(2, 3); // attach an ecoder to pins 2 and 3.
 lastVal = 0;


int sendPosition(){
 val = myEncoder.read();
 if (val != lastVal){
   lastVal = val;

void loop()

Title: Re: Encoder Library issue
Post by barragan on 08/13/07 at 02:15:28
hi Roy, please let me know which version of wiring are you using.

Title: Re: Encoder Library issue
Post by roypardi on 08/13/07 at 16:41:21
hi~ I'm using v12.

I've boiled down my issues some:
--turn count: getting a reported 600 counts (using Encoder lib) per full 360 degree turn of the motor shaft when the Maxon Tacho encoder is rated at 100 counts per turn. <--am I not understanding what this spec means?

--spurious values: I have a test rig moving a load back and forth between 0 and 2000. It works except that I get an anomalous value every n cycles (not predictable) where the count will be displayed as a very high number (655369, etc.). I can filter this in code but is it indicative of some other issue. I am using Serial.print() with this - could there be some issue there?

--my rig will run fine for 50 or so cycles and then it starts to slow down/stall. I assume this is some (really bad) issue with my hardware (H bridge/motor) set up but I thought I would mention it.

My other issues seem related to my code. In a loop I grab the encoder value at the start of the loop and then use that throughout the work done in the loop. I haven't used interrupts before so I think maybe that I haven't understood that the true value of the encoder can get out of scope from my initial read.

void loop()
 long v  = myEncoder.read();

// do some work

// ***** v may be out of scope of myEncoder state

//do some more work


Finally - how many interrupt pins are there? This page says 4:

This page says 8:

Oh... and since I am asking a million questions here... ;-)
I want to ramp the PWM up smoothly with relative to the number of steps my motor has to move. Thinking of doing something like speed = speed + .2 (or some forumla that determes the ramp rate based on the number of steps needed).

--can I use analogWrite with floating points or should I round first

--does the port rollover if I exceed 255?

--can I add/multiply an int and a float?

Thanks! -- Roy

Title: Re: Encoder Library issue
Post by Alan_Kilian on 04/03/08 at 12:25:17
So, where did you get with this?

I'm trying to use two encoders wired to different pins, but I get the same
reading from encoder1.read() and encoder2.read().

They both move with the encoder, but I should be getting two different readings.

I haven't run the code long enough to see if I'm getting jumpy readings
or drift yet.

I'm running Wiring 0015 Alpha.

Title: Re: Encoder Library issue
Post by barragan on 04/03/08 at 15:30:47
hi Alan, can you post some code with the problem?

Title: Re: Encoder Library issue
Post by Alan_Kilian on 04/03/08 at 18:39:38
Sure, I'll post later today.

How do I get "svn co" to work?
It seems that the server is down or restricted. (Or I'm using the wrong one.)

Title: Re: Encoder Library issue
Post by barragan on 04/03/08 at 18:42:45
It should be like this: svn co svn://wiring.uniandes.edu.co/trunk/wiring
please try and let me know, Iīll try it myself later today.

Title: Re: Encoder Library issue
Post by Alan_Kilian on 04/03/08 at 21:26:47
$ svn co svn://wiring.uniandes.edu.co/trunk/wiring
svn: Can't connect to host 'wiring.uniandes.edu.co': Connection timed out

kilian@kilian-xp-lap ~/Hobbies/Wiring
$ ping wiring.uniandes.edu.co
PING wiring.uniandes.edu.co ( 56 data bytes

----wiring.uniandes.edu.co PING Statistics----
2311 packets transmitted, 0 packets received, 100.0% packet loss

Title: Re: Encoder Library issue
Post by barragan on 04/04/08 at 00:53:00
yes, seems it is the schoolīs firewall, I sent a mail to the system administrator to fix this.
Thanks Alan.

Title: Re: Encoder Library issue
Post by Alan_Kilian on 04/04/08 at 01:08:10
I'm SOOO not a C++ guy.

How in the heck does service() know which _position to increment?

Maybe encoders[_index].position++;
or encoders[this].position++;

Maybe it should look at which pin caused the interrupt, and look
in some global array setup by attach() to get the location to
increment? (or decrement)

Title: Re: Encoder Library issue
Post by barragan on 04/30/08 at 05:53:02
I donīt know whatīs the problem with the address, thye said they will fix it soon, in the mean time here is the address you can get the code from svn co svn://aeon.uniandes.edu.co/trunk/wiring

I have tested it and seems to work out of the school server. This will migrate soon to another server. Let me know if it works for you. I have updated the build page in http://wiring.org.co/source as well to point to this address to get the source.

Wiring Forum » Powered by YaBB 2.5 AE!
YaBB Forum Software © 2000-2010. All Rights Reserved.