DigiKey-eMag-Sensors-Vol 19

Arduino sample code for SPI absolute encoders

ARDUINO UNO PIN

PROPERTY

ENCODER PIN NUMBER

AMT-DBC-1-036

White/green Blue/white White/blue Green/white Orange/white White/orange

+5 V

1

5 V

SCLK

2

13

MOSI

3 4

11

GND

GND

MISO

5

12

CS

6

2

Table 1: Arduino Uno wiring connections further defined. (Image source: Same Sky)

The AMT22 encoder starts transmitting its absolute position data immediately when the SPI communication begins, eliminating the need for a traditional command-response structure. During the first byte of the SPI transfer, the host sends 0x00, and the AMT22 responds simultaneously with valid position data. If the host needs to issue a command (Table 2), such as a zero-setting command, it will be sent in the second byte of the transmission. This is referred to as an extended command. For detailed technical specifics, refer to the AMT22 datasheet. Code tutorial – includes and defines Since the Arduino's SPI bus is being used to interface with the AMT22 encoder, the SPI

COMMAND

BYTE

NOTES

Get Position

0x00 0x00

Single-turn only

Set Zero

0x00 0x70

Multi-turn only

Get Turns

0x00 0xA0

Table 2: AMT22 commands defined. (Image source: Same Sky)

library needs to be included in the code. To send the position data from the Arduino to the computer, the built-in USB-serial connection within the Arduino IDE is utilized, configured at a baud rate of 115200.

In addition, the commands used by the AMT22 need to be defined. Since the encoder doesn't process the content of the first byte, a NOP (no-operation) is assigned to simplify the communication process (Listing 1).

Listing 1: Setting up the SPI interface.

48

Powered by