How to connect KY-040 Rotary Encoder to Raspberry Pi

In this project we are going to use a Rotary Encoder KY-040 to control a  Step Motor  and Display a Graphic Gauge.

Raspberry Pi is loaded with Raspbian OS. tkinter Library is included in new Raspbian OS.
A Graphic Rotary Gauge is provided as rotarylib.py.
Rotary Encoder is connected to Raspberry Pi GPIO18 and GPIO23.
these two GPIO are setup as input and binded to Rising and Falling edge event _callbackA.
Base on Rotary Encoder Timing in _callbackA Function we Step_it(1) or Step_it(-1) clockwise or counter clockwise .
in Step_it Function we select one setting for GPIO26, GPIO19, GPIO13 and GPIO6 connected to ULN2003 Inputs.
ULN2003 outputs drive the Step Motor inputs.

  1. On Raspberry Pi Create a directory
  2. download Test_RotaryEncoder and unzip it in that folder
  3. you will see two files rotarylib.py and Test_RotaryEncoder
  4. On Raspberry Pi under Menu Programming select Thonny Python IDE
  5. from menu file open file Test_RotaryEncoder.py
  6. Connect Rotary Encoder,ULN Driver board, Step Motor and 5V /12V Power supply to Raspberry Pi
  7. run the Python file

Rotary Encoder Timing


download:  Test_RotaryEncoder KY-040_StepMotor fritzing file 

Leave a Reply

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