Git Product home page Git Product logo

Comments (9)

nseidle avatar nseidle commented on July 3, 2024

Can you post the sketch you're using so we can try to replicate? I imagine it has to do with the time it takes to do the Serial.print statements.

from mma8452_accelerometer.

yududdd avatar yududdd commented on July 3, 2024

Thanks for the response! Sure!
#include <SparkFun_MMA8452Q.h>

/******************************************************************************
MMA8452Q_Basic.ino
SFE_MMA8452Q Library Basic Example Sketch
Jim Lindblom @ SparkFun Electronics
Original Creation Date: June 3, 2014
https://github.com/sparkfun/MMA8452_Accelerometer

This sketch uses the SFE_MMA8452Q library to initialize the
accelerometer, and stream values from it.

Hardware hookup:
Arduino --------------- MMA8452Q Breakout
3.3V --------------- 3.3V
GND --------------- GND
SDA (A4) --/330 Ohm/-- SDA
SCL (A5) --/330 Ohm/-- SCL

The MMA8452Q is a 3.3V max sensor, so you'll need to do some
level-shifting between the Arduino and the breakout. Series
resistors on the SDA and SCL lines should do the trick.

Development environment specifics:
IDE: Arduino 1.0.5
Hardware Platform: Arduino Uno

This code is beerware; if you see me (or any other SparkFun employee) at the
local, and you've found our code helpful, please buy us a round!

Distributed as-is; no warranty is given.
******************************************************************************/
#include <Wire.h> // Must include Wire library for I2C

// Begin using the library by creating an instance of the MMA8452Q
// class. We'll call it "accel". That's what we'll reference from
// here on out.

MMA8452Q accel(0x1D); // Initialize the MMA8452Q with an I2C address of 0x1C (SA0=0)

// The setup function simply starts serial and initializes the
// accelerometer.
void setup()
{
Serial.begin(9600);
Serial.println("MMA8452Q Test Code!");

// Choose your adventure! There are a few options when it comes
// to initializing the MMA8452Q:
// 1. Default init. This will set the accelerometer up
// with a full-scale range of +/-2g, and an output data rate
// of 800 Hz (fastest).
//accel.init();
// 2. Initialize with FULL-SCALE setting. You can set the scale
// using either SCALE_2G, SCALE_4G, or SCALE_8G as the value.
// That'll set the scale to +/-2g, 4g, or 8g respectively.
//accel.init(SCALE_4G); // Uncomment this out if you'd like
// 3. Initialize with FULL-SCALE and DATA RATE setting. If you
// want control over how fast your accelerometer produces
// data use one of the following options in the second param:
// ODR_800, ODR_400, ODR_200, ODR_100, ODR_50, ODR_12,
// ODR_6, or ODR_1.
// Sets to 800, 400, 200, 100, 50, 12.5, 6.25, or 1.56 Hz.
accel.init(SCALE_8G, ODR_800);
}

// The loop function will simply check for new data from the
// accelerometer and print it out if it's available.
void loop()
{
// Use the accel.available() function to wait for new data
// from the accelerometer.
if (accel.available())
{
// First, use accel.read() to read the new variables:
accel.read();
// accel.read() will update two sets of variables.
// * int's x, y, and z will store the signed 12-bit values
// read out of the accelerometer.
// * floats cx, cy, and cz will store the calculated
// acceleration from those 12-bit values. These variables
// are in units of g's.
// Check the two function declarations below for an example
// of how to use these variables.
printCalculatedAccels();
//printAccels(); // Uncomment to print digital readings

// The library also supports the portrait/landscape detection
//  of the MMA8452Q. Check out this function declaration for
//  an example of how to use that.
//printOrientation();

Serial.println(); // Print new line every time.

}
}

// The function demonstrates how to use the accel.x, accel.y and
// accel.z variables.
// Before using these variables you must call the accel.read()
// function!
// This function demonstrates how to use the accel.cx, accel.cy,
// and accel.cz variables.
// Before using these variables you must call the accel.read()
// function!
void printCalculatedAccels()
{
Serial.print(accel.cx, 3);
Serial.print("\t");
Serial.print(accel.cy, 3);
Serial.print("\t");
Serial.print(accel.cz, 3);
Serial.print("\t");
}

from mma8452_accelerometer.

nseidle avatar nseidle commented on July 3, 2024

Ok, your code loads. How are you verifying/testing the output freq?

from mma8452_accelerometer.

nseidle avatar nseidle commented on July 3, 2024

Looks like I can get ~40Hz at 9600 with a RedBoard, ~333Hz to 500Hz at 115200. So my guess is your bottleneck is printing, not the sensor.

Here's my code. It outputs frequency of reading.

https://gist.github.com/nseidle/658996fdf13cd52efd7f3bc941643954

from mma8452_accelerometer.

yududdd avatar yududdd commented on July 3, 2024

The way I am verifying the output frequency is not quite precise and general. Basically, I count the number of data collected within a period of time and get the out frequency. And thank you for your code, but i have a hard time understand your last comment, what do you mean by "bottleneck is printing, not the sensor."

from mma8452_accelerometer.

yududdd avatar yududdd commented on July 3, 2024

and I see you are changing your baud rate, how does that relate to the sampling frequency though?

from mma8452_accelerometer.

nseidle avatar nseidle commented on July 3, 2024

Forgive me but I can't go into more depth. To print the word "hello" takes a certain amount of time. At 9600 bps (bits per second) it will take approximately (1/9600bps * 10 bits per byte * 6 bytes) seconds. Your sketch spends more time printing the values "-0.98 1.21 0.23" then it does reading the accelerometer. Hence "The printing is the bottleneck". I recommend you google a bit and then let me know if you continue to have questions.

from mma8452_accelerometer.

yududdd avatar yududdd commented on July 3, 2024

Thank you so much for your help!

from mma8452_accelerometer.

nseidle avatar nseidle commented on July 3, 2024

No problem! Please keep asking questions when you run into problems.

from mma8452_accelerometer.

Related Issues (10)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.