C++ starting with GY-521

I start coding in c++ (again). After some struggle I am ready to play with the GY-521.
Well, first to start learning how to implement i2c.

Maybe someone else is at the same point and want to try it together? I want to use only the mbed library to get used to the i2c settings.

`#include <mbed.h>


I2C i2c(I2C_SDA, I2C_SCL);
DigitalOut onboardled(LED1);

const int addr7bit = 0x69; // 7 bit I2C address
const int addr8bit = 0x69 << 1; // 8bit I2C address, 0x90

int main() {
int test = 0;
char readdata[8];
char cmd[1];

while(1) {
test = test +1;
onboardled= 0;
// read the data
cmd[0] = 0x3B;
i2c.write(addr8bit, cmd, 8, true);
i2c.read(addr8bit, readdata, 8);
std::cout<<" “<<readdata[0]<<” “<<readdata[1]<<” “<<readdata[2]<<” “<<readdata[3]<<” "
<<readdata[4]<<" “<<readdata[5]<<” “<<readdata[6]<<” "<<readdata[7];

this is what i have and the output is super wrong :smiley:
i am not sure how to set the pointer on the mpu6050 so we can read data from it

Can someone help me out?
All i get is: � � ␂ � ␆ ␃

|address| 8-bit I2C slave address [ addr | 0 ]|

|data| Pointer to the byte-array data to send|
|length| Number of bytes to send|
|repeated| Repeated start, true - do not send stop at end default value is false.|

data is my variable where i want to store the data right?
length is how much data i want to receive right? so for example 8bit=> 1?

what do i have to do?
first write to the sensor which register i want to read
followed by a read i2c?

even a sample dosnt work

// Read the WHO_AM_I register, this is a good test of communication
uint8_t whoami = i2c.read(addr7bit,cmd,1); // Read WHO_AM_I register for MPU-6050
std::cout<<(“I AM 0x”)<<whoami<<("\n\r");

gives me: “␁”

cout tries to print ASCII characters. Most likely, your values are not printable. You want to cast them to unsigned int.

cout << +readdata[0] or cout << static_cast<unsigned>(readdata[0])

It seems like i2c.read does not store data in ‘data’, because the value is still at 0x11 at the end

//Set up I2C
i2c.frequency(400000); // use fast (400 kHz) I2C
int test = 0;
uint8_t whoami;
char data[1];
char data_write[1];
uint8_t whoam1 = 0x75;
data_write[0] = whoam1;
i2c.write(addr8bit, data_write, 1, 1);
i2c.read(addr8bit, data, 1, 0);
whoami = data[0];
printf(“I AM 0x%x\n\r”, whoami);

I would recommend using an oscilloscope/logic analyzer for troubleshooting.

i should buy me one i think :smiley:
it is working with https://os.mbed.com/users/onehorse/code/MPU6050IMU/
i need to get it to work without that library somehow

Hello Daniel,

always is good to check the result of read/write methods.

#include "mbed.h" //MbedOS 6.4

DigitalOut led(LED1);
I2C i2c(I2C_SDA, I2C_SCL);

int main()
    int addr = 0x68<<1; //change the value (0xXX) according to AD0 setting
    char dataSend[] = {0x75};
    char result;

    if(i2c.write(addr, "1", 1) == 0){
        printf("Addr ACK\n");
        if(i2c.write(addr, dataSend, sizeof(dataSend), true)==0){
            printf("Write cmd ACK\n");
            if(i2c.read(addr, &result,sizeof(result))==0){
                printf("Read reg ACK\n");
                printf("Result: I AM 0x%x\n", result);
            }else printf("Read reg NAK\n");
        }else printf("Write cmd NAK\n");
    }else printf("Addr NAK - address is not valid or connection is broken!\n");

    while (1) {
        led = !led;

BR, Jan