encoder

Hello,.
Hopefully, I want to use encoder with arduino.
I've learned that incremental type encoder puts out pulse signals and ansolute type encoder puts out binary signals.
I want to use either type of encoders and use arduino to receive those signal and do something motional(controlling on/off of motors or solenods) when the encoder is reached to a certain position or signal.
what kind of programing language can I utilize in order to control motor(or solenoid) using encoder signal?
frankly, it's difficult to follow this tutorial Arduino Playground - RotaryEncoders
please advise me gentlemen, then it will be highly appreciated

What rotary encoder are you trying to develop for exactly?

Its certainly possible to use them with the Arduino but specifics will depend on the actual encoder.

And it will depend on your actual application. Do you have a specific problem you are trying to solve or are you just interested in gaining practical experience with encoders?

Mingki, I am not clear on what you want to do and it is easier to advise about implementation with a clearer picture of the physical movement you want to detect.

I think you want to light an LED when you detect when something moves a certain distance (or perhaps angle). How is the object you want to detect moving, is it rotating?

thank you, mem.
i am very sorry for my lack of explanation on my question. i haven't bought any encoder yet to test it for myself. I will get my own encoder soon.
yes, when i rotate encoder, i want arduino to read pulse from encoder and i want to make one of arduino's digital pin to be in a HIGH state when arduino receive 10th pulse from encoder.
Ultimately, i want arduino to read encoder's pulse all the way to the tenth pulse(where digital pin goes in a high state) while bypassing 1th, 2nd, 3rd, 4th, 5th, 6th, 7th, 8th, 9th pulses.

Just so you know, some encoders have over 500 pulses per revolution.
Turning on a LED after every 10 will make it look always on. :wink:

Is there a URL for the encoder your looking at?

Hi, I am trying to figure out the purpose of below programing.
does anyone know what this commend for? Especially the commends in bold letters.
void loop() {
n = digitalRead(encoder0PinA);
if ((encoder0PinALast == LOW) && (n == HIGH)) {
** if (digitalRead(encoder0PinB) == LOW) {** encoder0Pos--;
} else {
encoder0Pos++;
}
Serial.print (encoder0Pos);
Serial.print ("/");
}
encoder0PinALast = n; }

Hi, I am trying to figure out the purpose of below programing.
does anyone know what this commend for? Especially the commends in bold letters.
void loop() {
n = digitalRead(encoder0PinA);
if ((encoder0PinALast == LOW) && (n == HIGH)) {
** if (digitalRead(encoder0PinB) == LOW) {** encoder0Pos--;
} else {
encoder0Pos++;
}
Serial.print (encoder0Pos);
Serial.print ("/");
}
encoder0PinALast = n; }

// encoder0Pos holds the position for encoder0
// encoder0PinA is the pin that encoder0 is connected to
// encoder0PinALast is the last processed state of encoder0

void loop() {
n = digitalRead(encoder0PinA); // n is current state of encoder0PinA pin
if ((encoder0PinALast == LOW) && (n == HIGH)) { // check if encoder0PinA pin has changed state
if (digitalRead(encoder0PinB) == LOW) {
encoder0Pos--; // if it has changed and its now low decrement encoder0Pos;
} else {
encoder0Pos++; // if it has changed and its now high, increment encoder0Pos
}
Serial.print (encoder0Pos);
Serial.print ("/");
}
encoder0PinALast = n; } // set the variable holding the previous state to the value n read above

thank you very much for your answer.
i still having some trouble understanding some of the program. sorry.
Please help me to understand some points.

}
Serial.print (encoder0Pos);
Serial.print ("/");
}
encoder0PinALast = n; } // set the variable holding the previous state to the value n read above

Do i need to use Serial.print commend in order to take puls signals(using arduino) from encoder in real time?
could you tell me what ("/") does for?
i hope i could get some more explination on this quote.

encoder0PinALast = n; } // set the variable holding the previous state to the value n read above

I assume the author of that code was using the serial port to display the status of the encoder. You don't need to send this data to the serial monitor but it is useful to see what is happening.

Anyway, the variable encoder0PinALast is used to remember if the previous state of the encoder sensor pin was high or low. The code uses this to determine the direction the encoder is rotating.

The variable encoder0Pos contains a count of the encoder position and you would use this value in your application. By the way, what is your application? It would be easier to guide you knowing a little more about what you are trying to achieve.

If you don't have a specific application in mind and just want to learn about encoders, then have a search on google for some tutorials on the use of phototransistors. Also have a search on any tutorials on the innards of mechanical mice, they are a good source of cheap (free?) encoders to play with if you have an old one lying around.

You don't say what programming experience you have but assuming you are just beginning to learn, I suggest you start with a simple sketch that detects the presence or absence of light on a phototransistor. You should be able to find a number of tutorials on doing this with the arduino by searching google.

Then enhance that sketch with functionality to count the number of times the sensor has changed state (i.e. has changed from light to dark) and send the count to the serial monitor.

Or are you already comfortable with the skills necessary to write and debug this kind of functionality?

Ladyada's tutorials are highly regarded so thats a good place to start:
http://www.ladyada.net/learn/arduino/

there isn't one on encoders yet but it will help you to get comfortable with the basics.

thanks for the advise.
I've choosen an encoder which is a type of line driver.
http://www.flickr.com/photos/23525729@N06/2243426483/
http://www.flickr.com/photos/23525729@N06/2244219512/
i followed this Arduino Playground - RotaryEncoders tutorial to receive pulse signals from encoder when it's rotating in cw or in ccw.
http://www.flickr.com/photos/23525729@N06/?saved=1
my question is, how can i send real time pulse signal from encoder to arduino and monitor them as ASCII?

If you have run that tutorial then you should be seeing the encoder pulse counter value as ascii on the serial monitor seperated by the '/' character.

Are you able to see that?

If you see nothing on the serial monitor, check that your baud rate in the serial monitor is set for 9600.

If that doesn't solve the problem then you may want to get the communication example sketch asciitable running to make sure you have the serial monitor configured correctly.

yes, i see serials on serial monitor.
i see below numbers.

start 1 2 3 4 5 6 7........65535 65534 65535 65534 65535 65534 65534 65534

too many numbers are appeared. but those numbers aren't coming from the rotational movement of encoder. they just gradually appeared and increased in numbers once the encoder, arduino's connection is made.
is my hardware connection correct?
i made a connection like this.
encoder arduino digital pin
A phase(red) #2
B phase(green) #4
Z phase(yellow) #0
the connection table is shown here http://www.flickr.com/photos/23525729@N06/2244219512/sizes/l/

The tutorial sketch in your link uses pins 3 & 4 for phases A and B but you are connecting to 2 and 4,

I think the Z phase is not needed in that code, and anyway it should not be connected to digital pin0, that is used for the serial port.

Try connecting the A sig to pin 3 and the B sig to pin 4. And disconnect the Z sig.

fantastic!!!. it's working...! thank you very much for your advise.
it's fantastic! now, i can get the point what interrupts does for.
this is the numbers i get from the serial.
3
2
1
0
65535
65534
65533
65532
when it is rotated in cw, it starts from 0 to 3 etc. when i rotate in ccw(opposite), it starts from starts from 65535 and decreases in values as you can see.
is this mean that if the encoder continues(in cw) to rotate until it reachs to 65535, the next number after 65535 is 0 again and continue increase 1, 2, 3, 4?
also, is it ok if those incoming numbers from encoder keep on piling up in serial? i'm worried about overloading the seiral.

also, is it ok if those incoming numbers from encoder keep on piling up in serial? i'm worried about overloading the seiral.

Dont worry. The serial can think faster than you can. ;D
It can go right up to over a tenth of a megabyte per second.

Worst case scenario is that some lines get dropped.
I've managed to do it only with a really stupid program and that was a computer sending tons of data to the Arduino. :slight_smile:

thanks. i hope i could get some answer on below question.

this is the numbers i get from the serial.
3
2
1
0
65535
65534
65533
65532
when it is rotated in cw, it starts from 0 to 3 etc. when i rotate in ccw(opposite), it starts from starts from 65535 and decreases in values as you can see.
is this mean that if the encoder continues(in cw) to rotate until it reachs to 65535, the next number after 65535 is 0 again and continue increase 1, 2, 3, 4?

i just finish testing the numbers for myself whether the numbers are appearing in the order of 0, 1, 2, 3, 4, 5... after 65535 as the encoder is rotating.
i keep on rotating my encoder by using a motor and it turned out that the maximum number is approximately 48700, not 65535.
what is interesting for me is that the numbers are decreasing once it reaches to the maximum number even if i am rotating the encoder in the same direction.
Also, this time, the numbers are irregular.
48001 48000 48001 48000 47999 48000 47999 47998 47999 47998 47999............47992 47993 47992 47993 47992 47991 47992 47991 47992 47993 47992
could anyone please explain what's the reason?