Skip to content

Commit

Permalink
Adds comments to examples
Browse files Browse the repository at this point in the history
  • Loading branch information
edspark committed Aug 11, 2022
1 parent 2a2ccba commit 8b80d2f
Show file tree
Hide file tree
Showing 4 changed files with 169 additions and 25 deletions.
34 changes: 33 additions & 1 deletion examples/example1_basic/example1_basic.ino
Original file line number Diff line number Diff line change
@@ -1,7 +1,31 @@
/*
example1-basic
This example shows the basic settings and functions for retrieving accelerometer
and gyroscopic data.
Please refer to the header file for more possible settings, found here:
..\SparkFun_6DoF_ISM330DHCX_Arduino_Library\src\sfe_ism330dhcx_defs.h
Written by Elias Santistevan @ SparkFun Electronics, August 2022
Product:
https://www.sparkfun.com/products/19764
Repository:
https://github.com/sparkfun/SparkFun_6DoF_ISM330DHCX_Arduino_Library
SparkFun code, firmware, and software is released under the MIT
License (http://opensource.org/licenses/MIT).
*/

#include <Wire.h>
#include "SparkFun_ISM330DHCX.h"

SparkFun_ISM330DHCX myISM;

// Structs for X,Y,Z data
sfe_ism_data_t accelData;
sfe_ism_data_t gyroData;

Expand All @@ -16,8 +40,11 @@ void setup(){
while(1);
}

// Reset the device to default settings. This if helpful is you're doing multiple
// uploads testing different settings.
myISM.deviceReset();

// Wait for it to finish reseting
while( !myISM.getDeviceReset() ){
delay(1);
}
Expand All @@ -29,15 +56,19 @@ void setup(){
myISM.setDeviceConfig();
myISM.setBlockDataUpdate();

// Set the output data rate and precision of the accelerometer
myISM.setAccelDataRate(ISM_XL_ODR_104Hz);
myISM.setAccelFullScale(ISM_4g);

myISM.setGyroFullScale(ISM_500dps);
// Set the output data rate and precision of the gyroscope
myISM.setGyroDataRate(ISM_GY_ODR_104Hz);
myISM.setGyroFullScale(ISM_500dps);

// Turn on the accelerometer's filter and apply settings.
myISM.setAccelFilterLP2();
myISM.setAccelSlopeFilter(ISM_LP_ODR_DIV_100);

// Turn on the gyroscope's filter and apply settings.
myISM.setGyroFilterLP1();
myISM.setGyroLP1Bandwidth(ISM_MEDIUM);

Expand All @@ -46,6 +77,7 @@ void setup(){

void loop(){

// Check if both gyroscope and accelerometer data is available.
if( myISM.checkStatus() ){
myISM.getAccel(&accelData);
myISM.getGyro(&gyroData);
Expand Down
68 changes: 49 additions & 19 deletions examples/example2_interrupt/example2_interrupt.ino
Original file line number Diff line number Diff line change
@@ -1,15 +1,43 @@
/*
example2-interrupt
This example shows the basic settings and functions for retrieving accelerometer
data. In addition we're setting the data ready signal to interrupt pin one in an
active high configuration and show additional ways in which the interrupts
can be configured.
Please refer to the header file for more possible settings, found here:
..\SparkFun_6DoF_ISM330DHCX_Arduino_Library\src\sfe_ism330dhcx_defs.h
Written by Elias Santistevan @ SparkFun Electronics, August 2022
Product:
6DoF: https://www.sparkfun.com/products/19764
9DoF: https://www.sparkfun.com/products/19895
Repository:
https://github.com/sparkfun/SparkFun_6DoF_ISM330DHCX_Arduino_Library
SparkFun code, firmware, and software is released under the MIT
License (http://opensource.org/licenses/MIT).
*/

#include <Wire.h>
#include "SparkFun_ISM330DHCX.h"

// Structs for X,Y,Z data
SparkFun_ISM330DHCX myISM;
sfe_ism_data_t accelData;
sfe_ism_data_t gyroData;


// Interrupt pin
byte interrupt_pin = D1;

void setup(){

// Set the interrupt to INPUT
pinMode(interrupt_pin, INPUT);
Serial.begin(115200);

Expand All @@ -20,8 +48,11 @@ void setup(){
while(1);
}

// Reset the device to default settings. This is helpful if you're doing multiple
// uploads testing different settings.
myISM.deviceReset();

// Wait for it to finish reseting
while( !myISM.getDeviceReset() ){
delay(1);
}
Expand All @@ -33,35 +64,44 @@ void setup(){
myISM.setDeviceConfig();
myISM.setBlockDataUpdate();

// Accelerometer config and enable
// Set the output data rate and precision of the accelerometer
myISM.setAccelDataRate(ISM_XL_ODR_104Hz);
myISM.setAccelFullScale(ISM_4g);

/// Accelerometer filter settings
// Turn on the accelerometer's filter and apply settings.
myISM.setAccelFilterLP2();
myISM.setAccelSlopeFilter(ISM_LP_ODR_DIV_100);

// Accelerometer Interrupt Settings
// Set the accelerometer's status i.e. the data ready to interrupt one.
// Commented out just below is the function to send the data ready
// to interrupt two.

myISM.setAccelStatustoInt1();
//myISM.setAccelStatustoInt2();

// Gyro config and enable
//myISM.setGyroDataRate(ISM_GY_ODR_104Hz);
//myISM.setGyroFullScale(ISM_500dps);

// Gyro Interrupt Settings
// We can just as easily set the gyroscope's data read signal to either interrupt

//myISM.setGyroStatustoInt1();
//myISM.setGyroStatustoInt2();


// Uncommenting the function call below will change the interrupt to
// active LOW instead of HIGH.

//myISM.setPinMode();

// This function call will modify which "events" trigger an interrupt. No
// argument has been given, please refer to the datasheet for more
// information.

// myISM.setIntNotification(uint8_t val);
}

void loop(){

if( digitalRead(interrupt_pin) == HIGH ){
myISM.getAccel(&accelData);
// myISM.getGyro(&gyroData);
Serial.print("Accelerometer: ");
Serial.print("X: ");
Serial.print(accelData.xData);
Expand All @@ -72,16 +112,6 @@ void loop(){
Serial.print("Z: ");
Serial.print(accelData.zData);
Serial.println(" ");
Serial.print("Gyroscope: ");
Serial.print("X: ");
Serial.print(gyroData.xData);
Serial.print(" ");
Serial.print("Y: ");
Serial.print(gyroData.yData);
Serial.print(" ");
Serial.print("Z: ");
Serial.print(gyroData.zData);
Serial.println(" ");
}

delay(100);
Expand Down
53 changes: 49 additions & 4 deletions examples/example3_sensor_hub/example3_sensor_hub.ino
Original file line number Diff line number Diff line change
@@ -1,23 +1,59 @@
/*
example3-sensor_hub
This example demonstrates the "sensor hub" feature of the ISM330DHCX.
The ISM330DHCX acts as a controller for external sensors connected to this
alternate bus (SDX/SCX). In this example, the ISM330DHCX is connected to the
MMC5983MA Magnetometer. You might notice that we have a 9DoF with these two
parts but not in this configuration. The reason is that the magnetometer requires
an initiate measurement bit to be flipped before every reading, while this is
possible (it's demonstrated below) it's also not ideal for this setup. A more
ideal setup would be a sensor that is just turned on and data is pulled
periodically.
Written by Elias Santistevan @ SparkFun Electronics, August 2022
Product:
6DoF: https://www.sparkfun.com/products/19764
9DoF: https://www.sparkfun.com/products/19895
Repository:
https://github.com/sparkfun/SparkFun_6DoF_ISM330DHCX_Arduino_Library
SparkFun code, firmware, and software is released under the MIT
License (http://opensource.org/licenses/MIT).
*/

#include <Wire.h>
#include "SparkFun_ISM330DHCX.h"

// 8 bit addressing for Sensor Hub
// 8 bit addressing is required for the 6DoF
// to communicate with its' external sensors.
#define MAG_ADDR_READ 0x61 // (0x30 << 1) | 1)
#define MAG_ADDR_WRITE 0x60 // (0x30 << 1)

#define MAG_READ_REG 0x00 // Read from 0x00
#define MAG_READ_LEN 0x07 // Read seven times consecutively
#define MAG_WRITE_REG 0x09 // INT_CTRL0 - register to initiate measurements on Magnetometer

// INT_CTRL0 (0x09) - contains the bit to initiate measurement.
// It must be written before each read and is cleared automatically.
#define MAG_WRITE_REG 0x09
#define MAG_WRITE_DATA 0x01 // Value to write to INT_CTRL0

SparkFun_ISM330DHCX myISM;

// Structs for X,Y,Z data
sfe_ism_data_t accelData;
sfe_ism_data_t gyroData;

// Settings for sensor hub
// The settings for the sensor hub have three specific fields.
// In addition there are different settings for reads and writes as
// indicated above.
sfe_hub_sensor_settings_t readMMC, writeMMC;

// Magnetometer data fields.
uint8_t shRawData[MAG_READ_LEN] = {};
unsigned int magXVal;
unsigned int magYVal;
Expand All @@ -30,10 +66,12 @@ double normalizedZ;
void setup(){


// Sensor hub settings for writing to the magnetometer.
writeMMC.address = MAG_ADDR_WRITE;
writeMMC.subAddress = MAG_WRITE_REG;
writeMMC.lenData = MAG_WRITE_DATA;

// Sensor hub settings for reading from the magnetometer.
readMMC.address = MAG_ADDR_READ;
readMMC.subAddress = MAG_READ_REG;
readMMC.lenData = MAG_READ_LEN;
Expand All @@ -47,9 +85,12 @@ void setup(){
while(1);
}

// Reset the device and the sensor hub to default settings.
// This is helpful if you're doing multiple uploads testing different settings.
myISM.deviceReset();
myISM.resetSensorHub();

// Wait for it to finish reseting
while( !myISM.getDeviceReset() ){
delay(1);
}
Expand Down Expand Up @@ -104,12 +145,16 @@ void loop(){
myISM.getAccel(&accelData);
myISM.getGyro(&gyroData);

// If you've given the 6DoF the wrong address for the external sensor, this
// bit will tell you. The zero argument is the external sensor to check (0-3).
if( myISM.getExternalSensorNack(0) )
Serial.println("MMC Nacked...");

// CHeck if the sensor hub is finished.
if( myISM.getHubStatus() )
{

// Get the data stored in the 6DoF's registers.
myISM.readPeripheralSensor(shRawData, (uint8_t)MAG_READ_LEN);

// Shift raw data
Expand Down Expand Up @@ -146,6 +191,7 @@ void loop(){
myISM.setHubSensorRead(0, &readMMC);
myISM.enableSensorI2C(true);

// Turn the accelerometer and gyrocope back on.
myISM.setAccelDataRate(ISM_XL_ODR_104Hz);
myISM.setGyroDataRate(ISM_GY_ODR_104Hz);

Expand Down Expand Up @@ -197,7 +243,6 @@ bool writeControlBit(sfe_hub_sensor_settings_t toWrite)

// Wait for write to complete
while( !myISM.getHubStatus() ){
// Serial.print(".");
delay(1);
}

Expand Down
Loading

0 comments on commit 8b80d2f

Please sign in to comment.