Pages: [1]   Go Down
Author Topic: Arduino with MMA7455 using I2C  (Read 3047 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 1
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I have searched many websites for whole week in order to get my MMA7455 work. But I just couldn't get it right!

my code is as below. and the result always showed " X: 0   Y: 0   Z: 0"

Is there anyone can help me? I am so sad.....

thanks lot.

#include <Wire.h>
#define ADDRESS 0x1D //Address for Accelerometer

//REGISTERS
#define MODE_CONTROL  0x16 //Mode control register
#define X_OUT      0x06 //8 bit register containing value for X
#define Y_OUT      0x07 //8 bit register containing value for Y
#define Z_OUT      0x08 //8 bit register containing value for Z
#define SETUP     0x05 //MESUREMENT MODE, 2g

//required setup function
void setup() {
    Wire.begin();
    Serial.begin(9600);
    I2C_Write(MODE_CONTROL, SETUP);
}

//required loop function
void loop() {
    Serial.print("X: ");
    Serial.print(I2C_Read(X_OUT));
    Serial.print("   Y: ");
    Serial.print(I2C_Read(Y_OUT));
    Serial.print("   Z: ");
    Serial.println(I2C_Read(Z_OUT));
    delay(100);
}

//function to write byte data into a register
void I2C_Write(byte REG, byte data) {
    Wire.beginTransmission(ADDRESS);   //device address 0x1D
    Wire.write(REG);                  //0x16
    Wire.write(data);                 //measurement mode,2g
    Wire.endTransmission();
}

//function to read byte data from a register
byte I2C_Read(byte REG){
    byte val = 0x00;
    Wire.beginTransmission(ADDRESS);
    Wire.write(REG);
    Wire.endTransmission();  
    Wire.requestFrom(ADDRESS, 1);
    if(Wire.available() > 0) {
     val = Wire.read();
    }
    Wire.endTransmission();
    return val;
}
 




Logged

Offline Offline
Edison Member
*
Karma: 9
Posts: 1016
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Wire.endTransmission returns an error.
You could use Serial.println to display that error.

First of all, run a scan sketch: http://arduino.cc/playground/Main/I2cScanner
Is the address okay, then check the error of Wire.endTransmission.
http://arduino.cc/en/Reference/WireEndTransmission
Is that okay, then check the datasheet if you set the registers to the proper values.
Is that okay, then do it tomorrow all over again.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 4
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi all. Need your help with MMA7455.

I've just rceived  my MMA7455 sensor? and i couldn't get it work.

As Krodal suggested before I've done such steps:

1) Run a scan sketch: http://arduino.cc/playground/Main/I2cScanner
    Result:
Code:
Scanning...
I2C device found at address 0x1D !
done

So, as far as I undestand, that means MMA7455 responded for a package with its correct adress.

2)  If I run such code as the author of this topic, I get:
Code:
X: 0   Y:
only once, and thats all! It doesn't send anithing more...

3) If I run such code:
Code:
#include <Wire.h>
#define ADDRESS 0x1D //Address for Accelerometer

//REGISTERS
#define MODE_CONTROL  0x16 //Mode control register
#define SETUP     0x05 //MESUREMENT MODE, 2g

byte error;

//required setup function
void setup() {
    Wire.begin();
    Serial.begin(9600);
}

//required loop function
void loop() { 
    Serial.println("Trying to write setup to MMA7455");   
    Wire.beginTransmission(ADDRESS);   //device address 0x1D
    Wire.write(MODE_CONTROL);          //0x16
    Wire.write(SETUP);                 //measurement mode,2g
    error = Wire.endTransmission();   
    Serial.print(F("error = "));
    Serial.println(error, DEC);
    Serial.println();
   
    delay(1000);
   
    Serial.println("Trying to read some registers from MMA7455"); 
    Serial.println("...Sending register adress...");
    byte val = 0x00;
    Wire.beginTransmission(ADDRESS);
    Wire.write(MODE_CONTROL);
    error = Wire.endTransmission();
    Serial.print(F("error = "));
    Serial.println(error, DEC);
    Serial.println("...Reading values from register...");
    Wire.requestFrom(ADDRESS, 1);
    if(Wire.available() > 0) {
     val = Wire.read();
    }
    Serial.print("value = ");
    Serial.println(val);
    error = Wire.endTransmission();
    Serial.print(F("error = "));
    Serial.println(error, DEC);
   
    delay(10000);
}


 I get:
Code:
Trying to write setup to MMA7455
error = 3

Trying to read some registers from MMA7455
...Sending register adress...
error = 3
...Reading values from register...
value = 0
error = 0

As I understand, I can't write to MMA7455 (even write a register adress) , so I can't get anything from it.

Any ideas why it does't work?
Logged

Offline Offline
Edison Member
*
Karma: 9
Posts: 1016
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hello,
You could have started a new topic, but it is okay.

The i2c_scanner found your sensor !  So everything should be working.
If the sensor sees its address on the i2c bus, it acknowledges that.

I have also made a ready-to-go sketch : http://arduino.cc/playground/Main/MMA7455
If you have problems with writing a single byte (the function at the bottom om the sketch), I could write a simple function for that. The current function is like the function to read a byte.

Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 4
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Krodal, thanks fo reply.

Now I see, that I didn't wrote about using your sketch. First of all I tried it, but it didn't work.

Dont remember exactly, but it said something like "Check your wiring..." then said "Status 9E" and stopped.
And if disconnect the sensor it said the same, plus "WHOAMI 9E", "TOUT 127" and  started to print lines like "error 3 X 0 Y 0 Z 0"

When I get home I'l post Serial Monitor output.

Also I tried to use MMA7455 library (from Arduino playground) but it also didn't work.
And then only I started to search same problems and found this topic  smiley

I have read your advice to get the error code of Wire.endTransmission, so I wrote that sketch to chek.
In the output of that sketch you can see, that I'm receiving Wire.endTransmission error code "3 - received NACK on transmit of data" , when I'm trying to write a register adress, whitch I whant to read... but I don't now what to do with this error code smiley

Any suggestions?
Logged

Offline Offline
Edison Member
*
Karma: 9
Posts: 1016
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

If the I2C was working, my sketch would be working.

Are you sure that the i2c_scanner did see it, and still your and my sketch return an error ? That's weird.
Check the I2C wiring and the power to the board very carefull.

How did you connect it, can you upload a photo or a drawing ?
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 4
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

2 Krodal:

First of all I've to say that I used to program AVR chips in WinAVR & CodeVision using C some time ago. But this one is my first and only Arduino board and my firs and only i2c device (sensor). So I can't chek if there any problems with my board or with my i2c sensor.

All I can do is to chek step-by-step what is wrong. MMA7455 is connected to the board in usual way - like on this pic:


1)
If I run i2c_scanner with disconnected sensor, I get:
Code:
Scanning...
No I2C devices found

If I run i2c_scanner with connected sensor, I get:
Code:
Scanning...
I2C device found at address 0x1D !
done

2)
If I run your code with connected sensor, I get:
Code:
Freescale MMA7455 accelerometer
May 2012
Check your wiring !
ADRESS : 9E
STATUS : 9E
and sketch stops (I think that it stops because I see nothing more in serial monitor for a long time)

At this moment I think my that I can read my sensors register and sensor detects its adress, but I can't wright anything to the sensor.
 Why? I dont understand....
 
« Last Edit: August 23, 2012, 02:10:23 pm by Vadimi4 » Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 4
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Finaly I'v got it!!!!!

In my case sensor works OK ONLY witout pull up resistors on i2c lines!

Now i get:

Code:
Freescale MMA7455 accelerometer
May 2012
The MMA7455 is okay
ADRESS : 1D
STATUS : 0
TOUT : 0
error = 0, xyz g-forces = 0.000, -0.031, 1.000
error = 0, xyz g-forces = 0.000, -0.016, 0.984
error = 0, xyz g-forces = 0.000, -0.016, 0.984
error = 0, xyz g-forces = 0.016, -0.031, 1.01
6
error = 0, xyz g-forces = 0.016, 0.000, 1.031
error = 0, xyz g-forces = 0.031, -0.016, 1.000
error = 0, xyz g-forces = -0.203, -1.141, 1.000
error = 0, xyz g-forces = 0.891, 0.125, 1.016
error = 0, xyz g-forces = 0.344, -0.594, 1.016
error = 0, xyz g-forces = -0.031, 0.063, 0.609
error = 0, xyz g-forces = -0.047, -0.266, 1.641
error = 0, xyz g-forces = 0.609, -0.391, 1.859
error = 0, xyz g-froces = -0.047, 0.063, 1.281
error = 0, xyz g-forces = -0.016, 0.094, -0.469
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 5
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi Vadimi4
What software is used to produce this image?


thanks
Logged

Topsham, Vermont USA
Offline Offline
Edison Member
*
Karma: 33
Posts: 1940
... in The Woods In Vermont
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

How-To page and example sketch for MMA7455 is here:
http://arduino-info.wikispaces.com/accelerometer-MMA7455

(That diagram was made with "Fritzing")..
Logged

Regards, Terry King terry@yourduino.com  - Check great prices, devices and Arduino-related boards at http://YourDuino.com
HOW-TO: http://ArduinoInfo.Info

Pages: [1]   Go Up
Jump to: