Projects Research & Design Hub

Magnetic Force Awakens

Written by Jeff Bachiochi

Jeff built a system to electronically monitor analog gauges and display their data on an LCD. In this article, after covering the basics of magnetism, he explains how to fix a magnet on a pressure gauge’s needle and start taking PSI readings.

  • How to fix a magnet on a pressure gauge’s needle and take PSI readings.

  • How magnets work

  • How do do field measurements with the AS5601 sensor

  • ams’ AS5601 Magnetic rotary position sensor

  • Mircotech’s PIC18F24K80 Microcontroller

  • SchmartPatch Board from Schartboard

No matter how high-tech things get, there are times when an analog representation just makes more sense. Let’s take a speedometer for example. I’ve seen a lot of attempts to replace the mechanical speedometer with some digital version in a car’s instrument cluster. But I still prefer the old standby. A digital readout, while accurate, does not portray any sense of acceleration like the rate of change you see from the pointer of a mechanical speedometer. The analog mimicry of a linear bar graph might be considered the most sincere form of digital flattery.

While growing up, my family had a weather station on the wall that consisted of three analog meters. These displayed the temperature, humidity, and barometric pressure. You may still have one these, as they last forever with no batteries to wear out. But it’s getting tough to find measurement devices and appliances that aren’t completely digital. Stove timers, digital multimeters, pediatric thermometers, and clocks (some might be blinking “12:00” at you right now) have all been modernized. However, like the speedometer, there are niches where analog meters are still used. We still see needles on tachometers, pressure and vacuum gauges, calipers, some clocks, temperature, and fuel gauges. A gauge’s information can be read off the face at a glance. Wouldn’t it be convenient to be able to monitor this electronically?

I’ll bet with some sophisticated software we could interpret the needle position from a live camera feed. I’m looking to do this for a fraction of the cost and man hours necessary for that object recognition solution. The first use of this technology showed up almost 10 years ago. Refer to my article, “Drive by Wire: A Look at Non-contact Sensing” (Circuit Cellar 220, 2008). That Melexis part output a voltage proportional to rotational position using magnetics. For this project, I used an austriamicrosystems (ams) AM5601 12-bit, on-axis magnetic rotary position sensor. The key is fixing a magnet onto the gauge’s needle. First, let’s make sure you choose the correct magnet.

We learned in school that the Earth is a giant magnet with invisible lines of force generated within its core flowing out through one pole, around its exterior, and back into the other pole. Any man-made or natural magnet allowed to pivot freely will align itself with the Earth’s magnetic field. This discovery enabled the development of the compass, which played a major role in exploration of our world. While a magnetic field is invisible, we can view its effect on a material like iron filings by sprinkling them onto a piece of paper covering a magnetic source.

It wasn’t until much later in my life, when I started to get interested in mapping and topography, that I found the north and south poles (axis of the Earth’s rotation) were not the same as the north and south magnetic poles (magnetic projection). According to National Oceanic and Atmospheric Association (NOAA), over the last 150 years, the north magnetic pole has moved approximately 1,000 miles approaching the true North Pole, while the south magnetic pole has moved half that distance away from the true South Pole.

Neodymium magnets are some of the strongest made by man. A hockey puck-sized neodymium magnet can literally crush your hand if it were placed between two. Therefore, use extreme caution while handling these magnets. While there are many forms of man-made magnets, there are basically two styles and these depend on the way in which they are magnetized. Let’s use the Earth to demonstrate this. Figure 1a is a sphere or the Earth with its magnetic poles shown in blue (north) and south (red). If we were to take the poles and press them together until we had a flattened disk, then the disk would be axially magnetized (see Figure 1b). Alternately, if we pressed opposite sides of the equator together until we had a flattened disk, then the disk would be diametrically magnetized (see Figure 1c).


Advertise Here

Figure 1 
We use the location (a-left) of the Earth’s magnetic poles to demonstrate the difference between a disk magnetized axially (b-center) and a disk magnetized diametrically (c-right).

These two types of magnetized disks produce different field patterns that can be demonstrated using the previously mentioned experiment (see Photo 1). The diametrically magnetized disk will have a field that is concentrated across its diameter extending outwards and wrapping back not only across its diameter but also its circumference. The axially magnetized disk will have a field that is concentrated through the center of its circular surfaces, extending outward and wrapping back to the opposite side passing over its circumference equally on all sides. With this understanding of how a magnetic field can differ in various magnet types, we can now turn our attention to the sensor itself.

Photo 1 
This child’s toy uses a magnetic stylus to paint a facial hair (iron filings) on an ugly puss allows me to demonstrate how a magnet’s invisible lines of force can be shown. The resulting forces can be seen here for two different neodymium disk magnets.

Based on planar Hall sensor technology, the AS5601 measures the perpendicular component of an exposed magnetic field. Figure 2 shows the sensor contains multiple hall sensors in a bridge configuration. The rotational axis of the exterior magnet must pass through the center of the sensor body top to bottom. A rotating diametrically magnetized disk will therefore have its magnetic lines passing perpendicularly through its rotational axis changing orientation as it is rotated. The strength of the exposed field is measured by perpendicular sensors whose outputs will vary as field is rotated in a sine and cosine relationship.

Figure 2 
The AS6501 sensor has both physical outputs and internal registers that can be read via an I2C interface. The device was designed to replace an optical encoder’s quadrature outputs, but angle information can be read digitally as well.

The two analog signals are first amplified and filtered before being converted into binary data by an analog-to-digital converter (ADC). Angle and magnitude computation of the magnetic field vector is handled by the Coordinate Rotation Digital Computer (CORDIC) block. The intensity of the magnetic field can be adjusted by the automatic gain control (AGC) to compensate for temperature and magnetic field variations.

The device has an I2C interface that can be used to configure it for standalone operation. In standalone mode, it can generate quadrature A/B phase output. This enables it to directly replace optical rotational encoders. However, you can also read angle directly through the I2C interface and this is how it will be used in this project. Let’s begin by looking into its register set in Table 1. First are the Status registers. The Magnitude register (0x1B:C) is an indication of signal strength automatically scaled via the AGC amplifier register (0x1A). The three status register (0x0B) bits tell when a magnet is recognized if the field strength is below the minimum or above the maximum level for optimum performance.

Table 1 
The internal registers of the AS5601 are accessible using I2C registered addressing. The Configuration register value can be “burned” once, while the other Configuration registers can be “burned” up to three times (Count in ZMC) using the appropriate bits in the Burn Register. A “burn” is a write to the default nonvolatile register, which is used upon power-up to initialize the configuration.

Next up are the two Output Registers. The RawAngleH:L (0x0C:D) is the 12-bit representation of the present rotational position. The AngleH:L (0x0E:F) is the 12-bit representation of the present rotational position after filtering.

The Configuration Registers contain user-alterable parameters that can be permanently changed by a “burn” command (write to the Burn Register, see ZMCount). The ZMCount register (0x00) is a two bit indicator of the how many times the ZpositionH:L has been written. (The limit is three.) The ZpositionH:L (0x01:2) defaults to zero and is used to shift the reference for the quadrature decoder so it can be aligned with the actual position of the shaft. The ConfigH:L registers (0x7:8) hold many optional parameters for optimizing the device for standalone mode. You can manage the automatic entry to a power down state by enabling the Watchdog function. The Fast and Slow filters can modify the response times to the input signal, while the Hysteresis value prevents jitter on the output while there is no rotation. It is important to verify proper operation after choosing alternate parameters as the last three configuration registers can only be “burned” once. Zposition and the remaining configuration registers have separate “Burn” commands and can be used independently. I won’t be using this “Burn” function since I can easily use the microcontroller to rewrite any parameter that isn’t to my liking.

I hope you now understand that obtaining a diametrically magnetized disk magnet is crucial to this project. Once you have something appropriate, you can begin by disassembling any gauge you wish to monitor. The pressure gauge I have has a plastic front cover that pops off and two screws on the rear allow the gauge guts to be removed (see Photo 2).

Photo 2 
The inside of this gauge shows the circular pressure chamber that will straighten slightly when pressure if applied. A pointer connected by cantilever will show the deflection of the pressure chamber as it relates to pounds per square inch (PSI).

I used super glue to bond the magnetic disk to the pivot point of the needle as close to “centered” as possible (see Photo 3). Then I reassembled the gauge. I had plenty of room between the needle and the cover for the magnet, so there was no interference when I snapped the cover back on. Potentially, this could be a project killer. Alternately, you might find a different internal setup that would allow the magnet and the IC sensor to be attached internally on the rear of the gauge.

Photo 3 
A diametrically magnetized magnet is “super glued” to the pointer’s center of rotation. The sensor will be attached to the outside of the gauge’s face.

I purchased a few Schmartboard SchmartPatch boards for eight-pin SOICs. These are handy for mounting small parts that may be external to main circuitry (see Photo 4). These can be dipped in nonconductive plastic to make them weather resistant. I used a small piece of double sided tape to hold the small board onto the face of the gauge centered over the magnet. The small size of the sensor allows the gauge needle to be seen so the gauge can still be read manually (see Photo 5).


Advertise Here

Photo 4 
This handy protoboard allows the sensor, capacitor, and four-wire interface to be mounted as an external subassembly to the project’s PCB.
Photo 5 
The face of the sensor is attached to the face of the gauge directly over (centered on) the magnet with a small piece of double sided tape.

In Circuit Cellar 307 2016, I presented a circuit that I used to measure air pressure from an I2C pressure sensor. The same circuit is applicable here. I substituted a 4 × 20 character display for the 2 × 16 display but the schematic stays identical. I did add a fourth push button before getting some real prototypes manufactured. (My Circuit Cellar 307 project used a hand-wired prototype.) This common circuit uses four buttons and a 4-bit LCD interface for user I/O. Alternately, you can use the serial connection with either a serial-to-USB adapter or a Bluetooth module for connection as a user I/O device.

The sensor interface is again I2C. (Refer to my Circuit Cellar 307 article for a discussion of the I2C protocol.) Based on what you know about the register set supported by the AS5601, you can probably pick out some registers it would be nice to monitor. Therefore, I set up the LCD to display different screens of information. Take a look at the flow chart of these in Figure 3 based on Mode. You’ll note there are some displays that offer information that is not in the register set, like pressure and scale. These are based on the sensor’s values. If you were paying attention when I discussed the register, you know that the Angle register will present a 12-bit value that represents the rotational angle in bits 0–4095. We can change this to degrees by dividing by 11.37 (i.e., 4095/360 = 11.37). To avoid decimals, I’ll use a factor of 10. So, full scale, 40950/114 = 359.21°. Close enough. My angle display will be in degrees.

Figure 3 
After every sample (and transfer of sensor data), the LCD displays one function. Four used inputs (buttons) allow the Mode or Data to be changed. These are based on the present Mode.  

You’ve probably noticed that most gauges and meters don’t use a full 360° minimum to maximum. We need to provide a scale factor that allows the sensor’s 0–360° to translate to the gauge’s units. In this case, a pressure gauge that registers 0–100 PSI. As you can see back in Photo 3, the gauge’s minimum pressure is 0 PSI. Its maximum pressure is 100 PSI, but this isn’t 360° from 0 PSI. If we look across the gauge (180°) from 0 PSI, we see 70 PSI. If the gauge was allowed to rotate 360°, then the reading would be 140 PSI (70 PSI × 360°/180° = 140 PSI). We’ll use a scale value of 140. By dividing the angle in bits by 140, we can determine that 1 PSI is equal to a value of 29.25. We’ll scale by 10 and use the value 293 to avoid decimals. So, full scale, 40950/293 = 139.76 degrees°. Close enough. Pressure will therefore be: angle (in bits) × 10 /293.

We only have one issue left. Unless we are improbably lucky, the chance that the magnet we’ve glued onto the gauge’s needle will be oriented correctly for the measured angle to be zero is zero! Now we could turn on the circuitry and rotate the sensor until it reads zero with 0 PSI on the gauge and then glue the sensor in place on the gauge’s face. Or we can use an auto zero function to set the present orientation to zero no mater what angle it is reading. This function merely remembers this arbitrary reading and subtracts it from whatever is in the Angle (in bits) register. I call this calculation MyAdjustedAngle. Any further calculations will be based on this value.

Since I won’t be using the “burn” function, I’ll need to save this value so it can be restored should the gauge (circuit) lose power. The Microchip Technology PIC18F24K80 microcontroller, I’m using has plenty of EE storage of which I’ll need just 2 bytes. If you wanted to change any of the default configuration values, these could also be saved and restored as well. At power-up, you must read these values from EEPROM and write them to the sensor’s register set so the sensor’s default values are updated with your preferences.

While the flowchart shows all modes are possible after each reading of the sensor chip, only one mode will be active at a time. Each mode will display four lines of information (see Photo 6). Line 1 shows the present state of that mode’s parameter value. The second line shows the status of the magnet, so you have confidence in the displayed value. The third line instructs you to use a button to determine a selection from the menu presented on the fourth line. The Menu presented on the fourth line is dependent on the present mode. Five menus are possible: Main, Configuration, Data, Auto, and AreYouSure. From a Main menu “Pres Ang Cfg Data,” you can choose (to display) Pressure, Angle, Configuration, or Data. The last two selections actually exit to submenus Configuration or Data.

Photo 6 
The project is now showing 0 PSI after I used the Auto Offset function to create the offset necessary to adjust the sensor’s magnetic position to that of “zero” PSI on the gauge.

The Configuration menu holds data that the user can change as the menu displayed is “Next Inc Dec Exit.” You can use buttons 2 and 3 to change the data shown on line 1. The Exit button returns to the Main menu displaying Pressure. The Next button cycles through the modes 2, 3, and 9, Offset, Scale, and Auto Offset. Modes 9 (and 10) were added to take care of the offset more efficiently. While in mode 2 (Offset), you can step through values of Offset, which is used to determine MyAdjustedAngle, but you have the power to set this value automatically. Mode 9 displays no data. It merely asks if you wish to set this value automatically and uses the Auto menu “Next Yes . Exit.”

Mode 10 (Are You Sure?) is entered if you pressed the Yes button. This special mode is used to verify your choice of Yes, with its own menu “Yes . . Exit.” The Yes button has changed to prevent unwanted alteration because this will permanently change the offset and save the value to EEPROM.

The Data menu has no alterable values but simply presents data on the display with the menu “Next . . Exit.” Buttons 2 and 3 have no function. The Exit button returns to the Main menu displaying Pressure. The Next button cycles through the modes 4, 5, 6, 7, and 8 Raw, Magnet, Field, AGC, and Magnitude.

The AS5601’s SOIC-8 package is easy to use. It is at the limit of what you can expect to solder with a standard iron with a very small tip. You can see the sensor and capacitor mounted on a small SOIC-8 protoboard along with the four interface wires back in Photo 4. The AS5601 requires a 3.3- or 5-V supply. There is an onboard 3.3-V regulator as the internals run at 3.3 V. I/O is rated at the supply voltage. This microcontroller has the unique ability to configure some outputs as open collector. This allows external pull-ups to go to voltages other than the operating voltage of the microcontroller. This makes it possible to interface with external peripherals that use different operating voltages without level shifting circuitry. From the AS5601’s datasheet, you can see that this device is meant for standalone operation as a quadrature encoder.

Magnetism isn’t exactly the basis of the force, nor can it contain a lightsaber’s crystal emissions. However, we can do great things by using this universal force that penetrates and binds all galaxies. While this project centered on pressure gauges, there are many other gauges and meters out there that can benefit from this application. I can think of a number of other configuration parameters that would allow this project to be more universal, like being able to rename some labels like “Pressure” and “PSI.” With just a few changes this project could be universal for any measurement unit from RPMs to temperature. While this project featured an LCD, being able to understand the position of a mechanical gauge and use it to make decisions is the whole point. That’s the reason for having a serial interface in addition to the display. Now you can have access to this information anywhere you need it, wired or wireless. This opens up a galaxy of possibilities. May the force be with you, always. 

ams, “AS5601: 12-Bit Programmable Contactless Encoder,” 2014,
———, “Buy Magnets Online,”
K&J Magnetics, “Axially and Diametrically Magnetized Magnets,” 2013,
NOAA, “Historical Magnetic Declination,” 2015,
Schmartboard, “SchmartPatch Surface Mount Family: 4 Pack of SchmartPatch for SOIC-8,” 203-0005-01,

AS5601 Magnetic rotary position sensor
ams USA, Inc. |
PIC18F24K80 Microcontroller
Microchip Technology |
SchmartPatch Board
Schartboard |


Keep up-to-date with our FREE Weekly Newsletter!

Don't miss out on upcoming issues of Circuit Cellar.


Advertise Here

Note: We’ve made the May 2020 issue of Circuit Cellar available as a free sample issue. In it, you’ll find a rich variety of the kinds of articles and information that exemplify a typical issue of the current magazine.

Would you like to write for Circuit Cellar? We are always accepting articles/posts from the technical community. Get in touch with us and let's discuss your ideas.

Website | + posts

Jeff Bachiochi (pronounced BAH-key-AH-key) has been writing for Circuit Cellar since 1988. His background includes product design and manufacturing. You can reach him at: or at:

Sponsor this Article

Supporting Companies

Upcoming Events

Copyright © KCK Media Corp.
All Rights Reserved

Copyright © 2021 KCK Media Corp.

Magnetic Force Awakens

by Jeff Bachiochi time to read: 14 min