Angle Sensor and analog RPM Gauge in NXT-G

The HiTechnic Angle Sensor can give you three pieces of data regarding the axle input: the angle, accumulated angle and RPM. In this blog post I demonstrate the RPM feature of the sensor and present a program that displays an analog RPM gauge on the NXT screen. The program also allows you to control the motor on port A of the NXT so you can test the gauge using a motor. If you have access to other LEGO motors, such as PF motors, you can use those as well with the Angle sensor.

HiTechnic Angle Sensor Block
Sin/Cos Block (for LEGO Mindstorms 2.0 only)
RPM Gauge Program for LEGO Mindstorms 2.0
Integer Sin/Cos Block (for LEGO Mindstorms 1.0)
RPM Gauge Program for LEGO Mindstorms 1.0

The Angle Sensor

The HiTechnic Angle Sensor has a mechanical axle input and gives you three pieces of information about the rotational position of that axle.

  1. Angle – The non-accumulated angle of the axle, in degrees. This value will always be in the range 0 to 359.
  2. Accumulated Angle – This value, also in degrees, accumulates as the sensor makes multiple rotations. This value is a 32 bit long value so its range is very large (-2,147,483,648 to 2,147,483,647).
  3. RPM – This is the angular velocity of the axle and its units are Rotations per Minute or RPM. Note that is a signed value so for one direction the RPM value will be negative while for the other it will be positive.

One nice feature of the Angle sensor is that the axle input has very low friction. In fact, there is only one moving part in the sensor, the axle shaft itself. The sensor uses a magnet and a magnetic encoder to measure the position and rotational rate of the axle.

Note regarding the magnet in the Angle Sensor: The magnet in the Angle sensor is quite powerful. If you are also using the HiTechnic Compass Sensor, make sure you mount it well away from the Angle sensor.

Attaching the Angle Sensor

Since the axle input on the sensor is in the middle of the sensor cap, it helps to add the following parts in order to get a couple of mounting holes that line up with the axle input:

(Thank you, Philo Hurbain, for your beautiful modelling of the Angle Sensor as an LDraw part!)

To connect directly to an NXT motor, you can do something like this:

If you want to have gears between the NXT motor and the Angle sensor, try something like this:

Note that this same basic technique for mounting the Angle Sensor will work in many different situations, not just with NXT Motors. For example, here the Angle sensor together with a PF Motor:

RPM Gauge Program

Note: that the program snippets below are from the 2.0 version. The 1.0 version is a little different because it can’t take advantage of the floating point math and the Abs Value Function available in the 2.0 software. Note that for the 1.0 version requires the Integer Sin/Cos block while for the 2.0 version requires the regular floating point Sin/Cos Block.

The RPM Gauge is setup so that for every 100 RPM, the needle should move 30 degrees. So the first math that is needed is to calculate the needle angle. Since the RPM value is a signed value, the very first thing is to take the absolute value of the RPM. Next the value is converted to degrees based on the ratio 30/100 (30 degrees for every 100 RPM). Since the 0 position of the needle is pointed off to the right, and we want the 0 position to be 30 degrees below x axis on the left side, we subtract 210 (180 plus 30 more) to set the 0 position in the correct place.

NeedleAngle = |RPM Value| * 30 / 100 – 210

Once we have the Needle Angle, we need to use the SinCos block to calculate the (x, y) point for the end of the needle. Since the center of the gauge is at (50, 26) and the desired needle length is 25, the math works out to this:

X = 50 + 25 * Cos(NeedleAngle)

Y = 26 – 25 * Sin(NeedleAngle)

The blank RPM Gauge is a .ric file and looks like this:

When the first Display block draws the RPM gauge, it will erase the previous needle from the screen. So immediately after the blank gauge is drawn, the new needle is drawn onto the gauge.

Of course, to make it all work, the whole thing is placed in a loop together with a short Wait, which reduces flickering.
The Program also lets you control an NXT Motor attached to Port A so that you can test the RPM Gauge. This is achieved with the lower loop of the program.

Notes on Loading the Programs into the LEGO Software
Both programs require the Angle Sensor block in order to read the RPM value from the Angle Sensor. You will also need to download and install the appropriate Sin/Cos block. If you have the 2.0 software, download and install the standard floating point version of the Sin/Cos block. If you have the 1.0 software, you will need the Integer version of the Sin/Cos block. Both can be found on the .

Opening the 2.0 program will automatically install the RPMGauge.ric file.

For the 1.0 software you will need to manually copy the RPMGauge.ric file into the Pictures folder of the LEGO Mindstorms installation. To do this, you will need to open the file window to the LEGO Mindstorms software. On the PC you will most likely find this either here “C:\Program Files\LEGO Software\” or here “C:\Program Files (x86)\LEGO Software\”. On a Mac there is a similar corresponding folder where the LEGO Mindstorms software is installed. Once you have located the LEGO Mindstorms software installation, you will need to find the Pictures sub-folder. It should be something like this “\LEGO MINDSTORMS NXT\engine\Pictures”. You should find a lot of .ric files there. From the 1.0 RPM Gauge zip file, copy the RPMGauge.ric to this folder.


The HiTechnic Angle Sensor, in addition to being a super accurate, low friction, rotation sensor, is also an accurate RPM sensor. RPM is a way to measure the rate of angular rotation and stands for Rotations Per Minute. The programs presented here demonstrate how to create an analog gauge to display the RPM on the NXT screen. Feel free to modify this gauge for your own projects for wherever you feel an analog gauge is best way (or most fun way!) to present a value.


2 Responses to “Angle Sensor and analog RPM Gauge in NXT-G”

  1. Sean says:

    Thank you so much! I needed this information for a science project. Thanks again!

  2. Gus says:

    You are welcome!

Leave a Reply