In a permanent magnet, brushless motor, a sensor or device known as a rotary encoder is utilized to detect the angle of the motor shaft. The question might come to your mind how this sensor works? The sensor generates an electrical signal when detecting any rational movement. And, thus, this helps to control the position or speed of any device. But, these types of sensors require a controller that drives them. For example, an Arduino. So, to understand how Arduino can drive the encoder, in this tutorial, we are going to Make an ” Arduino Rotary Encoder Interface “
#define encoderOutA 2 // CLK pin of Rotary Enocoder
#define encoderOutB 1 // DT pin of Rotary Enocoder
int counter = 0;
int presentState;
int previousState;
void setup() {
pinMode (encoderOutA,INPUT);
pinMode (encoderOutB,INPUT);
Serial.begin (9600);
previousState = digitalRead(encoderOutA); // Get current state of the encoderOutA
}
void loop() {
presentState = digitalRead(encoderOutA);
if (presentState != previousState)
{
if (digitalRead(encoderOutB) != presentState)
{
counter ++;
}
else {
counter --;
}
Serial.print("Position: ");
Serial.println(counter);
}
previousState = presentState; // Replace previous state of the encoderOutA with the current state
}
Code Explanation
First, define the Arduino pins connected with the encoder pins. Then define the variables counters (initialize the count as 0), presentState, previousState to store the integer values.
In void setup, declare
Application and Uses
It’s suitable for usage in door control systems.
It can also be used in robotics.
An encoder is incorporated into several testing devices.
Encoders are used in parts assembling machines.
Encoders are used in drilling machines, mixing machines, and other devices.
You May Also Like
Introduction Measuring distance using an HC-SR04 Ultrasonic sensor module and posting its status on an OLED SSD1306 White 128X64 IIC I2C Serial Display Module using an Arduino UNO microcontroller is a system that uses an ... Read moreRead more
Introduction This tutorial introduces a powerful and versatile display module – the 1.8 Inch SPI TFT Color Display. This small yet mighty module packs a punch with its ability to display vibrant colors and high-resolution ... Read moreRead more
Introduction In order to help the patients, engineers and designers are also doing their part since the frequency of heat-related illnesses and the number of those affected is increasing daily. The biomedical technologies used to ... Read moreRead more
Introduction Motors have greater use in engineering, either the DC motor, a servo motor, or a stepper motor, industries cannot operate without motors. And, just not industrial machines, these motors are also used at the ... Read moreRead more
Introduction Solar energy is an excellent source of renewable energy that can be used to power homes, businesses, and other applications. However, the amount of energy that can be generated by a solar panel depends ... Read moreRead more
Introduction Temperature sensors are electronic devices that convert temperature into electrical signals and are used to detect temperature. This might be air temperature, liquid temperature, or solid matter temperature. There are several types of temperature ... Read moreRead more
In this tutorial, we are going to “interface INA219 Current Sensor Module with Arduino” A current sensor is a device that detects electrical current, whether it is high or low, in a wire or a ... Read moreRead more
Introduction To understand the concept of accelerometer and gyroscope, take the example of your smartphone. While watching the video, when you change the direction of your phone, the phone automatically changes from portrait to landscape ... Read moreRead more