View All Posts
read
Want to keep up to date with the latest posts and videos? Subscribe to the newsletter
HELP SUPPORT MY WORK: If you're feeling flush then please stop by Patreon Or you can make a one off donation via ko-fi

Learn how to create a custom magnetic rotary encoder using an AS5048 sensor and a 3D printed enclosure, greatly improving the accuracy and gameplay of an Asteroids game controller.

Related Content
Transcript

[0:01] Hey Everyone,
[0:03] In this video, we’re going to be looking at an alternative option to the mechanical rotary encoder I’m using in my Asteroids game controller.
[0:10] The encoder I’m using is pretty good but the resolution it provides is pretty low.
[0:16] We’ve only got 20 steps which translates to 18 degrees per step.
[0:21] Fortunately, I’ve been given a magnetic rotary sensor - an AS5048.
[0:27] I’ve got the SPI version - the AS5048A, but there’s also an I2C version available which is called the AS5048B.
[0:37] This has a potential resolution of 14bits which translates to 0.0219 degrees.
[0:44] Possibly overkill for our use case… but we’ll see what kind of accuracy we can actually get
[0:50] In this video we’ll:
[0:52] Solder up the breakout board of the AS5048.
[0:56] Take apart one of my mechanical rotary encoders for parts
[1:00] Design and 3D print an enclosure for the new components
[1:03] And then assemble our new component and then see how well it works.
[1:07] Let’s get on with the show.
[1:08] First step is to solder up the breakout board.
[1:13] If you’ve done this kind of thing before then you might want to skip ahead to taking apart
[1:16] the rotary encoder as that is pretty interesting.
[1:21] There’s a nice trick to soldering the headers to these boards.
[1:24] A lot of people struggle to get them nice and straight and end up either with wonky headers or having to do rework.
[1:32] What I do is use a bit of breadboard.
[1:34] Put the header pins in the breadboard and then sit the board on top of the pins.
[1:39] This keeps everything nice and square and makes soldering pretty straightforward.
[1:59] We end up with a nicely soldered board with good straight header pins that will fit nicely into breadboard.
[2:09] With the board soldered up, we now need to work out how we’re going to actually build our own rotary encoder.
[2:17] I was struggling to think about how I could build the shaft and bearing part of the component
[2:21] and then realised that I had a big bag of rotary encoders that I could try and cannibalise for parts.
[2:28] Let’s take one of these apart and we’ll see how it works and what we can re-use.
[2:33] The first step is to get it off the circuit board.
[2:47] Once we’ve got it free from the circuit board, dissassembly is simple a case of lifting up
[2:51] the tabs on this metal retainer without stabbing yourself with the screwdriver.
[3:05] With the retainer off, we can see what’s inside.
[3:08] It’s pretty interesting!
[3:10] Looking in detail at the bottom part we’ve got the press button - this is a pretty standard clicky button type thing.
[3:18] And we have two brushes.
[3:20] These are actually broken into two parts an inner and an outer.
[3:26] Looking at the contact wheel we can see that the inner part of the brushes will make contact
[3:31] with the solid common part of the inner wheel.
[3:34] The outside part of the brushes will make contact with the switching part of the wheel.
[3:38] You can see why these mechanical rotary encoders needs a lot of debouncing.
[3:44] These brushes will be really noisy.
[3:47] Using our multimeter
[3:49] We can see how the pins are connected.
[3:52] The two inner brushes are connected to the C pin, on the KY-40 this is labelled as GND.
[4:00] The two outer brushes are connected to pins A and B which on the PCB are labelled as CLK and DI.
[4:09] The PCB that was attached to my board has pull resistors to the positive connector for both the outer brushes.
[4:18] If you examine the brushes carefully, you’ll see they are arranged so that
[4:22] when you turn the wheel clockwise the A connector connects before the B connector
[4:27] and when you turn the wheel counter-clockwise the B connector connects before the A connector.
[4:33] You can use this information you can detect the direction the encoder is turning.
[4:38] Looking at the bit we have
[4:39] I’m going to cannibalise the top part of the encoder.
[4:43] I’m supergluing a magnet onto this and that will give us a nice shaft and bearing for our new encoder.
[4:52] We now need something that will hold the shaft over the sensor chip.
[4:56] I’m going to use Fusion360 to design then print a holder.
[5:02] I’ve made a very simple model of the shaft and bearing in Fusion360 along with a model
[5:05] of the PCB and sensor chip.
[5:08] I’ve manually positioned these two components where I think they should go.
[5:13] I’m planning on holding the shaft in place with a friction fit - I’ll probably superglue it in place when it’s assembled,
[5:21] To attach to the PCB I’ll make a simple clip that it will snap into.
[5:27] After a couple of modifications, I’ve got something that works.
[5:31] I’ve decreased the thickness of the PCB clips to make them slightly more flexible and added a small amount of allowance to make it easier to clip in.
[5:40] It now fits in nicely and I think we’ve got our completed component ready to go.
[5:49] I’m actually pretty pleased with how it looks.
[5:51] It looks really nice and professional.
[5:56] Let’s see it in action.
[5:59] I’ve pulled together the code needed to talk to the AS5048A.
[6:03] I did struggle to find an up to date library and ended up hacking together some code from Rob Dobson’s GitHub repository
[6:10] and some code someone else had written for the ESP32.
[6:13] I’ve put my code up on GitHub - a link is in the description.
[6:18] I’m using my HELTEC OLED display and I’ve hooked it up to show the current angle.
[6:24] It seems to work pretty well,
[6:25] definitely much higher resolution than the mechanical rotary encoder.
[6:29] There’s a small amount of noise,
[6:31] some of this will be due to my circuit. I’ve not got any decoupling capacitors at the moment.
[6:35] And some will be due to mechanical noise from my slightly Heath Robinson contraption we’ve built.
[6:42] But, in terms of accuracy, I think it’s pretty good.
[6:46] I think we’re down to at least half a degree of accuracy.
[6:50] And you could improve this by averaging samples.
[6:53] Let’s try and retrofit this component to my Asteroids controller.
[6:59] Because I’m using the same shaft as my original rotary encoder this is a pretty straightforward drop-in replacement.
[7:05] We do however need another two wires to feed the signals back to the console.
[7:10] I’m starting to wonder if I should rebuild this controller and make it wiresless
[7:14] That might be an interesting project. Let me know if we should do this in the comments.
[7:20] I think the gameplay is much improved - we now have really good control over the direction of the ship.
[7:26] Now I just need to practice my game so I don’t die so often!
[7:31] Thanks for watching. I hope you found this video interesting.
[7:34] I’ve got some audio videos in the pipeline.
[7:37] And I’ve also got a Raspberry Pi Pico that I want to have a look at.
[7:40] So don’t forget to subscribe and hit the bell.
[7:43] I’ll see you in the next video!


HELP SUPPORT MY WORK: If you're feeling flush then please stop by Patreon Or you can make a one off donation via ko-fi
Want to keep up to date with the latest posts and videos? Subscribe to the newsletter
Blog Logo

Chris Greening

> Image

atomic14

A collection of slightly mad projects, instructive/educational videos, and generally interesting stuff. Building projects around the Arduino and ESP32 platforms - we'll be exploring AI, Computer Vision, Audio, 3D Printing - it may get a bit eclectic...

View All Posts