A motor encoder counts turns on the shaft of a motor. If the motor is attached to a wheel, it will tell how many turns the wheel does. Actually it's better than that because, with the motors we use, there are about 4000 counts for one turn of the wheel. So in theory, you can measure the rotation of the wheel down to a tiny fraction of a degree.
The encoder has two sensors which pick up the north and south poles of the spinning disc magnet. One of the sensors is arrowed in the photo. The other one is behind the magnet.
There is a library that provides some simple functions to use the encoder
You can find it by going File - Include Library - Manage Libraries. After a while a box comes up with a huge list of libraries in it. Type 'Encoder' in the search box and you will find Encoder by Paul Stoffregen. Click on install.
You can identify the encoder's pins in the photo on the right, above. M1 and M2 are the pins that control the motor; A and B are the output pins of the encoder. They must connect to pins 2 and 3 on the Arduino. (Pins 2 and 3 allow interrupts which let the counter work while the Arduino is doing other things.) Vcc is 5V and Gnd is ground.
Once you have the encoder hooked up to the Arduino correctly, look in File - Examples - Encoder and find a file called Basic.
Put this file into the Arduino and then start the Serial Monitor making sure the Arduino baud rate and the Serial Monitor baud rate are both 9600.
Then if you spin the magnetic disc with your fingers you should see the pulses being counted as the motor turns.
NB In the photo you can see the labels for all the wires that connect to the encoder. In the encoders we have, those labels are on the other side of the encoder, which makes it impossible to read them when the encoder is soldered to the motor. (Don't turn it over, the sensors need to be close to the magnetic disc.)
Here is some code that shows you how to use the encoder with a motor:
If you want to try this code,