Introduction Position Sensor

position sensor is a sensor that facilitates measurement of mechanical position. A position sensor may indicate absolute position (location) or relative position (displacement), in terms of linear travel, rotational angle, or three-dimensional space. 

Comparison Chart

Currently, there are six main types of sensors commonly used for position sensing, including Inductive, Hall Effect, and Magnetic Encoder. Among them, Inductive sensors have a higher threshold and require reference to the PCB’s coil winding. As shown in the figure below, different coil winding methods can result in different waveforms, and general manufacturers do not provide PCB winding support for design and verification. Therefore, Hall Effect and Magnetic Encoder are more widely used due to cost considerations, with Hall Effect being the most common.

AMS Magnetic Encoder Circuit diagram of working principle

Here we introduce Magnetic Encoder with AMS as the main axis. The comparison table below shows that AMS has a shorter response time than Allegro, although it loses in resolution. However, response time is more important in motor control. Another advantage of AMS products is their better ability to resist surrounding magnetic fields, as they use vertical magnetic field changes in contrast to horizontal magnetic fields on one side.

SPI Setting on STM32 and Coding Sample

Here we choose to explain SPI, which can be seen in the datasheet that the first 2 bits of each 16-bit data are defined separately, and the main data is transmitted by the following 14 bits. However, the full data is still transmitted in 16-bit through SPI transmission.

Here, we can see that in the Datasheet, there are separate Read and Write transmissions. We can see that after sending the Read message, there is a time difference until the DATA is received for the next data point.

Here is an example of a read operation.

SPI Read the Data

typedef union {
    uint16_t raw;
    struct __attribute__ ((packed)) {
        uint16_t data:14;
        uint16_t ef:1;
        uint16_t pard:1;
    } values;
} ReadDataFrame;
float getANGLE()

	 ReadDataFrame readDataFrame = readRegister(CMD_ANGLE);
	Angle angle;
	angle.raw =;
	return angle.values.cordicang/16384.*360.;

SPI on Position sensor

ReadDataFrame readRegister(uint16_t registerAddress)
	   CommandFrame command; = READ;
		command.values.commandFrame = registerAddress;
		command.values.parc = isEven(command.raw);

		CommandFrame nopCommand; = READ;
		nopCommand.values.commandFrame = CMD_NOP;
		nopCommand.values.parc = isEven(nopCommand.raw);

		ReadDataFrame receivedFrame;
		receivedFrame.raw = SPI_read(command.raw, nopCommand.raw);
		return receivedFrame;
void writeRegister(uint16_t registerAddress, uint16_t registerValue)
	CommandFrame command; = WRITE;
		command.values.commandFrame = registerAddress;
		command.values.parc = isEven(command.raw);

		WriteDataFrame contentFrame; = registerValue;
		contentFrame.values.low = 0;
		contentFrame.values.pard = isEven(contentFrame.raw);
		SPI_write(command.raw, contentFrame.raw);

Leave a Comment

Your email address will not be published. Required fields are marked *

Shopping Cart