I'm asking for help in using SPI to obtain measurements of a microforce sensor:
(Datasheet) https://www.farnell.com/datasheets/3676773.pdf
I'm using the 5N one that uses SPI.
Details on that specific sensor communication attached.
I'm struggling with the code. My code aims to read the sent value (for me to use it later to calculate force in Newtons). The sensor does not receive data therefore a MOSI is not required.
I'm using Seeeduino XIAO for this project and I've connected the sensor as shown (pardon my soldering):
#include <SPI.h>
//Using XIAO's D3/A3 pin for the SS/CS of the sensor
const int slaveAPin = 3;
// set up the speed, data order and data mode
// the speed was selected using the datasheet, I'm not so sure about which SPI_MODE to use
//I've tried all SPI_MODEX options and no luck
SPISettings settingsA(100000, MSBFIRST, SPI_MODE0);
void setup() {
// set the Slave Select Pins as outputs:
pinMode (slaveAPin, OUTPUT);
// initialize SPI:
SPI.begin();
// start serial port at 9600 bps, I would like to see the value received from the sensor for //debugging
Serial.begin(9600);
}
//Variable where I'll store value sent by sensor
float val1; //Or shall I use byte?
void loop() {
//Set spi using the settings
SPI.beginTransaction(settingsA);
//As I understood in the pic attached, the sensor works with a falling edge
digitalWrite (slaveAPin, LOW);
// I don't know what value to use in SPI.transfer() function, as the sensor does not receive data
val1 = SPI.transfer(0); //receive sensor data and store in val1
//Finish communication
digitalWrite (slaveAPin, HIGH);
SPI.endTransaction();
Serial.println(val1);
}
Yes, my problem is that I don't know how to read the data from the sensor , any reccommendations/corrections to my code are welcomed I'm new at using SPI communication
Should be exactly the same as any other SPI device - just leave the MOSI disconnected.
EDIT
As it says, "SPI transfer is based on a simultaneous send and receive" - so, even though this sensor doesn't use MOSI, your code will have to send some dummy data just to get the process working. That dummy data won't go anywhere (you're not connecting anything to MOSI), but is needed just to keep things happy.
Yup I have nothing connected at the MOSI pin. I've read through the Arduino SPI reference and followed other tutorials/examples. My code above is the furthest I've gotten but I get 0.00 on the serial output, so either I'm not reading the sensor data, I'm not reading and storing it on the correct variable type or something else, I've tried many things and haven't found a solution. Therefore if anyone has any pointers on what to change in my code i'd really appreciate it.
Nope, which makes me think that something in my code is not working (i.e. I'm not reading the value the sensor is sending via SPI or not handling it properly afterwards, sadly I don't know how to fix it )
include <SPI.h>
// Using XIAO's D3/A3 pin for the SS/CS of the sensor
const int slaveAPin = 3;
// set up the speed, data order and data mode
// the speed was selected using the datasheet, I'm not so sure about which SPI_MODE to use
// I've tried all SPI_MODEX options and no luck
SPISettings settingsA(100000, MSBFIRST, SPI_MODE1);
uint32_t val1;
void setup()
{
// set the Slave Select Pins as outputs:
pinMode (slaveAPin, OUTPUT);
// initialize SPI:
SPI.begin();
// start serial port at 9600 bps, I would like to see the value received from the sensor for
// debugging
Serial.begin(9600);
}
void loop()
{
// the sensor selection works with SS/CS LOW
digitalWrite (slaveAPin, LOW);
// Set SPI using the settings
SPI.beginTransaction(settingsA);
// I don't know what value to use in SPI.transfer() function, as the sensor does not receive data
val1 = SPI.transfer(0); //receive sensor data and store in val1
val1 *= 256;
val1 += SPI.transfer(0);
val1 *= 256;
val1 += SPI.transfer(0); //receive sensor data and store in val1
val1 *= 256;
val1 += SPI.transfer(0); //receive sensor data and store in val1
SPI.endTransaction();
// Finish communication
digitalWrite (slaveAPin, HIGH);
Serial.println(val1, HEX);
}
I've tried the code including awneil's modification, still no luck I've checked sensor connections to the board with my multimeter just in case and everything is fine.
Here's the code I tried:
#include <SPI.h>
// Using XIAO's D3/A3 pin for the SS/CS of the sensor
const int slaveAPin = 3;
// set up the speed, data order and data mode
// the speed was selected using the datasheet, I'm not so sure about which SPI_MODE to use
// I've tried all SPI_MODEX options and no luck
SPISettings settingsA(100000, MSBFIRST, SPI_MODE1);
uint32_t val1;
void setup()
{
// set the Slave Select Pins as outputs:
pinMode (slaveAPin, OUTPUT);
// initialize SPI:
SPI.begin();
// start serial port at 9600 bps, I would like to see the value received from the sensor for
// debugging
Serial.begin(9600);
}
void loop()
{
// the sensor selection works with SS/CS LOW
digitalWrite (slaveAPin, LOW);
// Set SPI using the settings
SPI.beginTransaction(settingsA);
val1 = val1 << 8; // Shift-up 8 bits, ready for the next 8 bits to come into the low byte
SPI.endTransaction();
// Finish communication
digitalWrite (slaveAPin, HIGH);
Serial.println(val1, HEX);
}
include <SPI.h>
// Using XIAO's D3/A3 pin for the SS/CS of the sensor
const int slaveAPin = 3;
// set up the speed, data order and data mode
// the speed was selected using the datasheet, I'm not so sure about which SPI_MODE to use
// I've tried all SPI_MODEX options and no luck
SPISettings settingsA(100000, MSBFIRST, SPI_MODE1);
uint32_t val1;
void setup()
{
// set the Slave Select Pins as outputs:
pinMode (slaveAPin, OUTPUT);
// initialize SPI:
SPI.begin();
// start serial port at 9600 bps, I would like to see the value received from the sensor for
// debugging
Serial.begin(9600);
}
void loop()
{
// the sensor selection works with SS/CS LOW
digitalWrite (slaveAPin, LOW);
// Set SPI using the settings
SPI.beginTransaction(settingsA);
// transfer 4 bytes from SPI
val1 = SPI.transfer(0); // transfer byte 1
val1 *= 256;
val1 += SPI.transfer(0); // transfer byte 2
val1 *= 256;
val1 += SPI.transfer(0); // transfer byte 3
val1 *= 256;
val1 += SPI.transfer(0); // transfer byte 4
SPI.endTransaction();
// Finish communication
digitalWrite (slaveAPin, HIGH);
Serial.println(val1, HEX);
}