Arduino R4 WiFi printing on OLED accelerometer readings

Hello,

I got an R4 WiFi with Arduino Sensor Kit Base (sensors still attached, I simply plug the shield).

When you run the combo demo it freezes. I tried using different sensors independently and it works.

What it's failing is reading accelerometer and printing on OLED. It does work reading accelerometer and printing on serial though.

I even tried putting some crazy delay between reading the sensor and printing to OLED, to no avail.

I assume I am missing to set the appropriate addresses for each I2C device...but have no idea how to do it.

#include "Arduino_SensorKit.h"

float x = 0;

void setup() {
  
  Accelerometer.begin();

  delay(500);

  Oled.begin();
  Oled.setFlipMode(true);
  Oled.setFont(u8x8_font_amstrad_cpc_extended_r); 
}

void loop() {

  // Read sensor
  x = Accelerometer.readX();

  delay(500);

  Oled.setCursor(0, 0);
  Oled.print("X: ");
  Oled.print(x);
  Oled.print("   ");

  delay(500);

}

I even tried the following, bypassing standard accelerometer setup and manually setting the address (it works fine if I comment the OLED piece, but if I keep it it reads 0):

#include "Arduino_SensorKit.h"
#include "LIS3DHTR.h"
#include <Wire.h>
LIS3DHTR<TwoWire> LIS; //IIC
#define WIRE Wire

float x = 0;

void setup() {
  
  Serial.begin(9600);  // Initialize Serial for debugging
  while(!Serial);

  LIS.begin(WIRE, 0x19);
  delay(100);
  if (!LIS) {
    Serial.println("Accelerometer initialization failed");
  } else {
    Serial.println("Accelerometer initialized successfully");
    LIS.setOutputDataRate(LIS3DHTR_DATARATE_50HZ);
    LIS.setHighSolution(true); //High solution enable
  }


  if (!Oled.begin()) {
    Serial.println("OLED initialization failed");
  } else {
    Serial.println("OLED initialized successfully");
    
    Oled.setFlipMode(true);
    Oled.setFont(u8x8_font_amstrad_cpc_extended_r); 
  }
}


void loop() {
  // Read sensor
  x = LIS.getAccelerationX();

  Oled.setCursor(0, 0);
  Oled.print("X: ");
  Oled.print(x);
  Oled.print("   ");
}

I tried running an i2c scanner. Found that the addresses are the following:
0x19
0x38
0x3C
0x77

I therefore tried using the other library for the OLED, and draw a line with a point depending on the accelerometer value. I selected this low level implementation because it allows me to manually set i2c address. I tried to set in turn all addresses above, and even some random ones, to no avail. The line is drawn, but the accelerometer value read is always 0 (as in other failed experiments)

#include <Arduino.h>
#include "Arduino_SensorKit.h"
#include <U8g2lib.h>

#ifdef U8X8_HAVE_HW_SPI
#include <SPI.h>
#endif
#ifdef U8X8_HAVE_HW_I2C
#include <Wire.h>
#endif

U8G2_SSD1306_128X64_NONAME_F_SW_I2C u8g2(U8G2_R0, /* clock=*/ SCL, /* data=*/ SDA, /* reset=*/ U8X8_PIN_NONE);   // All Boards without Reset of the Display

void setup(void) {
  u8g2.setI2CAddress(0x77); // trying to set an address to avoid conflict
  u8g2.begin();
  Accelerometer.begin();
}

void loop(void) {
  u8g2.clearBuffer();
  u8g2.drawLine(Accelerometer.readX()*100, 10, 40, 55);
  u8g2.sendBuffer();

  delay(100);
}

Solved. There was no error in my code. Apparently the last library of arduino sensor 1.0.10 is bugged. Reverting to 1.0.8 made the trick.

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