How to communicate with two encoders at same time using SPI Interface?

I am developing a mobile robot using Arduino Mega and I am going to use two AS5147P magnetic encoders to get wheel positions to develop a PID controller for robot.

I need to know is it possible to connect these two encoders to an Arduino mega via SPI Interface to get the wheel position at the same time?

I need to know is it possible to connect these two encoders to an Arduino mega via SPI Interface to get the wheel position at the same time?

The answer is yes, although to be exact "at the same time" is never possible in a single core von Neumann architecture CPU.

IUE:
I need to know is it possible to connect these two encoders to an Arduino mega via SPI Interface

According to page 4 of the datasheet, there is (as expected) a normal SPI active low chip selector, CSn.

So as long as you have physical access to that pin and a vacant pin on the Arduino then the answer should be yes (for some definition of "at the same time" :wink: , see spin-off.)

I wonder if there's a library or if you have to roll your own code to use it? Have you got one working?

Interesting discussion here about using the right kind of magnet (diametric), or standing the wrong kind (axial) on its side.

Who knew?

Now I am trying to communicate with two encoders via SPI interface according to the example program given in the discussion https://forum.arduino.cc/index.php?topic=355345.0

here is the program I modified

#include <SPI.h>

//Set Slave Select Pin
//MOSI, MISO, CLK are handeled automatically
int CSN = 53;
int SO = 50;
int SI = 51;
int CLK = 52 ;
int CSN2 = 49;
unsigned int angle;

void setup() {

Serial.begin(9600);

//Set Pin Modes
pinMode(CSN, OUTPUT);
pinMode(SI, OUTPUT);
pinMode(SO, INPUT);
pinMode(CLK, OUTPUT);
pinMode(CSN2,OUTPUT);
//Set Slave Select High to Start i.e disable chip
digitalWrite(CSN, HIGH);
digitalWrite(CSN2,HIGH);

//Initialize SPI
SPI.begin();
}

void loop() {

SPI.beginTransaction(SPISettings(10000000, MSBFIRST, SPI_MODE1));

//Send the Command Frame
digitalWrite(CSN2, HIGH);

digitalWrite(CSN, LOW);
delayMicroseconds(1);
SPI.transfer16(0xFFFF);
digitalWrite(CSN,HIGH);

//Read data frame
digitalWrite(CSN, LOW);
delayMicroseconds(1);
angle = SPI.transfer16(0xC000);
digitalWrite(CSN, HIGH);
SPI.endTransaction;

angle = (angle & (0x3FFF));

double pos = ( (unsigned long) angle)*360UL/16384UL;

Serial.println(pos);

delay(100);

}

Here, I was just trying to get a reading from one encoder while other one is disable. I connected SI, CLK, & SO commonly and used different CSN pins (49 & 53).

But the problem is I only get the value 0 from the both encoders… I checked the connectivity of the system and there is no issue with the connectivity.

Can anyone help me to solve this matter…