HiTechnic recently released the Magnetic Sensor. This sensor is designed to detect the magnetic field of a magnet and return a value that corresponds to the polarity and strength of the field. In this post I will present a simple program to view the sensor value and give some explanation on what kind of values you can expect from the sensor.
The Magnetic Sensor, or perhaps it should be more accurately called the Magnetic Field Sensor, has a sensor element known as a Hall effect sensor. This sensor element is capable of measuring magnetic fields. If there is no magnet near the sensor, then the value will be close to zero. But if a magnet is near the sensor then the value will change depending on the relative orientation of the magnet.
To understand how the Magnetic Sensor gets its value, you need to understand a bit about magnets. Around a magnet there is a magnetic field that can be represented by field lines that run from the north pole of the magnet to the south pole. Where these lines are close together, the magnetic field is stronger and where they are more spaced out the field is weaker. When the Magnetic Sensor is close to a magnet, try to imagine these field lines passing through the sensor on their way from the north pole to the south pole of the magnet. When these lines pass through the sensor vertically, from top to bottom, then the sensor value will be positive.
In this diagram you can see how some of the field lines are going from the north pole to the south pole through the sensor. In this situation the sensor value will be positive.
If you were to reverse the magnet, then you would get a negative value instead.
To experiment with the Magnetic Sensor I create a simple NXT-G program to display the value on the NXT screen. If you want to try this program you will first need to download and install the Magnetic Sensor Block for Mindstorms NXT Software from the downloads page.
You can download a zip file that contains the program here.
Notice that the first thing in the program is that it reads the sensor and then it passes this initial value into the loop to another Magnetic sensor block and uses it as the offset. This is necessary to make the sensor accurate when there is no magnetic field present. If you don’t set the offset is a similar way, you will likely get a small positive or negative value from the sensor even when there is no magnet present.
You can also use the NXC program that you can copy from the bottom of the Magnet Sensor product page.
To give you an idea of what to expect from the sensor, I will give you the results of some simple testing that I did with the sensor. For this testing I used a 1″ rare earth cylinder magnet that I attached to some LEGO pieces using a rubber band. I oriented the magnet so that the north pole is marked with a red beam while the south pole is marked with a black beam. I also created a simple stand so that I could postion the magnet around the sensor.
Here you see the magnet placed similar to the diagram above. In this situation the value was a bit over 200. If I turned the magnet upside down:
Then I got value below -200. I can even put the magnet on either side of the sensor and the value will remain nearly the same since the field lines are still flowing through the sensor from top to bottom.
On the other hand, when I oriented the magnet on its side, like this:
then the value is nearly zero. The reason is that the field lines are now running through the sensor from the side. The sensor only measures the component of the magnetic field that flows vertically through the sensor.
Another way to get a clear and strong reading from the magnet is to place it entirely above like this:
In this situation I still got a value that was well above 200. This orientation is useful because if you move the magnet away from the sensor then the value will still be positive though it will get less as you move it away. When I tested it the value went from about 230 right above the sensor to around 35 when I moved it 2cm away. If I moved it to 4 cm away then the value was nearly zero.
You can also position the magnet entirely below the sensor but the value will be slightly less because internally the sensor element is closer to the top of the sensor than the bottom.
I think you will find the Magnetic Sensor to be a useful addition to our sensor lineup. Use it to find hidden objects or make your robot respond to a magnet that moves under a surface. Or use it as a marker that is easy to detect. Or simply use it to experiment with magnetic fields.
As an additional bonus, the Magnetic Sensor has a low introductory price of $22.95. We are offering the sensor at this price to support the teams participating in FTC. Not involved with FTC? That’s okay. This price is available right now to everyone! After the FTC season is over the price will go up.