Jump to content

Trying to understand a rotary encoder's pinout

Acorn Eye

I'm not entirely clear on this pinout.

 

https://www.mouser.com/datasheet/2/15/EC18A-1370731.pdf (EC18AGB20401 [no 3])

 

Pins 2,4,3,1 are all data outs. Pin 5 is a COM terminal. 

 

There's no VIN and GND.

 

I'm not clear on what a COM terminal is but I believe it acts as the VIN.

 

I'm going to guess the ground is part of the chassis?

 

Reading the encoder would involve comparing outputs of 1,2,3,4 to each other. In the case where none of them output 1 its 0, and when all of them do, it's 225deg.

Link to comment
Share on other sites

Link to post
Share on other sites

30 minutes ago, Acorn Eye said:

I'm not entirely clear on this pinout.

Without firsthand knowledge, here are my thoughts: pin 5 should always be connected to the voltage source (up to 10V, per the spec, but probably 3.3 or 5V depending on your microcontroller), and then you sample the inputs from pins 1-4.

 

The encoder isn't a smart device (i.e. no silicon), so it doesn't have VIN or GND. It's basically a fancy switch changing connections between the pins and COM as you rotate it.

 

On your microcontroller, if you can, set an interrupt on all four of your input pins for rising and falling edge, and run the same code (to handle encoder state change) for any of those four interrupts. Otherwise, sample all four pins periodically (every tenth of a second?) and look for changes.

Main System (Byarlant): Ryzen 7 5800X | Asus B550-Creator ProArt | EK 240mm Basic AIO | 16GB G.Skill DDR4 3200MT/s CAS-14 | XFX Speedster SWFT 210 RX 6600 | Samsung 990 PRO 2TB / Samsung 960 PRO 512GB / 4× Crucial MX500 2TB (RAID-0) | Corsair RM750X | Mellanox ConnectX-3 10G NIC | Inateck USB 3.0 Card | Hyte Y60 Case | Dell U3415W Monitor | Keychron K4 Brown (white backlight)

 

Laptop (Narrative): Lenovo Flex 5 81X20005US | Ryzen 5 4500U | 16GB RAM (soldered) | Vega 6 Graphics | SKHynix P31 1TB NVMe SSD | Intel AX200 Wifi (all-around awesome machine)

 

Proxmox Server (Veda): Ryzen 7 3800XT | AsRock Rack X470D4U | Corsair H80i v2 | 64GB Micron DDR4 ECC 3200MT/s | 4x 10TB WD Whites / 4x 14TB Seagate Exos / 2× Samsung PM963a 960GB SSD | Seasonic Prime Fanless 500W | Intel X540-T2 10G NIC | LSI 9207-8i HBA | Fractal Design Node 804 Case (side panels swapped to show off drives) | VMs: TrueNAS Scale; Ubuntu Server (PiHole/PiVPN/NGINX?); Windows 10 Pro; Ubuntu Server (Apache/MySQL)


Media Center/Video Capture (Jesta Cannon): Ryzen 5 1600X | ASRock B450M Pro4 R2.0 | Noctua NH-L12S | 16GB Crucial DDR4 3200MT/s CAS-22 | EVGA GTX750Ti SC | UMIS NVMe SSD 256GB /

TEAMGROUP MS30 1TB | Corsair CX450M | Viewcast Osprey 260e Video Capture | Mellanox ConnectX-2 10G NIC | LG UH12NS30 BD-ROM | Silverstone Sugo SG-11 Case | Sony XR65A80K

 

Camera: Sony ɑ7II w/ Meike Grip | Sony SEL24240 | Samyang 35mm ƒ/2.8 | Sony SEL50F18F | Sony SEL2870 (kit lens) | PNY Elite Perfomance 512GB SDXC card

 

Network:

Spoiler
                           ┌─────────────── Office/Rack ────────────────────────────────────────────────────────────────────────────┐
Google Fiber Webpass ────── UniFi Security Gateway ─── UniFi Switch 8-60W ─┬─ UniFi Switch Flex XG ═╦═ Veda (Proxmox Virtual Switch)
(500Mbps↑/500Mbps↓)                             UniFi CloudKey Gen2 (PoE) ─┴─ Veda (IPMI)           ╠═ Veda-NAS (HW Passthrough NIC)
╔═══════════════════════════════════════════════════════════════════════════════════════════════════╩═ Narrative (Asus USB 2.5G NIC)
║ ┌────── Closet ──────┐   ┌─────────────── Bedroom ──────────────────────────────────────────────────────┐
╚═ UniFi Switch Flex XG ═╤═ UniFi Switch Flex XG ═╦═ Byarlant
   (PoE)                 │                        ╠═ Narrative (Cable Matters USB-PD 2.5G Ethernet Dongle)
                         │                        ╚═ Jesta Cannon*
                         │ ┌─────────────── Media Center ──────────────────────────────────┐
Notes:                   └─ UniFi Switch 8 ─────────┬─ UniFi Access Point nanoHD (PoE)
═══ is Multi-Gigabit                                ├─ Sony Playstation 4 
─── is Gigabit                                      ├─ Pioneer VSX-S520
* = cable passed to Bedroom from Media Center       ├─ Sony XR65A80K (Google TV)
** = cable passed from Media Center to Bedroom      └─ Work Laptop** (Startech USB-PD Dock)

Retired/Other:

Spoiler

Laptop (Rozen-Zulu): Sony VAIO VPCF13WFX | Core i7-740QM | 8GB Patriot DDR3 | GT 425M | Samsung 850EVO 250GB SSD | Blu-ray Drive | Intel 7260 Wifi (lived a good life, retired with honor)

Testbed/Old Desktop (Kshatriya): Xeon X5470 @ 4.0GHz | ZALMAN CNPS9500 | Gigabyte EP45-UD3L | 8GB Nanya DDR2 400MHz | XFX HD6870 DD | OCZ Vertex 3 Max-IOPS 120GB | Corsair CX430M | HooToo USB 3.0 PCIe Card | Osprey 230 Video Capture | NZXT H230 Case

TrueNAS Server (La Vie en Rose): Xeon E3-1241v3 | Supermicro X10SLL-F | Corsair H60 | 32GB Micron DDR3L ECC 1600MHz | 1x Kingston 16GB SSD / Crucial MX500 500GB

Link to comment
Share on other sites

Link to post
Share on other sites

You have the codes on page 2 of the datasheet

Here's EC18AGB20401

 

It can work both ways, depending on how you configure the microcontroller.

You can connect the COM to ground and send power through the four pins ... when you rotate, the device sinks that pin to ground and the microcontroller detects the drop.

Alternatively, you can connect the COM to voltage and when you rotate, some pins get voltage and your microcontroller detects a digital 1 on the pin.

Traditionally a COM indicates going to ground, and if i remember correctly, from a hardware point of view, it's easier for a microcontroller to "feel" the current being sunk to ground, then to receive voltage in the pin... but you as a microcontroller user shouldn't care about it, it won't affect you in any way, you can use it either way.

 

 

image.png.e4dc05f686919713a9705cf159170ae5.png


Now if you pay attention to the codes, you will see that you have groups of signals that mirror .. first 4 mirror the next 4 and so on

 

 

So you can do something like this :

 

value = bit1  [ 0..1]

if bit2 = 1 then value = 3-value  [ 0, 1, 2, 3 ]

 

so far you have :

0,1,2,3 | 3,2,1,0 | 0,1,2,3 | 3,2,1,0

now you add bit 3 in the mix , when bit 3 is 1, do 7 - value so now you have
 

0,1,2,3 | 3,2,1,0 | 0,1,2,3 | 3,2,1,0

0,0,0,0 | 1,1,1,1 | 1,1,1,1 | 0,0,0,0  =

0,1,2,3 | 4,5,6,7 | 7,6,5,4 | 3,2,1,0

Now add bit 4 in the mix : when bit 4 is 1, do 15-value

 

 0, 1, 2, 3 |  4, 5, 6, 7 |  7, 6, 5, 4 |  3, 2, 1, 0

 0, 0, 0, 0 |  0, 0, 0, 0 |  1, 1, 1, 1 |  1, 1, 1, 1

 0, 1, 2, 3 |  4, 5, 6, 7 |  8, 9,10,11 | 12,13,14,15

 

And that's your sequence in order

 

Oh... and use some resistors in series (between micro pin and knob pin) on each pin, something like 1-100 ohm,  maybe even a tiny capacitor (1-100nF) from pin to ground, as a sort of debouncing and to reduce the amount of current that will instantly jump between contacts in the knob thing (reduce wear on the metal bits inside)

Link to comment
Share on other sites

Link to post
Share on other sites

On 10/12/2019 at 6:52 AM, mariushm said:

You have the codes on page 2 of the datasheet

Here's EC18AGB20401

 

It can work both ways, depending on how you configure the microcontroller.

You can connect the COM to ground and send power through the four pins ... when you rotate, the device sinks that pin to ground and the microcontroller detects the drop.

Alternatively, you can connect the COM to voltage and when you rotate, some pins get voltage and your microcontroller detects a digital 1 on the pin.

Traditionally a COM indicates going to ground, and if i remember correctly, from a hardware point of view, it's easier for a microcontroller to "feel" the current being sunk to ground, then to receive voltage in the pin... but you as a microcontroller user shouldn't care about it, it won't affect you in any way, you can use it either way.

 

 

image.png.e4dc05f686919713a9705cf159170ae5.png


Now if you pay attention to the codes, you will see that you have groups of signals that mirror .. first 4 mirror the next 4 and so on

 

 

So you can do something like this :

 

value = bit1  [ 0..1]

if bit2 = 1 then value = 3-value  [ 0, 1, 2, 3 ]

 

so far you have :


0,1,2,3 | 3,2,1,0 | 0,1,2,3 | 3,2,1,0

now you add bit 3 in the mix , when bit 3 is 1, do 7 - value so now you have
 


0,1,2,3 | 3,2,1,0 | 0,1,2,3 | 3,2,1,0

0,0,0,0 | 1,1,1,1 | 1,1,1,1 | 0,0,0,0  =

0,1,2,3 | 4,5,6,7 | 7,6,5,4 | 3,2,1,0

Now add bit 4 in the mix : when bit 4 is 1, do 15-value

 


 0, 1, 2, 3 |  4, 5, 6, 7 |  7, 6, 5, 4 |  3, 2, 1, 0

 0, 0, 0, 0 |  0, 0, 0, 0 |  1, 1, 1, 1 |  1, 1, 1, 1

 0, 1, 2, 3 |  4, 5, 6, 7 |  8, 9,10,11 | 12,13,14,15

 

And that's your sequence in order

 

Oh... and use some resistors in series (between micro pin and knob pin) on each pin, something like 1-100 ohm,  maybe even a tiny capacitor (1-100nF) from pin to ground, as a sort of debouncing and to reduce the amount of current that will instantly jump between contacts in the knob thing (reduce wear on the metal bits inside)

So I only had 4 100Ω resistors lying around, the only capacitor I have is 330uF, so I tied pin 5 to ground with a 10.2Ω resistor.

image.png.c1c564cd464111a2962d4b20a9b67205.png

Do I need to tie each pin to 5v as well?

Link to comment
Share on other sites

Link to post
Share on other sites

The resistor placement is confusing in your picture.

No, you'd connect the 4 pins to the microcontroller and the 5th pin (COM) to voltage or ground, depending on how you want to read the pin states with your microcontroller. Probably easiest would be to connect 5v to voltage (5v).

You don't need a resistor on the 5th power to limit amount of energy going into the device.

 

 

 

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

×