The KY-040 rotary encoder is a 360-degree rotary input device (i.e. a control knob) that provides an indication of how much the knob has been rotated AND which direction it is rotating in.
It is a great device for stepper and servo motor control. You can also use it to control devices like digital potentiometers.
The KY-040 rotary encoder has a fixed number of 20 positions per revolution. These positions are felt as small “clicks” when you turn the encoder. Inside the encoder, there are two switches. One switch connects pin A to pin C and the other switch connects pin B to C. In each encoder position, both switches are either opened or closed. Each click causes these switches to change states. If both switches are closed, turning the encoder either clockwise or counterclockwise one position will cause both switches to open. If both switches are open, turning the encoder either clockwise or counterclockwise one position will cause both switches to close.
Determining which switch changed states first is how the direction of rotation is determined. If A changed states first, the switch is rotating in a clockwise direction. If B changed states first, the switch is rotating in a counter clockwise direction.
A push button switch is built into this encoder. If you push on the shaft, the normally open switch "SW" will close. This can be used as an On/Off switch, or to change control functions, ranges, etc.