Wemos Lolin with OLEd - Noob I2C question

Piie_ESP32
Posts: 2
Joined: Fri Oct 30, 2020 4:52 pm

Wemos Lolin with OLEd - Noob I2C question

Postby Piie_ESP32 » Fri Oct 30, 2020 5:10 pm

Hi

I have a Wemos LOLIN32 with ESP-Wroom chip and built-in OLED display that I am trying to connect to a LIS331 accellerometer via I2C.

The "problem"is that the built-in OLED is also connected via I2C on pins 4 and 5 "internally", and all examples I could find to drive the display uses the Wire.h library to do that.

All the examples for the LIS331 Sparkfun board says to use the I2C Master external library.

I cannot get the two to work together so to speak.

I don't know where/how to connect the LIS331 now ( at first I tried pins 4 and 5, but I suspect I cannot do that since the display uses it), and then I tried 12 and 13, but I don't know where/how/if I can specify it in the code...

The below code is a Frankenstein attempt to merge the two examples for the Screen and the LIS331 I found so far, but it will not compile...

Any help/pointers would be much appreciated!

Code: Select all



#include <I2C.h>
#include <LIS331HH.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>

#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64




#define ACCELEROMETER_ADDR 0x18 // Alternative: 0x19

static LIS331 accelerometer{ACCELEROMETER_ADDR};

Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);

void print_err(byte err_code)
{
    switch (err_code) {
    case START:
        Serial.println(F("START"));
        break;
    case REPEATED_START:
        Serial.println(F("REPEATED_START"));
        break;
    case MT_SLA_ACK:
        Serial.println(F("MT_SLA_ACK"));
        break;
    case MT_SLA_NACK:
        Serial.println(F("MT_SLA_NACK"));
        break;
    case MT_DATA_ACK:
        Serial.println(F("MT_DATA_ACK"));
        break;
    case MT_DATA_NACK:
        Serial.println(F("MT_DATA_NACK"));
        break;
    case MR_SLA_ACK:
        Serial.println(F("MR_SLA_ACK"));
        break;
    case MR_SLA_NACK:
        Serial.println(F("MR_SLA_NACK"));
        break;
    case MR_DATA_ACK:
        Serial.println(F("MR_DATA_ACK"));
        break;
    case MR_DATA_NACK:
        Serial.println(F("MR_DATA_NACK"));
        break;
    case LOST_ARBTRTN:
        Serial.println(F("LOST_ARBTRTN"));
        break;
    case E_OK:
        Serial.println(F("E_OK"));
        break;
    case E_WRONG_INTERRUPT:
        Serial.println(F("E_WRONG_INTERRUPT"));
        break;
    case E_NUM_TOO_BIG:
        Serial.println(F("E_NUM_TOO_BIG"));
        break;
    case E_WRONG_SCALE:
        Serial.println(F("E_WRONG_SCALE"));
        break;
    case E_CONV_ERROR:
        Serial.println(F("E_CONV_ERROR"));
        break;
    default:
        Serial.print(F("Unknown error message: 0x"));
        Serial.println(err_code, HEX);
        break;
    }
}



void testdrawchar(void) {
  display.clearDisplay();

  display.setTextSize(1);      // Normal 1:1 pixel scale
  display.setTextColor(SSD1306_WHITE); // Draw white text
  display.setCursor(0, 0);     // Start at top-left corner
  display.cp437(true);         // Use full 256 char 'Code Page 437' font

  // Not all the characters will fit on the display. This is normal.
  // Library will draw what it can and the rest will be clipped.
// for(int16_t i=0; i<256; i++) {
//    if(i == '\n') display.write(' ');
//    else          display.write(i);
//  }
  display.write('!');
  display.write(' ');
  display.write('H');
  display.write(' ');
  display.write('A');
  display.write(' ');
  display.write('L');
  display.write(' ');
  display.write('L');
  display.write(' ');
  display.write('O');
  display.write(' ');
  display.display();
  delay(2000);
}
 
void setup() {
  Serial.begin(115200);

    uint8_t err = accelerometer.setRebootMemoryContent(true); // Reset sensor register values to default
    print_err(err);
    bool rebootInProgress = true;
    while (rebootInProgress && err == E_OK) {
        err = accelerometer.getRebootMemoryContent(rebootInProgress);
    }
    err = accelerometer.setPowerMode(LIS331::PowerMode::normalMode);
    print_err(err);
    err = accelerometer.setDataRate(LIS331::DataRate::odr1000Hz);
    print_err(err);
    err = accelerometer.setBDUEnabled(true);
    print_err(err);
    err = accelerometer.setScale(LIS::Scale::scale24g);
    print_err(err);
    err = accelerometer.setAxisEnabled(LIS::Axis::X, true);
    print_err(err);
    err = accelerometer.setAxisEnabled(LIS::Axis::Y, true);
    print_err(err);
    err = accelerometer.setAxisEnabled(LIS::Axis::Z, true);
    print_err(err);
  
  // Start I2C Communication SDA = 5 and SCL = 4 on Wemos Lolin32 ESP32 with built-in SSD1306 OLED
 Wire.begin(5, 4);
 Wire.begin(13,12);

  if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C, false, false)) {
    Serial.println(F("SSD1306 allocation failed"));
    for(;;);
  }
  //delay(2000); // Pause for 2 seconds
 
  // Clear the buffer.
  display.clearDisplay();
  testdrawchar();
  display.clearDisplay();
 
}
 
void loop() {

 int16_t x = 0, y = 0, z = 0; // Raw values. To get them in [g] use `getAxisValuesG()`
    accelerometer.getAxisValue(LIS::Axis::X, x);
    accelerometer.getAxisValue(LIS::Axis::Y, y);
    accelerometer.getAxisValue(LIS::Axis::Z, z);

    bool xOverran = false, yEnabled = false, zAvailable = false;
    accelerometer.isAxisDataOverrun(LIS::Axis::X, xOverran);
    accelerometer.isAxisEnabled(LIS::Axis::Y, yEnabled);
    accelerometer.isAxisDataAvailable(LIS::Axis::Z, zAvailable);

    Serial.print(F("X: "));
    Serial.print(x);
    Serial.print(F(", Y: "));
    Serial.print(y);
    Serial.print(F(", Z: "));
    Serial.print(z);
    Serial.print(F(", X overran: "));
    Serial.print(xOverran);
    Serial.print(F(", Y enabled: "));
    Serial.print(yEnabled);
    Serial.print(F(", Z available: "));
    Serial.println(zAvailable);

  
}

Who is online

Users browsing this forum: No registered users and 11 guests