The technology we are using today is advancing at an exponential rate! Knowing all the basics of these innovative new technologies is essential to keep up with the changing times. Most electronic devices today rely on sensors to measure and detect information. Have you heard of a rotary encoder before? When you use a rotary encoder, you will realize that it is much more than a simple switch! The rotating disc on the switch sends out data to the Arduino, showing its position. How does this help? I will explain all the technical details later.
What Is a Rotary Encoder?
A rotary encoder is an electronic component that counts the number of times you rotate the knob. It does this by sending an electrical signal as you turn it and its direction. That way, you can tell exactly how much change has occurred in your project and what is happening currently. This may sound not very easy, but it is very simple. If you are familiar with how a potentiometer works, this is the same thing. You can rotate a potentiometer only from 0 to maximum and back again. The rotary encoder can rotate continuously. It allows you to have more control over your project.
How Does a Rotary Encoder Work?
The sensor of this device directly connects to digital pins on the Arduino board via a cable. We only need to use two digital pins to make the rotary encoder functional. We use one for data and direction input. When moving, the signal on these pins sends information about how far you have turned. Binary numbers represent the signal. Each group of four represents a single turn. The Arduino converts these binary numbers to send the micro-controller an on or off signal. Now that you have the technical details out of the way let’s move on to using it!
We can install an encoder on the motor shaft or mounted just above the shaft, similar to a potentiometer. After installing the sensor, it will work automatically without further input. If your motor has a gearbox or other moving parts, you will want to install the sensor just above these parts to obtain accurate readings. If you are working with a gearbox and want to measure the speeds of different gears, this is a great way to do it. We attach the rotary encoder used in this project directly above the shaft.
Arduino is an open-source hardware and software platform designed on a microcontroller board. It uses special integrated circuit chips. This IT-friendly platform lets you build objects that easily interact with your world. Arduino boards are currently available for about $35 to $80 on most major manufacturers such as Rayming PCB & Assembly. We can use other boards, but we will talk about those later. You will need at least an Arduino Uno board and the Arduino IDE for this project. You can also buy the Arduino Starter Kit for about $100. It has many components and comes with several manuals and projects to get you started.
Installing the Encoder
With the Arduino micro-controller, it is simple to install the encoder. The first step is to download and install the Arduino IDE from the Arduino encoder library. Once you have done this, access the Tools menu in your start window and click on Board: ARDUINO UNO. This will bring up a new window in which you will then select Tools -> Board Manager. Then, install the appropriate version of Arduino for your system. After doing this, you can install our rotary encoder. Go back to the Tools menu and select Tools -> Board: MKS GEN3. This will bring up a new window in which you will then select Tools -> Boards Manager. The first thing you want to do is search for rotary and check this option.
Click the install button and wait for the installation process to complete. Then, we can move on to installing our encoder. Go back to the Tools menu and select Tools -> Board: SINAMICS ENCODER. This will create a new window in which you will then select Boards -> Unknown type. If all goes well, the message should read OK. Now we can move on to installing our rotary encoder.
Go back to the main Arduino IDE window, select File. Then open to find the folder where you have saved your files and open this folder in your start window. The next step is to select Tools -> Boards and check the box next to Standard 16MHz Arduino boards and the board you have just installed. After doing this, go back to Tools -> Ports and select your port from the list. The last part is to select Tools -> Serial Port and select whatever serial port corresponds with your installation. If all goes well, you should see a blinking light where your Arduino IDE is. After this, load up the code into your IDE and upload it to your Arduino board.
How to Use a Rotary Encoder
The project is fairly simple, with the only hardware required being a rotary encoder. After installing your Arduino and encoder, connect it to your Arduino IDE. After doing this, load up the code from your Arduino IDE and upload it to your Arduino board. Using an Arduino rotary encoder will give you more control over what your motors do than using just a switch. You will be able to move forwards, backward, and turn for increased control. The code is fairly easy to use. You can modify it to have different effects. Also, you can manipulate a few variables for different effects.
Rotary Encoder Module Pinout
The rotary encoder is a small module with three pins as inputs and ground.
These are the pins you will need to connect to your Arduino board. The pins that you use will depend on the Arduino board you have purchased, but the most common ones are:
This module has five pins. The only two that we need to use are the data pin and direction pin, but you can use them in other ways if you want!
- CLK: This is the clock pin
- DT: This is the data pin
- VCC: This is the positive power to run the module
- GND: This is the ground connecting pin to our Arduino board,
- SW: This is the switch pin
- Arduino IDE (Software)
- Male to Male Jumper Wires (Hardware)
- Rotary Encoder Module with Push Switch (Hardware)
- Arduino UNO R3 (Hardware)
Determining the Position of the Rotary Encoder Shaft
The rotary encoder shaft comes as a rubber bearing attached by a plastic ring to the shaft. By looking at the inside of the bearing, you can see how it attaches to the shaft.
The center hole has little holes around it. They are slightly larger than the holes in our encoder module.
A picture of a rotary encoder installed on a belt drive mechanism with an encoder dial sensor demonstrates how these fit into our project.
Connect the + to 5V and the – to the ground of our Arduino
Then, connect the CLK, DT, and VCC to our Arduino board
Finally, connect the GND and SW to our Arduino board, as shown above
Positioning Your Rotary Encoder
To fit on your belt, you need to cut off a small part of the plastic ring eventually. The size of this depends on your encoder module. So, you can use some trial and error to figure out what size works best for your application.
To determine where you will place the encoder, measure from center to center on your belt’s axis.
If we were to rotate our belt clockwise by several holes, we would shift our rotary encoder position to the right by about 1/4 turn. To set up for this, we need to shift our rotary encoder closer by 1/4 turns in either direction.
Then we will precisely measure how far the rotary encoder is from the center of our axis.
Move your belt (or one side of your carriage) to face away from you, and pull the belt to its “home” position. Then measure how far it moves by counting one hole at a time and moving it in 1/4 turns. In our example, we will want to move the belt 1 hole to the right, or 3/4 of a turn.
How to wire encoder to Arduino UNO
We want to wire it to the Arduino clock, data, ground, and switch pins.
If we look at the back of our encoder, we can see there are five pins.
The three pins on the left are VCC, Ground (GND), VCC, and Switch (SW).
Next, we need to figure out which pin is Data (DT) and Clock (CLK).
We do this by using a multimeter to determine which pin is high when not pressed down.
Once you get this information, you need to wire it up to the Arduino.
Plug your Arduino into the USB port on your computer.
Ensure that you do not connect the Arduino board to anything else, or it will not detect.
Which Arduino pin for vex encoder signal
The encoder signal is a 2-wire serial interface. The Arduino uses an interrupt pin for the encoder. Which one you need to use depends on your Arduino encoder. With Arduino UNO or Nano models, you will use digital pin 2 (D2). Arduino Mega and Leonardo use digital pin 3 (D3). You can change the interrupt line by modifying the ‘encoder_pin’ variable in the code.
How to create a menu using rotary encoder in Arduino
This menu has four functions, forward, reverse, left, and right.
The encoder is a rotary device that you can rotate clockwise or counterclockwise. The rotary encoder is a mechanism to encode the input value as the number of steps that it makes a full revolution. The mechanical mechanism consists of three-position sensors and an electronic encoder. Each of these parts has four components. The ‘position sensor’ has a set of coils (cathode, anode, shield). It makes the sensor react when an electric current passes through it. The entire mechanism’s construction depends on a single axis parallel-axis rotary shaft.
Now let’s start learning about rotary encoder pinout in Arduino IDE.
Select Tools > Board and select the board you want to use.
Next, go to Tools> Port and select the port there too.
After this, you will get a new project (File>New):
In this case, we choose Arduino UNO R3:
This code works with an Arduino UNO R3 with an encoder that outputs 8 bits of data.
The code is simple and easy to read. What you have to do is change the values at the start of your program, which are:
- encoder_pin = 2
- encoder_show = 1
- encoder_mode = 0
- shift = 0
Types of Rotary Encoders
There are a few types of rotary encoders that you can purchase. So, it is essential to know the difference between them.
1. Mechanical Absolute Rotary Encoders
A mechanical absolute rotary encoder measures the rotation of the encoder shaft in a fixed circular path. Each click represents one revolution. An internal stop will not allow the shaft to rotate any further. We use this type of encoder in applications that require a limited number of positions, such as turning on a set of lights or operating an elevator. These encoders are cheaper but are more limited in terms of capabilities.
It is a common low-cost option for low-volume products.
2. Optical Absolute Rotary Encoders
Optical absolute encoders measure rotation by emitting a light beam. It also measures the reflection with a photoelectric sensor.
This type of encoder has a very long life span, is easy to implement, and is more durable than its relative.
They are the right choice for applications that need to measure the position of their motor under harsh conditions. These types tend to have higher price tags but can be worth it if they will be in continuous use.
You can construct them with either plastic or glass. We mount the shaft in a manner that prevents damage to the encoder.
They are also available with incremental optical encoders. It allows for incrementing rotation and gives you more control over your application.
3. Magnetic Absolute Encoders
Magnetic absolute encoders use magnetic fields to determine how far the shaft has rotated.
These are great for applications with high precision. They need a higher level of accuracy.
You can even use them with low speeds and are excellent if you need to determine the position of your motor under extreme conditions.
This type of encoder is usually more expensive but can be worth it if you need that level of accuracy in your application.
Incremental rotary encoders are less expensive than absolute encoders. But, they only provide incremental rotation.
This means they only tell you which direction of rotation is happening but not how many turns there are.
They are perfect for applications that do not need to know the exact position of your motor, such as a refrigerator motor. They also allow for repeatable adjustments in your application to maintain the same level of accuracy.
It is important to note that the resolution of incremental rotary encoders is usually not as high as that of absolute encoders.
Incremental rotary encoders typically have a center detent. This means that when we “home” our motor, our index will be at noon.
They are simple to use and are the first option you should consider if you only need to determine rotation.
They are less expensive than absolute encoders and can be very small.
Rotary Encoders vs. Potentiometer
Rotary encoders can measure rotation in a small space.
Because of their limited space, rotary encoders make it challenging to achieve the precise positions we want.
Using a potentiometer instead, we can easily achieve precise positions. You do this by mounting the motor to our PCB and adjusting the potentiometer from there. Then we can “home” our motor by turning the potentiometer to its center position.
Potentiometers are great if you want to achieve exact positioning but not give us feedback on the absolute position. Therefore, we must use a rotary encoder and feedback the Arduino data to accomplish this task.
In our application, the sensor’s output is 8 bits of data, and our program will then translate them into 0’s and 1’s.
We know that the Arduino will always return 1’s when the encoder is home and 0 when not.
We can use a 16-bit program to do this translation, if we account for the limitations of the rotary encoder Arduino.
There are libraries available to help with this translation, and you can find them through your Arduino IDE.
How Can Outputs Help You?
Outputs are an essential part of any robotics application. They allow us to track what is happening in our applications to make adjustments as necessary. This will save time as we troubleshoot during the development process.
The outputs also allow our programs to make decisions and change their behavior accordingly. For example, a digital output can tell us when our motor is home and idle. This will allow us to open the circuit to our devices without overheating the motor. If the motor is not home, it can tell us by turning off the relay. We can also generally expect our program to decide on its own whether it should run at low speed or high speed. That decision will depend on how long the motor has been running.
Selecting a Rotary Encoder
There are many different types of rotary encoders. You should be aware of the advantages and disadvantages of each one.
Each type has advantages that other types do not have. For example, absolute encoders are more accurate than incremental encoders. But they tend to be more expensive.
Thus, you should research your application before buying a rotary encoder. The type you select will depend on the application you attempt to achieve.
1. Rotary Encoder with Switch
The first rotary encoder you should consider is a knob and a switch.
These are great for applications such as clocks, where you want to make adjustments in your application.
You can adjust this type of encoder to exactly how you want it by adjusting the value of the switching knob. In addition, these are also very cost-effective compared to other types.
Grove has a great selection of different rotary encoders. They include some with LED indicators and push buttons.
Grove also has another significant advantage. They provide us with the circuit board and necessary components to mount in Arduino Uno.
Grove makes it easy to make your product, and there is a huge community that uses its parts. This allows you to get help from other makers if you encounter any problems.
Grove rotary encoders are the king of rotary encoders because of their low price and unique community. However, Grove is not all that great for applications that need feedback.
There are many different applications for a rotary encoder. We can use the data from a rotary encoder to tell us when our motor has reached home, how fast it is running and how many revolutions it has completed. This will all depend on our rotary encoder setup guide.
Grove also keeps track of the values entered your controller.
1. Sensor for “turn and push” encoding
This is a great way to communicate with a motor and turn it on or off. We can mount the sensor to the top of the motor. When you move away from the motor, it turns it on, and when you are closer to the motor, it turns it off.
We could use this for many things. For instance, turning on a light when you leave your home or turning off an air conditioner when you leave your office. This sensor can have many other applications in our different projects.
2. Sensor for motion, speed, and direction
We can use our rotary encoders to tell us how many revolutions our motor has completed and whether it is home (centered) or not. We can also use our rotary encoders to measure the speed and direction of the motor.
In addition, the rotary encoder can tell us exactly how far away we are from a given point. We can then calculate this data onto a graph to achieve better precision.
3. Accurate position sensor for encoder
We can use this type of sensor in any application that requires precise measurement. These sensors will be very accurate and measure other things besides our motor.
The advantage of these sensors is their accuracy and durability. However, the price tag will often exceed the cost of other types.
4. Automotive, optical sensors
These sensors are exactly what they sound like: an optical sensor mounted to the bottom of a motor. This type of sensor can tell us how fast our motor has been traveling and the direction that it has been moving in.
Inputs and outputs are critical components to any Arduino project. You should now understand what they are, how they work, and the many different types you can use in your projects. You will also be able to identify the advantages of each type of input and output depending on your application.
The next step is implementing an effective rotary encoder setup guide into your programming environment.