Multiple AS5047P Encoders

Hello,
Does anyone know how to use two AS5047P encoders, wired into the same Arduino Nano 33 IOT?
I am using the: Jonas Merkle AS5047P library and reading a single encoder works fine but I want to swap to the second encoder, on a second motor, and I can't work out how... Does anyone have experience with this, could perhaps suggest a solution I could test?
Below is the example code which works fine for a single encoder:

/**
 * @file BasicReadAngle.ino
 * @author Jonas Merkle [JJM] (jonas@jjm.one)
 * @brief This is a basic example program to read the angle position from a AS5047 rotary encoder.
 *        The angle postion gets updated and printed to the serial consol once a second. 
 * @version 2.2.1
 * @date 2023-07-04
 * 
 * @copyright Copyright (c) 2023 Jonas Merkle. This project is released under the GPL-3.0 License License.
 * 
 * More Information can be found here:
 * https://github.com/jonas-merkle/AS5047P
 */

// include the library for the AS5047P sensor.
#include <AS5047P.h>

// define a led pin.
#define LED_PIN 13

// define the chip select port.
#define AS5047P_CHIP_SELECT_PORT 9 

// define the spi bus speed 
#define AS5047P_CUSTOM_SPI_BUS_SPEED 100000

// initialize a new AS5047P sensor object.
AS5047P as5047p(AS5047P_CHIP_SELECT_PORT, AS5047P_CUSTOM_SPI_BUS_SPEED);

// arduino setup routine
void setup() {

  // set the pinmode of the led pin to output.
  pinMode(LED_PIN, OUTPUT);

  // initialize the serial bus for the communication with your pc.
  Serial.begin(115200);

  // initialize the AS5047P sensor and hold if sensor can't be initialized.
  while (!as5047p.initSPI()) {
    Serial.println(F("Can't connect to the AS5047P sensor! Please check the connection..."));
    delay(5000);
  }

}

// arduino loop routine
void loop() {

  // read the sensor
  digitalWrite(LED_PIN, HIGH);                    // activate the led.
  Serial.print("Angle: ");                        // print some text to the serial consol.
  Serial.println(as5047p.readAngleDegree());      // read the angle value from the AS5047P sensor an print it to the serial consol.
  delay(500);                                     // wait for 500 milli seconds.

  // wait
  digitalWrite(LED_PIN, LOW);                     // deactivate the led.
  delay(500);                                     // wait for 500 milli seconds.

}

thanks for your time,
Matt

Im under the umbrella, but it looks like the device use the SPI bus, and to have multiple units you just need to assign each its own

// define the chip select port.
# define AS5047P_CHIP_SELECT_PORT 9 

which appears to serve the roll of the SPI slave select.

// define the chip select port.
#define CHIP_SELECT_A 9 
#define CHIP_SELECT_B 8 

and say so

// initialize a new AS5047P sensor object.
AS5047P as5047p_A(CHIP_SELECT_A, AS5047P_CUSTOM_SPI_BUS_SPEED);
AS5047P as5047p_B(CHIP_SELECT_B, AS5047P_CUSTOM_SPI_BUS_SPEED);

Here's an article you might familiarize yourself with:

In the rest of the code, talk to whichever one you need to by using the name of the object in the calls, as5047p_A or as5047p_A.

Use better names. More than two, you might think about an array of objects you could index.

Both will need the call to the initSPI() method you see in the setup() function.

That is what I would try, I cannot test this.

HTH

a7

That was a clear explanation and exactly what I needed! Thanks for taking the time alto777

1 Like