Introduction: Tim's Electronic Dividers [1D]

About: Retired due to health. Oldish. My background is in Structural Engineering. Also smith of many trades. The majority of my project will be what can be made sat in a chair within arm's reach, on a plotter, 3D pri…

I thought I would do a small tool. A Pair of Electronic Dividers, to show the basic principles of a Hall-Effect Sensor.

I did a little video just to show there use, while I was making the video I thought I would show how I find the poles of the magnets used.

I will be using my 3D Printer to print the parts needed.

  • Other materials can be used, but accuracy is a key factor in the assembly.

I will be using a 49E Sensor with two magnets.

  • The 49E is a small, versatile Linear Hall-Effect Device.

I will be using an Arduino NANO to do the calculations.

  • The AtMega328p only has a 10 bit DAC, calculations use Sine, so there will be limits.

I will be using a 1602 display to show the results.

  • A 1602 display is a 2 line 16 characters per line display.


I may make make some more Instructables using Hall-Effect sensors. A [2D] (Two Dimensional) Tool and a [3D] (Three Dimensional) tool (If all goes well of course).

Supplies

One 49E Linear Hall-Effect Sensor.

Two Neodymium Magnets. Width: 5mm, Depth: 5mm, Height: 2mm. (North/South is in the 2mm direction)

A tool to find the poles of the magnets. I used my phone with an application.

Arduino NANO.

1602 Display with an I2C Interface. (PCF8575 Adapter)

Cable

Some Glue, I use UV resin.

A 3D Printer is required to print the two arms of the dividers.

  • Attached are the STL Files for the Divider Arms.

Step 1: 49E Linear Hall-Effect Sensor

There are many manufacturers of Hall-Effect Sensors, I have attached a data sheet I found from Honeywell.

  • Who ever makes them they are not very different.
  • I have used the cheapest I could find on the web.
  • I have made my plastic parts to fit the 49E type.

The 49E has only 3 connections.

  • Pin 1 = Power (+2.7 to +6.5 volt).
  • Pin 2 = Ground (GND).
  • Pin 3 = Output Analog (Typically between 1 to input - 1 volt)

The output is linear, mV/Gauss.

The data sheet shows the direction it detects the strength of magnetic flux. [N S]

  • If we move the magnet close and far away from the sensor, the output will be relative to the strength of the magnet in the direction shown.

Step 2: Magnets

I am using two neodymium magnet to alter the strength of the magnetic field around the sensor.

  • The size of the magnets are: Width = 5 mm, Depth = 5 mm and Height = 2 mm.
  • The North/South direction is Bottom to Top. (In the 2 mm direction)

Lets look at the magnetic field one magnet would produce.

  • The magnetic field is concentrated in the centre of the magnet.
  • In the centre of the magnet the field is moving South to North, pretty much parallel.
  • As the magnetic field leaves the ends of the magnet, the field comes back on itself returning to the opposite pole.
  • If we was to place the sensor at the end of one pole and move it to one side the strength would decrease dramatically because the magnet field is turning back on its self.

These sensors are very sensitive, any misalignment will give a bad reading on the sensor.

  • We need to reduce the errors that can be created.
  • If we could put the sensor inside the magnet would be ideal.

My idea to reduce the error that could be made, is to use two magnets.

  • If we use two magnets slightly apart from each other, the area between them should create a magnetic field close to what would be inside a magnet.
  • If we place the sensor in-between the magnets, the magnetic field should be uniform in this area and hopefully as the magnets turn around the sensor the magnetic field will be at the same angle as the magnets.

Step 3: Calculating the Angle

We are going to rotate the magnets around the sensor.

  • Rotating magnets around the sensor is not the same as moving them away from the sensor.
  • What I mean is: rotating around the sensor will not give a linear output.
  • The output will give a Sine value of the angle it rotates.

I have done a sketch to try and explain the output.

  • The purple circle is the path the magnets take around the centre where the sensor is.
  • The blue lines represent angles/positions of the magnets as they rotate around the sensor.
  • The yellow lines represent linear strengths the sensor would see South to North.
  • The green line shows the Sine calculation.
  • The red is the accurate zone.

Because the output of the sensor is Analog value, you would think the accuracy is only limited to number of decimal places we can convert Analog reading to.

  • The Arduino NANO DAC is 10 bit.
  • The Arduino NANO double is the same as a float, the float data type has only 6-7 decimal digits of precision.

Because we are rotating and not moving away from the sensor, the output is a Sine calculation of the output.

  • We are going to to have areas that can not be calculated well with only a 10 bit Digital to Analog Converter (DAC)
  • The blue lines with blue circles at the end of them show angles that can be calculated accurately.
  • The blue lines with blue crosses at the end of them show angles that can not be calculated accurately.

You can see that there are more divisions of degrees in the deviations of magnetic force at the extremes of rotation.

I have over emphasized the areas of accuracy to try and show it better.

  • I am using the simplest type of Hal-Effect Sensor.
  • To gain accuracy another sensor could be used. One with another sensor inbuilt at 90 degrees to the first, so only the areas of good accuracy are used on each.

The maths is basic trigonometry, I'll show that when we come to the code.

Step 4: Lets Make a Pair of Dividers

I have mentioned that the sensor is very sensitive, the placing of the components need to be accurate.

  • Luckily I have a 3D Printer, this can print things accurately.

Some small misalignment is acceptable but once in place it should not move unless it is supposed to do.

  • some misalignment can be compensated for in the code.

Step 5: Fit the Sensor

The Sensor needs to be glued into the Sensor Arm of the dividers.

  • The printed part has a shaped hole to orientate the way the sensor should be fitted.

Step 6: Magnet Orientation

The magnet orientation is critical.

To aid finding the magnet orientation I used an application on my phone.

  • The phone needs to be one that has magnetometer inside, usually if it has GPS it will have a magnetometer.
  • There are many applications that use the magnetometer, I find the best ones are the 3D ones.
  • The image shows a typical 3d application. The arrow points in the direction of north.
  • The one I use is: 3D Compass and Magnetometer. by, planecode

The magnetometers in phones are not always in the centre of the phone.

  • It is best to do some experimenting with a magnet to find its location

Once you find where the magnetometer is in your phone, it makes it easier to get the poles of your magnets.

I usually support my phone above my worktop and place the magnet on the worktop underneath the location of the magnetometer.

Step 7: Fitting Magnets and Second Arm Together

It is a little fiddley to fit the magnets and second Arm.

  • Before fitting the two arms together, make sure that the magnets can travel inside the groove of the first arm.
  • needs to be a tight fit in the groove.

You have to hold the magnets in place, ensuring the correct pole orientation, while placing the outer part of the second arm over the first arm.

  • It is a tight fit, the second arm has to expand over the first arm.

Once the two arms are fitted together, the only movement should be: moving the arms together and away from each other.

  • There should be no lateral movement or twist or this will create errors.

Step 8: Add Cable to the Dividers

Next step is to add cables to the sensor you so it can be connected to an Arduino NANO.

  • I have my Arduino NANO on a breakout board, so I have fitted a DuPont female connector to the end of the cable.
  • Check Step 1 for correct connections.

Step 9: Liquid Crystal Display (LCD)

To get feedback from the sensor I am using a 2 Line 16 Character per line LCD.

  • I am using a PCF8575 adapter so I only have to connect to the I2C pins.
  • I have a 4 wire cable fitted with DuPont female connectors to connect this to my Arduino NANO.

Step 10: Connect to Arduino NANO

The Fritzing shows the connections to the Arduino NANO.

Step 11: Code

I assume that if you have an Arduino NANO you have experimented with it and have been to Arduino.cc site to learn things about it.

If this is your first time using a Device with the Arduino Architecture, then first go here: Arduino IDE 2 Tutorials

  • Here you can download the Arduino IDE and there are tutorials from the very people who created Arduino.
  • The tutorial show how to upload a sketch to a device.

Below is the code.

  • I have attached the Sketch "Tims_Electronic_Deviders.ino" so you can download it.
  • When you download the Sketch, you need to put it in a folder with the same name without the ".ino".
/*
	Tims_Electronic_Deviders.ino
	By Tim Jackson.1960

	Creadits:
		Arduino.
		LiquidCrystal_I2C based on work by DFRobot.

	This is code for: Tim's Electronic Deviders.
	I am using 3D Printed Deviders with a 49E Linear Hall Effect Sensor and two Magnets.
	I am using an Arduino NANO to calculate the postion of the deviders from the values recived from the 49E Linear Hall Effect Sensor.
	The sensor is linear: 3 mV/GS, but to get the angle a Sine of the value is calculated.

	S=O/H C=A/H T=O/A
	Degrees to Radians = degrees * (PI / 180)
	Radians to Degrees = radians * 180.0 / pi

*/
#include <Wire.h> 
#include <LiquidCrystal_I2C.h>

#define Hall_49R_Pin					A1		//	define Hall Effect Pin
#define CAL_0							377		//	Value of hall efect sensor at 0 angle
#define CAL_180							690		//	Value of hall efect sensor at 180 angle
#define CAL_RANGE (CAL_180 - CAL_0)				//	Highest CAL - Lowest CAL
#define CAL_RAD ((double)CAL_RANGE / 2)			//	Used in the sine H value to get angle.
#define LEG_LENGTH 99							//	Length of Devider Legs.
#define MAG_BIOS						-3.11	//	Set to zero, then change to a value that will correct the mesurment about 60 Degrees.
double Angle = 0;								//	Variable to hold Angle value.
double Length = 0;								//	Variable to hold Length value.
int SensorValue = CAL_0 + 1;					//	Variable to hold Sensor value.

LiquidCrystal_I2C lcd(0x27, 16, 2);		//20 to	27	//	Set the LCD address to 0x27 for a 16 chars and 2 line display


void setup()
{
	Serial.begin(115200);			//	Start Serial.
	pinMode(Hall_49R_Pin, INPUT);	//	Define as Input.

	lcd.init();						//	Start LCD.
	lcd.backlight();				//	Turn on back light.
	lcd.setCursor(0, 0);			//	Set Cursor at the begining of line 0 (Top Line).
	lcd.print(" Angle:");			//	Display Label for Angle on top line.
	lcd.setCursor(0, 1);			//	Set Cursor at the begining of line 1 (Bottom Line).
	lcd.print("Length:");			//	Display Label for Length on bottom line.

}
void loop() {

	SensorValue = analogRead(Hall_49R_Pin);	//	Read the value from sensor.

	Serial.println(SensorValue);			//	Send value to serial.
	CalcAngle();							//	Do sub routeen for angle.
	CalcLength();							//	Do sub routeen for length.
	Serial.println();						//	Send a new line to serial to seperate values.

	delay(200);								//	Wait a little for things to happen.
}
/*
	Calculate the Angle using Value from Sensor.

		S=O/H
		Radians to Degrees = radians * 180.0 / pi

		O = SensorValue - CAL_0 - CAL_RAD
		H = CAL_RAD
		Angle in radians = asin(O / H)
		Angle in degrees = Angle in radians * 180.0 / pi;

*/
void CalcAngle() {

	double O = (double)SensorValue - CAL_0 - CAL_RAD;
	double H = (double)O / CAL_RAD;
	Angle = 90.0 + (asin(H) * 180.0 / PI);

	Serial.print("Angle ");
	Serial.println(Angle + MAG_BIOS, 4);

	lcd.setCursor(7, 0);
	lcd.print("  ");
	lcd.setCursor(Xpos(Angle + MAG_BIOS), 0);
	lcd.print(Angle + MAG_BIOS, 3);
	lcd.print("  ");

}
/*
	Calculate the Length using degrees calculated from Sensor.

		C=A/H
		A=C*H
		Degrees to Radians = degrees * (PI / 180)

		C = (180 - Angle in degrees) / 2
		H = LEG_LENGTH
		A = C * H
		Length = A * 2

*/
void CalcLength() {

	double _angle = (180.0 - Angle) / 2;
	double _rad = _angle * (PI / 180);
	double C = cos(_rad);
	double H = LEG_LENGTH;
	Length = C * H * 2;

	Serial.print("Length ");
	Serial.println(Length + MAG_BIOS, 4);

	lcd.setCursor(7, 1);
	lcd.print("  ");
	lcd.setCursor(Xpos(Length + MAG_BIOS), 1);
	lcd.print(Length + MAG_BIOS, 3);
	lcd.print("  ");

}
/*
	This a function to calculate the position of value displayed on LCD.

		Check to see if value is hundreds, tens or single.
		To keep numbers alighned.
*/
byte Xpos(byte number) {
	byte val = 8;
	if (number < 100) { val = 9; }
	if (number < 10) { val = 10; }
	return val;
}


Step 12: Calibration

Before calibration the #defined value for MAG_BIOS value needs setting to 0 (zero).

#define MAG_BIOS						-3.11	//	Set to zero, then change to a value that will correct the measurement about 60 Degrees.

To calibrate the dividers you need to have the Serial Monitor running.

  • Close the Dividers and take a note of the first number, the one before/above Angle.
  • Open the Dividers and take a note of the first number, the one before/above Angle. (I find it best to place the Dividers flat on the worktop to get a true 180 degree angle)
  • The number noted with the Dividers open, should be greater than the number noted with Dividers closed.
  • If the number are the wrong way around, then the polarity of the magnets are wrong way around.

Change the #defined values for CAL_0 and CAL_180 according to the noted values.

#define CAL_0							377		//	Value of hall efect sensor at 0 angle
#define CAL_180							690		//	Value of hall efect sensor at 180 angle

Using a protractor set the Dividers to 60 degrees, take a note of the displayed angle.

Using a rule measure the distance between the ends of the Dividers while they are at 60 degrees and take a note.

  • The error of both angle (degrees) and distance (mm) will be similar.

Change the #defined value for MAG_BIOS to compensate for the error.

#define MAG_BIOS						0	//	Set to zero, then change to a value that will correct the measurement about 60 Degrees.

After calibration it will be close, apart from the extremes.

  • 0 to 15 degrees.
  • 165 to 180 degrees.

This is because these areas are where the Sine curve, dose not change very much.

Step 13: How to Get Better Accuracy

This was to show how a simple measuring tool can be made and to show the basics of a Hall-Effect sensor. The 49E is just a single linear Analog sensor.

There are other sensors that can be used like:

  • The AS5600 is an easy to program magnetic rotary position sensor with a high-resolution 12-bit Analog or PWM output. Has an I²C interface.
  •  The MLX90393 sensor offers a 16-bit output proportional to the magnetic flux density sensed along the X, Y, and Z axes. Selectable SPI and I2 C bus protocols.

Using more expensive sensors bring more accuracy.

Step 14: It's a Trilogy

Build a Tool Contest

Participated in the
Build a Tool Contest