Getting randomly peak data from gyroscope

Hello everyone,

I'm trying to collect data from L3GD20H gyroscope sensor using arduino nano and logging with matlab with reading serial port.

I have a problem,

My gyroscope raw data is peaking randomly.

I didn't figure it out what is the problem

anyone can help ?

thanks a lot

#include <Wire.h>
#include <L3G.h>

L3G gyro;

double starttime;
double waittime = 0;
double inittime;


void setup() {
  Serial.begin(115200);
  Wire.begin();

  if (!gyro.init())
  {
    Serial.println("Failed to autodetect gyro type!");
    while (1);
  }

  gyro.writeReg(gyro.CTRL4, 0b00000000); // 00 on bits 5:4 = 250 dps
// Set 95 Hz output data rate and 12.5 Hz cutoff frequency
  gyro.writeReg(gyro.CTRL1, 0b00001111); // Normal mode, all axes, 95 Hz, 12.5 Hz
  inittime = micros();
}

void loop() {
  starttime = micros();
  gyro.read();

  float gx = gyro.g.x * 8.75 / 1000.0;
  float gy = gyro.g.y * 8.75 / 1000.0;
  float gz = gyro.g.z * 8.75 / 1000.0;


  // Print X, Y, Z raw values in columns
  Serial.print(gx);
  Serial.print(",");
  Serial.print(gy);
  Serial.print(",");
  Serial.println(gz);

  waittime = waittime + 10500;
  while (micros() - inittime < waittime) {
  }
}```

If you use micros() this way, then just use delayMicroseconds()

What’s the value of the peak? What happens if you just read the gyro and print the raw values?.


#include <Wire.h>
#include <L3G.h>
L3G gyro;

void setup() {
  Serial.begin(115200);
  Wire.begin();
  if (!gyro.init())  {
    Serial.println("Failed to autodetect gyro type!");
    while (true) yield();
  }

  gyro.writeReg(gyro.CTRL4, 0b00000000); 
  gyro.writeReg(gyro.CTRL1, 0b00001111); 
}

void loop() {
  gyro.read();

  Serial.print(gyro.gx);
  Serial.write(',');
  Serial.print(gyro.gy);
  Serial.write(',');
  Serial.println(gyro.gz);

  delayMicroseconds(10500);
}

Also try their default sketch

If you see weird data, You might want to filter outliers.

Do you have any decoupling capacitors in your circuit?

Best to post a schematic so we can see how things are wired up.

My values are in Scale Factor counts not in radians and my scale factor is setted to 250.
with delayMicroseconds im getting same error.

Sure it’s not the fix. The issue is somewhere else but your code with micros and the maths for the delay did not make much sense.

What happens if you use their default sketch ?

@Grumpy_Mike
I do not have any decoupling capacitors.
I will add the schematic as soon as possible. I'm quite busy right now

@J-M-L
I have already tried it out with different libraries and with their own default codes. The output is same.

Ensure stable power, might indeed help out.

Then that may very well your problem.

When you have time read this tutorial about the problem:-
Decoupling tutorial

You might want to look at this How to get the best out of this forum before you proceed any further.

@Grumpy_Mike
@J-M-L

I'm gonna check the power firstly after that i will add the schematic with decoupling capacitors.

after that i will add the results.

@Grumpy_Mike
@J-M-L

I double check the power everything is fine.

@Grumpy_Mike

Sir, this is the sensor module that i used.

and this is the schematic of the sensor. They added some capacitors thorught te VIN to VDD. Is that enough or not ?
If is not i will add a capactior through VDD and Ground

L3G4200D random spikes - #24 by ritchie888.._gaMTExMzA5ODQyNi4xNzQ1MzA5MjE4*_ga_NEXN8H46L5*MTc0NTMwOTIxNS4xLjAuMTc0NTMwOTIxNS4wLjAuMzEwMjcwNjQz

and in this page, some people are solved this problem with setting all CTRL register's. Maybe problem is the software not hardware.

Thanks a lot

#include <Wire.h>
#include <L3G.h>
L3G gyro;

void setup() {
  Serial.begin(115200);
  Wire.begin();
  if (!gyro.init())  {
    Serial.println("Failed to autodetect gyro type!");
    while (true) yield();
  }

  gyro.writeReg(gyro.CTRL4, 0b00000000); 
  gyro.writeReg(gyro.CTRL1, 0b00001111); 
}

void loop() {
  gyro.read();

  Serial.print(gyro.gx);
  Serial.write(',');
  Serial.print(gyro.gy);
  Serial.write(',');
  Serial.println(gyro.gz);

  delayMicroseconds(10500);
}

Won't that print 95 lines a second scrolling monitor?

I would use a button or key press to trigger collecting X reads then showing them to scroll through.

OTOH maybe only print extreme values and time for each.
That would cut down on printing.