Top Banner
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) Arduino Uno microcontroller board Interfacing a Rotary Encoder with an Arduino living with the lab 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.
6

Interfacing a Rotary Encoder with an Arduino

Feb 10, 2016

Download

Documents

living with the lab. 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. Interfacing a Rotary Encoder with an Arduino. Arduino Uno microcontroller board. Panasonic EVE-KC2F2024B - PowerPoint PPT Presentation
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Interfacing a Rotary Encoder with an Arduino

Panasonic EVE-KC2F2024B24 pulses per revolution

6mm diameter flattened output shaftoutput type: quadrature (incremental)

minimum life: 15,000 rotationscost: less than $1 (USD)

Arduino Unomicrocontroller board

Interfacing a Rotary Encoder with an Arduino

living with the lab

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.

Page 2: Interfacing a Rotary Encoder with an Arduino

living with the lab

2

AB

COM

clockwise rotation of knobcounterclockwise rotation of knob

Bpin B = ON

pin B = OFF

Apin 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

Encoder Output and Rotational Direction

Page 3: Interfacing a Rotary Encoder with an Arduino

living with the lab

3

The Guts of a Mechanical Encoder

spring-loaded electrical contacts

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 continuitybetween A and COM

no electrical continuitybetween A and COM

Page 4: Interfacing a Rotary Encoder with an Arduino

Sensor Wiring (need four 10kΩ resistors and two 0.01F capacitors)

living with the lab

4

10kΩ10kΩ10kΩ

10kΩ

the encoder is the part in the red box

this part of the circuit keeps the A, B and COM “switches” from flickering at the beginning or end contact . . . this “debounces” the switches

Page 5: Interfacing a Rotary Encoder with an Arduino

A Simple Sketch

living with the lab

5

volatile int encoderPos = 0; // the value of a volatile variable can change // in the function called by the interruptvoid 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)) // get state of encoder pins A & B encoderPos++; // increment position both are HIGH else encoderPos--; // decrement position if both are LOW Serial.println (encoderPos, DEC); // send encoderPos to serial monitor

• 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.

Page 6: Interfacing a Rotary Encoder with an Arduino

living with the lab

6

Example ApplicationThis 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.