Search
Filters
Close

HiTechnic NXT Magnetic Sensor

The NXT Magnetic Sensor will enable you to build robots that can detect magnetic fields. The sensor detects magnetic fields that are present around the front of the sensor in a vertical orientation.

Note: This is a special introductory price in our support for the FTC Challenge. The price will go up after the FTC Challenge season is over.

SKU: NMS1035
$22.95
$16.95

HiTechnic NXT Magnetic Sensor for LEGO Mindstorms NXT

The sensor detects magnetic fields when the orientation of the magnet is as shown below.

magnetic sensor

When the orientation of the magnetic field has north uppermost, the reading will increase. If the magnet is presented sideways, it may not be detected.

The Magnetic Sensor connects to an NXT sensor port using a standard NXT wire and uses the analog sensor interface. The sensor can be read up to approximately 300 times per second.

The Magnetic Sensor is housed in a standard Mindstorms sensor housing to match the other Mindstorms elements.

To quick test your new sensor, plug it into port 1 of your NXT brick and select View – Ambient light – Port 1. As you bring a magnet close to the front of the sensor, you'll notice that the readings will change as the magnet gets closer.

Programming

Mindstorms NXT-G

The Magnetic Sensor Block provides access to the sensor output in addition to other features.

  1. The number shows which of your NXT's ports are connected to the Magnetic Sensor. You can change this number in the configuration panel if you need to.
  2. The block's data hub will open automatically when the block is placed in the work area. At least one data wire must be dragged from the block's output plug to another block's data hub. (See the Data Hub section below for more information.)

Configuring the Magnetic Sensor Block

The sensor may be connected directly to an NXT sensor port or via the HiTechnic Sensor Multiplexer (MUX). When configuring the sensor via the multiplexer, there are two port selections to make. The first is the NXT to MUX connection and the second, the MUX to SENSOR connection.

The Magnetic Reading output indicates the relative strength of the magnetic field measured. When the sensor is not in the proximity of a magnetic field, the output should be zero. Due to manufacturing tolerances, temperature and other effects, the output may be a small positive or negative number. To cancel out this zero offset error, also referred to as bias, an offset input can be applied.

Offsetting bias

The simplest way to measure and offset the bias, is to keep the sensor away from magnetic fields and read the output with the Offset value set to zero. This value can then be used in the future as the Offset value which will then cause the output value to be zero when no magnetic field is present.

Other Programming Environments

NXC

NXC is a C like programming language that can access all the features of this sensor.
For more information go to http://bricxcc.sourceforge.net/nbc/.

Example NXC Code

// The Magnet sensor has the same interface as the Gyro Sensor.
// These are temporary macros until native functions are available
// in the official NXC release.
#define SensorHTMagnet(port,offset) SensorHTGyro(port,offset)
#define SetSensorHTMagnet(port)       SetSensorHTGyro(port)

#define MAGNET   IN_1

task main()
{
  int offset, magnetic_value;
  
  SetSensorHTMagnet(MAGNET);

  TextOut(0, LCD_LINE1, "HiTechnic");
  TextOut(0, LCD_LINE2, " Magnetic Sensor");

  TextOut(0, LCD_LINE4, "Calibrating, ");
  TextOut(0, LCD_LINE5, "keep magnets");
  TextOut(0, LCD_LINE6, "away...");
  Wait(1000);
  //Get inital offset assuming no magnetic field is present.
  offset = SensorHTMagnet(MAGNET, 0);
  TextOut(0, LCD_LINE4, "     Value:      ");
  TextOut(0, LCD_LINE5, "                 ");
  TextOut(0, LCD_LINE6, "                 ");

  while(true) {
    magnetic_value = SensorHTMagnet(MAGNET, offset);
    
    TextOut(6*7, LCD_LINE5, "     ");
    NumOut(6*7,LCD_LINE5, magnetic_value);

    Wait(100);
  }
}

HiTechnic NXT Magnetic Sensor for LEGO Mindstorms NXT

The sensor detects magnetic fields when the orientation of the magnet is as shown below.

magnetic sensor

When the orientation of the magnetic field has north uppermost, the reading will increase. If the magnet is presented sideways, it may not be detected.

The Magnetic Sensor connects to an NXT sensor port using a standard NXT wire and uses the analog sensor interface. The sensor can be read up to approximately 300 times per second.

The Magnetic Sensor is housed in a standard Mindstorms sensor housing to match the other Mindstorms elements.

To quick test your new sensor, plug it into port 1 of your NXT brick and select View – Ambient light – Port 1. As you bring a magnet close to the front of the sensor, you'll notice that the readings will change as the magnet gets closer.

Programming

Mindstorms NXT-G

The Magnetic Sensor Block provides access to the sensor output in addition to other features.

  1. The number shows which of your NXT's ports are connected to the Magnetic Sensor. You can change this number in the configuration panel if you need to.
  2. The block's data hub will open automatically when the block is placed in the work area. At least one data wire must be dragged from the block's output plug to another block's data hub. (See the Data Hub section below for more information.)

Configuring the Magnetic Sensor Block

The sensor may be connected directly to an NXT sensor port or via the HiTechnic Sensor Multiplexer (MUX). When configuring the sensor via the multiplexer, there are two port selections to make. The first is the NXT to MUX connection and the second, the MUX to SENSOR connection.

The Magnetic Reading output indicates the relative strength of the magnetic field measured. When the sensor is not in the proximity of a magnetic field, the output should be zero. Due to manufacturing tolerances, temperature and other effects, the output may be a small positive or negative number. To cancel out this zero offset error, also referred to as bias, an offset input can be applied.

Offsetting bias

The simplest way to measure and offset the bias, is to keep the sensor away from magnetic fields and read the output with the Offset value set to zero. This value can then be used in the future as the Offset value which will then cause the output value to be zero when no magnetic field is present.

Other Programming Environments

NXC

NXC is a C like programming language that can access all the features of this sensor.
For more information go to http://bricxcc.sourceforge.net/nbc/.

Example NXC Code

// The Magnet sensor has the same interface as the Gyro Sensor.
// These are temporary macros until native functions are available
// in the official NXC release.
#define SensorHTMagnet(port,offset) SensorHTGyro(port,offset)
#define SetSensorHTMagnet(port)       SetSensorHTGyro(port)

#define MAGNET   IN_1

task main()
{
  int offset, magnetic_value;
  
  SetSensorHTMagnet(MAGNET);

  TextOut(0, LCD_LINE1, "HiTechnic");
  TextOut(0, LCD_LINE2, " Magnetic Sensor");

  TextOut(0, LCD_LINE4, "Calibrating, ");
  TextOut(0, LCD_LINE5, "keep magnets");
  TextOut(0, LCD_LINE6, "away...");
  Wait(1000);
  //Get inital offset assuming no magnetic field is present.
  offset = SensorHTMagnet(MAGNET, 0);
  TextOut(0, LCD_LINE4, "     Value:      ");
  TextOut(0, LCD_LINE5, "                 ");
  TextOut(0, LCD_LINE6, "                 ");

  while(true) {
    magnetic_value = SensorHTMagnet(MAGNET, offset);
    
    TextOut(6*7, LCD_LINE5, "     ");
    NumOut(6*7,LCD_LINE5, magnetic_value);

    Wait(100);
  }
}

The RobotC driver suite supports HiTechnic products for RobotC 4.x and RobotC 3.x. Select the corresponding repository at the link below and download the zip file. 

https://github.com/botbench

HiTechnic Magnetic Sensor Block for Mindstorms NXT Software
The Magnetic Sensor Block adds support for this sensor to the LEGO MINDSTORMS NXT Software.
This block includes support for the HiTechnic Sensor Multiplexer as well as data logging with the LEGO Mindstorms 2.0 Education software.
  Description Version Release Date Size  
NXTIcon HiTechnic Magnetic Sensor Block for Mindstorms NXT Software 1.0a September 22nd, 2010 429.44 KB Download Now
HiTechnic Magnetic Sensor Viewer
This program displays the value from the Magnetic Sensor on the NXT screen.
  Description Version Release Date Size  
NXTIcon HiTechnic Magnetic Sensor Viewer 1.0 September 23rd, 2010 94.96 KB Download Now
FTC Update For LabVIEW 2009
LabVIEW 2009 VIs and sample program VIs for the sensors commonly used by FTC teams. This set includes VIs for the Acceleration, Compass, Gyro, IR Seeker V2, and Magnetic Sensors as well the Touch Sensor Multiplexer. These VIs support the HiTechnic Sensor Multiplexer. See included readme.txt file on how to install into LabVIEW 2009.
  Description Version Release Date Size  
LabView FTC Update For LabVIEW 2009 1.0 October 6th, 2010 261.37 KB Download Now
Magnetic Sensor VI For LabVIEW2009
Magnetic Sensor VI and sample program VI for LabVIEW 2009. Supports the HiTechnic Sensor Multiplexer. See included readme.txt file on how to install into LabVIEW 2009.
  Description Version Release Date Size  
LabView Magnetic Sensor VI For LabVIEW2009 1.0 October 7th, 2010 71.71 KB Download Now
HiTechnic EV3 Analog Sensor Blocks
This is a release of the HiTechnic EV3 blocks for four HiTechnic Analog Sensors: the Gyro Sensor, Force Sensor, and Magnetic Sensor. The zip file also includes sample programs that will display the sensor value on the EV3 screen. Note that this blocks only support the EV3 and that the NXT is not supported.
  Description Version Release Date Size  
EV3Logo HiTechnic EV3 Analog Sensor Blocks 0.1 February 7th, 2014 41.63 KB Download Now