Enhanced IRSeekerV2 NXT-G Block and NXC Function

Enhanced IRSeekerV2 functionality is now available as both an NXT-G block and as an NXC function. This NXT-G block and NXC function uses a combination of AC and DC modes for improved IR direction determination when used with the HiTechnic IRSeekerV2. This block and function were specifically designed to assist in creating WRO Football Robots.

HiTechnic Enhanced IRSeekerV2 Block (beta)
Enhanced IRSeekerV2 Sample Programs
Enhanced IRSeekerV2 Function (NXC)

The HiTechnic IRSeekerV2 was designed to determine the direction to an IR source such as the HiTechnic Infrared Ball. The sensor supports two modes: AC for a pulsating 1200Hz signal and DC which responds to any IR signal, pulsating or not. It turns out that because the sensor has a high gain on the pulsating signal, necessary when the ball is up to 2 meters away, it suffers from sensor saturation when the ball is very close (less than 10cm away). This sensor saturation can result in direction and strength values that are not correct. Fortunately, DC mode works better when the ball is close by. This block and function takes advantage of that to give a consistent direction and strength value that can handle a range from very close (5 cm) to far away (2 m). Unlike the standard NXT-G IRSeeker Block, this block gives you a single combined strength value that makes it easy to determine if the ball is far away or close by.

The NXT-G Block

The NXT-G Block has four outputs:

IRDirection A value from 0 to 9 based on the chart below.
DegDirection A direction value in degrees. Calculated as follows:
DegDirection = (IRDirection – 5) * 30
Yes/No True if IR signal detected (IRDirection not 0), otherwise False.
Strength A value indicating signal strength.

IRDirection chart:

Note that IRDirection of 0 indicates no signal detected. If there is no signal, then the DegDirection value will be -150.

Here is the sample program to view the Enhanced IRSeekerV2 direction and strength values on the NXT Screen:

This is one of the two sample program that is part of the sample program download above.

If you want to make your robot drive toward the IRBall, one way to do that is to wire the DegDirection value directly with the Steering input of a Move block inside a loop. If the ball is to the left of the robot, then DegDirection will be negative. For example, if IRDirection is 2 then DegDirection will be -90. If you pass in -90 to the Steering input of a Move block then the robot will turn in place to the left. In other words, it will steer toward the IRBall. Here is a simple program to drive the robot toward the IR Ball:

This is the other sample program that is part of the sample program download above.

Note what happens if there is no signal detected. In this case the DegDirection value will be -150 and the robot will simply spin in place. This can be considered as scanning for the IR signal.

The Enhanced IRSeekerV2 NXC Function

The NXC function implements the same algorithm for determining IR direction as the Enhanced IRSeeker V2 block. The entire function can be seen here:

void HTEnhancedIRSeekerV2(const byte  port, int &dir, int &strength)
  int cResp;
  byte cmdBuf[] = {0x10, 0x43};  // Read DC signal strengths
  byte respBuf[];                 // Response Buffer
  bool fSuccess;
  int i, iMax;
  long dcSigSum, dcStr;

  dir = 0;
  strength = 0;

  // Read DC mode
  fSuccess = I2CBytes(port, cmdBuf, cResp, respBuf);
  if (fSuccess) {
    // Find the max DC sig strength
    iMax = 0;
    for (i=1; i<5; i++) if (respBuf[i] > respBuf[iMax]) iMax = i;
    // Calc base DC direction value
    dir = iMax*2+1;
    // Set base dcStrength based on max signal and average
    dcSigSum = respBuf[iMax] + respBuf[5];
    // Check signal strength of neighboring sensor elements
    if ((iMax > 0) && (respBuf[iMax-1] > respBuf[iMax]/2)) {
        dcSigSum += respBuf[iMax-1];
    if ((iMax < 4) && (respBuf[iMax+1] > respBuf[iMax]/2)) {
        dcSigSum += respBuf[iMax+1];
    // Make DC strength compatible with AC strength.
	// use: sqrt(dcSigSum*500)
    dcSigSum *= 500; dcStr = 1;
    repeat(10) dcStr = (dcSigSum/dcStr + dcStr) / 2;  // sqrt
    strength = dcStr;
    // Decide if using DC strength
    if (strength <= 200) {
      // Use AC Dir
      dir = 0; strength = 0;
      cmdBuf[1] = 0x49; // Recycle rest of cmdBuf
      fSuccess = I2CBytes(port, cmdBuf, cResp, respBuf);
      if (fSuccess) {
        dir = respBuf[0];
        // Sum the sensor elements to get strength
        if (dir > 0) for (i=1; i<=5; i++) strength += respBuf[i];

If you download the NXC function using the link above from the downloads page then you will get the function along with a task main that uses the function to display the direction and strength values on the NXT screen.


2 Responses to “Enhanced IRSeekerV2 NXT-G Block and NXC Function”

  1. matthias feicht says:

    I’ve bought a IRseeker.
    on the Hitechnic website I would like to find some more information about the APIs (NXC).
    Could you please publish some more infos about the SetHTIRSeeker2Mode function.
    especially the HiTechnic IRSeeker2 constants

    Matthias Feicht

  2. Gus says:

    The only constants that you need to use are the mode constants: HTIR2_MODE_1200 and HTIR2_MODE_600. The other constants are the I2C registers that the function implementation uses to communicate with the sensor. You don’t need these unless you do your own I2C interactions with the sensor.

    Use SetHTIRSeeker2Mode to set the AC mode of the IRSeeker V2. 1200 and 600 refers to the pulsed signal that the sensor should look for when you use AC mode. If you have a pulsed 1200Hz signal, then the default 1200 Hz mode will work the best. If you have a 600 Hz pulsed signal, then you will want to change the mode of the sensor to HTIR2_MODE_600. If you are looking at the DC direction and signal strength then the 1200 or 600 mode setting will have no effect.

Leave a Reply