Without going into much detail, linear encoder are “simple” ways of knowing a devices position along a linear track. It can be used for many things, but I’m interested in using them to build a cheap 3D printer that uses DC motors (most 3D printer designs I’ve seen so far use stepper motors for accuracy, but DC motors are generally cheaper and can be obtained for free from old broken printers). Using the linear encoder, it’s possible to know how much something has moved AND the direction it’s moved in. So in theory, I can use the linear encoder to know the exact position of the extruder in all 3 dimensions, rather than just “guessing” from the number of steps from the motors.

This guy’s made a really good video covering the theory of linear encoders, but he uses optical encoders from printers. Now I’ve tried the instructions from his video, but I snapped one of the encoder strips and seemed to brake both the encoder IC’s!

I then looked into buying standalone optical encoder strips and chips and didn’t find ANYTHING that would work well together (if anyone has found a good combination, please let me know).

Magnetic encoders

After a bit more research I found out about magnetic encoders that can measure distances down to a few microns.

After even more work, I managed to find a food magnetic encoder IC and strip set that looked like they would work well, but didn’t cost a fortune: the AS5306 and it’s associated magnetic strip, both of which are from ASM. The pole distance of the strip is 1mm, and when used with the encoder chip, gives a resolution of 0.015mm!

Now that is more than enough for what I need, but what holds a lot…

The chip is around $5 and the strip is about $7. Now this isn’t too bad and for all 3 axis, would cost $30, but for 15 micron resolution, I’d be happy to pay that. However, shipping costs are a flat $30, which is just crazy!

Now I can find the IC on RS, but I can’t find the strip anywhere else and this price is just crazy. If anyone knows a local supplier, please let me know.

I was however, able to order a sample of the strip to test out, so at least I have one strip to play with.

Setup

The setup of the encoder is actually quite simple (despite the scary number of pins…). Once I got the IC soldered to a breakout board, I simply needed to connect two pins from the IC to the Arduino. The IC has 3 digital output pins for use: quadrature A; quadrature B; and index. But at this stage, I’m only using the quadrature outputs (the index goes high at every pole change).

Connecting the quadrature pins to the Arduino’s INT0 and INT1 pins seems to be enough at this stage, but I’ll probably use the index pin later.

As you can see below, now much to it.

Basic setup of the magnetic linear encoder

Basic setup of the magnetic linear encoder

Arduino Code

Normally, I code my devices from AVR-GCC, but I do have an Arduino, so put together a very basic sketch that uses the quadrature interrupts and increments (or decrements) a variable holding the current position in mm. The main loop simply prints out this distance value via serial.

I know this isn’t the best way of doing this, but for this basic test it works pretty well.

The code is shown below:

void setup(){
  //start serial connection
  Serial.begin(9600);
  
  pinMode(2, INPUT);
  pinMode(3, INPUT);
  attachInterrupt(1, APulse, CHANGE);
  attachInterrupt(0 , BPulse, CHANGE);
}

// Linear distance in mm
volatile float distance = 0;

void APulse(){
  if (digitalRead(2) == digitalRead(3))
    distance += 0.015;
  else
    distance -= 0.015; 
}

void BPulse(){
  if (digitalRead(2) == digitalRead(3))
    distance -= 0.015;
  else
    distance += 0.015; 
}

void loop(){
  // Print out the distance value
  Serial.println(distance);
}

Test

Using the above setup and code, I was able to run the system and have it accurately give me the distance when I moved the magnetic strip over the IC. However, using the Arduino Uno is going to limit the amount of ICs I can connect. In fact I’m not too sure if the atmega328 would actually have enough i/o pins to control all the motors and the encoders, so might have to look into alternatives soon.

Next Step

The next step is to connect the IC and magnetic strip to a stripped out printer and see if I can control the distance using the Arduino and a simple PID controller.

Advertisements