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) {
}
}```
J-M-L
April 22, 2025, 8:07am
2
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
/*
The sensor outputs provided by the library are the raw 16-bit values
obtained by concatenating the 8-bit high and low gyro data registers.
They can be converted to units of dps (degrees per second) using the
conversion factors specified in the datasheet for your particular
device and full scale setting (gain).
Example: An L3GD20H gives a gyro X axis reading of 345 with its
default full scale setting of +/- 245 dps. The So specification
in the L3GD20H datasheet (page 10) states a conversion factor of 8.75
mdps/LSB (least significant bit) at this FS setting, so the raw
reading of 345 corresponds to 345 * 8.75 = 3020 mdps = 3.02 dps.
*/
#include <Wire.h>
#include <L3G.h>
L3G gyro;
void setup() {
This file has been truncated. show original
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.
J-M-L
April 22, 2025, 8:21am
5
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.
J-M-L
April 22, 2025, 8:23am
8
Ensure stable power, might indeed help out.
Then that may very well your problem.
hazretipid:
I'm quite busy right now
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
This sensor is a carrier/breakout board for the ST L3GD20H three-axis gyroscope, which measures the angular rates of rotation about the roll (X), pitch (Y), and yaw (Z) axes. Angular velocity measurements with a configurable range of ±245°/s,...
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 .._ga MTExMzA5ODQyNi4xNzQ1MzA5MjE4*_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.