Another illustration that demonstrate using rotary encoder. Rotary encoder is an electro mechanical device that convert the angular position or a motion of a shaft or axle to analog or digital code. Rotary encoder are usually used at the side which is perpendicular to the shaft. They act as sensors for detecting angle, speed, length, position and acceleration in automation field. As you turn the knob you will hear a sound from the knob indicating that one position has been rotated. If the internal contact were originally HIGH after a single click they would now both in LOW state breaking the circuit.


With a simple bit of logic you can work out the direction of the rotation. Most rotary encoder has a 5 pins with 3 functions or tuning left, right and pressing down. Pin 1 and Pin 2 are switch wiring terminals used to press. They are similar to buttons. Pin 4 usually placed in ground and the Pin 3 and Pin 5 are first connected to pull up resistor and then wired to the microcontroller.  The diagram below demonstrates the right and left rotation pulse.


Incremental encoder provides two phase square wave, these phase difference are 90 degree, usually called A and B channel. The rotational speed of a channel related information is given, while the two channel signal by comparing the sequence detail obtained in the rotation direction. There is a unique signal called the (Z) or zero channel that provided the absolute encoder zero, this signal is a square wave accord with the center line of the channel (A) square wave.


Accuracy depends on mechanical and electrical factors, these issues are indexing errors, disc eccentricity, bearing eccentric, electronic device errors and inaccuracies introduced by optical parts. The encoder determines the degree of accuracy of the measurement unit and electrical pulses generated by the encoder stars. The following degrees rotation of the mechanical axis to represent, and the axis of rotation must be a complete cycle. You have to know how much the equivalent of a 360 degree electrical angle, you can use these formula to calculate the degree. 360 = 360*/n* pulse / revolution.

Components Required

  • Raspberry Pi / Banana Pi / Orange Pi  (If your using Banana Pi or Orange See first the GPIO Pins)
  • Rotary Encode
  • Solder Less Bread Board
  • Jumper Wire / DuPont Wire

Wiring Guide



C Code / Wiring Pi

Python Code


Download TW700198 Rotary Encoder Datasheet | PDF

Wiring the 360 Rotary Encoder on C Code & Python
Tagged on:                                     


2 thoughts on “Wiring the 360 Rotary Encoder on C Code & Python

  • at

    Why this C program is consuming almost 100% CPU

    • at

      Chick the code line While and should be return to 0 ) or perhaps there is a technical issues in your rotary coder.


Leave a Reply

Your email address will not be published. Required fields are marked *