Jump to content

Programming rotary encoder

Sub2pewdiepie

I am trying to build a sim racing station and have moved on to the steering wheel. I have purchased a rotary encoder and an Arduino Leonardo to be used in the project i am not sure how to write the code for the Arduino to have my pc look at it as a controller so it can be used as such. if anybody could help point me in the right direction that would be great. the rotary encoder only has 2 outputs which im assuming are x and y 

 

Link to comment
Share on other sites

Link to post
Share on other sites

The rotary encoder almost certainly outputs over an i2c interface. Have you read the datasheet?

Quote me to see my reply!

SPECS:

CPU: Ryzen 7 3700X Motherboard: MSI B450-A Pro Max RAM: 32GB I forget GPU: MSI Vega 56 Storage: 256GB NVMe boot, 512GB Samsung 850 Pro, 1TB WD Blue SSD, 1TB WD Blue HDD PSU: Inwin P85 850w Case: Fractal Design Define C Cooling: Stock for CPU, be quiet! case fans, Morpheus Vega w/ be quiet! Pure Wings 2 for GPU Monitor: 3x Thinkvision P24Q on a Steelcase Eyesite triple monitor stand Mouse: Logitech MX Master 3 Keyboard: Focus FK-9000 (heavily modded) Mousepad: Aliexpress cat special Headphones:  Sennheiser HD598SE and Sony Linkbuds

 

🏳️‍🌈

Link to comment
Share on other sites

Link to post
Share on other sites

4 minutes ago, kelvinhall05 said:

The rotary encoder almost certainly outputs over an i2c interface. Have you read the datasheet?

I got the encoder off of amazon and only the encoder came in the package nothing else 

Link to comment
Share on other sites

Link to post
Share on other sites

Just now, Sub2pewdiepie said:

I got the encoder off of amazon and only the encoder came in the package nothing else 

Link the Amazon page.

Quote me to see my reply!

SPECS:

CPU: Ryzen 7 3700X Motherboard: MSI B450-A Pro Max RAM: 32GB I forget GPU: MSI Vega 56 Storage: 256GB NVMe boot, 512GB Samsung 850 Pro, 1TB WD Blue SSD, 1TB WD Blue HDD PSU: Inwin P85 850w Case: Fractal Design Define C Cooling: Stock for CPU, be quiet! case fans, Morpheus Vega w/ be quiet! Pure Wings 2 for GPU Monitor: 3x Thinkvision P24Q on a Steelcase Eyesite triple monitor stand Mouse: Logitech MX Master 3 Keyboard: Focus FK-9000 (heavily modded) Mousepad: Aliexpress cat special Headphones:  Sennheiser HD598SE and Sony Linkbuds

 

🏳️‍🌈

Link to comment
Share on other sites

Link to post
Share on other sites

Quote me to see my reply!

SPECS:

CPU: Ryzen 7 3700X Motherboard: MSI B450-A Pro Max RAM: 32GB I forget GPU: MSI Vega 56 Storage: 256GB NVMe boot, 512GB Samsung 850 Pro, 1TB WD Blue SSD, 1TB WD Blue HDD PSU: Inwin P85 850w Case: Fractal Design Define C Cooling: Stock for CPU, be quiet! case fans, Morpheus Vega w/ be quiet! Pure Wings 2 for GPU Monitor: 3x Thinkvision P24Q on a Steelcase Eyesite triple monitor stand Mouse: Logitech MX Master 3 Keyboard: Focus FK-9000 (heavily modded) Mousepad: Aliexpress cat special Headphones:  Sennheiser HD598SE and Sony Linkbuds

 

🏳️‍🌈

Link to comment
Share on other sites

Link to post
Share on other sites

Take a look at the product description:

Quote

Wiring Output:
A phase, B phase, Vcc positive supply, V0, the shield wire.

 

Connection:
Green = A phase, white = B phase, red = power +, black = V0
Note: AB two -phase outputs must not be connected directly to VCC, otherwise, they will burn the output transistor.

 

Output
AB two -phase quadrature output rectangular pulse, the circuit output is NPN open collector output type ,the output of this type can and with additonal added pull-up resistor(not included) is directly connected MCU or PLC, such as 51 single or Mitsubishi PLC (PLC input mode should be connected to 0V switch function ).

and there you go: Green = A phase, white = B phase

 

Edit:

I've never worked with things like this, but i think you should use the A and B signals as interrupts.

 

Rotary encoder on wikipedia:

Quote

A rotary incremental encoder has two output signals, A and B, which issue a periodic digital waveform in quadrature when the encoder shaft rotates. This is similar to sine encoders, which output sinusoidal waveforms in quadrature (i.e., sine and cosine),[13] thus combining the characteristics of and encoder and a resolver. The waveform frequency indicates the speed of shaft rotation and the number of pulses indicates the distance moved, whereas the A-B phase relationship indicates the direction of rotation.

So you've got 2 functions for dealing with the 2 interrupts and all you have to do is a read from the other input to determine the direction and adjust the angle/counter accordingly.

 

Edit 2:

There are libraries that do exactly what do you want:

Rotary stuff: https://www.arduino.cc/reference/en/libraries/rotaryencoder/

Joystick lib.: https://github.com/MHeironimus/ArduinoJoystickLibrary/tree/version-2.0

 

ಠ_ಠ

Link to comment
Share on other sites

Link to post
Share on other sites

That's a rotary encoder with 600 pulses per revolution. That's what 600 P/R means, I suspect.

Here's a datasheet though it doesn't matter.

You have red and black which are 5v and ground,  and you have the two phases (green and white)

 

You have to monitor the signals coming from those two phases, and depending on the direction it spins, those will change from low to high differently, and that's how you can figure out the direction. Then you can figure out how fast it spins, by the amount the signals stay high or low.

Probably need a resistor between voltage and each phase wire, something like 2.2k or higher.

image.png.7d226fa202de3d9ed10f4ddc42a023ac.png

 

 

You can use a quadrature decoder chip between the encoder and your arduino, which would measure those signals and convert them into two simple  "up" and "down" signals for your arduino.

An example of such chip is LS7084 :

https://www.digikey.com/en/products/detail/lsi-csi/LS7084N/12760881

https://www.digikey.com/en/products/detail/lsi-csi/LS7084N-S/12760888

 

Here's the datasheet for it : LS7084_LSIComputerSystems.pdf

 

Here's how the chip would be used :

YA4VS.png.cc9b7a0f64d1cc62a7ebe137fbbf0bbb.png

You have 5v , ground,  up/down and clock  ... each time there's a pulse on the clk pin, you can read the signal from the up/down pin and you know the encoder went up or down  / left or right  ... and if you measure the ms / cpu cycles between clock signals you can estimate the rotation speed of the encoder.

 

 

page1.thumb.png.e64c118f398599429ba65554fb5a851c.png

Link to comment
Share on other sites

Link to post
Share on other sites

It's not my thread but the question is highly related so let me ask this:

The wheel in my mouse is an incremental optical encoder? 😮

 

What are the advantages and disadvantages of these methods?

ಠ_ಠ

Link to comment
Share on other sites

Link to post
Share on other sites

The wheel of your mouse has some holes in it of a certain shape.

image.png.820587eaf5b4bca05a0505c929ae97d2.png

There's an infrared emitter on one side that sends a beam through the holes of the wheel and on the other side of the wheel, there's a sensor which picks up the infrared beam.

Depending on the position of the wheel there's some amount of infrared beam passing through , and the mouse can figure out the direction of the wheel by looking if the infrared signal increases or decreases.

 

You can see a better explanation in the video below at around 2 minutes into the video, but the whole video is short enough you should watch it whole:

 

 

your encoder works about the same way, a phase turns on before the second phase turns on, and then both turn off after a period of time (a quarter of rotation or something like that).  You can figure out which way it turns, by seeing which phase turns on first, which phase follows the other.

 

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×