Encoder y Arduino
Encoder y Arduino
Incremental Encoders I
Overview
Motion Input!
Encoder Basics!
Unidirectional Decoding!
Bi-Directional Decoding!
Using Pin Change Interrupts!
Using Assembly Language
Hands On Arduino: Encoders
(Resistance Changes)
Hands On Arduino: Encoders
Velocity!
Tachometer!
Rate Gyro!
Estimator!
Acceleration!
Accelerometer
Hands On Arduino: Encoders
What is an Encoder?
A General concept.!
Implementation depends on the application!
Used for!
Data Compression!
Information Security
Pulse Technology
Magnetic!
Switch Contacts!
Optical Reflective!
Optical Transmissive
Encoder
Decoder
Absolute Encoders
Encoders where each position provides a unique
code.!
Often based on a Gray code, where the codes for
adjacent positions differ by at most 1 bit.!
No data compression, thus requires n channels.!
More complicated to work with!
Exact position is always known.
Hands On Arduino: Encoders
Absolute Encoders
8 bit absolute encoder disc!
each ring corresponds to a
different bit.!
The number of PPR is 28 = 256!
Requires 8 channels!
only get 1x decoding!
know where you are at startup!
Hands On Arduino: Encoders
Emitter
Number of!
Pulses per !
Revolution!
(PPR): N
1x Resolution: 360/N!
2x Resolution: 180/N
void
setup()
{
Serial.begin(9600);
//connect
Channel
A
to
pin
3
pinMode(3,
INPUT);
//set
the
initial
time
timep
=
micros();
//set
the
initial
value
of
A
Ap
=
digitalRead(3);
}
void
loop()
{
A
=
digitalRead(3);
if
(A^Ap)//is
there
a
difference?
{
count++;//if
so,
increment.
}
Ap
=
A;
time
=
micros();
etime
=
time
-
timep;
if
(etime
>
1000000)//every
1
sec
{
Serial.println(count);
timep
=
time;//reset
timer
}
}
Sample Code:
Using Interrupts
unsigned
long
count=0;
unsigned
long
timep,
time,
etime;
void
setup()
{
Serial.begin(9600);
//connect
Channel
A
to
pin
3
pinMode(3,
INPUT);
attachInterrupt(1,transition,
CHANGE);
//set
the
initial
time
timep
=
micros();
}
!
void
transition()
{
count++;
}
!
void
loop()
{
time
=
micros();
etime
=
time
-
timep;
if
(etime
>
1000000)//
1
second
{
Serial.println(count);
timep
=
time;
//reset
timer
}
}
Sample Code:
Velocity Estimation
//This
displays
the
number
of
transitions
//
per
second.
unsigned
long
count=0;
unsigned
long
timep,
time,
etime;
void
setup()
{
Serial.begin(9600);
//connect
Channel
A
to
pin
3
pinMode(3,
INPUT);
attachInterrupt(1,transition,
CHANGE);
//set
the
initial
time
timep
=
micros();
}
!
void
transition()
{
count++;
}
!
void
loop()
{
time
=
micros();
etime
=
time
-
timep;
if
(etime
>
1000000)//
1
second
{
Serial.println(count);
count
=
0;
//reset
the
counter
timep
=
time;
//reset
timer
}
}
Absolute Encoders
Encoders where each position provides a unique
code.!
Often based on a Gray code, where the codes for
adjacent positions differ by at most 1 bit.!
No data compression, thus requires n channels.
BiDirectional Encoding
Detectors
Emitters
A
1 1 0 0
1 1 0 0
In-Phase
1 0 0
1 0 0
Quadrature
BiDirectional Encoding
BiDirectional Encoding
A
1
B
1
state: 1
State
BiDirectional Encoding
A
FORWARD:
t
B switches before A
Reverse
t
A switches before B
t
B
1
state: 1
Edge
counter
A-rising
increment
A-falling
decrement
B
1
B
1
state: 1
Edge
counter
A-rising
decrement
A-falling increment
A-falling decrement
A-rising
increment
present
state
AB
11
11
dec
inc
10
inc
dec
00
inc
dec
01
dec
inc
2
10
3
00
4
01
A
1
state: 1
X = do nothing.
BD4x: SwitchCase
long
count=0;
unsigned
long
timep,
time,
etime;
boolean
A,B;
byte
state,
statep;
void
setup()
{
Serial.begin(9600);
pinMode(2,
INPUT);//Channel
A
pinMode(3,
INPUT);//Channel
B
attachInterrupt(0,Achange,CHANGE);
attachInterrupt(1,Bchange,CHANGE);
timep
=
micros();
//set
the
initial
time
//read
the
initial
value
of
A
&
B
A
=
digitalRead(2);
B
=
digitalRead(3);
//set
initial
state
value
if
((A==HIGH)&&(B==HIGH))
statep
=
1;
if
((A==HIGH)&&(B==LOW))
statep
=
2;
if
((A==LOW)&&(B==LOW))
statep
=
3;
if
((A==LOW)&&(B==HIGH))
statep
=
4;
}
void
loop()
{
time
=
micros();
etime
=
time
-
timep;
if
(etime
>
1000000)
{
Serial.println(count);
timep
=
time;
}
}
BD4x: SwitchCase
void
Achange()
{
A
=
digitalRead(2);
B
=
digitalRead(3);
//determine
state
value
if
((A==HIGH)&&(B==HIGH))
state
=
1;
if
((A==HIGH)&&(B==LOW))
state
=
2;
if
((A==LOW)&&(B==LOW))
state
=
3;
if
((A==LOW)&&(B==HIGH))
state
=
4;
switch
(state)
{
case
1:
{
if
(statep
==
2)
count--;
if
(statep
==
4)
count++;
break;
}
case
2:
{
if
(statep
==
1)
count++;
if
(statep
==
3)
count--;
break;
}
case
3:
{
if
(statep
==
2)
count++;
if
(statep
==
4)
count--;
break;
}
default:
{
if
(statep
==
1)
count--;
if
(statep
==
3)
count++;
}
}
statep
=
state;
}
BD4x: Array
State
present
0
1
2
3
prior
AB
11
10
00
01
0
11
X
inc
X
dec
1
10
dec
X
inc
X
2
00
X
dec
x
inc
3
01
inc
X
dec
x
BD4x: Array
void
Achange()
{
A
=
digitalRead(2);
B
=
digitalRead(3);
//determine
state
value
if
((A==HIGH)&&(B==HIGH))
state
=
0;
if
((A==HIGH)&&(B==LOW))
state
=
1;
if
((A==LOW)&&(B==LOW))
state
=
2;
if
((A==LOW)&&(B==HIGH))
state
=
3;
index
=
4*state
+
statep;
count
=
count
+
QEM[index];
statep
=
state;
}
void
Bchange()
{
A
=
digitalRead(2);
B
=
digitalRead(3);
//determine
state
value
if
((A==HIGH)&&(B==HIGH))
state
=
0;
if
((A==HIGH)&&(B==LOW))
state
=
1;
if
((A==LOW)&&(B==LOW))
state
=
2;
if
((A==LOW)&&(B==HIGH))
state
=
3;
index
=
4*state
+
statep;
count
=
count
+
QEM[index];
statep
=
state;
}