Go Down

Topic: SPI sesnor (Read 1 time) previous topic - next topic

chutki

Can anybody suggest a sensor with an SPI communication interface, that can be attached with DUE (which has commands for interaction) . I would like to learn the SPI with programmable sensors.

ard_newbie


chutki

Hello,

Thanks for the reply. But that sensor is very costly. Is there any simple sensor that can be used for learning purpose, as I am not doing any project ,just for learning.


MorganS

An ADXL345 is fun to use. You can make a spirit level or just measure the acceleration of simple hand movements.

Remember on the Due that the main SPI pins are only available on the "ISP" header in the middle of the board and the power pin in that header is 5V, not 3.3V.
"The problem is in the code you didn't post."

chutki

Hello,

Thanks for the guidance. It was very useful. For SPI pins,I had used the SPI header itself. It worked for me. And I connected to power supply 3.3volts. I had followed instructions and program from this link:

https://forum.arduino.cc/index.php?topic=159313.0

Here is the code:

Code: [Select]
//Add the SPI library so we can communicate with the ADXL345 sensor
#include <SPI.h>

//Assign the Chip Select signal to pin 52.
int CS=10;

//This is a list of some of the registers available on the ADXL345.
//To learn more about these and the rest of the registers on the ADXL345, read the datasheet!
char POWER_CTL = 0x2D;  //Power Control Register
char DATA_FORMAT = 0x31;
char DATAX0 = 0xB2;  //X-Axis Data 0
char DATAX1 = 0xB3; //X-Axis Data 1
char DATAY0 = 0xB4; //Y-Axis Data 0
char DATAY1 = 0xB5; //Y-Axis Data 1
char DATAZ0 = 0xB6; //Z-Axis Data 0
char DATAZ1 = 0xB7; //Z-Axis Data 1

//This buffer will hold values read from the ADXL345 registers.
char values[10];
//These variables will be used to hold the x,y and z axis accelerometer values.
int16_t x,y,z;
byte buff;
void setup(){
  //Initiate an SPI communication instance.
  SPI.begin();
  SPI.setClockDivider(42);  
  //Configure the SPI connection for the ADXL345.
  SPI.setDataMode(SPI_MODE3);
  //Create a serial connection to display the data on the terminal.
  Serial.begin(9600);  
  //Set up the Chip Select pin to be an output from the Arduino.
  pinMode(CS, OUTPUT);
  //Before communication starts, the Chip Select pin needs to be set high.
  digitalWrite(CS, HIGH);
  delay(100);
  digitalWrite(CS, LOW);
  SPI.transfer(DATA_FORMAT);
  SPI.transfer(0x01);
  digitalWrite(CS, HIGH);
  delay(50);  
  digitalWrite(CS, LOW);
  SPI.transfer(POWER_CTL);
  SPI.transfer(0x08);
  digitalWrite(CS, HIGH);
  delay(50);  
}

void loop(){
  digitalWrite(CS, LOW);
  SPI.transfer(DATAX0);
  values[0] = SPI.transfer(0x00);
  digitalWrite(CS, HIGH);
  delay(50);  
//  
  digitalWrite(CS, LOW);
  SPI.transfer(DATAX1);
  values[1] = SPI.transfer(0x00);
  digitalWrite(CS, HIGH);
  delay(50);    
//  
  digitalWrite(CS, LOW);
  SPI.transfer(DATAY0);
  values[2] = SPI.transfer(0x00);
  digitalWrite(CS, HIGH);
  delay(50);  
//  
  digitalWrite(CS, LOW);
  SPI.transfer(DATAY1);
  values[3] = SPI.transfer(0x00);
  digitalWrite(CS, HIGH);
  delay(50);  
//
  digitalWrite(CS, LOW);
  SPI.transfer(DATAZ0);
  values[4] = SPI.transfer(0x00);
  digitalWrite(CS, HIGH);
  delay(50);  
//
  digitalWrite(CS, LOW);
  SPI.transfer(DATAZ1);
  values[5] = SPI.transfer(0x00);
  digitalWrite(CS, HIGH);
  delay(50);  
//  
  //The ADXL345 gives 10-bit acceleration values, but they are stored as bytes (8-bits). To get the full value, two bytes must be combined for each axis.
  //The X value is stored in values[0] and values[1].
  x = (((int)values[1]) <<8) | values[0];
  //The Y value is stored in values[2] and values[3].
  y = (((int)values[3]) <<8) | values[2];
  //The Z value is stored in values[4] and values[5].
  z = (((int)values[5]) <<8) | values[4];
  //Print the results to the terminal.
  Serial.print(x, DEC);
  Serial.print(',');
  Serial.print(y, DEC);
  Serial.print(',');
  Serial.println(z, DEC);  
  delay(1000);
}


My results:

-218,-105,310
-217,-105,310
-78,23,402
-112,89,395
-231,173,319
-146,166,309
-79,61,186
-160,60,191
-150,1,196
-102,-31,307
-125,191,427
-110,12,347
-128,112,248
-209,102,186

My doubt is ,I changed the position of the sensor,but still the z axis value is alwayz +ve and the x-axis value is alwaz -ve. Why is this happening?

MorganS

If you flip it over completely, the Z axis should go to positive.

Quote
Code: [Select]
//Assign the Chip Select signal to pin 52.
int CS=10;
Well, that may be a problem. Which pin did you plug CS into? 52 or 10? It will only work with 10 with the above code.
"The problem is in the code you didn't post."

chutki

Hi,

I made to 10. Just forgot to change in the comment. In hardware also I wired to 10.  I realised one thing, I gave the VCC from 3.3v and I put the ground connection from the SPI header. Is that any problem?

MorganS

No, all the grounds on the Due are interchangeable.
"The problem is in the code you didn't post."

Go Up