Initial Release

This commit is contained in:
graham sanderson
2021-01-20 10:49:34 -06:00
commit 46078742c7
245 changed files with 21157 additions and 0 deletions

View File

@@ -0,0 +1,12 @@
add_executable(mpu6050_i2c
mpu6050_i2c.c
)
# Pull in our (to be renamed) simple get you started dependencies
target_link_libraries(mpu6050_i2c pico_stdlib hardware_i2c)
# create map/bin/hex file etc.
pico_add_extra_outputs(mpu6050_i2c)
# add url via pico_set_program_url
example_auto_set_url(mpu6050_i2c)

View File

@@ -0,0 +1,43 @@
= Attaching a MPU6050 accelerometer/gyroscope via I2C
This example code shows how to interface the Raspberry Pi Pico to the MPU6050 accelerometer/gyroscope board. This device uses I2C for communications, and most MPU6050 parts are happy running at either 3.3 or 5v. The Raspberry Pi RP2040 GPIO's work at 3.3v so that is what the example uses.
[NOTE]
======
This is a very basic example, and only recovers raw data from the sensor. There are various calibration options available that should be used to ensure that the final results are accurate. It is also possible to wire up the interrupt pin to a GPIO and read data only when it is ready, rather than using the polling approach in the example.
======
== Wiring information
Wiring up the device requires 4 jumpers, to connect VCC (3.3v), GND, SDA and SCL. The example here uses I2C port 0, which is assigned to GPIO 4 (SDA) and 5 (SCL) in software. Power is supplied from the 3.3V pin.
[NOTE]
======
There are many different manufacturers who sell boards with the MPU6050. Whilst they all appear slightly different, they all have, at least, the same 4 pins required to power and communicate. When wiring up a board that is different to the one in the diagram, ensure you connect up as described in the previous paragraph.
======
[[mpu6050_i2c_wiring]]
[pdfwidth=75%]
.Wiring Diagram for MPU6050.
image::mpu6050_i2c_bb.png[]
== List of Files
CMakeLists.txt:: CMake file to incorporate the example in to the examples build tree.
mpu6050_i2c.c:: The example code.
== Bill of Materials
.A list of materials required for the example
[[mpu6050-bom-table]]
[cols=3]
|===
| *Item* | *Quantity* | Details
| Breadboard | 1 | generic part
| Raspberry Pi Pico | 1 | http://raspberrypi.org/
| MPU6050 board| 1 | generic part
| M/M Jumper wires | 4 | generic part
|===

View File

@@ -0,0 +1,110 @@
/**
* Copyright (c) 2020 Raspberry Pi (Trading) Ltd.
*
* SPDX-License-Identifier: BSD-3-Clause
*/
#include <stdio.h>
#include <string.h>
#include "pico/stdlib.h"
#include "hardware/i2c.h"
/* Example code to talk to a MPU6050 MEMS accelerometer and gyroscope
This is taking to simple approach of simply reading registers. It's perfectly
possible to link up an interrupt line and set things up to read from the
inbuilt FIFO to make it more useful.
NOTE: Ensure the device is capable of being driven at 3.3v NOT 5v. The Pico
GPIO (and therefor I2C) cannot be used at 5v.
You will need to use a level shifter on the I2C lines if you want to run the
board at 5v.
Connections on Raspberry Pi Pico board, other boards may vary.
GPIO 4 (pin 6)-> SDA on MPU6050 board
GPIO 5 (pin 7)-> SCL on MPU6050 board
3.3v (pin 36) -> VCC on MPU6050 board
GND (pin 38) -> GND on MPU6050 board
*/
// By default these devices are on bus address 0x68
static int addr = 0x68;
#define I2C_PORT i2c0
static void mpu6050_reset() {
// Two byte reset. First byte register, second byte data
// There are a load more options to set up the device in different ways that could be added here
uint8_t buf[] = {0x6B, 0x00};
i2c_write_blocking(I2C_PORT, addr, buf, 2, false);
}
static void mpu6050_read_raw(int16_t accel[3], int16_t gyro[3], int16_t *temp) {
// For this particular device, we send the device the register we want to read
// first, then subsequently read from the device. The register is auto incrementing
// so we don't need to keep sending the register we want, just the first.
uint8_t buffer[6];
// Start reading acceleration registers from register 0x3B for 6 bytes
uint8_t val = 0x3B;
i2c_write_blocking(I2C_PORT, addr, &val, 1, true); // true to keep master control of bus
i2c_read_blocking(I2C_PORT, addr, buffer, 6, false);
for (int i = 0; i < 3; i++) {
accel[i] = (buffer[i * 2] << 8 | buffer[(i * 2) + 1]);
}
// Now gyro data from reg 0x43 for 6 bytes
// The register is auto incrementing on each read
val = 0x43;
i2c_write_blocking(I2C_PORT, addr, &val, 1, true);
i2c_read_blocking(I2C_PORT, addr, buffer, 6, false); // False - finished with bus
for (int i = 0; i < 3; i++) {
gyro[i] = (buffer[i * 2] << 8 | buffer[(i * 2) + 1]);;
}
// Now temperature from reg 0x41 for 2 bytes
// The register is auto incrementing on each read
val = 0x41;
i2c_write_blocking(I2C_PORT, addr, &val, 1, true);
i2c_read_blocking(I2C_PORT, addr, buffer, 2, false); // False - finished with bus
*temp = buffer[0] << 8 | buffer[1];
}
int main() {
stdio_init_all();
printf("Hello, MPU6050! Reading raw data from registers...\n");
// This example will use I2C0 on GPIO4 (SDA) and GPIO5 (SCL) running at 400kHz.
i2c_init(I2C_PORT, 400 * 1000);
gpio_set_function(4, GPIO_FUNC_I2C);
gpio_set_function(5, GPIO_FUNC_I2C);
gpio_pull_up(4);
gpio_pull_up(5);
mpu6050_reset();
int16_t acceleration[3], gyro[3], temp;
while (1) {
mpu6050_read_raw(acceleration, gyro, &temp);
// These are the raw numbers from the chip, so will need tweaking to be really useful.
// See the datasheet for more information
printf("Acc. X = %d, Y = %d, Z = %d\n", acceleration[0], acceleration[1], acceleration[2]);
printf("Gyro. X = %d, Y = %d, Z = %d\n", gyro[0], gyro[1], gyro[2]);
// Temperature is simple so use the datasheet calculation to get deg C.
// Note this is chip temperature.
printf("Temp. = %f\n", (temp / 340.0) + 36.53);
sleep_ms(100);
}
return 0;
}

Binary file not shown.

Binary file not shown.

After

Width:  |  Height:  |  Size: 223 KiB