SPI Comuniication question

Hi
It maybe a dumb question but i got completely lost here.
I Try to connect a pressure sensor (https://www.first-sensor.com/cms/upload/datasheets/DS_Standard-LDE_E_11815.pdf) and try to communicate with the Arduino to collect the pressure and temperature via the SPI interface.
The sensor have a nice pdf about the SPI communication (https://www.first-sensor.com/cms/upload/appnotes/AN_LDE-LME-SPI-bus_E_11168.pdf).
But now after read it i am completely confuse.

can i use the standard SPI library to operate this sensor?
is there some example of how to communicate with this type of device (which i understand have to get a request for the data i want to read ) and then read it?

thank you

hathatool:
Hi
It maybe a dumb question but i got completely lost here.
I Try to connect a pressure sensor (https://www.first-sensor.com/cms/upload/datasheets/DS_Standard-LDE_E_11815.pdf) and try to communicate with the Arduino to collect the pressure and temperature via the SPI interface.
The sensor have a nice pdf about the SPI communication (https://www.first-sensor.com/cms/upload/appnotes/AN_LDE-LME-SPI-bus_E_11168.pdf).
But now after read it i am completely confuse.

can i use the standard SPI library to operate this sensor?
is there some example of how to communicate with this type of device (which i understand have to get a request for the data i want to read ) and then read it?

thank you

I took the liberty to re-write the code from the pdf to that its is Arduino IDE compatible. (Compiles but NOT tested!)

/**SPI bus communication with LDE/LME pressure sensors

  This sample code provides a very generic example of code that can be used to implement the

  pressure reading operation described in https://www.first-sensor.com/cms/upload/appnotes/AN_LDE-LME-SPI-bus_E_11168.pdf
**/

//SPI library to include (This example uses the Hardware SPI)
#include "SPI.h"

//Constant for Chip select pin used
const uint8_t CS = 10;

//Constants for commands to be sent to the LDE/LME device.
const uint8_t READ_LDE = 0x2D;

const uint8_t SEND2DHR = 0x14;

const uint8_t READ_DHR = 0x98;

// Set the scale factor forthe LDE/LME device in question
//(see the LDE/LME datasheet for more information on scale factors).
//As an example, let's suppose we're using an LDES250UF6S device.
const float SCALE_FACTOR = 120.0;

void setup() {
  //Initialise Serial UART communication
  Serial.begin(115200);

  //Initialise SPI communication
  SPI.begin();

  //Initialise Chip Select pin
  pinMode(CS, OUTPUT);
  digitalWrite(SS, HIGH);

  Serial.println("LDE/LME Pressure Sensor SPI Example");

}

// The main program loop continuously gets a pressure measurement.
void loop() {

  // Create a buffer to read the result into.

  uint16_t val;

  // Send the command to poll the current pressure reading.
  writeByte(READ_LDE);

  // Send the command to send the pressure reading to the data register.
  writeByte(SEND2DHR);

  // Send the command to read the data register.
  writeByte(READ_DHR);

  // Read the first byte back from the LDE/LME sensor.
  val = readByte();

  // Shift this byte up.
  val <<= 8;

  // Read the second byte back from the LDE/LME sensor.
  val |= readByte();

  // Get the value of the pressure measurement in Pascals.
  float result = val / SCALE_FACTOR;

  //print result to serial Monitor
  Serial.print("Pressure Read(Pa): ");
  Serial.println(result, 3);

  //loop around for a new read ever second
  delay(1000);
}

// Send one byte to the sensor.
void writeByte(uint8_t toSend) {
  // Drive the CS pin low to enable communication with the slave.
  digitalWrite(CS, LOW);

  //Send the byte to sensor
  SPI.transfer(toSend);

  // Drive the CS pin high to disable communication with the slave.
  digitalWrite(CS, HIGH);
}

// Read one byte from the sensor.
uint8_t readByte() {

  // Create a variable to store the received byte.
  uint8_t data;

  // Drive the CS pin low to enable communication with the slave.
  digitalWrite(CS, LOW);

  //Read value from sensor
  data = SPI.transfer(0x00);

  // Drive the CS pin high to disable communication with the slave.
  digitalWrite(CS, HIGH);

}

Hope that helps

That device seems to be a fragile little bugger. Looking at the datasheet and application note you should be able to use standard SPI.

While I was look at the documents sherzaad did a nice job at converting the app note code.

Is there a backup plan in case the sensor firmware gets corrupted? Otherwise I would use an oscillsocope to make sure the signals are in line with the datasheet, before you connect the real sensor.

Thank you for your response.
I am trying it now, Hope it will work correctly.
The comments really help me to understand how to do the communication and how it was converted.

just one question why the serial speed is 115200 and not 9600?

Thank you

just one question why the serial speed is 115200 and not 9600?

It's faster.

If you use a real UART (Tx and Rx to a separate USB chip on the board), then using a higher baudrate allows you to send more characters to your Serial Monitor.
If the board uses USB directly from the microcontroller to the PC, then the baudrate setting does not matter. The setting is ignored, but having it in the code makes the example portable.

Good evening
I try the code, but it can not read any data to the 16 bit val
the output is constant and not change for each reading.
the commands transfer right maybe the problen in that function:
// Read one byte from the sensor.
uint8_t readByte() {

// Create a variable to store the received byte.
uint8_t data;

// Drive the CS pin low to enable communication with the slave.
digitalWrite(CS, LOW);

//Read value from sensor
data = SPI.transfer(0x00); //<-Why it is 0x00? how it can read the second part? Why not to read a 16 bit data type?

// Drive the CS pin high to disable communication with the slave.
digitalWrite(CS, HIGH);

data = SPI.transfer(0x00); //<-Why it is 0x00? how it can read the second part? Why not to read a 16 bit data type?

SPI has a master and a slave and they communicate full duplex. While the masters sends a byte the slave sends a byte back.

You have 3 wires for the communication. The rest are optional control. Like chip select CS.

  • Data from master to slave - MOSI - Master Out Slave In
  • Data from slave to master - MISO - Master In Slave Out
  • clock - generated by the master

To read a 16 bit value, just send two 0x00 values and combine them. ( It might be low byte first, high byte second. Have a look into the datasheet.)

digitalWrite(CS, LOW);

dataHighByte = SPI.transfer(0x00);
dataLowByte = SPI.transfer(0x00);

digitalWrite(CS, HIGH);