Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

GY-89(LSM303D) + arduino + SPI

Status
Not open for further replies.

abimann

Member level 4
Joined
Jun 21, 2016
Messages
77
Helped
0
Reputation
0
Reaction score
0
Trophy points
6
Activity points
673
Here is smth wrong with SPI i think. Where it needed to change CPHA,CPOL and etc.. to get stable data, in attachment picture of output acceleration x axis data,when sensor not move ..
I tryed kalman filter and filters inside LSM303D there are too much difference between normal signal and high level noise, so filtering is not a way.

not_stable_data.png
not_stable_data.jpg.

pls help !

some kind of kalman https://www.youtube.com/watch?v=AA1tqkkzmeI
 

Hi,

You need help. Could you please be so kind to give almost complete informations?

Here is smth wrong with SPI i think
Where can we see: SPI code, SPI schematic, SPI scope pictures

Where it needed to change CPHA,CPOL and etc.
Where can we see any informatin about CPHA, CPOL?

in attachment picture of output acceleration x axis data,
Sure you mean X-data?
We don´t see any X nor any Y data information?

I tryed kalman filter
Where can we see this?

... and so on

I really tried to find you what problem you see and what problem you describe.

Klaus
 

Thank you for quick reply, I already tried all SPI mode (SPI_MODE0,1,2,3) , speed, MSB|LSB first settings, but no success :( , I thought it must be mode3 I add peace of datasheet? In attachment result and in a green color i expected result from lSM303D with movement in X-axis, and other file noise from accelerometer which is not move, just stay on my table.


**broken link removed****broken link removed**

Code:
/*
 * Interface Code for Explore Labs 3D Accelerometer + 3D Magnetometer
 * LSM303D Compass Breakout Board and Arduino Uno. This code assumes
 * selected communication method is Serial Peripheral Interface (SPI)
 * and hardware connections are made as follows:
 * 
 * Sensor VIN - Arduino Uno 5V or 3.3V
 * Sensor GND - Arduino Uno GND
 * Sensor SDI/SDA - Arduino Uno Digital Pin 11 or MOSI (Master Out Slave In)
 * Sensor SCK/SCL - Arduino Uno Digital Pin 13 or SCK (Serial Clock)
 * Sensor SDO/ADR - Arduino Uno Digital Pin 12 or MISO (Master In Slave Out)
 * Sensor CS - Arduino Uno Digital Pin 10 or SS (Slave Select or Chip Select)
 */
 
#include <SPI.h>          // Include Arduino SPI library

#define OUT_X_L_M   0x08  // Register addresses from sensor datasheet.
#define OUT_X_H_M   0x09  // Only the registers that are used
#define OUT_Y_L_M   0x0A  // in this program are defined here.
#define OUT_Y_H_M   0x0B
#define OUT_Z_L_M   0x0C
#define OUT_Z_H_M   0x0D
#define CTRL1       0x20
#define CTRL2       0x21
#define CTRL5       0x24
#define CTRL7       0x26
#define OUT_X_L_A   0x28
#define OUT_X_H_A   0x29
#define OUT_Y_L_A   0x2A
#define OUT_Y_H_A   0x2B
#define OUT_Z_L_A   0x2C
#define OUT_Z_H_A   0x2D
    
int8_t readData   = 0x80;
int8_t writeData  = 0x00;

int16_t ax, ay, az;       // 16-bit variables to hold raw data from sensor
int16_t mx, my, mz;
float heading;

const int CS = 10;        // Chip Select pin for SPI
// Some kind of Kalman filter variables
float varVolt = 5.8;  // average deviation (find in excel file)
float varProcess = 0.5; // reaction speed for deviation (manually)
float Pc = 0.0;
float G = 0.0;
float P = 1.0;
float Xp = 0.0;
float Zp = 0.0;
float Xe = 0.0;

int f_ax;
// переменные для калмана
void setup() {
  Serial.begin(115200);
  SPI.begin();
  SPI.beginTransaction (SPISettings (10000000, MSBFIRST, SPI_MODE0));  // already try SPI_MODE0,1,2,3  and changed speed also 
  pinMode(CS, OUTPUT);
  writeReg(CTRL1, 0x37);  // Initialize the sensor by setting
  writeReg(CTRL2, 0xE2);
  writeReg(CTRL5, 0x04);  // control registers
  writeReg(CTRL7, 0x00);  
  delay(100);
}

void loop() {  
  mx = (int16_t) readReg(OUT_X_H_M) <<8 | readReg(OUT_X_L_M); 
  my = (int16_t) readReg(OUT_Y_H_M) <<8 | readReg(OUT_Y_L_M); 
  mz = (int16_t) readReg(OUT_Z_H_M) <<8 | readReg(OUT_Z_L_M);
  ax = (int16_t) readReg(OUT_X_H_A) <<8 | readReg(OUT_X_L_A);
  ay = (int16_t) readReg(OUT_Y_H_A) <<8 | readReg(OUT_Y_L_A);
  az = (int16_t) readReg(OUT_Z_H_A) <<8 | readReg(OUT_Z_L_A);
  
  
  Serial.print(ax); Serial.print("\t");
  Serial.print(f_ax); Serial.print("\t");
  Serial.println();


  f_ax = filter(ax);
  
 delay(100);
}

int8_t readReg(int8_t address) {
  int8_t buffer = 0;
  digitalWrite(CS, LOW); 
  SPI.transfer(readData | address);
  buffer = SPI.transfer(writeData);
  digitalWrite(CS, HIGH);
  return(buffer);  
}

void writeReg(int8_t address, int8_t val) {
  digitalWrite(CS, LOW);
  SPI.transfer(writeData | address);
  SPI.transfer(val);
  digitalWrite(CS, HIGH);
}

float filter(float val) {  /////////////// filtering function
  Pc = P + varProcess;
  G = Pc/(Pc + varVolt);
  P = (1-G)*Pc;
  Xp = Xe;
  Zp = Xp;
  Xe = G*(val-Zp)+Xp; //////////// filtered value
  return(Xe);
}
No_movement.jpgI_expected.jpgdatasheet.png
 

Hi,

Without the requested informations it's impossible to analyze the problem.

Klaus
 

I think SPI wiring has no problem, it communicated with gy-89 through SPI, only when mode1 and 2 used it cannot understand each other.

spi_mode3.pngspi_mode2.pngspi_mode0.pngspi_mode1.png

But data jumping even sensor doesn't move. Plz never mind for red color diagram that kalman filtered data. In a diagram is X-Axis data only. with and filtered one.
 

Hi,

It seems you don't read my posts.

Klaus
 

SPI code ---> 7th May 2019, 04:14, that is Arduino UNO
SPI schematic, ---> information below can be interpreted as a schematic :
* Sensor VIN - Arduino Uno 5V or 3.3V
* Sensor GND - Arduino Uno GND
* Sensor SDI/SDA - Arduino Uno Digital Pin 11 or MOSI (Master Out Slave In)
* Sensor SCK/SCL - Arduino Uno Digital Pin 13 or SCK (Serial Clock)
* Sensor SDO/ADR - Arduino Uno Digital Pin 12 or MISO (Master In Slave Out)
* Sensor CS - Arduino Uno Digital Pin 10 or SS (Slave Select or Chip Select)
SPI scope pictures, ---> Yesterday, 15:53 message above, 4 images with SPI communication analyze that is in 24Million samples per second,right side below you can see what packets are works
CPHA, ---> Yesterday, 15:53 message above, inside a picture , you can see a SPI_MODES0,1,2,3, Diagnostic diagram from Arduino result, also can find logic analyzer spi modes by CPHA,CPOL,
CPOL? ---> above answer
Here is in attachment csv files with data , for all modes.
 

Attachments

  • LA_SPI.rar
    5.7 KB · Views: 94

Hi,

The most important is the power supply:
Now you give new information:
* Sensor VIN - Arduino Uno 5V or 3.3V
There is no "VIN" at the LSM303D.
* Please use the same terminology as in the datasheet to avoid confusions.
* The LSM303D is not designed to operate at 5V. Any voltage at any pin above 4.8V may immediately kill the IC. Please check this.
(Maybe I´m blind, but I can´t find the information in your posts to verify this.)


Then focus on the SPI.

You need to verify:
1) voltage levels HIGH, LOW (and other wavefrom informations like: ringing, rise time, fall time...)
2) timing according datasheet (All values specified in datasheet section: 2.4.1)
3) data / protocol
(Here, too, I can´t verify neither voltage waveform nor timing of the SPI with your given informations.)

Klaus

My personal opinion: If you want us to help you, then you are responsible to give useful informations and (links to) datasheets.
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top