Arduino SPI code for TLE5012 Magnetic Encoder not working with ESP32

Hi, I have tested the code below with Arduino NANO and TLE5012 using SPI which worked perfectly. What i was trying to read angle.
But i can't make the same code working with ESP32 even trying different SPI initialization.
Can you please help me with the code so that i can have the correct value coming from TLE5012 with ESP32 using SPI ?
Here is the code:

#include <SPI.h>
int angleValue_command = 0x8021; // Register command für Anglevalue read
int ss = 10;    //Chipselect pin
void setup()
{
  SPI.begin();
  SPI.setBitOrder(MSBFIRST);
  SPI.setDataMode(SPI_MODE0);
  SPI.setClockDivider(SPI_CLOCK_DIV2);  // CLOCK speed einstellen 16MHZ/2 -> 8 MHZ
  Serial.begin(115200);
  pinMode(ss, OUTPUT);    //Chip select as output
  pinMode(SCK, OUTPUT);   //Clock pin as output
  pinMode(MISO, INPUT);   //Set MISO to input
  pinMode(MOSI, OUTPUT);  //Set MOSI to output
}
void loop()
{
  int miso_data = spi_read(angleValue_command);
  if (miso_data < 0) miso_data = abs(miso_data);
  
  // Scale and map the raw angle value to the 0-360 range
  int scaledAngle = map(miso_data, 16384, 0, 0, 360);
  
  //int angle = miso_data * (360/(pow(2, 15)));
  Serial.println(scaledAngle);
}
int spi_read (int read_command)
{
  int data1 = 0;
  int data2 = 0;
  int safety = 0;

  digitalWrite(ss, LOW);      // Chipselect auf low
/*------------ Send request to the slave ------------------------
    Send 16-bit command */
  pinMode(MOSI, OUTPUT);
  SPI.transfer((byte)(read_command >> 8));    // 8 High-Bits to the Slave sent
  delay(0.2);
  SPI.transfer((byte)(read_command & 0x00FF)); // 8 Low-Bits to the Slave sent
  pinMode(MOSI, INPUT);                        // MOSI Pin Switch to high impedance
  delay(0.2);
  /* ende 16 Bit Commands */
  /* 16Bit Data 1 received */

  /*-----------------Response from slave----------------------*/
  data1 = SPI.transfer(0);

  // Für den Fall wenn der Sensor einen 16 Bit Wert zurücksendet ...
  data1 <<= 8;
  data1 |= SPI.transfer(0);

  /* 16Bit Data 2 received */
  data2 = SPI.transfer(0); // Response from slave

  // In case the sensor sends back a 16 bit value...
  data2 <<= 8;
  data2 |= SPI.transfer(0);

  /* 16Bit safety received */
  safety = SPI.transfer(0); // Response from slave
  // In case the sensor sends back a 16 bit value...
  safety <<= 8;
  safety |= SPI.transfer(0);
  int miso = data1;
  digitalWrite(ss, HIGH);     // Chipselect on high
  return miso;
}

Do you have a link to the data sheet?

Why that? Do you have more masters on your SPI bus?

Do you realize that from now on your slave receives random garbage with each data transfer? How can you expect valid data if your slave interprets the garbage as commands?

You must have all SPI settings as required by your slave. E.e. a wrong bit order sends crap to the slave instead of valid commands.

No, only one .
here is the link of the datasheet:

See chapter 5: Interfaces.
I can't find SPI in the supported interfaces.

Try to find an Arduino library from the vendor of your device or return it.

Hi, @saad21
Welcome to the forum.
Thanks for using code tags. :+1:
Thanks for the data sheet.

Can you please post a schematic of your project?

A picture of your project would also help.

Is your sensor on a breakout board or have you just got the IC itself?

Thanks..Tom... :smiley: :+1: :coffee: :australia:

Hi, iam using the ic and the schematic is:

using Arduino SPI library i made it work, what i can't do , making it work with esp32.
that's why needed help on ESP32 Coding with arduino ide.

Hi,
Have you looked at;

Tom... :smiley: :+1: :coffee: :australia:

Yes, but couldn't make it work.
Can you take a look at the code and tell me what changes requires for esp32 ?

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.