# Thread: Magnetic compass build and code

1. ## Magnetic compass build and code

Hello everyone,

Ive started building a Magnetic compass (see Pics) using a cheap Stepper Motor 28BYJ-48 + ULN2003 Driver (ebay), The stepper is pretty accurate considering its price but there is a small problem regarding the gearbox which causes a deflection of approx <1 degree per stepper revolution which is not a major issue unless you are constantly flying in circles... Yes Ive been lost as well LOL!!

The stepper does 64 half steps per rev making 4075.7728395 steps per gearbox revolution(output spindle)<<< my maths may be off a wee bit here lol.... im still working on the code!!

Im installing a photo interrupter on North(360) so that the compass can be zeroed in the Void setup and am going to use this as a reference/spindle adjustment point each time a heading of 360 is received from LINK2FS, im hoping that this will not be a very visibly noticeable adjustment as a full rotation of the compass shows an approx 7 degree error which can be easily corrected (compass graduations are in 10 degree steps)... not an ideal solution and Ive still to test the theory

Photo interrupter is from an old printer and is basically just a switch that triggers each time the beam is broken between the emitter and collector (but you already knew that)

More to follow on this and hopefully some code very soon,

Doug..

2. ## Re: Magnetic compass build and code

doug, recon you don't really need to gear it up that much, if you use half steps only, on a gear less stepper, you only have to do a modulus 9 calculation then add 1 pulse to compensate. 10 mod 9 = 1.
0.9 deg step every 10 you add one. so 40 deg will need 44 pulses, 44 x 0.9 = 39.6 deg or 45 x 0.9 = 40.5 deg.
100 pulses will do 90 degrees so you have a reference here, and at 180 and 270 ( or -90) so the error shouldn't really accumulate to much over the range.
im going to try a micro switch at the N point to zero it at first , you can then position it for to the first reading, then check for 90,180 -90 positions and then use them as cal points. you could even use the 45 deg points as references, 50 pulses.
Sorry if it sounds a bit random (thats the way i role) just throwing ideas out there.
the other thing is the change over at 0-360 degrees.
you can check if the last reading was > 350 or <10 degrees then add or subtract 360 to the new value that should resolve that problem.
mark
( must not drink espresso when going onto forum!)

3. ## Re: Magnetic compass build and code

Hi Mark,

I have already posted the graphics (pics) with my opening post on this thread, also I am using a stepper that has a built on gearbox (Stepper Motor 28BYJ-4 as it is very cheap...the only draw back with this is the gearing causes it to have errors over several rotations

Im trying to write a sketch that will compensate for this as well as use a known position on the compass card

Did you get my private message regarding the 000-360 degree issue??.... I believe this is no longer an issue as the heading is outputted from Link2FS-multi as 1-360 (please correct me if im wrong)

Im just about to post my sketch (not working yet)

Doug...

4. ## Re: Magnetic compass build and code

Okey Dokey Folks,

Ive been flying in the fog over this for the last couple of days (im still learning to code).... this is what Ive got so far and it doesnt work so well, only rotates very slowly in one direction

You have permission to smirk but laughter is strictly Forbidden lol.... any help much appreciated

#include
CustomStepper stepper(8,9,10,11, (byte[]){8, B1000, B1100, B0100, B0110, B0010, B0011, B0001, B1001}, 4075.7728395, 12, CW);
boolean rotate1 = false;
boolean rotatedeg = false;
boolean crotate = false;
int stepnew;
int Count; // This variable used in loops in the LESSTHAN\ function
int CodeIn;// used on all serial reads
int x;
int compassold;
int compass[3];
String Digit;

void setup()
{
//sets the RPM
stepper.setRPM(12);
//sets the Steps Per Rotation, in this case it is 64 * the 283712/4455 annoying gear ratio
//for my motor (it works with float to be able to deal with these non-integer gear ratios)
stepper.setSPR(4075.7728395);
Serial.begin(115200);
}
void loop() {
if (Serial.available()) { // now lets check the serial buffer for any input data
CodeIn = getChar();
if (CodeIn == '<') {LESSTHAN();}// The first identifier is "<"

} // end of serial available
}// end of void loop

char getChar()// Get a character from the serial buffer
{
while(Serial.available() == 0);// wait for data
}//end of getchar

void LESSTHAN(){ // The first identifier was "<"
//delay (11);
CodeIn = getChar(); // Get another character
switch(CodeIn) {// Now lets find what to do with it

Count = 0;
while (Count < 3 ) // Counting 3 three digits to store as the compass
{ //
Digit = ""; // Clear Digit
Digit += getChar(); // Get a character put it in the String "Digit"
if (Digit ==".") // ****** This looks for the "." *************
{
Digit = ""; // If we find the "." we clear it and get the next digit
Digit += getChar(); // Because we know where the "." always goes.
}
compass[Count] = Digit.toInt(); // Turn String Digit into an Integer and store it in the Array for later
Count++; // add one to the count
x = compass[0,1,2];

}

if (stepper.isDone() && rotatedeg == true && crotate == false)

{
if (x < compassold)stepper.setDirection(CCW);
stepnew = compassold - x;

if (x > compassold)stepper.setDirection(CW);
stepnew = x - compassold;
}

{
stepper.rotateDegrees(stepnew);
rotatedeg = true;
}

stepper.run();

}
compassold = x;
}

Thanks all

Doug...

5. ## Re: Magnetic compass build and code

yeah got the message doug, still think its going to be an issue if your moving from 359 deg to 002 or 002 to 359 for example.
eg x= 359 compassold = 002, 002-359 = -357, but if you add 360 to x , 362-359 = 3.
i may be wrong on this but its stuck in my head now! wheres the nearest wall!
mark

6. ## Re: Magnetic compass build and code

Ahhh I see where you are coming from now Mark... and my cunning plan was to tell the stepper to step X amount of degrees CW or CCW instead of goto X heading... OH WAIT!!! arghhhhhhhhhhh !!!!!!!... Im currently using the wall but will reserve you a space next

Doug..

ARRRRRRRRRGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHH

7. ## Re: Magnetic compass build and code

oh i meant the svg of the art work if youve got it.

8. ## Re: Magnetic compass build and code

Hi Mark,

Sorry if Im being a bit thick buddy... Artwork? SVG? do you mean you would like the file of the compass card?..Ive drawn it on FlexisignPRO vinyl cutting software and it saves as a .FS file (no export to other formats), im happy to send you the file if you want it or you can give me the dimensions you want and I can cut one for you

Doug..

And no you cant have it in Sparkly Purple vinyl...LOL

9. ## Re: Magnetic compass build and code

Hey Guys,
Consider the alternative of using pots to provide compass heading feedback - this design needs far less computations and can't ever get out of sync. Here's a look at this design for a whiskey compass. It uses a cheap 1.5rpm dc gear motor and a dual potentiometer with the mechanical stops removed (180deg out of phase). The motor is coupled to the pot shaft which in turn rotates the compass wheel. I can't take credit for making this, but it's very slick.
compass-pots.jpg

10. ## Re: Magnetic compass build and code

If I'm reading the issue correctly...
If your compass value goes from 0 to 359....

You need a trap
if (x < 0) { x = x + 360;}
if (x > 359) { x = x - 360;}

~Fess

PostScript.... Let me try that again....

Page 1 of 5 12345 Last