Skip to main content

Magnetic Encoder

In this tutorial, we will explore how to use a 14-bit magnetic encoder, MT6701 with Aotenjo One.

Prerequisites

In this tutorial, the following hardware is used:

You need a ST-LINK V2 Clone to upload the firmware to the board. You can purchase a cheap clone ST-LINK V2 under $5.

USB to Serial Adapter

Since the Aotenjo One board doesn't have an USB port, you need a USB to Serial adapter to communicate with the board via serial. In this tutorial, we will use a Blackpill STM32 board as a USB to Serial adapter to communicate with the Aotenjo One board, but you can use a cheap USB to Serial adapter or another STM32 board like the Blackpill as an USB to Serial adapter.

tip

You can use the following development boards as a USB to Serial adapter:

  • BlackPill STM32 board
  • BluePill STM32 board
  • ESP32-DevKitC

USB to Serial Adapter Diagram

If you're not sure how to use the BlackPill STM32 board as a USB to Serial adapter, you can refer to the Serial Communication tutorial.

Installation

VScode with PlatformIO

You can install PlatformIO from the VSCode extension marketplace.

STM32CubeProgrammer

You need this to flash the firmware to the board.

Clone the repository

git clone https://github.com/aotenjo-xyz/one.git
cd one

The example code for the LED blink is located in the examples/MT6701 directory, so run the following command if you use linux or macOS to copy the example code to the src directory:

rm src/*
cp examples/MT6701/main.cpp src/
cp examples/MT6701/platformio.ini platformio.ini

For Windows, you can copy the files manually.

Quickstart

  1. Place the magnet on the motor shaft.

    Magnet on Motor Shaft

  2. Connect the Aotenjo One board to the ST-LINK V2 programmer.

    ST-LINK V2 <-> Aotenjo One
    3.3V <---> 3.3V
    GND <---> GND
    SWDIO <---> SWDIO
    SWCLK <---> SWCLK
    RST <---> NRST
    info

    Unlike the blackpill board, you need to connect the NRST pin to the ST-LINK V2 programmer for STM32G4 series.

  3. Connect the ST-LINK V2 programmer to your computer.

  4. Open the project folder in VSCode.

  5. Click on the PlatformIO tab on the left sidebar and click on the Upload button to upload the firmware to the board.

  6. Connect the USB to Serial adapter to the Aotenjo One board as follows:

    Aotenjo One <-> Blackpill STM32
    3.3V <---> 3.3V
    TX <---> PA3 (RX)
    RX <---> PA2 (TX)
    GND <---> GND
    note

    You need to connect TX to RX and RX to TX.

  7. Connect the USB to Serial adapter to your computer using a USB cable like this:

  8. Click on the PlatformIO tab on the left sidebar and click on the Monitor button to open the serial monitor. PlatformIO Monitor

Now you should see the motor’s angle (in radians) and velocity (in radians per second) displayed in the serial monitor. Since the values are in radians, 6.28 rad corresponds to 360 degrees — one full rotation. You can rotate the motor to see if the values change accordingly.

MT6701 Serial Output

References