Page 1
Welcome! Thank you for purchasing our AZ-Delivery KY-040 Rotary Encoder Module. On the following pages, you will be introduced to how to use and set up this handy device. Have fun!
Page 2
Areas of application Education and teaching: Use in schools, universities and training institutions to teach the basics of electronics, programming and embedded systems. Research and development: Use in research and development projects to create prototypes and experiments in the fields of electronics and computer science. Prototype development: Use in the development and testing of new electronic circuits and devices.
Page 3
consult a doctor. Caution: Keep the product out of the reach of children and pets to avoid accidental contact and swallowing of small parts. Note: Store the product in a safe, closed container when not in use. Attention: Avoid contact of the product with food and drinks.
Table of Contents Introduction......................3 Working principle....................4 The output......................6 Specifications....................7 The pinout......................7 How to set-up Arduino IDE................8 How to set-up the Raspberry Pi and Python..........12 Connecting the module with Atmega328p.............13 Sketch example..................14 Connecting the module with Raspberry Pi.............18 Python script....................19 - 2 -...
Introduction The KY-040 rotary encoder module is a rotary input device (as in knob) that provides an indication of how much the knob has been rotated and in what direction it is rotating. A rotary encoder is an electro-mechanical device that converts rotational motion into digital or analog information.
Working principle There are two switches inside the encoder. One switch connects pin A (or CLK) to pin C and the other switch connects pin B (or DT) to C. In each encoder position, both switches are either opened or closed. The illustration below is the representation of how the switch is constructed.
Page 7
If the opening and closing of the switches is represented as wave forms, it would look something as shown on the following image: Essentially, 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. »...
The output The module is designed so that the LOW state is an output when the switches are closed and the HIGH state when the switches are open. The LOW state is generated by placing a ground at pin C and passing it to the CLK and DT pins when switches are closed.
-40°C to 85°C » Output: digital » Dimensions: 17 x 29 x 30mm [0.7 x 1.14 x 1.2in] The pinout The KY-040 rotary encoder module has five pins. The pinout diagram is shown on the following image: - 7 -...
How to set-up Arduino IDE If the Arduino IDE is not installed, follow the link and download the installation file for the operating system of choice. For Windows users, double click on the downloaded .exe file and follow the instructions in the installation window. - 8 -...
Page 11
For Linux users, download a file with the extension .tar.xz, which has to be extracted. When it is extracted, go to the extracted directory and open the terminal in that directory. Two .sh scripts have to be executed, the first called arduino-linux-setup.sh and the second called install.sh.
Page 12
Almost all operating systems come with a text editor preinstalled (for example, Windows comes with Notepad, Linux Ubuntu comes with Gedit, Linux Raspbian comes with Leafpad, etc.). All of these text editors are perfectly fine for the purpose of the eBook. Next thing is to check if your PC can detect an Atmega328p board.
Page 13
If the Arduino IDE is used on Windows, port names are as follows: For Linux users, for example port name is /dev/ttyUSBx, where x represents integer number between 0 and 9. - 11 -...
How to set-up the Raspberry Pi and Python For the Raspberry Pi, first the operating system has to be installed, then everything has to be set-up so that it can be used in the Headless mode. The Headless mode enables remote connection to the Raspberry Pi, without the need for a PC screen Monitor, mouse or keyboard.
Connecting the module with Atmega328p Connect the KY-039 module with the Atmega328p as shown on the following connection diagram: KY-040 pin > Mc pin + (VCC) > Red wire > Black wire > Green wire > Orange wire > Blue wire...
Page 18
Upload the sketch to the Atmega328p and open Serial Monitor (Tools > Serial Monitor). The result should look like the output on the following image: To get these values, move or press the shaft of the rotary encoder. NOTE: There is a lot of noise in the output of the module. A high number of errors is expected with high noise.
Page 19
The sketch starts with creating three macros called CLK_PIN, DT_PIN and SW_PIN. These macros represent the digital pins of Atmega328p on which pins of the module are connected. Next, several variables are created which are used in the algorithm that detect when and in which direction the shaft of the encoder is moving.
Connecting the module with Raspberry Pi Connect the module with the Raspberry Pi as shown on the following connection diagram: KY-040 pin > Raspberry Pi pin + (VCC) > [pin 17] Red wire > [pin 25] Black wire > GPIO5...
Page 22
print('[Press CTRL + C to end the script!]') try: # Main program loop while True: a, b GPIO.input(CLK_PIN), GPIO.input(DT_PIN) taster = not GPIO.input(SW_PIN) taster last_taster: print('{}|{}'.format(position, taster)) sleep(0.01) last_taster taster a_last b_last: while not GPIO.input(CLK_PIN) GPIO.input(DT_PIN) position print('{}|{}'.format(position, taster)) while not GPIO.input(CLK_PIN) GPIO.input(DT_PIN) position...
Page 23
Save the script by the name RotaryEncoder.py. To run the script, open the terminal in the directory where the script is saved and run the following command: python3 RotaryEncoder.py The result should look like the output on the following image: To stop the script press CTRL + C on the keyboard.
Page 24
If you are looking for the high quality microelectronics and accessories, AZ-Delivery Vertriebs GmbH is the right company to get them from. You will be provided with numerous application examples, full installation guides, eBooks, libraries and assistance from our technical experts.
Need help?
Do you have a question about the KY-040 and is the answer not in the manual?
Questions and answers