Introduction: DIY Rotary Encoder

About: Chemistry and electronics have been a staple in my life since I was 8 and have pretty much been my only hobbies although I have dabbled in herbalism, art, music, and various other areas as well.

Sorry for the lack of pictures, I didn’t decide to do a tutorial on this until after I was almost finished with it.


Rotary encoders use two or more sensors to detect the position, direction of rotation, speed, and number of turns the device has turned. This particular one uses hall effect sensors and magnets. This particular type can be easily waterproofed by either encapsulating the sensors or waterproofing in another way. Hall effect rotary encoders of some flavor are used in some vehicles for both the wheel speed sensor, and the crankshaft position sensor for the engine, and is also used in some anemometers. There are three main types of rotary encoders:

1. Electrical, using conductive tracks and brushes

2. Optical, using a light and sensor

3. Magnetic, using a magnetic sensor of some sort and a magnetic material, such as hall effect sensors and magnets. The actual rotating part could be magnetized as well.

A linear encoder could be made in much the same way as a rotary encoder.

I tested the encoder I made up to ~1500 RPM with the python code on a raspberry pi. A link for the code and schematic will be at the end. The manufacturer specifications on the drill that I used to test it said a maximum speed of 1500 RPM and the speed I got was ~1487 RPM from the encoder both forwards and ~1485 backwards. This could either be from the battery not being fully charged or the bad timing inherent of raspberry pi’s. An arduino would be better to use but the one I had didn’t like 12v on the analog pin haha oops.


1. A rotating thing (I used a chuck from an electric drill)

2. Two or more hall effect sensors (depends on the resolution that you’re aiming for)

3. Four magnets (depends on the resolution that you’re aiming for)

4. Glue

5. Wire (I used a couple of connectors from some broken servos I had)

6. Solder

7. Soldering iron

8. Heat shrink tubing, electrical tape, or other insulating stuff for wires, of your flavor

9. Marking device such as a marker or scriber

Step 1: Glue the Magnets On.

Step 1: Mark equal points around the outside of the rotating part and glue the magnets, in the proper orientation, to these points. It helps to mark the polarity of the magnets. In my case it was every 90 deg (0, 90, 180, and 270 degrees) for a resolution of 4/rotation which was more than plenty for my application, but it may be different for you depending on the resolution you’re shooting for. A good way to figure the spacing out is: (360 degrees/number of magnets) if you’re going by degrees, or (circumference/number of magnets) if you’re going by a measurement. In my case, the detents for the hand grip were already spaced out quite well for my application so I didn’t have to measure anything.

Step 2: Wire the Sensors

Solder wires onto the sensors, insulate, and heat shrink it. Take care not to get the sensor too hot and be sure to test it to see if it still works after you’re finished. Testing it is easy, just hook up the power and hook up an LED to the signal wire. If the LED turns on when a magnet of proper orientation is brought next to it and off when it is pulled away (non-latching type), or the opposite pole of the magnet is applied (latching type), then you’re good to go. The particular sensor I used is non-latching and connects to ground (-) when activated.

Step 3: Mark for the Sensors

Make marks where the sensors should go. For this particular arrangement, this was at 1/16th divisions of the circumference (0, 1/16th). The reason for this is that one sensor has to fire before the other but in a way that allows the controller to distinguish the timing differences between forwards and backwards. I tried it at the 1/8th mark originally but I couldn’t tell which direction it was going because the timing differences were the same. It helps to temporarily tape the sensors down until you get the positioning right, then make the marks. You could do the 1/8th division, you won’t have direction sensing but you’ll have double the resolution. One thing that could be done is use a second set of two sensors offset with 1/8th division spacing on the other side at the 5/16th and 7/16th division from the other sensors to get a resolution of 16 pulses/turn, but I had no need for that good of resolution. A timing demonstration is in the video.

Step 4: Glue the Sensors On

Glue the sensors at the marks and tape them in place until the glue cures. Make sure to leave clearance between the magnets and the sensors so they don’t hit and also make sure the sensors are aligned with the magnets and in the proper orientation. Wait for the glue to dry and you’re done.

To get the schematic and the python code for a raspberry pi to measure rotational speed in RPM, direction of rotation, and number of turns go here, and to get the PDF for this go here or here.

The reason I'm charging for the code is that it took ~4 days to get everything working properly whereas the rest of the project, including all of the documentation, only took ~7hrs (5 of which was the documentation), besides, $1 isn't much and it helps support bigger and more complex projects, in fact, this is the only project I have yet to charge anything for, at the time this was posted of course.