INTERFACING ROTARY ENCODER WITH BHARAT -PI

by madhankarthick in Circuits > Sensors

226 Views, 0 Favorites, 0 Comments

INTERFACING ROTARY ENCODER WITH BHARAT -PI

R.jpeg
rotary-encoder.jpg

Today in this instructables, I am going to show you how we can interface a rotary encoder to BHARAT PI. Rotary Encoders are sensors that detect position and speed by converting rotational mechanical displacements into electrical signals. So let's get started.

Rotary Encoders Vs Potentiometers

Rotary encoders are the modern digital equivalent of potentiometers and are more versatile. Potentiometers are used in situations where you need to know the exact position of the knob. Rotary encoders, on the other hand, are used in situations where you need to know the change in position rather than the exact position.

 

How Rotary Encoders Work?

Screenshot 2023-09-15 150610.png

Inside the encoder is a slotted disc that is connected to pin C, the common ground. It also has two contact pins A and B, as shown below.

When you turn the knob, A and B make contact with the common ground pin C in a specific order depending on which direction you turn the knob.

When they make contact with common ground, two signals are generated. These signals are 90° out of phase because one pin makes contact with common ground before the other. It is referred to as quadrature encoding.


When the knob is turned clockwise, the A pin connects to ground before the B pin. When the knob is turned anticlockwise, the B pin connects to ground before the A pin.

By monitoring when each pin connects or disconnects from ground, we can determine the direction in which the knob is being rotated. This can be accomplished by simply observing the state of B when A’s state changes.

ROTARY ENCODER PINOUT

Rotary-Encoder-Pinout.jpg

GND is the ground connection.

VCC is the positive supply voltage, which is typically between 3.3 and 5 volts.

SW is the output of the push button switch (active low). When the knob is depressed, the voltage goes LOW.

DT (Output B) is similar to CLK output, but it lags behind CLK by a 90° phase shift. This output is used to determine the direction of rotation.

CLK (Output A) is the primary output pulse used to determine the amount of rotation. Each time the knob is turned in either direction by just one detent (click), the ‘CLK’ output goes through one cycle of going HIGH and then LOW.

REQUIREMENTS

Artboard-1-e1686767346235.jpg
rotary-encoder.jpg
wires.jpg

·      Bharat-pi (board)

·      Jumper wires

·      Aurdino-ide

·      Rotary encoder

Interfacing ROTARY ENCODER WITH BHARAT -PI

Screenshot 2023-09-15 151027.png
WhatsApp Image 2023-09-06 at 12.56.41.jpg

Now that we understand how the rotary encoder works, it’s time to put it to use!


Let’s hook up the rotary encoder to the Bharat pi. The connections are quite simple. Begin by connecting the module’s +V pin to the Bharat pi 5V output and the GND pin to ground.

Now connect the CLK and DT pins to digital pins #26 and #27, respectively. Finally, connect the SW pin to digital pin #4.

SOURCE CODE AND PROGRAMMING

code.jpg

#define CLK 26

#define DT 27

#define SW 32

 

int counter = 0;

int currentStateCLK;

int lastStateCLK;

String currentDir ="";

unsigned long lastButtonPress = 0;

 

void setup() {

    

 // Set encoder pins as inputs

 pinMode(CLK,INPUT);

 pinMode(DT,INPUT);

 pinMode(SW, INPUT_PULLUP);

 

 // Setup Serial Monitor

 Serial.begin(9600);

 

 // Read the initial state of CLK

 lastStateCLK = digitalRead(CLK);

}

 

void loop() {

    

 // Read the current state of CLK

 currentStateCLK = digitalRead(CLK);

 

 // If last and current state of CLK are different, then pulse occurred

 // React to only 1 state change to avoid double count

 if (currentStateCLK != lastStateCLK && currentStateCLK == 1){

 

  // If the DT state is different than the CLK state then

  // the encoder is rotating CCW so decrement

  if (digitalRead(DT) != currentStateCLK) {

   counter --;

   currentDir ="clockwise";

  } else {

   // Encoder is rotating CW so increment

   counter ++;

   currentDir ="anti Clock wise";

  }

 

  Serial.print("Direction: ");

  Serial.print(currentDir);

  Serial.print(" | Counter: ");

  Serial.println(counter);

 }

 

 // Remember last CLK state

 lastStateCLK = currentStateCLK;

 

 // Read the button state

 int btnState = digitalRead(SW);

 

 //If we detect LOW signal, button is pressed

 if (btnState == LOW) {

  //if 50ms have passed since last LOW pulse, it means that the

  //button has been pressed, released and pressed again

  if (millis() - lastButtonPress > 50) {

   Serial.println("Button pressed!");

  }

 

  // Remember last button press event

  lastButtonPress = millis();

 }

 

 // Put in a slight delay to help debounce the reading

 delay(1);

}

code avilable at git hub:https://github.com/Madhankarthick/rotary-encoder-code-bharat-pi.git

Downloads

RUN THE PROGRAM,OUTPUT AT (SERIAL MONITOR)

Screenshot 2023-09-15 151812.png

This sketch simply monitors digital pins 26 (corresponding to interrupt 0) and 27 (corresponding to interrupt 1) for signal changes. In other words, it detects when the voltage changes from HIGH to LOW or LOW to HIGH as a result of turning the knob.


When a change occurs, the Arduino immediately detects it, saves its execution state, executes the function updateEncoder() (also known as the Interrupt Service Routine or simply ISR), and then returns to whatever it was doing before.

The following two lines configure the interrupts. The attachInterrupt() function instructs the Arduino which pin to monitor, which ISR to execute when the interrupt is triggered, and what type of trigger to look for.

APPLICATONS

OIP (3).jpeg
OIP (2).jpeg
How-Does-a-CNC-Machine-Work-A-Simple-Guide.jpg
Stijgband-1.jpg

Applications

The applications of rotary encoder include the following.

  1. These are used where speed, direction, acceleration, and monitoring rotation rate are required.
  2. These are used in different industries like material handling, packaging & conveyor.
  3. In the automation field, these encoders are utilized as sensors for speed, angle, acceleration & position.
  4. These are used to measure linear motion by using gear racks, spindles, cable pulls, or measuring wheels.
  5. These encoders are used to change a mechanical input to electrical signals by using tachometers, counters, PLC systems & PCs in industries.
  6. These are used in packaging, assembly machines, indication systems, printers, CNC machines, testing machines, robotics, textiles, motor feedback, medical equipment, drilling, and labeling machines.


VIDEO