Uno, Accelerometer and OLED Woes

I’m new to this and i’m having the hardest time getting readings from the accelerometer when I have data going to the OLED. None of my pins are crossing so I’m not really sure the issue. Both are using SPI but I’ve assigned all their own pins.
I’m using an adafruit LIS3DH as well as a sparkfun micro oled breakout

Does something in my testing code look off? Is there a better way to approach this?

// Basic demo for accelerometer readings from Adafruit LIS3DH

#include <Wire.h>
#include <SPI.h>
#include <Adafruit_LIS3DH.h>
#include <Adafruit_Sensor.h>
#include <SFE_MicroOLED.h>  // Include the SFE_MicroOLED library


// Used for software SPI
#define LIS3DH_CLK 12 //SCL(SCK)
#define LIS3DH_MISO 6 //SDO
#define LIS3DH_MOSI 2 //SDA(SDI)
// Used for hardware & software SPI
#define LIS3DH_CS 10 //CS

//////////////////////////
// MicroOLED Definition //
//////////////////////////
#define PIN_RESET 9  // Connect RST to pin 9
#define PIN_DC    5  // Connect DC to pin 8
#define PIN_CS    3 // Connect CS to pin 10
#define DC_JUMPER 0


// software SPI
Adafruit_LIS3DH lis = Adafruit_LIS3DH(LIS3DH_CS, LIS3DH_MOSI, LIS3DH_MISO, LIS3DH_CLK);
// hardware SPI
//Adafruit_LIS3DH lis = Adafruit_LIS3DH(LIS3DH_CS);
// I2C
//Adafruit_LIS3DH lis = Adafruit_LIS3DH();

MicroOLED oled(PIN_RESET, PIN_DC, PIN_CS); // SPI declaration


#if defined(ARDUINO_ARCH_SAMD)
// for Zero, output on USB Serial console, remove line below if using programming port to program the Zero!
   #define Serial SerialUSB
#endif

void setup(void) {

  
#ifndef ESP8266
  while (!Serial);     // will pause Zero, Leonardo, etc until serial console opens
#endif


  Serial.begin(9600);

  
  Serial.println("LIS3DH test!");
  
  if (! lis.begin(0x18)) {   // change this to 0x19 for alternative i2c address
    Serial.println("Couldnt start");
    while (1);
  }
  

  
  Serial.println("LIS3DH found!");
  
  lis.setRange(LIS3DH_RANGE_4_G);   // 2, 4, 8 or 16 G!
  
  Serial.print("Range = "); Serial.print(2 << lis.getRange());  
  Serial.println("G");
   
  oled.begin();
  oled.begin();
  oled.clear(ALL);
}

void loop() {

  lis.read();      // get X Y and Z data at once
  // Then print out the raw data
  Serial.print("X:  "); Serial.print(lis.x); 
  Serial.print("  \tY:  "); Serial.print(lis.y); 
  Serial.print("  \tZ:  "); Serial.print(lis.z); 

  /* Or....get a new sensor event, normalized */ 
  sensors_event_t event; 
  lis.getEvent(&event);

  printTitle("Hello", 1);
  oled.display(); 
  
  /* Display the results (acceleration is measured in m/s^2) */
  Serial.print("\t\tX: "); Serial.print(event.acceleration.x);
  Serial.print(" \tY: "); Serial.print(event.acceleration.y); 
  Serial.print(" \tZ: "); Serial.print(event.acceleration.z); 
  Serial.println(" m/s^2 ");

  Serial.println();
 
  delay(200); 
}

void printTitle(String title, int font)
{
  int middleX = oled.getLCDWidth() / 2;
  int middleY = oled.getLCDHeight() / 2;
  
  oled.clear(PAGE);
  oled.setFontType(font);
  // Try to set the cursor in the middle of the screen
  oled.setCursor(middleX - (oled.getFontWidth() * (title.length()/2)),
                 middleY - (oled.getFontWidth() / 2));
  // Print the title:
  oled.print(title);
  oled.display();
  oled.clear(PAGE);
}

Hi,
Welcome to the forum.

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?

Did you write your code in stages?
That is,
did you write the OLED display on its own and get it working,
then,
write the accel code on its own and get it working.
then combine the two.

What exactly is not working?
What model Arduino are you using.
Please provide links to data/specs of your devices.

Thanks.. Tom... :slight_smile:

Didn’t realize i was missing so much info!
It’s an Arduino Uno r3.
I had both the accelerometer and the oled screen working independently using the example code for each and then pieced my above code together.
I will have to do a CAD version in a bit as I’m about to head out of town.

LIS3DH library:

micro oled library:

When i try to run my above code I can not get any readings from the accelerometer nor the screen. But if i comment out the items related to the oled screen then everything properly functions. I think the SPI may be conflicting somewhere but I’m new to this.

i'm having the hardest time getting readings from the accelerometer when I have data going to the OLED.

My belief is a conflict in the library resources.

I typically run OLEDs on the I2C bus.

Ray

I was able to get this working and figured out. Problem seemed to have been my pins, I was using hardware SPI for the OLED. Hardware SPI on the Uno uses pins 11, 12, and 13. You can't use pin 12 for software SPI on the LIS3DH - thanks to the help of one of the adafruit CS guys!

But why are you using software SPI in the first place? Why not just use the hardware SPI?