Presentation is loading. Please wait.

Presentation is loading. Please wait.

Interfacing a Rotary Encoder with an Arduino

Similar presentations


Presentation on theme: "Interfacing a Rotary Encoder with an Arduino"— Presentation transcript:

1 Interfacing a Rotary Encoder with an Arduino
Arduino Uno microcontroller board Panasonic EVE-KC2F2024B Rotary encoder

2 DISCLAIMER & USAGE The content of this presentation is for informational purposes only and is intended for students attending Louisiana Tech University only. The authors of this information do not make any claims as to the validity or accuracy of the information or methods presented. Any procedures demonstrated here are potentially dangerous and could result in damage and injury. Louisiana Tech University, its officers, employees, agents and volunteers, are not liable or responsible for any injuries, illness, damage or losses which may result from your using the materials or ideas, or from your performing the experiments or procedures depicted in this presentation. The Living with the Lab logos should remain attached to each slide, and the work should be attributed to Louisiana Tech University. If you do not agree, then please do not view this content. boosting application-focused learning through student ownership of learning platforms

3 Rotary Encoders Rotary encoders are used keep track of the rotational position of a knob, like a volume knob on a stereo, or the rotational position of a motor shaft. Panasonic EVE-KC2F2024B 24 pulses per revolution 6mm diameter flattened output shaft Output type: quadrature (incremental) Minimum life: 15,000 rotations Cost: less than $1 (USD)

4 Encoder Output and Rotational Direction
clockwise rotation of knob counterclockwise rotation of knob A B COM B pin B = ON pin B = OFF A pin A = ON pin A = OFF When switch A goes ON and B is OFF, then rotation must be clockwise. When switch A goes ON and B is ON, then rotation must be counterclockwise. When switch A goes OFF and B is ON, then rotation must be clockwise. When switch A goes OFF and B is OFF, then rotation must be counterclockwise. increment rotational counter decrement rotational counter

5 Inside a Mechanical Rotary Encoder
As the encoder knob is turned, the spring-loaded contacts pass over metal segments that connect to the A, B and COM pins. Electrical continuity occurs when a contact touches metal, but no continuity occurs when a contact touches the black plastic. One of the three contacts is always touching COM. A COM B electrical continuity between A and COM no electrical continuity between A and COM spring-loaded electrical contacts

6 Sensor Wiring Rotary encoder Four 10kΩ resistors Two 0.01μF capacitors
This part of the circuit keeps the A, B and COM “switches” from flickering at the beginning or end contact this “debounces” the switches The encoder is the part in the red box

7 A Simple Sketch This sketch increments the variable “encoderPos” when the encoder knob is turned clockwise and decrements the variable when the knob is turned counterclockwise. The sketch uses an “interrupt” to avoid missing any changes in the position of the knob. The Arduino Uno has two interrupts attached to digital pins 2 and 3; we only use pin 2 as an interrupt here. volatile int encoderPos = 0; // the value of a volatile variable can change // in the function called by the interrupt void setup() { pinMode(2, INPUT); // encoder pinA is attached to digital pin2 pinMode(3, INPUT); // encoder pinB is attached to digital pin3 attachInterrupt(0, encoder, CHANGE); // interrupt0 maps to pin2 Serial.begin (9600); } void loop(){ } // main body of the sketch employing the interrupt void encoder() { if(digitalRead(2) == digitalRead(3)) // check to see if pins A & B have the same state {encoderPos--; } // decrement position if A & B are the same else {encoderPos++; } // increment position if A & B are not the same Serial.println(encoderPos, DEC); // send encoderPos to serial monitor

8 Example Application This implementation shows a knob mounted to the rotary encoder. This hardware includes two LEDS that come on and off as the encoder passes over the contacts. The hardware can be used with the sketch on the previous slide to demonstrate how the encoder works.


Download ppt "Interfacing a Rotary Encoder with an Arduino"

Similar presentations


Ads by Google