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