Wire.beginTransmission(0x69) and device

Hi all,
the sketch from:

used ADXL345 compiling passed and uploaded into ESP32 no rebooting happened.

I changed the ADXL345's I2C address from 0x53 to 0x68 (for MPU6050 ) and 0x69 by AD0: HIGH because I don't have ADXL handy, uploaded and ESP32 keep rebooting, why?
Thanks
Adam

#include <Wire.h>
#include <SPI.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
// Declaration for SSD1306 display connected using software SPI (default case):
/*
#define OLED_MOSI   9
#define OLED_CLK   10
#define OLED_DC    11
#define OLED_CS    12
#define OLED_RESET 8
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT,
  OLED_MOSI, OLED_CLK, OLED_DC, OLED_RESET, OLED_CS);
*/

//Adafruit_MPU6050 mpu;
Adafruit_SSD1306 display = Adafruit_SSD1306(128, 32, &Wire);

//.......................................................................................

const int MPU=0x69;  // I2C address of the MPU-6050

//........................................................................................

// 'eeworldonlineblack-135x45', 130x45px
const unsigned char epd_bitmap_eeworldonlineblack_135x45 [] PROGMEM = {
  0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfc, 0x1f, 0xff, 0xf8, 
  0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfc, 0x1f, 0xff, 
  0xf8, 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfc, 0x1f, 
  0xff, 0xf8, 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfc, 
  0x1f, 0xff, 0xf8, 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 
  0xfc, 0x1f, 0xff, 0xf8, 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 
  0xff, 0xfc, 0x1f, 0xff, 0xf8, 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 
  0xff, 0xff, 0xfc, 0x1f, 0xff, 0xf8, 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 
  0xff, 0xff, 0xff, 0xfc, 0x1f, 0xff, 0xf8, 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 
  0xff, 0xff, 0xff, 0xff, 0xfc, 0x1f, 0xff, 0xf8, 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 
  0xff, 0xff, 0xff, 0xff, 0xff, 0xfc, 0x1f, 0xff, 0xf8, 0x00, 0xe0, 0x00, 0x60, 0x00, 0x67, 0xf8, 
  0x1f, 0xc1, 0xf8, 0x00, 0x3f, 0x06, 0x0c, 0x1f, 0xe0, 0x18, 0x00, 0xe0, 0x00, 0x60, 0x00, 0x67, 
  0xf0, 0x1f, 0xc1, 0xe0, 0x00, 0x1f, 0x04, 0x0c, 0x1f, 0x80, 0x00, 0x00, 0xe0, 0x00, 0x60, 0x00, 
  0x47, 0xf0, 0x1f, 0xc1, 0xe0, 0x00, 0x0f, 0x00, 0x0c, 0x1f, 0x00, 0x00, 0x00, 0xe1, 0xff, 0xe1, 
  0xff, 0xc3, 0xf0, 0x1f, 0xc1, 0xc0, 0x00, 0x0f, 0x00, 0x0c, 0x1f, 0x00, 0x00, 0x00, 0xe3, 0xff, 
  0xe1, 0xff, 0xc3, 0xf0, 0x0f, 0xc3, 0xc0, 0x00, 0x07, 0x00, 0x0c, 0x1e, 0x00, 0x00, 0x00, 0xe3, 
  0xff, 0xe3, 0xff, 0xc3, 0xe0, 0x0f, 0x83, 0xc1, 0xfe, 0x07, 0x03, 0xfc, 0x1e, 0x07, 0xf0, 0x00, 
  0xc3, 0xff, 0xe3, 0xff, 0xc3, 0xe0, 0x0f, 0x83, 0xc1, 0xff, 0x07, 0x07, 0xfc, 0x1e, 0x0f, 0xf8, 
  0x00, 0xc3, 0xff, 0xc3, 0xff, 0xc3, 0xe1, 0x0f, 0x83, 0x83, 0xff, 0x07, 0x07, 0xfc, 0x1e, 0x0f, 
  0xf8, 0x00, 0xc3, 0xff, 0xc3, 0xff, 0xc1, 0xe1, 0x07, 0x87, 0x83, 0xff, 0x07, 0x07, 0xfc, 0x1e, 
  0x0f, 0xf8, 0x00, 0xc3, 0xff, 0xc3, 0xff, 0xc1, 0xe1, 0x07, 0x87, 0x83, 0xff, 0x07, 0x07, 0xfc, 
  0x1e, 0x0f, 0xf8, 0x00, 0xc0, 0x00, 0xc0, 0x00, 0xc1, 0xc1, 0x87, 0x07, 0x83, 0xff, 0x07, 0x07, 
  0xfc, 0x1e, 0x0f, 0xf8, 0x00, 0xc0, 0x00, 0xc0, 0x00, 0xe1, 0xc1, 0x87, 0x07, 0x83, 0xff, 0x07, 
  0x07, 0xfc, 0x1e, 0x0f, 0xf8, 0x00, 0xc0, 0x00, 0xc0, 0x00, 0xe0, 0xc3, 0x87, 0x07, 0x83, 0xff, 
  0x07, 0x07, 0xfc, 0x1e, 0x0f, 0xf8, 0x00, 0x80, 0x01, 0xc0, 0x01, 0xe0, 0xc3, 0x83, 0x0f, 0x83, 
  0xff, 0x07, 0x07, 0xfc, 0x1e, 0x0f, 0xf8, 0x00, 0x87, 0xff, 0x87, 0xff, 0xe0, 0xc3, 0xc3, 0x0f, 
  0x83, 0xff, 0x07, 0x07, 0xfc, 0x1e, 0x0f, 0xf8, 0x00, 0x87, 0xff, 0x87, 0xff, 0xf0, 0x83, 0xc2, 
  0x0f, 0x83, 0xff, 0x07, 0x07, 0xfc, 0x1e, 0x0f, 0xf8, 0x00, 0x87, 0xff, 0x87, 0xff, 0xf0, 0x87, 
  0xc2, 0x0f, 0x83, 0xff, 0x07, 0x07, 0xfc, 0x1e, 0x0f, 0xf8, 0x00, 0x8f, 0xff, 0x87, 0xff, 0xf0, 
  0x07, 0xc2, 0x1f, 0xc1, 0xff, 0x07, 0x07, 0xfc, 0x1e, 0x0f, 0xf8, 0x00, 0x8f, 0xff, 0x87, 0xff, 
  0xf0, 0x07, 0xc0, 0x1f, 0xc1, 0xfe, 0x07, 0x07, 0xfc, 0x1e, 0x07, 0xf0, 0x00, 0x8f, 0xff, 0x8f, 
  0xff, 0xf0, 0x07, 0xe0, 0x1f, 0xc0, 0x00, 0x07, 0x07, 0xfc, 0x1e, 0x00, 0x00, 0x00, 0x00, 0x03, 
  0x80, 0x03, 0xf8, 0x0f, 0xe0, 0x1f, 0xc0, 0x00, 0x0f, 0x07, 0xfc, 0x1f, 0x00, 0x00, 0x00, 0x00, 
  0x03, 0x80, 0x01, 0xf8, 0x0f, 0xe0, 0x1f, 0xe0, 0x00, 0x0f, 0x07, 0xfc, 0x1f, 0x00, 0x00, 0x00, 
  0x00, 0x03, 0x00, 0x03, 0xf8, 0x0f, 0xe0, 0x3f, 0xf0, 0x00, 0x1f, 0x07, 0xfc, 0x1f, 0x80, 0x08, 
  0x00, 0x00, 0x03, 0x00, 0x03, 0xf8, 0x0f, 0xf0, 0x3f, 0xf8, 0x00, 0x7f, 0x07, 0xfc, 0x1f, 0xe0, 
  0x3c, 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 
  0xff, 0xff, 0xc0, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 
  0xff, 0xff, 0xff, 0xc0, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x83, 0xff, 0x7d, 0xfe, 0x7f, 
  0xf7, 0xfb, 0xef, 0xf0, 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x7d, 0xff, 0x3d, 0xfe, 
  0x7f, 0xf7, 0xf9, 0xef, 0xf7, 0xc0, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x7e, 0xff, 0x5d, 
  0xfe, 0x7f, 0xf7, 0xfa, 0xef, 0xf7, 0xc0, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe, 0xfe, 0xff, 
  0x4d, 0xfe, 0x7f, 0xf7, 0xfa, 0xef, 0xf3, 0xc0, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe, 0xfe, 
  0xff, 0x6d, 0xfe, 0x7f, 0xf7, 0xfb, 0x6f, 0xf0, 0x40, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe, 
  0xfe, 0xff, 0x75, 0xfe, 0x7f, 0xf7, 0xfb, 0xaf, 0xf7, 0xc0, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 
  0xff, 0x7e, 0xff, 0x71, 0xfe, 0x7f, 0xf7, 0xfb, 0x8f, 0xf7, 0xc0, 0xff, 0xff, 0xff, 0xff, 0xff, 
  0xff, 0xff, 0x3d, 0xff, 0x79, 0xfe, 0x7f, 0xf7, 0xfb, 0xcf, 0xf7, 0xc0, 0xff, 0xff, 0xff, 0xff, 
  0xff, 0xff, 0xff, 0x83, 0xff, 0x7d, 0xff, 0x0f, 0xf7, 0xfb, 0xef, 0xf8, 0x00
};

// Array of all bitmaps for convenience. (Total bytes used to store images in PROGMEM = 784)
const int epd_bitmap_allArray_LEN = 1;
const unsigned char* epd_bitmap_allArray[1] = {
  epd_bitmap_eeworldonlineblack_135x45
};

float xavg=0, yavg=0, zavg=0;
float xcur=0, ycur=0, zcur=0;
float xnxt=0, ynxt=0, znxt=0;
int steps = 0;

void setup() {
ssd1306_init();
adxl345_init();
read_av_acc();
}

void loop() {
for(int i = 0; i<50; i++){
Wire.beginTransmission(0x69);
Wire.write(0x32); 
Wire.endTransmission();
Wire.requestFrom(0x69, 1);
byte x0 = Wire.read();

Wire.beginTransmission(0x69);
Wire.write(0x33); 
Wire.endTransmission();
Wire.requestFrom(0x69, 1);
byte x1 = Wire.read();
x1 = x1 & 0x03;

uint16_t x = (x1 << 8) + x0;
int16_t xf = x;
if(xf > 511)
{
xf = xf - 1024;
}
float xa = xf * 0.004;
xcur = xcur + xa;


Wire.beginTransmission(0x69);
Wire.write(0x34); 
Wire.endTransmission();
Wire.requestFrom(0x69, 1);
byte y0 = Wire.read();

Wire.beginTransmission(0x69);
Wire.write(0x35); 
Wire.endTransmission();
Wire.requestFrom(0x69, 1);
byte y1 = Wire.read();
y1 = y1 & 0x03;

uint16_t y = (y1 << 8) + y0;
int16_t yf = y;
if(yf > 511)
{
yf = yf - 1024;
}
float ya = yf * 0.004;
ycur = ycur + ya;


Wire.beginTransmission(0x69);
Wire.write(0x36); 
Wire.endTransmission();
Wire.requestFrom(0x69, 1);
byte z0 = Wire.read();

Wire.beginTransmission(0x69);
Wire.write(0x37); 
Wire.endTransmission();
Wire.requestFrom(0x69, 1);
byte z1 = Wire.read();
z1 = z1 & 0x03;

uint16_t z = (z1 << 8) + z0;
int16_t zf = z;
if(zf > 511)
{
zf = zf - 1024;
}
float za = zf * 0.004;
zcur = zcur + za;
}
xcur = xcur/50; 
ycur = ycur/50;
zcur = zcur/50;

float acc = sqrt(((xcur - xavg) * (xcur - xavg)) + 
               ((ycur - yavg) * (ycur - yavg)) + 
               ((zcur - zavg) * (zcur - zavg)));
delay(250);

//Next Acceleration
for(int i = 0; i<50; i++){
Wire.beginTransmission(0x69);
Wire.write(0x32); 
Wire.endTransmission();
Wire.requestFrom(0x69, 1);
byte x0 = Wire.read();

Wire.beginTransmission(0x69);
Wire.write(0x33); 
Wire.endTransmission();
Wire.requestFrom(0x69, 1);
byte x1 = Wire.read();
x1 = x1 & 0x03;

uint16_t x = (x1 << 8) + x0;
int16_t xf = x;
if(xf > 511)
{
xf = xf - 1024;
}
float xa = xf * 0.004;
xnxt = xnxt + xa;


Wire.beginTransmission(0x69);
Wire.write(0x34); 
Wire.endTransmission();
Wire.requestFrom(0x69, 1);
byte y0 = Wire.read();

Wire.beginTransmission(0x69);
Wire.write(0x35); 
Wire.endTransmission();
Wire.requestFrom(0x69, 1);
byte y1 = Wire.read();
y1 = y1 & 0x03;

uint16_t y = (y1 << 8) + y0;
int16_t yf = y;
if(yf > 511)
{
yf = yf - 1024;
}
float ya = yf * 0.004;
ynxt = ynxt + ya;


Wire.beginTransmission(0x69);
Wire.write(0x36); 
Wire.endTransmission();
Wire.requestFrom(0x69, 1);
byte z0 = Wire.read();

Wire.beginTransmission(0x69);
Wire.write(0x37); 
Wire.endTransmission();
Wire.requestFrom(0x69, 1);
byte z1 = Wire.read();
z1 = z1 & 0x03;

uint16_t z = (z1 << 8) + z0;
int16_t zf = z;
if(zf > 511)
{
zf = zf - 1024;
}
float za = zf * 0.004;
znxt = znxt + za;
}
xnxt = xnxt/50; 
ynxt = ynxt/50;
znxt = znxt/50;

float acc2 = sqrt(((xnxt - xavg) * (xnxt - xavg)) + 
               ((ynxt - yavg) * (ynxt - yavg)) + 
               ((znxt - zavg) * (znxt - zavg)));

if (acc2-acc > 0.05)
    {
      steps = steps + 1;
    }

display.clearDisplay();
display.setTextSize(2); 
display.setTextColor(SSD1306_WHITE);
display.setCursor(0, 0);
display.print("steps: ");
display.println(steps);
display.setTextColor(SSD1306_BLACK, SSD1306_WHITE); // Draw 'inverse' text
display.print("Distance: ");
display.print(steps*0.3048);
display.println("M");
display.display();
delay(400);
}

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

Wire.beginTransmission(0x69);
Wire.write(0x2C); 
Wire.write(0x08); 
Wire.endTransmission();

Wire.beginTransmission(0x69);
Wire.write(0x31); 
Wire.write(0x08); 
Wire.endTransmission();

Wire.beginTransmission(0x69);
Wire.write(0x2D); 
Wire.write(0x08); 
Wire.endTransmission();

}

void ssd1306_init(){
display.begin(SSD1306_SWITCHCAPVCC);
display.display();
delay(1000);
display.clearDisplay();
display.drawBitmap(0, 0, epd_bitmap_eeworldonlineblack_135x45, 130, 45, WHITE);
display.display();
delay(1000);
display.clearDisplay();
display.setTextSize(2); 
display.setTextColor(SSD1306_WHITE);
display.setCursor(0, 0);
display.println("STEPS");
display.println("COUNTER");
display.display();
delay(1000);
}

float read_av_acc(){
for(int i = 0; i<50; i++){
Wire.beginTransmission(0x69); // was: 0x53
Wire.write(0x3B);             // was: 0x32
Wire.endTransmission();
Wire.requestFrom(0x69, 1);
byte x0 = Wire.read();

Wire.beginTransmission(0x69);
Wire.write(0x3C);           // was: 0x33 
Wire.endTransmission();
Wire.requestFrom(0x69, 1);
byte x1 = Wire.read();
x1 = x1 & 0x03;

uint16_t x = (x1 << 8) + x0;
int16_t xf = x;
if(xf > 511)
{
xf = xf - 1024;
}
float xa = xf * 0.004;
xavg = xavg + xa;


Wire.beginTransmission(0x69);
Wire.write(0x34); 
Wire.endTransmission();
Wire.requestFrom(0x69, 1);
byte y0 = Wire.read();

Wire.beginTransmission(0x69);
Wire.write(0x35); 
Wire.endTransmission();
Wire.requestFrom(0x69, 1);
byte y1 = Wire.read();
y1 = y1 & 0x03;

uint16_t y = (y1 << 8) + y0;
int16_t yf = y;
if(yf > 511)
{
yf = yf - 1024;
}
float ya = yf * 0.004;
yavg = yavg + ya;


Wire.beginTransmission(0x69);
Wire.write(0x36); 
Wire.endTransmission();
Wire.requestFrom(0x69, 1);
byte z0 = Wire.read();

Wire.beginTransmission(0x69);
Wire.write(0x37); 
Wire.endTransmission();
Wire.requestFrom(0x69, 1);
byte z1 = Wire.read();
z1 = z1 & 0x03;

uint16_t z = (z1 << 8) + z0;
int16_t zf = z;
if(zf > 511)
{
zf = zf - 1024;
}
float za = zf * 0.004;
zavg = zavg + za;
}
xavg = xavg/50; 
yavg = yavg/50;
zavg = zavg/50;
}

This may not work without an ADXL.

1 Like

I don't know.

I changed the ADXL345's I2C address from 0x53 to 0x68 (for MPU6050 )

Do you mean you just hooked up an MPU6050 where the original code had you using and ADXK345?

Are these parts interchangeable, or did you rework the code to accommodate any differences that exist between the two parts?

a7

1 Like

Thanks.
the modified code still rebooting:
commented out: // adxl345_init();
added:

#include <Adafruit_MPU6050.h>
Adafruit_MPU6050 mpu;
const int MPU=0x68;  // I2C address of the MPU-6050

.......

if (!mpu.begin()) {
    Serial.println("Sensor init failed");
    while (1)
      yield();
  }

NEW CODE:

#include <Wire.h>
#include <SPI.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
// Declaration for SSD1306 display connected using software SPI (default case):
/*
#define OLED_MOSI   9
#define OLED_CLK   10
#define OLED_DC    11
#define OLED_CS    12
#define OLED_RESET 8
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT,
  OLED_MOSI, OLED_CLK, OLED_DC, OLED_RESET, OLED_CS);
*/

Adafruit_MPU6050 mpu;
Adafruit_SSD1306 display = Adafruit_SSD1306(128, 32, &Wire);

//.......................................................................................

const int MPU=0x68;  // I2C address of the MPU-6050

//........................................................................................

// 'eeworldonlineblack-135x45', 130x45px
const unsigned char epd_bitmap_eeworldonlineblack_135x45 [] PROGMEM = {
  0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfc, 0x1f, 0xff, 0xf8, 
  0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfc, 0x1f, 0xff, 
  0xf8, 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfc, 0x1f, 
  0xff, 0xf8, 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfc, 
  0x1f, 0xff, 0xf8, 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 
  0xfc, 0x1f, 0xff, 0xf8, 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 
  0xff, 0xfc, 0x1f, 0xff, 0xf8, 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 
  0xff, 0xff, 0xfc, 0x1f, 0xff, 0xf8, 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 
  0xff, 0xff, 0xff, 0xfc, 0x1f, 0xff, 0xf8, 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 
  0xff, 0xff, 0xff, 0xff, 0xfc, 0x1f, 0xff, 0xf8, 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 
  0xff, 0xff, 0xff, 0xff, 0xff, 0xfc, 0x1f, 0xff, 0xf8, 0x00, 0xe0, 0x00, 0x60, 0x00, 0x67, 0xf8, 
  0x1f, 0xc1, 0xf8, 0x00, 0x3f, 0x06, 0x0c, 0x1f, 0xe0, 0x18, 0x00, 0xe0, 0x00, 0x60, 0x00, 0x67, 
  0xf0, 0x1f, 0xc1, 0xe0, 0x00, 0x1f, 0x04, 0x0c, 0x1f, 0x80, 0x00, 0x00, 0xe0, 0x00, 0x60, 0x00, 
  0x47, 0xf0, 0x1f, 0xc1, 0xe0, 0x00, 0x0f, 0x00, 0x0c, 0x1f, 0x00, 0x00, 0x00, 0xe1, 0xff, 0xe1, 
  0xff, 0xc3, 0xf0, 0x1f, 0xc1, 0xc0, 0x00, 0x0f, 0x00, 0x0c, 0x1f, 0x00, 0x00, 0x00, 0xe3, 0xff, 
  0xe1, 0xff, 0xc3, 0xf0, 0x0f, 0xc3, 0xc0, 0x00, 0x07, 0x00, 0x0c, 0x1e, 0x00, 0x00, 0x00, 0xe3, 
  0xff, 0xe3, 0xff, 0xc3, 0xe0, 0x0f, 0x83, 0xc1, 0xfe, 0x07, 0x03, 0xfc, 0x1e, 0x07, 0xf0, 0x00, 
  0xc3, 0xff, 0xe3, 0xff, 0xc3, 0xe0, 0x0f, 0x83, 0xc1, 0xff, 0x07, 0x07, 0xfc, 0x1e, 0x0f, 0xf8, 
  0x00, 0xc3, 0xff, 0xc3, 0xff, 0xc3, 0xe1, 0x0f, 0x83, 0x83, 0xff, 0x07, 0x07, 0xfc, 0x1e, 0x0f, 
  0xf8, 0x00, 0xc3, 0xff, 0xc3, 0xff, 0xc1, 0xe1, 0x07, 0x87, 0x83, 0xff, 0x07, 0x07, 0xfc, 0x1e, 
  0x0f, 0xf8, 0x00, 0xc3, 0xff, 0xc3, 0xff, 0xc1, 0xe1, 0x07, 0x87, 0x83, 0xff, 0x07, 0x07, 0xfc, 
  0x1e, 0x0f, 0xf8, 0x00, 0xc0, 0x00, 0xc0, 0x00, 0xc1, 0xc1, 0x87, 0x07, 0x83, 0xff, 0x07, 0x07, 
  0xfc, 0x1e, 0x0f, 0xf8, 0x00, 0xc0, 0x00, 0xc0, 0x00, 0xe1, 0xc1, 0x87, 0x07, 0x83, 0xff, 0x07, 
  0x07, 0xfc, 0x1e, 0x0f, 0xf8, 0x00, 0xc0, 0x00, 0xc0, 0x00, 0xe0, 0xc3, 0x87, 0x07, 0x83, 0xff, 
  0x07, 0x07, 0xfc, 0x1e, 0x0f, 0xf8, 0x00, 0x80, 0x01, 0xc0, 0x01, 0xe0, 0xc3, 0x83, 0x0f, 0x83, 
  0xff, 0x07, 0x07, 0xfc, 0x1e, 0x0f, 0xf8, 0x00, 0x87, 0xff, 0x87, 0xff, 0xe0, 0xc3, 0xc3, 0x0f, 
  0x83, 0xff, 0x07, 0x07, 0xfc, 0x1e, 0x0f, 0xf8, 0x00, 0x87, 0xff, 0x87, 0xff, 0xf0, 0x83, 0xc2, 
  0x0f, 0x83, 0xff, 0x07, 0x07, 0xfc, 0x1e, 0x0f, 0xf8, 0x00, 0x87, 0xff, 0x87, 0xff, 0xf0, 0x87, 
  0xc2, 0x0f, 0x83, 0xff, 0x07, 0x07, 0xfc, 0x1e, 0x0f, 0xf8, 0x00, 0x8f, 0xff, 0x87, 0xff, 0xf0, 
  0x07, 0xc2, 0x1f, 0xc1, 0xff, 0x07, 0x07, 0xfc, 0x1e, 0x0f, 0xf8, 0x00, 0x8f, 0xff, 0x87, 0xff, 
  0xf0, 0x07, 0xc0, 0x1f, 0xc1, 0xfe, 0x07, 0x07, 0xfc, 0x1e, 0x07, 0xf0, 0x00, 0x8f, 0xff, 0x8f, 
  0xff, 0xf0, 0x07, 0xe0, 0x1f, 0xc0, 0x00, 0x07, 0x07, 0xfc, 0x1e, 0x00, 0x00, 0x00, 0x00, 0x03, 
  0x80, 0x03, 0xf8, 0x0f, 0xe0, 0x1f, 0xc0, 0x00, 0x0f, 0x07, 0xfc, 0x1f, 0x00, 0x00, 0x00, 0x00, 
  0x03, 0x80, 0x01, 0xf8, 0x0f, 0xe0, 0x1f, 0xe0, 0x00, 0x0f, 0x07, 0xfc, 0x1f, 0x00, 0x00, 0x00, 
  0x00, 0x03, 0x00, 0x03, 0xf8, 0x0f, 0xe0, 0x3f, 0xf0, 0x00, 0x1f, 0x07, 0xfc, 0x1f, 0x80, 0x08, 
  0x00, 0x00, 0x03, 0x00, 0x03, 0xf8, 0x0f, 0xf0, 0x3f, 0xf8, 0x00, 0x7f, 0x07, 0xfc, 0x1f, 0xe0, 
  0x3c, 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 
  0xff, 0xff, 0xc0, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 
  0xff, 0xff, 0xff, 0xc0, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x83, 0xff, 0x7d, 0xfe, 0x7f, 
  0xf7, 0xfb, 0xef, 0xf0, 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x7d, 0xff, 0x3d, 0xfe, 
  0x7f, 0xf7, 0xf9, 0xef, 0xf7, 0xc0, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x7e, 0xff, 0x5d, 
  0xfe, 0x7f, 0xf7, 0xfa, 0xef, 0xf7, 0xc0, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe, 0xfe, 0xff, 
  0x4d, 0xfe, 0x7f, 0xf7, 0xfa, 0xef, 0xf3, 0xc0, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe, 0xfe, 
  0xff, 0x6d, 0xfe, 0x7f, 0xf7, 0xfb, 0x6f, 0xf0, 0x40, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe, 
  0xfe, 0xff, 0x75, 0xfe, 0x7f, 0xf7, 0xfb, 0xaf, 0xf7, 0xc0, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 
  0xff, 0x7e, 0xff, 0x71, 0xfe, 0x7f, 0xf7, 0xfb, 0x8f, 0xf7, 0xc0, 0xff, 0xff, 0xff, 0xff, 0xff, 
  0xff, 0xff, 0x3d, 0xff, 0x79, 0xfe, 0x7f, 0xf7, 0xfb, 0xcf, 0xf7, 0xc0, 0xff, 0xff, 0xff, 0xff, 
  0xff, 0xff, 0xff, 0x83, 0xff, 0x7d, 0xff, 0x0f, 0xf7, 0xfb, 0xef, 0xf8, 0x00
};

// Array of all bitmaps for convenience. (Total bytes used to store images in PROGMEM = 784)
const int epd_bitmap_allArray_LEN = 1;
const unsigned char* epd_bitmap_allArray[1] = {
  epd_bitmap_eeworldonlineblack_135x45
};

float xavg=0, yavg=0, zavg=0;
float xcur=0, ycur=0, zcur=0;
float xnxt=0, ynxt=0, znxt=0;
int steps = 0;

void setup() {
ssd1306_init();
/// adxl345_init();

if (!mpu.begin()) {
    Serial.println("Sensor init failed");
    while (1)
      yield();
  }
  
MPU6050_init();
read_av_acc();
}

void loop() {
for(int i = 0; i<50; i++){
Wire.beginTransmission(0x68);
Wire.write(0x32); 
Wire.endTransmission();
Wire.requestFrom(0x68, 1);
byte x0 = Wire.read();

Wire.beginTransmission(0x68);
Wire.write(0x33); 
Wire.endTransmission();
Wire.requestFrom(0x68, 1);
byte x1 = Wire.read();
x1 = x1 & 0x03;

uint16_t x = (x1 << 8) + x0;
int16_t xf = x;
if(xf > 511)
{
xf = xf - 1024;
}
float xa = xf * 0.004;
xcur = xcur + xa;


Wire.beginTransmission(0x68);
Wire.write(0x34); 
Wire.endTransmission();
Wire.requestFrom(0x68, 1);
byte y0 = Wire.read();

Wire.beginTransmission(0x68);
Wire.write(0x35); 
Wire.endTransmission();
Wire.requestFrom(0x68, 1);
byte y1 = Wire.read();
y1 = y1 & 0x03;

uint16_t y = (y1 << 8) + y0;
int16_t yf = y;
if(yf > 511)
{
yf = yf - 1024;
}
float ya = yf * 0.004;
ycur = ycur + ya;


Wire.beginTransmission(0x68);
Wire.write(0x36); 
Wire.endTransmission();
Wire.requestFrom(0x68, 1);
byte z0 = Wire.read();

Wire.beginTransmission(0x68);
Wire.write(0x37); 
Wire.endTransmission();
Wire.requestFrom(0x68, 1);
byte z1 = Wire.read();
z1 = z1 & 0x03;

uint16_t z = (z1 << 8) + z0;
int16_t zf = z;
if(zf > 511)
{
zf = zf - 1024;
}
float za = zf * 0.004;
zcur = zcur + za;
}
xcur = xcur/50; 
ycur = ycur/50;
zcur = zcur/50;

float acc = sqrt(((xcur - xavg) * (xcur - xavg)) + 
               ((ycur - yavg) * (ycur - yavg)) + 
               ((zcur - zavg) * (zcur - zavg)));
delay(250);

//Next Acceleration
for(int i = 0; i<50; i++){
Wire.beginTransmission(0x68);
Wire.write(0x32); 
Wire.endTransmission();
Wire.requestFrom(0x68, 1);
byte x0 = Wire.read();

Wire.beginTransmission(0x68);
Wire.write(0x33); 
Wire.endTransmission();
Wire.requestFrom(0x68, 1);
byte x1 = Wire.read();
x1 = x1 & 0x03;

uint16_t x = (x1 << 8) + x0;
int16_t xf = x;
if(xf > 511)
{
xf = xf - 1024;
}
float xa = xf * 0.004;
xnxt = xnxt + xa;


Wire.beginTransmission(0x68);
Wire.write(0x34); 
Wire.endTransmission();
Wire.requestFrom(0x68, 1);
byte y0 = Wire.read();

Wire.beginTransmission(0x68);
Wire.write(0x35); 
Wire.endTransmission();
Wire.requestFrom(0x68, 1);
byte y1 = Wire.read();
y1 = y1 & 0x03;

uint16_t y = (y1 << 8) + y0;
int16_t yf = y;
if(yf > 511)
{
yf = yf - 1024;
}
float ya = yf * 0.004;
ynxt = ynxt + ya;


Wire.beginTransmission(0x68);
Wire.write(0x36); 
Wire.endTransmission();
Wire.requestFrom(0x68, 1);
byte z0 = Wire.read();

Wire.beginTransmission(0x68);
Wire.write(0x37); 
Wire.endTransmission();
Wire.requestFrom(0x68, 1);
byte z1 = Wire.read();
z1 = z1 & 0x03;

uint16_t z = (z1 << 8) + z0;
int16_t zf = z;
if(zf > 511)
{
zf = zf - 1024;
}
float za = zf * 0.004;
znxt = znxt + za;
}
xnxt = xnxt/50; 
ynxt = ynxt/50;
znxt = znxt/50;

float acc2 = sqrt(((xnxt - xavg) * (xnxt - xavg)) + 
               ((ynxt - yavg) * (ynxt - yavg)) + 
               ((znxt - zavg) * (znxt - zavg)));

if (acc2-acc > 0.05)
    {
      steps = steps + 1;
    }

display.clearDisplay();
display.setTextSize(2); 
display.setTextColor(SSD1306_WHITE);
display.setCursor(0, 0);
display.print("steps: ");
display.println(steps);
display.setTextColor(SSD1306_BLACK, SSD1306_WHITE); // Draw 'inverse' text
display.print("Distance: ");
display.print(steps*0.3048);
display.println("M");
display.display();
delay(400);
}

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

Wire.beginTransmission(0x68);
Wire.write(0x2C); 
Wire.write(0x08); 
Wire.endTransmission();

Wire.beginTransmission(0x68);
Wire.write(0x31); 
Wire.write(0x08); 
Wire.endTransmission();

Wire.beginTransmission(0x68);
Wire.write(0x2D); 
Wire.write(0x08); 
Wire.endTransmission();

}

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

Wire.beginTransmission(0x68);
Wire.write(0x2C); 
Wire.write(0x08); 
Wire.endTransmission();

Wire.beginTransmission(0x68);
Wire.write(0x31); 
Wire.write(0x08); 
Wire.endTransmission();

Wire.beginTransmission(0x68);
Wire.write(0x2D); 
Wire.write(0x08); 
Wire.endTransmission();

}

void ssd1306_init(){
display.begin(SSD1306_SWITCHCAPVCC);
display.display();
delay(1000);
display.clearDisplay();
display.drawBitmap(0, 0, epd_bitmap_eeworldonlineblack_135x45, 130, 45, WHITE);
display.display();
delay(1000);
display.clearDisplay();
display.setTextSize(2); 
display.setTextColor(SSD1306_WHITE);
display.setCursor(0, 0);
display.println("STEPS");
display.println("COUNTER");
display.display();
delay(1000);
}

float read_av_acc(){
for(int i = 0; i<50; i++){
Wire.beginTransmission(0x68); // was: 0x53
Wire.write(0x3B);             // was: 0x32
Wire.endTransmission();
Wire.requestFrom(0x68, 1);
byte x0 = Wire.read();

Wire.beginTransmission(0x68);
Wire.write(0x3C);           // was: 0x33 
Wire.endTransmission();
Wire.requestFrom(0x68, 1);
byte x1 = Wire.read();
x1 = x1 & 0x03;

uint16_t x = (x1 << 8) + x0;
int16_t xf = x;
if(xf > 511)
{
xf = xf - 1024;
}
float xa = xf * 0.004;
xavg = xavg + xa;


Wire.beginTransmission(0x68);
Wire.write(0x34); 
Wire.endTransmission();
Wire.requestFrom(0x68, 1);
byte y0 = Wire.read();

Wire.beginTransmission(0x68);
Wire.write(0x35); 
Wire.endTransmission();
Wire.requestFrom(0x68, 1);
byte y1 = Wire.read();
y1 = y1 & 0x03;

uint16_t y = (y1 << 8) + y0;
int16_t yf = y;
if(yf > 511)
{
yf = yf - 1024;
}
float ya = yf * 0.004;
yavg = yavg + ya;


Wire.beginTransmission(0x68);
Wire.write(0x36); 
Wire.endTransmission();
Wire.requestFrom(0x68, 1);
byte z0 = Wire.read();

Wire.beginTransmission(0x68);
Wire.write(0x37); 
Wire.endTransmission();
Wire.requestFrom(0x68, 1);
byte z1 = Wire.read();
z1 = z1 & 0x03;

uint16_t z = (z1 << 8) + z0;
int16_t zf = z;
if(zf > 511)
{
zf = zf - 1024;
}
float za = zf * 0.004;
zavg = zavg + za;
}
xavg = xavg/50; 
yavg = yavg/50;
zavg = zavg/50;
}

Thanks.
I just changed I2C address and hooked a MPU6050 to I2C pins: 21/22

I may have misunderstood, I think the adxl345_init(); is just a function, the original sketch even doesn't included ADXL345 library. that's why I just renamed it as: MPU6050_init()

OK, I don't know either part at this level. I use the MPU6050 only because it is part of quite a few things I have.

So I wonder if the two can just be interchanged, and woukd be surprised if they could.

So answer that question first. Do you need to do more than change a part and its I2C address (and maybe details around instantiating and initialising the object)?

A look at the library examples for the two parts might shed light on this angle.

Then, I would reduce the skecth to the proverbial minimum reproducible error stage - strip out all the junk except the things that part, or the one you seek to replace it with, does.

Just read the sensor and report the values.

Then see if it crashes. Then start adding back in the fun stuff.

HTH

a7

1 Like

Thank you.
Yes, that need some more works on it.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.