Skip to main content
Corrected capitalisation of products, corrected grammar. Enabled syntax highlighting.
Source Link

I'm working with ESP32 Dev kit1-Dev-Kit1 and MMA8452qMMA8452Q accelerometer. I want the espESP to read the accelerometer for 10s, then go to sleep for 10s.If If within the sleep period, the accelerometer magnitude reading exceedexceeds the threshold, it will also wake up the espESP.

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

MMA8452Q accel;

void setup()
{
  Serial.begin(9600);
  Serial.println("MMA8452Q Test Code!");

  accel.init(SCALE_2G, ODR_6);

  // Configure the wake-up source
  esp_sleep_enable_ext0_wakeup(GPIO_NUM_33, 0); // GPIO 33, HIGH logic level
}

void loop()
{
  // Read accelerometer and print values for 10 seconds
  unsigned long startTime = millis();
  unsigned long duration = 10000; // 10 seconds
  while (millis() - startTime < duration) {
    if (accel.available()) {
      accel.read();
      printCalculatedAccels();
    }
  }

  // Sleep for 10 seconds or wake up immediately if magnitude > 1.0
  Serial.println("Going to sleep or waking up if magnitude exceeds 1.0...");
  Serial.flush(); // Ensure all serial data is sent
  float magnitude = sqrt(pow(accel.cx, 2) + pow(accel.cy, 2) + pow(accel.cz, 2));
  if (magnitude > 1.0) {
    Serial.println("Magnitude exceeded 1.0, waking up...");
    esp_sleep_enable_ext0_wakeup(GPIO_NUM_33, 1);
    esp_deep_sleep_start();
    
  } else {
    esp_sleep_enable_timer_wakeup(10 * 1000000); // 10 seconds
    esp_deep_sleep_start();
  }
}

void printCalculatedAccels()
{
  Serial.print(accel.cx, 3);
  Serial.print("\t");
  Serial.print(accel.cy, 3);
  Serial.print("\t");
  Serial.print(accel.cz, 3);
  Serial.println("\t");
}

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

MMA8452Q accel;

void setup()
{
  Serial.begin(9600);
  Serial.println("MMA8452Q Test Code!");

  accel.init(SCALE_2G, ODR_6);

  // Configure the wake-up source
  esp_sleep_enable_ext0_wakeup(GPIO_NUM_33, 0); // GPIO 33, HIGH logic level
}

void loop()
{
  // Read accelerometer and print values for 10 seconds
  unsigned long startTime = millis();
  unsigned long duration = 10000; // 10 seconds
  while (millis() - startTime < duration) {
    if (accel.available()) {
      accel.read();
      printCalculatedAccels();
    }
  }

  // Sleep for 10 seconds or wake up immediately if magnitude > 1.0
  Serial.println("Going to sleep or waking up if magnitude exceeds 1.0...");
  Serial.flush(); // Ensure all serial data is sent
  float magnitude = sqrt(pow(accel.cx, 2) + pow(accel.cy, 2) + pow(accel.cz, 2));
  if (magnitude > 1.0) {
    Serial.println("Magnitude exceeded 1.0, waking up...");
    esp_sleep_enable_ext0_wakeup(GPIO_NUM_33, 1);
    esp_deep_sleep_start();
    
  } else {
    esp_sleep_enable_timer_wakeup(10 * 1000000); // 10 seconds
    esp_deep_sleep_start();
  }
}

void printCalculatedAccels()
{
  Serial.print(accel.cx, 3);
  Serial.print("\t");
  Serial.print(accel.cy, 3);
  Serial.print("\t");
  Serial.print(accel.cz, 3);
  Serial.println("\t");
}

I'm working with ESP32 Dev kit1 and MMA8452q accelerometer. I want the esp to read the accelerometer for 10s, then go to sleep for 10s.If within the sleep period, the accelerometer magnitude reading exceed the threshold, it will also wake up the esp.

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

MMA8452Q accel;

void setup()
{
  Serial.begin(9600);
  Serial.println("MMA8452Q Test Code!");

  accel.init(SCALE_2G, ODR_6);

  // Configure the wake-up source
  esp_sleep_enable_ext0_wakeup(GPIO_NUM_33, 0); // GPIO 33, HIGH logic level
}

void loop()
{
  // Read accelerometer and print values for 10 seconds
  unsigned long startTime = millis();
  unsigned long duration = 10000; // 10 seconds
  while (millis() - startTime < duration) {
    if (accel.available()) {
      accel.read();
      printCalculatedAccels();
    }
  }

  // Sleep for 10 seconds or wake up immediately if magnitude > 1.0
  Serial.println("Going to sleep or waking up if magnitude exceeds 1.0...");
  Serial.flush(); // Ensure all serial data is sent
  float magnitude = sqrt(pow(accel.cx, 2) + pow(accel.cy, 2) + pow(accel.cz, 2));
  if (magnitude > 1.0) {
    Serial.println("Magnitude exceeded 1.0, waking up...");
    esp_sleep_enable_ext0_wakeup(GPIO_NUM_33, 1);
    esp_deep_sleep_start();
    
  } else {
    esp_sleep_enable_timer_wakeup(10 * 1000000); // 10 seconds
    esp_deep_sleep_start();
  }
}

void printCalculatedAccels()
{
  Serial.print(accel.cx, 3);
  Serial.print("\t");
  Serial.print(accel.cy, 3);
  Serial.print("\t");
  Serial.print(accel.cz, 3);
  Serial.println("\t");
}

I'm working with ESP32-Dev-Kit1 and MMA8452Q accelerometer. I want the ESP to read the accelerometer for 10s, then go to sleep for 10s. If within the sleep period, the accelerometer magnitude reading exceeds the threshold, it will also wake up the ESP.

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

MMA8452Q accel;

void setup()
{
  Serial.begin(9600);
  Serial.println("MMA8452Q Test Code!");

  accel.init(SCALE_2G, ODR_6);

  // Configure the wake-up source
  esp_sleep_enable_ext0_wakeup(GPIO_NUM_33, 0); // GPIO 33, HIGH logic level
}

void loop()
{
  // Read accelerometer and print values for 10 seconds
  unsigned long startTime = millis();
  unsigned long duration = 10000; // 10 seconds
  while (millis() - startTime < duration) {
    if (accel.available()) {
      accel.read();
      printCalculatedAccels();
    }
  }

  // Sleep for 10 seconds or wake up immediately if magnitude > 1.0
  Serial.println("Going to sleep or waking up if magnitude exceeds 1.0...");
  Serial.flush(); // Ensure all serial data is sent
  float magnitude = sqrt(pow(accel.cx, 2) + pow(accel.cy, 2) + pow(accel.cz, 2));
  if (magnitude > 1.0) {
    Serial.println("Magnitude exceeded 1.0, waking up...");
    esp_sleep_enable_ext0_wakeup(GPIO_NUM_33, 1);
    esp_deep_sleep_start();
    
  } else {
    esp_sleep_enable_timer_wakeup(10 * 1000000); // 10 seconds
    esp_deep_sleep_start();
  }
}

void printCalculatedAccels()
{
  Serial.print(accel.cx, 3);
  Serial.print("\t");
  Serial.print(accel.cy, 3);
  Serial.print("\t");
  Serial.print(accel.cz, 3);
  Serial.println("\t");
}

coding changes
Source Link
Lily
  • 11
  • 2

I'm working with ESP32 Dev kit1 and MMA8452q accelerometer. I want the esp to read the accelerometer for 10s, then go to sleep for 10s.If within the sleep period, the accelerometer magnitude reading exceed the threshold, it will also wake up the esp. I run in Arduino IDE, but it seems the esp not sleeping for 10s.

This is the output from serial monitor as you can see it not sleeping for 10 secs:

04:09:38.965#include -><Wire.h>
#include -0"SparkFun_MMA8452Q.170h"

MMA8452Q accel;

void -0.047setup()
{
  0Serial.981begin(9600);
  Serial.println("MMA8452Q Test Code!");
04:09:39
  accel.090init(SCALE_2G, ->ODR_6);

  // Configure the wake-up source
  esp_sleep_enable_ext0_wakeup(GPIO_NUM_33, 0.171); // GPIO 33, HIGH logic level
}

void loop()
{
  // Read accelerometer and print values for 10 seconds
  unsigned long startTime = millis();
  unsigned long duration = 10000; // 10 seconds
  while (millis() -0 startTime < duration) {
    if (accel.048available()) {
  0    accel.983read();
      printCalculatedAccels();
04:09:39.165 -   }
  }

  // Sleep for 10 seconds or wake up immediately if magnitude > Going1.0
  Serial.println("Going to sleep or waking up if magnitude exceeds 1.0...");
04:09:39  Serial.760flush(); ->// ��Ensure �W.SPMP���9C"�#Ն�MMA8452Qall Testserial Code!data is sent
04:09:40.115 -> -0.141float magnitude -0= sqrt(pow(accel.134cx, 2) 1+ pow(accel.052cy, 2) + 
04:09:40pow(accel.241cz, -2));
  if (magnitude > -1.0) {
    Serial.135println("Magnitude exceeded -1.0, waking up.117..");
  0  esp_sleep_enable_ext0_wakeup(GPIO_NUM_33, 1);
    esp_deep_sleep_start();
    
  } else {
    esp_sleep_enable_timer_wakeup(10 * 1000000); // 10 seconds
    esp_deep_sleep_start();
  }
}

void printCalculatedAccels()
{
  Serial.979print(accel.cx, 3);
  Serial.print("\t");
04:09:40  Serial.424print(accel.cy, ->3);
 -0 Serial.152print("\t");
  -0Serial.086print(accel.cz, 3);
  0Serial.976println("\t");
}

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

MMA8452Q accel;

void setup()
{
  Serial.begin(9600);
  Serial.println("MMA8452Q Test Code!");

  accel.init(SCALE_2G, ODR_6);

  // Configure the wake-up source
  esp_sleep_enable_ext0_wakeup(GPIO_NUM_33, 1); // GPIO 33, HIGH logic level
}

void loop()
{
  // Read accelerometer and print values for 10 seconds
  unsigned long startTime = millis();
  unsigned long duration = 10000; // 10 seconds
  while (millis() - startTime < duration) {
    if (accel.available()) {
      accel.read();
      printCalculatedAccels();
    }
  }

  // Sleep for 10 seconds or wake up immediately if magnitude > 1.0
  Serial.println("Going to sleep or waking up if magnitude exceeds 1.0...");
  Serial.flush(); // Ensure all serial data is sent
  float magnitude = sqrt(pow(accel.cx, 2) + pow(accel.cy, 2) + pow(accel.cz, 2));
  if (magnitude > 1.0) {
    Serial.println("Magnitude exceeded 1.0, waking up...");
  } else {
    esp_sleep_enable_timer_wakeup(10 * 1000000); // 10 seconds
    esp_deep_sleep_start();
  }
}

void printCalculatedAccels()
{
  Serial.print(accel.cx, 3);
  Serial.print("\t");
  Serial.print(accel.cy, 3);
  Serial.print("\t");
  Serial.print(accel.cz, 3);
  Serial.println("\t");
}


I'm working with ESP32 Dev kit1 and MMA8452q accelerometer. I want the esp to read the accelerometer for 10s, then go to sleep for 10s.If within the sleep period, the accelerometer magnitude reading exceed the threshold, it will also wake up the esp. I run in Arduino IDE, but it seems the esp not sleeping for 10s.

This is the output from serial monitor as you can see it not sleeping for 10 secs:

04:09:38.965 -> -0.170  -0.047  0.981   
04:09:39.090 -> -0.171  -0.048  0.983   
04:09:39.165 -> Going to sleep...
04:09:39.760 -> �� �W.SPMP���9C"�#Ն�MMA8452Q Test Code!
04:09:40.115 -> -0.141  -0.134  1.052   
04:09:40.241 -> -0.135  -0.117  0.979   
04:09:40.424 -> -0.152  -0.086  0.976
#include <Wire.h>
#include "SparkFun_MMA8452Q.h"

MMA8452Q accel;

void setup()
{
  Serial.begin(9600);
  Serial.println("MMA8452Q Test Code!");

  accel.init(SCALE_2G, ODR_6);

  // Configure the wake-up source
  esp_sleep_enable_ext0_wakeup(GPIO_NUM_33, 1); // GPIO 33, HIGH logic level
}

void loop()
{
  // Read accelerometer and print values for 10 seconds
  unsigned long startTime = millis();
  unsigned long duration = 10000; // 10 seconds
  while (millis() - startTime < duration) {
    if (accel.available()) {
      accel.read();
      printCalculatedAccels();
    }
  }

  // Sleep for 10 seconds or wake up immediately if magnitude > 1.0
  Serial.println("Going to sleep or waking up if magnitude exceeds 1.0...");
  Serial.flush(); // Ensure all serial data is sent
  float magnitude = sqrt(pow(accel.cx, 2) + pow(accel.cy, 2) + pow(accel.cz, 2));
  if (magnitude > 1.0) {
    Serial.println("Magnitude exceeded 1.0, waking up...");
  } else {
    esp_sleep_enable_timer_wakeup(10 * 1000000); // 10 seconds
    esp_deep_sleep_start();
  }
}

void printCalculatedAccels()
{
  Serial.print(accel.cx, 3);
  Serial.print("\t");
  Serial.print(accel.cy, 3);
  Serial.print("\t");
  Serial.print(accel.cz, 3);
  Serial.println("\t");
}


I'm working with ESP32 Dev kit1 and MMA8452q accelerometer. I want the esp to read the accelerometer for 10s, then go to sleep for 10s.If within the sleep period, the accelerometer magnitude reading exceed the threshold, it will also wake up the esp.

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

MMA8452Q accel;

void setup()
{
  Serial.begin(9600);
  Serial.println("MMA8452Q Test Code!");

  accel.init(SCALE_2G, ODR_6);

  // Configure the wake-up source
  esp_sleep_enable_ext0_wakeup(GPIO_NUM_33, 0); // GPIO 33, HIGH logic level
}

void loop()
{
  // Read accelerometer and print values for 10 seconds
  unsigned long startTime = millis();
  unsigned long duration = 10000; // 10 seconds
  while (millis() - startTime < duration) {
    if (accel.available()) {
      accel.read();
      printCalculatedAccels();
    }
  }

  // Sleep for 10 seconds or wake up immediately if magnitude > 1.0
  Serial.println("Going to sleep or waking up if magnitude exceeds 1.0...");
  Serial.flush(); // Ensure all serial data is sent
  float magnitude = sqrt(pow(accel.cx, 2) + pow(accel.cy, 2) + pow(accel.cz, 2));
  if (magnitude > 1.0) {
    Serial.println("Magnitude exceeded 1.0, waking up...");
    esp_sleep_enable_ext0_wakeup(GPIO_NUM_33, 1);
    esp_deep_sleep_start();
    
  } else {
    esp_sleep_enable_timer_wakeup(10 * 1000000); // 10 seconds
    esp_deep_sleep_start();
  }
}

void printCalculatedAccels()
{
  Serial.print(accel.cx, 3);
  Serial.print("\t");
  Serial.print(accel.cy, 3);
  Serial.print("\t");
  Serial.print(accel.cz, 3);
  Serial.println("\t");
}

added 3 characters in body
Source Link
jsotola
  • 1.6k
  • 2
  • 13
  • 22

04:09:38.965 -> -0.170 -0.047 0.981
04:09:39.090 -> -0.171 -0.048 0.983
04:09:39.165 -> Going to sleep... 04:09:39.760 -> �� �W.SPMP���9 C"�#Ն�MMA8452Q Test Code! 04:09:40.115 -> -0.141 -0.134 1.052
04:09:40.241 -> -0.135 -0.117 0.979
04:09:40.424 -> -0.152 -0.086 0.976

#include <Wire.h>
#include "SparkFun_MMA8452Q04:09:38.h"

MMA8452Q accel;

void setup()
{
965 -> Serial-0.begin(9600);
170  Serial-0.println("MMA8452Q Test Code!");

047  accel0.init(SCALE_2G, ODR_6);

  // Configure the wake-up source
  esp_sleep_enable_ext0_wakeup(GPIO_NUM_33, 1); // GPIO 33, HIGH logic level
}

void loop()
{
  // Read accelerometer and print values for 10 seconds
  unsigned long startTime = millis();
  unsigned long duration =981 10000; // 10 seconds
  while (millis()04:09:39.090 - startTime < duration) {
    if> (accel-0.available()) {
    171  accel-0.read();
      printCalculatedAccels();
 048  0.983 }
  }

  // Sleep for 10 seconds or wake up immediately if magnitude > 104:09:39.0
165 -> Serial.println("GoingGoing to sleep or waking up if magnitude exceeds 1.0...");
  Serial04:09:39.flush(); // Ensure760 all-> serial�� data�W.SPMP���9C"�#Ն�MMA8452Q isTest sentCode!
  float magnitude = sqrt(pow(accel04:09:40.cx, 2)115 +-> pow(accel-0.cy, 2)141 + pow(accel-0.cz, 2));
  if (magnitude134 > 1.0) {
 052   Serial
04:09:40.println("Magnitude241 exceeded-> 1.-0, waking up...");
  } else {
    esp_sleep_enable_timer_wakeup(10 * 1000000); // 10 seconds
    esp_deep_sleep_start();
  }
}

void printCalculatedAccels()
{
135  Serial.print(accel-0.cx, 3);
117  Serial0.print("\t");
979  Serial.print(accel.cy, 3);
04:09:40.424 -> Serial-0.print("\t");
152  Serial.print(accel-0.cz, 3);
086  Serial0.println("\t");
}

976
#include <Wire.h>
#include "SparkFun_MMA8452Q.h"

MMA8452Q accel;

void setup()
{
  Serial.begin(9600);
  Serial.println("MMA8452Q Test Code!");

  accel.init(SCALE_2G, ODR_6);

  // Configure the wake-up source
  esp_sleep_enable_ext0_wakeup(GPIO_NUM_33, 1); // GPIO 33, HIGH logic level
}

void loop()
{
  // Read accelerometer and print values for 10 seconds
  unsigned long startTime = millis();
  unsigned long duration = 10000; // 10 seconds
  while (millis() - startTime < duration) {
    if (accel.available()) {
      accel.read();
      printCalculatedAccels();
    }
  }

  // Sleep for 10 seconds or wake up immediately if magnitude > 1.0
  Serial.println("Going to sleep or waking up if magnitude exceeds 1.0...");
  Serial.flush(); // Ensure all serial data is sent
  float magnitude = sqrt(pow(accel.cx, 2) + pow(accel.cy, 2) + pow(accel.cz, 2));
  if (magnitude > 1.0) {
    Serial.println("Magnitude exceeded 1.0, waking up...");
  } else {
    esp_sleep_enable_timer_wakeup(10 * 1000000); // 10 seconds
    esp_deep_sleep_start();
  }
}

void printCalculatedAccels()
{
  Serial.print(accel.cx, 3);
  Serial.print("\t");
  Serial.print(accel.cy, 3);
  Serial.print("\t");
  Serial.print(accel.cz, 3);
  Serial.println("\t");
}


04:09:38.965 -> -0.170 -0.047 0.981
04:09:39.090 -> -0.171 -0.048 0.983
04:09:39.165 -> Going to sleep... 04:09:39.760 -> �� �W.SPMP���9 C"�#Ն�MMA8452Q Test Code! 04:09:40.115 -> -0.141 -0.134 1.052
04:09:40.241 -> -0.135 -0.117 0.979
04:09:40.424 -> -0.152 -0.086 0.976

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

MMA8452Q accel;

void setup()
{
  Serial.begin(9600);
  Serial.println("MMA8452Q Test Code!");

  accel.init(SCALE_2G, ODR_6);

  // Configure the wake-up source
  esp_sleep_enable_ext0_wakeup(GPIO_NUM_33, 1); // GPIO 33, HIGH logic level
}

void loop()
{
  // Read accelerometer and print values for 10 seconds
  unsigned long startTime = millis();
  unsigned long duration = 10000; // 10 seconds
  while (millis() - startTime < duration) {
    if (accel.available()) {
      accel.read();
      printCalculatedAccels();
    }
  }

  // Sleep for 10 seconds or wake up immediately if magnitude > 1.0
  Serial.println("Going to sleep or waking up if magnitude exceeds 1.0...");
  Serial.flush(); // Ensure all serial data is sent
  float magnitude = sqrt(pow(accel.cx, 2) + pow(accel.cy, 2) + pow(accel.cz, 2));
  if (magnitude > 1.0) {
    Serial.println("Magnitude exceeded 1.0, waking up...");
  } else {
    esp_sleep_enable_timer_wakeup(10 * 1000000); // 10 seconds
    esp_deep_sleep_start();
  }
}

void printCalculatedAccels()
{
  Serial.print(accel.cx, 3);
  Serial.print("\t");
  Serial.print(accel.cy, 3);
  Serial.print("\t");
  Serial.print(accel.cz, 3);
  Serial.println("\t");
}


04:09:38.965 -> -0.170  -0.047  0.981   
04:09:39.090 -> -0.171  -0.048  0.983   
04:09:39.165 -> Going to sleep...
04:09:39.760 -> �� �W.SPMP���9C"�#Ն�MMA8452Q Test Code!
04:09:40.115 -> -0.141  -0.134  1.052   
04:09:40.241 -> -0.135  -0.117  0.979   
04:09:40.424 -> -0.152  -0.086  0.976
#include <Wire.h>
#include "SparkFun_MMA8452Q.h"

MMA8452Q accel;

void setup()
{
  Serial.begin(9600);
  Serial.println("MMA8452Q Test Code!");

  accel.init(SCALE_2G, ODR_6);

  // Configure the wake-up source
  esp_sleep_enable_ext0_wakeup(GPIO_NUM_33, 1); // GPIO 33, HIGH logic level
}

void loop()
{
  // Read accelerometer and print values for 10 seconds
  unsigned long startTime = millis();
  unsigned long duration = 10000; // 10 seconds
  while (millis() - startTime < duration) {
    if (accel.available()) {
      accel.read();
      printCalculatedAccels();
    }
  }

  // Sleep for 10 seconds or wake up immediately if magnitude > 1.0
  Serial.println("Going to sleep or waking up if magnitude exceeds 1.0...");
  Serial.flush(); // Ensure all serial data is sent
  float magnitude = sqrt(pow(accel.cx, 2) + pow(accel.cy, 2) + pow(accel.cz, 2));
  if (magnitude > 1.0) {
    Serial.println("Magnitude exceeded 1.0, waking up...");
  } else {
    esp_sleep_enable_timer_wakeup(10 * 1000000); // 10 seconds
    esp_deep_sleep_start();
  }
}

void printCalculatedAccels()
{
  Serial.print(accel.cx, 3);
  Serial.print("\t");
  Serial.print(accel.cy, 3);
  Serial.print("\t");
  Serial.print(accel.cz, 3);
  Serial.println("\t");
}


Source Link
Lily
  • 11
  • 2
Loading