Rotary decoders have been around for years. They can be used in robotics and especially in motors to detect direction and speed. Moreover, you can use them for photographic lenses, trackballs, optomechanical mice, and, of course, Arduino projects. Here, we’re going to talk about it.
The main components of the rotary encoders are:
- Breadboard
- Jumper wires
- Rotary encoder module
- USB cable
Most basic encoders have 3 pins and one of them is common. The other pins can change the state and be either low or high, so you can get 4 combinations in total – 00, 01, 10, and 11. So when you turn it, your controller can say you were at 00, and now you’re at 01. Some encoders can have 4 or 5 pins and they’re a bit harder to work with.
How Rotary Encoders Work?
Let’s have a look at its working principles. It generates square wave pulses – the encoder has a disk with contact zones that are connected to pins A, B, and C. The first two pins start making contact with the pin C when the disk starts rotating step by step. Then, the two square wave output signals are generated.
You can use any of the output signals to determine the rotated position and count the number of pulses. However, if you want to know the direction as well, you need to evaluate both outputs at the same time.
For example, if you have two output signals displaced at 90 degrees out of phase and your encoder is rotating clockwise, the output B is behind the output A. It may sound confusing at first but when you try this once, you’ll see it’s very simple.
There is an easy way to program your Arduino controller to read the rotation direction and the encoder position. If the rotation of your RT is counterclockwise, the signals will have equal values. Thus, if you count the steps every time the signal changes from Low to High or from High to Low, you will see that the output signals have equal values.
As you can see, instead of using old analog potentiometers, you can use RT as an alternative. Rotary recorders look very similar and you can use them as rotation sensors. They continuously rotate around and are divided into segments, so working with them is easier.
Each clockwise or counterclockwise movement makes switches to close or to open and every segment has a clicky feeling. There are different RT models and modern ones have a push-on momentary switch with 3 pins on one side (A and B are the coding pins and C is the ground or common pin) and 2 pins on the other side (N/O switch contacts) for the switch.
What Are The Interrupts?
There are special pins that can stop your Arduino at any moment so that it will do something else before proceeding to the next step. These special pins are interrupt-pins and even though you can only get a few of them on Arduino, these pins can watch for falling, rising or change.
When you attach an interrupt function to the pins, it will stop doing everything and run for a new function if a change happens. Even though it can be a bit too much for the beginners as it breaks the linear nature of the Arduino loops, it is a very powerful tool for experts.
As you may know, global variables used in the functions have a name – volatile variables. They can change their value when you need it. For example, if you use a volatile several times in one loop, it might have a different value the second time if there was a change in the interrupt function.
One of the most common problems with reading your encoders is that quite often if your controller is doing something else like executing the code, the user starts turning the knob. If it happens, how can you check the input lines quickly and not miss a rise or fall?
The thing is, when you miss a rise or fall, you will lose the position of the encoder. Moreover, you might not be able to interpret the direction. Luckily, you can use interrupts to accomplish this task.
As the name suggests, interrupts pause the current action. If the code is running and an event is qualified as an interrupt-driven (for example, you can specify that interrupt happens when the signal goes from high to low) then whatever is happening will pause, and the new code will be executed.
As a result, the user doesn’t have to control code timing, which is very helpful because you often have a series of operations and it’s easy to miss something.
How to Connect A Rotary Encoder To Your Arduino?
When you need to connect your RT to Arduino, you should add a debouncer with the setup to avoid encoding errors. It is important because there are two pieces of metal contacts in the rotary encoder and they can touch each other in an imprecise way like in a mechanical switch. As a result, it will manifest itself as steps back when going forward or as missed steps. In order to avoid this, you can use an RC filter or add a code line in software.
To make everything even easier, there is an encoder library available for Arduino users, which will do the hard work for you and figure out the step and the direction. All you need to do is to add the library to your current Arduino project and make sure you’re using the right pins. Then, define the pins for the library and encode away.
You might also like:
- Arduino hx711 tutorial
- Arduino magnetic switch
- Best Arduino Kits
- gifts for engineering students
- gifts for engineers
- mpu6050 Arduino projects
- Current sensor Arduino
- Soil Moisture Sensor With Arduino
- Arduino Count up Timer Using the Nokia 5110 LCD
- Arduino Yun: Integrating or Juxtaposing Arduino with Linux
- Arduino Projects: Line Follower Robot
- Arduino RFID Project for Beginners
- Arduino MOSFET Project
- Which Arduino Should You Buy
- What Can You Do With Arduino Boards?
- Great Alternatives to the Arduino Microcontroller
- Arduino Projects: Color Sensor
- Arduino IDE Alternatives
- Arduino Mega vs. Uno
- Arduino Projects: Arduino LCD Display
- Read Arduino Rotary Encoders
- A Selection of the Best Arduino Simulators
- Arduino Projects: IR Receiver
- Arduino Light Sensor Project
- Arduino Projects: Arduino Decibel Meter
- Arduino Stopwatch Project
- Arduino Bluetooth RC Car Project
- Arduino Temperature Logger Project
- Arduino Projects: Arduino 7 Segment Display
- Arduino Projects: Clap ON Clap OFF Light
- Arduino Relay Project
- Install a Library Onto the Arduino IDE
- Arduino Projects: Rainfall Detector
- Arduino Projects: RGB LED Arduino
- Arduino Stepper Motor Project
- Arduino Projects: Arduino DC Motor Control
- The Top Affordable Arduino Robot Kit
- Arduino 3D Printed Case
- Arduino Projects: Asynchronous LEDs Blink
- Arduino Projects: Ultrasonic Distance Sensor
- Arduino Projects: LED – 4X4X4 LED Cube
- Arduino Car Projects: Build an Obstacle Avoiding Robot With Less Than $30
- Arduino Projects: Servo Potentiometer Control
- Arduino LED Project: Knight Rider
- Arduino Projects: PIR Motion Sensor
- The Difference between Arduino and Raspberry Pi
- Top 9 Books Every Engineer Should Read
- Top Used Sensors for Arduino
- First Hand on the Arduino Uno Board