SparkFun Forums 

Where electronics enthusiasts find answers.

General project discussion / help
Did you make a robotic coffee pot which implements HTCPCP and decafs unauthorized users? Show it off here!
By BigRixon
#180186
Hey guys, electronics novice here but I am having a go at it. Firstly yes i have googled. Lots... and lots. Hopefully this is an easy fix but this is what I want to achieve.

The job is for a metal fabrication workshop for their sheet metal "folder". I want to measure the angle of the fold by attaching an ADXL345. You can probably buy these pre made but I wanted to have a go myself and well.... I'm stuck.

I'll be using ADXL345 from SparkFun https://www.sparkfun.com/products/9836

And I will be displaying the calculated angle on an Adafruit 1.2" 7 segment display. It needs to be easy to read from a distance, hence the large display. https://www.adafruit.com/product/1270

I'm currently using an Arduino Uno for prototyping but the finished product I'll be using a 5v pro mini (already have one). Yes I know the ADXL345 is 3.3v, i'll be using LLC.

The ADXL will be approx 1.5m from the power supply so I am hoping voltage drop won't affect my readings too much.

The problem is (with my current code) it hangs after a few seconds of operation and needs to be power cycled to make it start up again.

Here's a sample of Serial Monitor during those few seconds
Code: Select all
14.00 -9.00 9.00 0.00-18.00 27.00 9.00 18.00 -18.00 18.00 18.00 36.00 23.00
Yes I have covered the basics. Connections are solid. I2C working (have used I2C scanner and both devices appear).

Finally here is my code. Any help you can offer is greatly appreciated.

Thanks!
Code: Select all
#include <Wire.h>
#include <math.h>

#include "Adafruit_LEDBackpack.h"
#include "Adafruit_GFX.h"

#define X0 0x32
#define X1 0x33
#define Y0 0x34
#define Y1 0x35
#define Z0 0x36
#define Z1 0x37

Adafruit_7segment matrix = Adafruit_7segment();

void setup()
{
  Wire.begin();        // join i2c bus (address optional for master)
  
  // Activate the 7-segment i2c
  matrix.begin(0x70);
  
  //Turning on the ADXL345
  Wire.beginTransmission(0x1D); // transmit to ADXL345
  Wire.write(0x2D);              // POWER_CTL
  Wire.write(0x09);              // measurement mode, 4hz wakeup
  Wire.endTransmission();       // stop transmitting
  
  Serial.begin(9600);
}

int num_samples = 10;    // take the average of multiple samples

void loop()
{
  float sum = 0.0f;
  
  for (int i=0; i < num_samples; i++)
  {
    float z = getZ();  // +z is up from the top of the pcb
    float y = getY();  // +y is lengthwise to the right
    
    float angle = atan2(z, y) * 180.0f / M_PI;  // angle will be between -360 and 360 degrees
    sum += angle;
    delay(15);
  } 
  
  float average = round(sum / num_samples);
  matrix.printFloat(average);
  matrix.writeDisplay();
  
  Serial.print(average);
}

byte requestByte(char dir)
{
  Wire.beginTransmission(0x1D); // transmit to ADXL345
  Wire.write(dir);
  Wire.endTransmission(); // stop transmitting
  Wire.requestFrom(0x1D, 1); // request 1 byte from ADXL345
  while(Wire.available())
  {
    return(Wire.read()); //  
  }
}

int getX()
{
  int val = requestByte(X0);
  val += requestByte(X1) << 8;
  return val;
}

int getY()
{
  int val = requestByte(Y0);
  val += requestByte(Y1) << 8;
  return val;
}

int getZ()
{
  int val = requestByte(Z0);
  val += requestByte(Z1) << 8;
  return val;
}
By BigRixon
#180247
Thank you for your reply. I was just thinking about this yesterday and was wondering if I am missing something as simple as you have mentioned. I'm not actually using any pull-ups. What value should i be using? 10K? I am not using a LLC yet as I am still using my Uno R3 for prototyping. But final product i will be using SparkFun's Bi-Directional one https://www.sparkfun.com/products/12009

Thanks for your help.
Mee_n_Mac wrote:What value are the I2C pull-ups on both sides of the level converter ? What level converter are you using ?
By Mee_n_Mac
#180254
I have no idea how you have any good, let alone "solid", communications over I2C w/o any pull-ups. IIRC some thing like 4.7k is standard, though exact values aren't critical w/most buses. In your case that would be to 3.3v. And hope that the 5v Ardunio will recognize 3.3v as a logic HIGH. You might also try slowing the bus speed down drastically.