-
Notifications
You must be signed in to change notification settings - Fork 471
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Magnetometer only returns zeros #82
Comments
Shouldn't this line: m_x = (float)((Ax_1 << 8) | Ax_2); read: m_x = (float)((mx_1 << 8) | mx_2); ??? -----Original Message----- Hi! Your page is a great resource and help for the MPU-9250. I have been trying My initialization is: I2CSend(MPU9250_ADDRESS, PWR_MGMT_1, 0x01); // Auto-selecting best clock I read the values of the x-axis of the magnetometer the following way: mx_1 = I2C2Receive(MAG_ADDRESS, MAG_XOUT_H); mx_1, mx_2 and m_x are defined as uint16_t #define MAG_ADDRESS 0x0C #define MPU9250_ADDRESS 0x68 I also tried with different values of delay. I noticed if I use the Would you have any idea why it doesn't work? You are receiving this because you are subscribed to this thread. |
Oups sorry, my bad. Yes it should. I changed the names of the variables here to make it clearer to explain. I used Ax_1 and Ax_2 in my original program. |
So, does it work now? -----Original Message----- Oups sorry, my bad. Yes it should. I changed the names of the variables here You are receiving this because you commented. |
No, that wasn't the problem. I just made a stupid mistake when changing the name of the variables here. In my program it is Ax_1 and Ax_2. But I thought mx_1 and mx_2 would be more explicit. |
Your conversions are wrong, mx_1 and mx_2 should be uint8_t since they are just register reads (bytes), then m_x should be int16_t, and m_x = ((int16_t) mx_1 << 8) | mx_2; Then you can set float Mx = (float) m_x; Don't know if this is your problem though. If you can read thr WHO_AM_I refister successfully you should be able to read the data/ Are you also reading the Status register each time? You might need to leave a few tens of milliseconds for the I2C transaction to complete. I have no trouble reading mag data with the sketchin my github repository. |
The other thing is, these CJMCU modules (the purple ones) are crap; I bought two and got crazy values from the mag. |
You need to read the seven bytes starting with MX_L: #define MAG_XOUT_L 0x03 each time you read data; the last byte must be read or the device won't work properly. Check the AK8963C data sheet. |
Thank you for your fast answer! Thank you for pointing out the conversion problem. I corrected it but like you said it wasn't the problem. I read all the seven bytes like you suggested with >10ms delay after each reading and sending but it didn't help. So, I tried to read in a loop all the magnetometer values: WHO_AM_I (0x00) which gives 0x48 I don't know what the INFO value is but when it is going all wrong, -1 value everywhere, it gives 0xFF. I'm worried about the CNTL_1. If I set it to 0x16 and read it right after a delay it gives some weird values like 0x4f, 0x33,0x7B, etc.. |
I mentioned it earlier but when I try to send any value to PWR_MGMT_2, 0x00, 0x3F, etc... The modules' magnetometers stop working, return 0xFF to all registers from WHO_AM_I to CNTL_2 (-1 for magnetometers values). I can read it and it always returns 0x00. I think there might be a problem in that. Especially because both of them react the same way. The only way to get the magnetometers to "work" again is to remove command that changes the PWR_MGMT_2 register, physically power down the module and turn on again. Software reset (PWR_MGMT_1 to 0x80) doesn't work. |
Well, you have four options as I see it:
Not sure I can help you with 1), I don't have the time to debug your code Sorry I can't be of more help. Kris -----Original Message----- I mentioned it earlier but when I try to send any value to PWR_MGMT_2, 0x00, You are receiving this because you commented. |
Thank you for your help. I have also some experience with CJ. But when you don't have the money to buy good stuff, they are better than nothing. |
I figured it out. I forgot to set the filter with CONFIG. Now the values seem stable. |
Glad you got it working. -----Original Message----- I figured it out. I forgot to set the filter with CONFIG. Now the values You are receiving this because you commented. |
Hi there, Please Kryzzalid, can you explain how do you finally solve it? Because I have the same problem and I am not able to give a solution... |
Hi! My problem was a mistake I made when sending values to the registers. I forgot to specify the number of values I was sending. So, the configuration of the chip was all wrong that's why it didn't work. |
Many thanks! Maybe the mistake is in my code too... I am able to read ID register but when I try tor read Measurement Data or Control it only returns zeros. |
I had the same problem. I could read from every readable registers but the measurements were only zeros. I noticed there was a problem in the writing function when I wrote something in a register and read it right after, the value was different from what I wrote. |
I think I'm not configuring the register properly because when I select the MODE in Control 1 Register and I try to read this register it shows zero as well. Do you think I'm doing right? StartI2C(); StopI2C(); Many thanks. |
StartI2C(); The last part was wrong but it still does not work... |
I would say that the sequence seems to be alright but, I can't be sure since I have never seen those fonctions. How do you read the values from the registers? Because like Kriswiner mentioned, for the magnetometer AK8963 you have to read all the bytes: |
Are you setting INT_CFG for bypass mode? If not, you can't talk with the AK8963C. |
Hi again! Finally I was able to solve the problem but I still don't know how was wrong. I think it was the Start() and Stop() functions that I wrote here wrong. |
hello, i still have the same issue reading i get -1 void maginit(float* destination){
// // Configure the magnetometer for continuous read and highest resolution
}
// } } |
Did you put the MPU9250 in pass through mode?
…On Wed, Dec 28, 2016 at 4:50 PM, rajveersinhraol ***@***.***> wrote:
hello, i still have the same issue reading i get -1
void maginit(float* destination){
Mscale = MFS_16BITS; // MFS_14BITS or MFS_16BITS, 14-bit or 16-bit
magnetometer resolution
Mmode = 0x06; // Either 8 Hz 0x02) or 100 Hz (0x06) magnetometer data ODR
// scale resolutions per LSB for the sensors
uint8_t rawData[3]; // x/y/z gyro calibration data stored here
writeByte(0x0c, AK8963_CNTL, 0x00); // Power down magnetometer
Delayms(10);
writeByte(0x0c, AK8963_CNTL, 0x0F); // Enter Fuse ROM access mode
Delayms(10);
readBytes(0x0c, AK8963_ASAX, 3, &rawData[0]); // Read the x-, y-, and z-axis calibration values
destination[0] = (float)(rawData[0] - 128)/256.0f + 1.0f; // Return x-axis sensitivity adjustment values, etc.
destination[1] = (float)(rawData[1] - 128)/256.0f + 1.0f;
destination[2] = (float)(rawData[2] - 128)/256.0f + 1.0f;
writeByte(0x0c, AK8963_CNTL, 0x00); // Power down magnetometer
Delayms(10);
// // Configure the magnetometer for continuous read and highest resolution
// // set Mscale bit 4 to 1 (0) to enable 16 (14) bit resolution in CNTL
register,
// and enable continuous mode data acquisition Mmode (bits [3:0]), 0010
for 8 Hz and 0110 for 100 Hz sample rates
writeByte(0x0c, AK8963_CNTL,Mscale << 4 | Mmode); // Set magnetometer data
resolution and sample ODR
Delayms(10);
}
void readMagData(void)
{
uint8_t rawData[7]; // x/y/z gyro register data, ST2 register stored here,
must read ST2 at end of data acquisition
// if(readByte(0x0c, AK8963_ST1) & 0x01) { // wait for magnetometer data ready bit to be set
I2C_start(I2C2,0x0c<<1, I2C_Direction_Transmitter);
I2C_write(I2C2, AK8963_ST1 | 0x80); // write address to the slave
I2C_stop(I2C2);
I2C_start(I2C2,0x0c<<1, I2C_Direction_Receiver); // start a transmission in Master receiver mode
rawData[0] = I2C_read_ack(I2C2);
rawData[1] = I2C_read_ack(I2C2);
rawData[2] = I2C_read_ack(I2C2);
rawData[3] = I2C_read_ack(I2C2);
rawData[4] = I2C_read_ack(I2C2);
rawData[5]=I2C_read_ack(I2C2);
rawData[6]=I2C_read_ack(I2C2);
rawData[7]=I2C_read_nack(I2C2);
I2C_stop(I2C2);
mx = (int16_t)(((int16_t)rawData[2] << 8) | rawData[1]) ; // Turn the MSB and LSB into a signed 16-bit value
my = (int16_t)(((int16_t)rawData[4] << 8) | rawData[3]) ;
mz = (int16_t)(((int16_t)rawData[6] << 8) | rawData[5]) ;
// readByte(0x0c,AK8963_ST2)|0x08
// }
}
—
You are receiving this because you commented.
Reply to this email directly, view it on GitHub
<#82 (comment)>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/AGY1qmU8M7_vIWWEl1eiLIT4AVuJfez9ks5rMwPmgaJpZM4KkbbE>
.
|
i think so using |
i am using an stm32f4 with gcc on eclipse... |
Do you get the correct WHO_AM_I return from the AK8963C? Maybe the I2C
addressing is mesed up?
…On Wed, Dec 28, 2016 at 4:59 PM, rajveersinhraol ***@***.***> wrote:
i am using an stm32f4 with gcc on eclipse...
—
You are receiving this because you commented.
Reply to this email directly, view it on GitHub
<#82 (comment)>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/AGY1qt856vma0XiIg0yvQfkvaSqyKdl5ks5rMwXsgaJpZM4KkbbE>
.
|
hey i think i fixed that issue but my quartonion always result in the same value , q1=q2=q3=q4=0.5 for a weird reason. |
Hello I am using CJMCU-116 (Chinese Junk as @kriswiner put it :) ). I also tried reading 8 bytes in a row, starting with register 0x02. Same result - 0's. I tried reading these registers one by one, initiating a new i2c "reading session" for each byte, getting same results - zeros. I am wondering if I did (or didn't) something wrong during initialization of either MPU or the AK? @Kryzzalid, |
Can you verify that pin 1 of the MPU9250 is connected to something?
On my version they left this unconnected, which causes the mag either to
work erratically or not at all.
Secondly, can you read the fuze ROM data? This is the factory axis
sensitivity data which is read only. Another test of whether the AK8963C is
working.
This is what I use to initialize the AK8963C:
void initAK8963(float * destination)
{
// First extract the factory calibration for each magnetometer axis
uint8_t rawData[3]; // x/y/z gyro calibration data stored here
writeByte(AK8963_ADDRESS, AK8963_CNTL, 0x00); // Power down magnetometer
delay(10);
writeByte(AK8963_ADDRESS, AK8963_CNTL, 0x0F); // Enter Fuse ROM access mode
delay(10);
readBytes(AK8963_ADDRESS, AK8963_ASAX, 3, &rawData[0]); // Read the x-, y-,
and z-axis calibration values
destination[0] = (float)(rawData[0] - 128)/256. + 1.; // Return x-axis
sensitivity adjustment values, etc.
destination[1] = (float)(rawData[1] - 128)/256. + 1.;
destination[2] = (float)(rawData[2] - 128)/256. + 1.;
writeByte(AK8963_ADDRESS, AK8963_CNTL, 0x00); // Power down magnetometer
delay(10);
// Configure the magnetometer for continuous read and highest resolution
// set Mscale bit 4 to 1 (0) to enable 16 (14) bit resolution in CNTL
register,
// and enable continuous mode data acquisition Mmode (bits [3:0]), 0010 for
8 Hz and 0110 for 100 Hz sample rates
writeByte(AK8963_ADDRESS, AK8963_CNTL, Mscale << 4 | Mmode); // Set
magnetometer data resolution and sample ODR
delay(10);
}
Notice the delays, you might need to add more delay depending on the
quality of the I2C bus signals. Make sure you define Mscale and Mmode
properly.
And of course, you need to set the MPU9250 in bypass mode, which you say
you have done. Can you verify this by rading the INT_CFG register back?
If this doesn't work, you then need to try a different sensor breakout to
be sure it is the software and not the hardware.
…On Wed, Jan 25, 2017 at 7:54 AM, TediumRemedy ***@***.***> wrote:
Hello
I am having exactly the same problem as @Kryzzalid
<https://github.com/Kryzzalid> had. I am able to read registers WHO_I_AM
and INFO (which return 0x48 and 0x9a respectively), but the rest of the
registers simply return 0x00.
I am using CJMCU-116 (Chinese Junk as @kriswiner
<https://github.com/kriswiner> put it :) ).
After initializing the MPU itself (including setting 2nd bit into
MPU9250_INT_PIN_CFG, which is a BYPASS_EN), I am writing 0x16 into
AK8963_CNTL1. Then I just try to read 10 bytes in a row, starting with
register WHO_AM_I (register 0x00). The response I am getting is
0x48 0x9a 0 0 0 0 0 0 0 0
I also tried reading 8 bytes in a row, starting with register 0x02. Same
result - 0's.
I tried reading these register one by one, initiating a new i2c "reading
session" for each byte, getting same results - zeros.
I am wondering if I did (or didn't) something wrong during initialization
of either MPU or the AK?
I am able to get bytes from the gyro and accelerometer just fine (they are
noisy and jumping from time to time, I suppose this is because I do not
filter them and didn't calibrate the gyro and accelerometer, but at least
they are not zeros). Only the magnetometer's measurements are 0's.
@Kryzzalid <https://github.com/Kryzzalid>,
Could you please post your AK8963 and MPU9250 initialization code, as well
as the code that does measurements?
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<#82 (comment)>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/AGY1qisEVc0vZDQdyCyxVGoFX89-8m4kks5rV3BHgaJpZM4KkbbE>
.
|
Hi @kriswiner Regarding the fuze ROM data (three registers AK8963_ASAX=0x10, AK8963_ASAY=0x11, AK8963_ASAXZ=0x12?), I can read them. I used your initialization code, including powering down the mag 2 times and added delays, now I am getting real bytes returned. It looks like the high byte of every axis can only have values either 0xFF (means, negative direction?) or 0x00 (means positive direction), and the actual azimuth is specified only by the lowest byte. Is this correct? |
You have to ready 7 bytes (including the status register) for the AK8963 to function properly in continuous mode. |
@kriswiner yes I am reading all the values between (and including) the registers st1 and st2. As a result I am getting numbers that change when i rotate the board. Its just the high byte of each returned axis is either ff or 00 (while the low byte shows seemingly correct value). So I was just wondering if its normal? (Wrong "scale"? It looks like the high byte's ff value means something like "negative complement", when i rotate the board past 0 in the "decreasing direction" ) |
Yes, are you not using int16_t to do the two's complement conversion?
…On Wed, Jan 25, 2017 at 6:32 PM, TediumRemedy ***@***.***> wrote:
@kriswiner <https://github.com/kriswiner> yes I am reading all the values
between (and including) the registers st1 and st2. As a result I am getting
numbers that change when i rotate the board. Its just the high byte of each
returned axis is either ff or 00 (while the low byte shows seemingly
correct value). So I was just wondering if its normal? (Wrong "scale"? It
looks like the high byte's ff value means something like "negative
complement", when i rotate the board past 0 in the "decreasing direction" )
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<#82 (comment)>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/AGY1qm89cpgrZOHVzvh1WnFdAIx7TYATks5rWAW0gaJpZM4KkbbE>
.
|
Try int16_t rawCount = (int16_t) ( (int16_t) (MSB << 8) | LSB); |
That was it. Thanks! |
@TediumRemedy can you share the full working code |
Hi @kriswiner. Thank you. |
Might not be the code, there are a lot of fake MPU9250 out there so maybe
try another board or two before focusing on the software. Generally, you
need to read the data and ST2 to get data update from the AK8963C, as you
know,, also, you need to put the device into continuous mode after reading
the fuse ROM. This is demonstrated in all of the example sketches...
…On Tue, Mar 1, 2022 at 5:40 PM MehranMsba ***@***.***> wrote:
Hi @kriswiner <https://github.com/kriswiner>.
I have a problem with Mag read of MPU9250 module.
I configured the INT_PIN_CFG and MPU9250_USER_CTRL and I can read
WHO_AM_I_MPU9250, AK8963_WHO_AM_I with true values, Then I set AK8963 mode
to Fuse ROM and read the ASAX,ASAY,ASAZ and calculate the values,
Mag cal off X: 1.183594
Mag cal off Y: 1.187500
Mag cal off Z: 1.144531
But the mag data is always zero and DRDY bit will never be 1.
I try to read ST2 and ST1 in loop,or try to read 7 bytes from
AK8963_XOUT_L to ST2 ,or simple ST1 read and any combination of these that
I can.
I am completely disappointed! All data = 0 !
Some times The CTRL1 is zero too when I read back it!
It's the code: Github Link
<https://github.com/MehranMsba/CodeOnDiscuss/blob/main/main.c>
What's wrong?
Thank you.
—
Reply to this email directly, view it on GitHub
<#82 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/ABTDLKRFGHVRXPEE72UP7QLU53BKRANCNFSM4CURW3CA>
.
Triage notifications on the go with GitHub Mobile for iOS
<https://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675>
or Android
<https://play.google.com/store/apps/details?id=com.github.android&referrer=utm_campaign%3Dnotification-email%26utm_medium%3Demail%26utm_source%3Dgithub>.
You are receiving this because you were mentioned.Message ID:
***@***.***>
|
Thanks for your fast response. |
Sounds like something wrong with your connections or I2C API or MCU? Does
any other I2C sensor work OK?
…On Tue, Mar 1, 2022 at 6:02 PM MehranMsba ***@***.***> wrote:
Thanks for your fast response.
Ok so I must buy another module and test again.
Something that I see is that the 100khz i2c connection is not work with
ak8963 of mpu9250 and I changed it to 400khz to read "who am I" register
correctly.
—
Reply to this email directly, view it on GitHub
<#82 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/ABTDLKTAYA4ZES3MKKFZ2KLU53D33ANCNFSM4CURW3CA>
.
Triage notifications on the go with GitHub Mobile for iOS
<https://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675>
or Android
<https://play.google.com/store/apps/details?id=com.github.android&referrer=utm_campaign%3Dnotification-email%26utm_medium%3Demail%26utm_source%3Dgithub>.
You are receiving this because you were mentioned.Message ID:
***@***.***>
|
Yes, I test mpu6050 too, Same API and same connection, there is no problem with mpu6050. |
So could be a bad MPU9250. Try some other MPU9250 test sketches to be sure.
…On Tue, Mar 1, 2022 at 11:16 PM MehranMsba ***@***.***> wrote:
Yes, I test mpu6050 too, Same API and same connection, there is no problem
with mpu6050.
—
Reply to this email directly, view it on GitHub
<#82 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/ABTDLKQZSZOSWDPRYUKDFXLU54IWHANCNFSM4CURW3CA>
.
Triage notifications on the go with GitHub Mobile for iOS
<https://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675>
or Android
<https://play.google.com/store/apps/details?id=com.github.android&referrer=utm_campaign%3Dnotification-email%26utm_medium%3Demail%26utm_source%3Dgithub>.
You are receiving this because you were mentioned.Message ID:
***@***.***>
|
Yes, I bought a new one. It is now work without problem. Thank you. |
Hi!
Your page is a great resource and help for the MPU-9250. I have been trying for a while to get the MPU-9250 to work with a TM4C123 (Tiva C) via an IIC connection. I get the right answer (0x71) from the WHO_AM_I inquiry and also from the magnetometer's WHO_AM_I (0x48). I can read the values of the accelerometer and the gyroscope but the magnetometer only gives 0 on the 3 axis. I haven't calibrated anything yet since the magnetometer does not work.
My initialization is:
I read the values of the x-axis of the magnetometer the following way:
mx_1, mx_2 and m_x are defined as uint16_t
The preprocessor macros are:
I also tried with different values of delay. I noticed if I use the PWR_MGMT_2 the magnetometer stops functioning and returns -1 on the 3 axis.
I'm using CJMCU 10DOF 9 Axis MPU9250 + BMP180 Sensor Module (picture front, picture back). The physical connections are:
SDA --> tiva SDA
SCL --> tiva SCL
AD0 --> GND
NCS --> VCC (3.3V)
+3V3 --> VCC
GND --> GND common to all circuits (Tiva, module) since I use an external 3.3V power supply
Would you have any idea why it doesn't work?
I really hope you can help. Thank you very much for your time!
The text was updated successfully, but these errors were encountered: