Hello,
I am trying to run a Raspberry Pi Pico connected to an MPU6050. I am using this MPU6050:
https://www.adafruit.com/product/3886
I have used the code from pico-examples for the MPU6050. My connections are as follows:
Pico -> MPU6050
3v -> VIN
GND -> GND
GP4 -> SDA
GP5 -> SCL
I was able to load are run the program using VSCode, but it is printing out the following each loop:Here is my code I am using:
And here is my CMakeLists.txt
Any help on getting this to read the the values from the MPU would be great.
Thank yoU!
I am trying to run a Raspberry Pi Pico connected to an MPU6050. I am using this MPU6050:
https://www.adafruit.com/product/3886
I have used the code from pico-examples for the MPU6050. My connections are as follows:
Pico -> MPU6050
3v -> VIN
GND -> GND
GP4 -> SDA
GP5 -> SCL
I was able to load are run the program using VSCode, but it is printing out the following each loop:
Code:
Acc. X = 0, Y = 0, Z = 29458Gyro. X = 0, Y = 0, Z = 29458Temp. = 36.530000
Code:
#include <stdio.h>#include <string.h>#include "pico/stdlib.h"#include "pico/binary_info.h"#include "hardware/i2c.h"// By default these devices are on bus address 0x68static int addr = 0x6B;#ifdef i2c_defaultstatic 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, 0x80}; i2c_write_blocking(i2c_default, 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_default, addr, &val, 1, true); // true to keep master control of bus i2c_read_blocking(i2c_default, 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_default, addr, &val, 1, true); i2c_read_blocking(i2c_default, 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_default, addr, &val, 1, true); i2c_read_blocking(i2c_default, addr, buffer, 2, false); // False - finished with bus *temp = buffer[0] << 8 | buffer[1];}#endifint main() { stdio_init_all();#if !defined(i2c_default) || !defined(PICO_DEFAULT_I2C_SDA_PIN) || !defined(PICO_DEFAULT_I2C_SCL_PIN) #warning i2c/mpu6050_i2c example requires a board with I2C pins puts("Default I2C pins were not defined"); return 0;#else printf("Hello, MPU6050! Reading raw data from registers...\n"); // This example will use I2C0 on the default SDA and SCL pins (4, 5 on a Pico) i2c_init(i2c_default, 400 * 1000); gpio_set_function(PICO_DEFAULT_I2C_SDA_PIN, GPIO_FUNC_I2C); gpio_set_function(PICO_DEFAULT_I2C_SCL_PIN, GPIO_FUNC_I2C); gpio_pull_up(PICO_DEFAULT_I2C_SDA_PIN); gpio_pull_up(PICO_DEFAULT_I2C_SCL_PIN); // Make the I2C pins available to picotool bi_decl(bi_2pins_with_func(PICO_DEFAULT_I2C_SDA_PIN, PICO_DEFAULT_I2C_SCL_PIN, GPIO_FUNC_I2C)); 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(1000); }#endif}
Code:
# Generated Cmake Pico project filecmake_minimum_required(VERSION 3.13)set(CMAKE_C_STANDARD 11)set(CMAKE_CXX_STANDARD 17)set(CMAKE_EXPORT_COMPILE_COMMANDS ON)# Initialise pico_sdk from installed location# (note this can come from environment, CMake cache etc)# == DO NEVER EDIT THE NEXT LINES for Raspberry Pi Pico VS Code Extension to work ==if(WIN32) set(USERHOME $ENV{USERPROFILE})else() set(USERHOME $ENV{HOME})endif()set(sdkVersion 2.0.0)set(toolchainVersion 13_2_Rel1)set(picotoolVersion 2.0.0)include(${USERHOME}/.pico-sdk/cmake/pico-vscode.cmake)# ====================================================================================set(PICO_BOARD pico CACHE STRING "Board type")# Pull in Raspberry Pi Pico SDK (must be before project)include(pico_sdk_import.cmake)project(mpu6050_i2c C CXX ASM)# Initialise the Raspberry Pi Pico SDKpico_sdk_init()# Add executable. Default name is the project name, version 0.1add_executable(mpu6050_i2c mpu6050_i2c.c )# pull in common dependencies and additional i2c hardware supporttarget_link_libraries(mpu6050_i2c pico_stdlib hardware_i2c)pico_enable_stdio_usb(mpu6050_i2c 1)pico_enable_stdio_uart(mpu6050_i2c 0)# create map/bin/hex file etc.pico_add_extra_outputs(mpu6050_i2c)# add url via pico_set_program_url
Thank yoU!
Statistics: Posted by michaelp91 — Sun Sep 08, 2024 2:03 am