# [SOLVED]Angle measurement using MOC7811

Status
Not open for further replies.

#### Anantha Krishna

##### Member level 1
Hi All,

I am using MOC7811 to detect the angle of a wheel. I have attached a small wheel with 90 slotted cuts, which cuts the connection between receiver and the transmitter of MOC7811, and by detecting the edges, I can measure the angle.
I am using two optos adjacent to each other, through which I will able to measure 360 degrees.[i.e. each opto will give two edges for each slot. The small wheel contains 90 slots. So each opto gives 180 deg. Totally 360 deg].

Please see the attached image of the MOC7811 circuit that I am using.

Detecting the angle in single direction is easy. I am not able to figure out how to measure angle in both +ve and -ve directions. i.e. The indicator should show +ve angle in clockwise and -ve angle in anticlockwise directions.

Please someone help to solve this problem.

Thank you.

#### IanP

If you re-design the encoder to have two sets of LED/phototransistor pairs, those pairs aligned such that their square-wave output signals are 90o out of phase with each other, you’ll have what is known as a quadrature output encoder ..
A phase detection circuit may be made from a D-type flip-flop, to distinguish a clockwise pulse sequence from a counter-clockwise pulse sequence ..
See attached picture ..

:wink:
IanP

#### Attachments

• Rotary Shaft Encoder - Direction1.png
17.1 KB · Views: 50
DrWhoF

V
Points: 2

### DrWhoF

Points: 2

#### Anantha Krishna

##### Member level 1
Thank you.

But I am using a microcontroller to detect the angle, not flip flop or counters.

Could you explain how to achieve 360 deg bidirectional angle measurement using C, if we implement both D flip flop and up/down counter in C programming?

#### JayantD

##### Member level 3
First of all, to sense direction, you need quadrature outputs from your sensing arrangement. This is what IanP has suggested. From what you have described, it is not clear whether you are getting phase shifted outputs. A photograph of your sensor mounting arrangement will be helpful..Refer Austria microsystems AS5035 datasheet.
When you get quadrature outputs, using the D flipflop as suggested by IanP, Q output will be logic high for one direction, logic low for the other.

#### Anantha Krishna

##### Member level 1
Hi,

I have ensured that the sensor arrangement results in quadrature outputs.

If we see the image attached by IanP, Q is updated at the rising edge of the Clk. But since the outputs are quadrature, whenever the clk is high the D will be low. So we will get only one state output at Q. So the counter updates in the single direction, not both. This what I observed while implementing this arrangement in my program.

Please correct me if I am wrong and give a solution.

#### FvM

##### Super Moderator
Staff member
You can easily count four events per period by combining the present and the previous state of both detectors.

Code:
A=detA;
B=detB;
deltaA = A ^ prevA;
deltaB = B ^ prevB;
if (deltaA || deltaB) // any change
{
if (deltaA && deltaB)
{
// overrun
}
else if (A ^ prevB)
{
// count up
}
else
{
// count down
}
}
prevA = A;
prevB = B;

Points: 2

### Anantha Krishna

Points: 2

#### IanP

Have a look at both pictures, they are self-explanatory ..
Perhaps your circuit can not deliver 50% duty cycle ??

:wink:
IanP

#### Attachments

• Encoder - A leads B.gif
13.6 KB · Views: 25
• Encoder - B leads A.gif
14.4 KB · Views: 27

#### FvM

##### Super Moderator
Staff member
Perhaps your circuit can not deliver 50% duty cycle ??
I didn't hear yet a clear statement about deviation from an ideal quadrature signal. The OP wants 4 counts per quadrature cycle, which is a reasonable requirement. The previously suggested D-FF circuit can't provide it, and it also tends to produce false counts with bouncing inputs or incremental back and forth rotations, that can occur in some applications. Decoding the quadrature signal like a gray counter gives exact count results in all situations.

tiger135

### tiger135

Points: 2

#### IanP

...D-FF circuit can't provide it, and it also tends to produce false counts with bouncing inputs or incremental back and forth rotations, that can occur in some applications...
D-FF does not count anything, it's purely used to indicate direction ..
:wink:

DrWhoF

### DrWhoF

Points: 2

#### JayantD

##### Member level 3

Self Explanatory..

Anantha Krishna

### Anantha Krishna

Points: 2

#### Anantha Krishna

##### Member level 1
@FvM - I have already tried similar code. It works fine for upcounting. But down counting will not work properly. Further, this type of coding doesn't actual counts if the change in pulses is too fast, even if are deteting the edges in interrupt mode. This is what I have observed.

@JayntD and IanP - the images are indeed self explanatory. But could you please help to realize this in programming instead of using D flip flop? i.e how to identify which one is leading and which is lagging?

#### JayantD

##### Member level 3
it would be easier to implement a D flip flop. State machines would be helpful in writing code.
BTW, what is the maximum frequency of your signals ?

#### Anantha Krishna

##### Member level 1
Thank you all.

It is working fine with a pull up resistor of 5.1k at the output.

#### FvM

##### Super Moderator
Staff member
I have already tried similar code. It works fine for upcounting. But down counting will not work properly. Further, this type of coding doesn't actual counts if the change in pulses is too fast, even if are deteting the edges in interrupt mode. This is what I have observed.
I think, your intentions are unclear. To refer to your previous statement:
But I am using a microcontroller to detect the angle, not flip flop or counters.
Could you explain how to achieve 360 deg bidirectional angle measurement using C, if we implement both D flip flop and up/down counter in C programming?
There's no such thing as a D-FF in C. Furthermore you would need FFs acting on both edges to implement the requested 360 steps counter in hardware.

The problem is, that a processor solution relies on the processor speed - or needs dedicated quadrature decoder hardware.

Also your observations are unclear. Overrun with too fast input pulses is one thing, not counting correctly another one. Your processor solution should at least count correctly on slow rotation. If it doesn't, either the code or the quadrature signals are wrong.

You previously gave the impression, that you don't know how to write the respective C-code. Now you tell, you already have tried it. You may want to tell more clearly, what you're missing exactly.

My code should mainly give an example. There are different ways to implement a software quadrature decoder with maximum speed. You didn't tell yet about count frequencies or processor speed, so we don't know, if a processor solution can work at all.

D-FF does not count anything, it's purely used to indicate direction ..
The problem is, that the direction information is only updated on the rising edge of one channel. I'm sure, you can figure out yourself, why the said solution produces false counts with "unsuitable" decoder moves.

P.S.:
It is working fine with a pull up resistor of 5.1k at the output
Surprizing.... But good news anyway.

#### JayantD

##### Member level 3
This seems a good opportunity to introduce a sticky template about specifications that must be included along with a query..
I haven't looked, it might already be there.. don't pounce on me! :wink: