Skip to content

Update on reset of mpu6050_i2c.c #319

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

Merged
merged 1 commit into from
Feb 10, 2023
Merged

Conversation

akhodeir
Copy link
Contributor

To reset the MPU6050, the device_reset bit shall be set to '1'.

reference from "MPU-6000/MPU-6050 Register Map and Descriptions"
DEVICE_RESET : When set to 1, this bit resets all internal registers to their default values. The bit automatically clears to 0 once the reset is done.

To reset the MPU6050, the device_reset bit shall be set to '1'.

reference from "MPU-6000/MPU-6050 Register Map and Descriptions"
DEVICE_RESET : When set to 1, this bit resets all internal registers to their default values. The bit automatically clears to 0 once the reset is done.
@peterharperuk
Copy link
Contributor

Did you test this change?

kilograham
kilograham previously approved these changes Feb 10, 2023
@kilograham kilograham changed the base branch from master to develop February 10, 2023 15:39
@kilograham kilograham dismissed their stale review February 10, 2023 15:39

The base branch was changed.

@kilograham kilograham merged commit 42ffd51 into raspberrypi:develop Feb 10, 2023
@akhodeir akhodeir deleted the patch-1 branch February 11, 2023 01:01
@akhodeir
Copy link
Contributor Author

I made a test using this code :

uint8_t res[] = {0x6B, 0x80};
i2c_write_blocking(i2c_default, addr, res, 2, false);
sleep_ms(200);
uint8_t buf[] = {0x6B, 0x00};
i2c_write_blocking(i2c_default, addr, buf, 2, false);
sleep_ms(200);

res will be used to rest the MPU then wait 200 ms then put the reset bit again to zero.

if the reset bit was not set back to zero the MPU will not work.

The above solution is tested and verfied.

@kilograham
Copy link
Contributor

tsk; see #352 ... I assumed you tested your change and it worked, but what you say in the above comment is not the code in the commit. can you please clarify, and submit a new PR

@kilograham
Copy link
Contributor

and explain what happens if 0x80 is not set first

@vexown
Copy link

vexown commented Mar 21, 2023

Hi, just wanted to add an explanation to this solution.

0x80 is the correct value that should be written to the PWR_MGMT_1 (0x6B) register to cause a reset of all internal registers of the MPU6050 to their default values.
The reason the value "0x00" needs to be written after the reset is that the reset value of PWR_MGMT_1 is 0x40 (sleep mode) - this causes the device to go into sleep mode by default after a reset. So writing 0x00 to this register is simply waking up the device.

Also if you really care about timing, you could reduce the delays quite a bit, I've found even a 1ms after reset is enough. I suppose this doesn't matter much for most applications but just wanted to mention it.

Below is my slightly modified version of the code:

static void mpu6050_reset() 
{
	size_t length;

	/* Register: PWR_MGMT_1 (0x6B), Value: 0x80, Action: Reset all internal registers (of MPU6050) to default values */
        uint8_t outputData_Reset[] = {0x6B, 0x80};
	length = sizeof(outputData_Reset);
	i2c_write_blocking(i2c_default, MPU6050_I2C_ADDRESS, outputData_Reset, length, false);
	sleep_ms(1); /* Give the slave device some time to perform the reset */
	
	/* Register: PWR_MGMT_1 (0x6B), Value: 0x00, Action: Take MPU6050 out of sleep mode (which is the default state after reset) */
	uint8_t outputData_WakeUp[] = {0x6B, 0x00};
	length = sizeof(outputData_WakeUp);
	i2c_write_blocking(i2c_default, MPU6050_I2C_ADDRESS, outputData_WakeUp, length, false);
}

@civcode
Copy link

civcode commented Apr 6, 2023

Hi @kilograham, I can confirm the explanation of @vexown. See MPU-6050 datasheet below.

The example code in the current version results in the device only transmitting 0x00 for each acceleration, gyro and the temperature value because it's going right to SLEEP state.

Actually, just writing 0x00 to register 0x6b results in a minimal working example because the device goes to SLEEP state on power-up as the default. However, the naming "mpu6050_reset()" does not really fit in this case. Something like "mpu6050_wakeup()" would match the actual effect.

static void mpu6050_reset() {
    uint8_t buf[] = {0x6B, 0x00};
    i2c_write_blocking(i2c_default, mpu6050_addr, buf, 2, false);
}

BR,
civ

2023-04-06_13h18_09
2023-04-06_13h42_39

akhodeir added a commit to akhodeir/pico-examples that referenced this pull request Sep 5, 2023
The reset process requires to set DEVICE_RESET bit in PWR_MGMT_1 (0x6B) bits to one ===> 0x80.
then the we must release the reset bit or the device will remain always in reset mode. 

detailed explanation is here: raspberrypi#319
akhodeir added a commit to akhodeir/pico-examples that referenced this pull request Sep 5, 2023
The reset process requires to set DEVICE_RESET bit in PWR_MGMT_1 (0x6B) bits to one ===> 0x80.
then the we must release the reset bit or the device will remain always in reset mode. 

detailed explanation is here: raspberrypi#319
@akhodeir akhodeir mentioned this pull request Sep 5, 2023
akhodeir added a commit to akhodeir/pico-examples that referenced this pull request Sep 12, 2023
The reset process requires to set DEVICE_RESET bit in PWR_MGMT_1 (0x6B) bits to one ===> 0x80. then the we must release the reset bit or the device will remain always in reset mode.

detailed explanation is here: raspberrypi#319
@vacoff
Copy link

vacoff commented Feb 17, 2024

It is not merged yet I guess? Problem is still occuring

@rafaelcorsi
Copy link

i can confirm that 0x80 wont work, and 0x00 works!

@akhodeir
Copy link
Contributor Author

akhodeir commented Apr 26, 2024

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants