slook
(Daniel mi)
November 2, 2020, 6:28pm
1
Hi
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.
slook
(Daniel mi)
November 2, 2020, 7:00pm
2
`#include <mbed.h>
#include
#include
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];
cmd[0]=0x1A;
while(1) {
test = test +1;
wait_us(1000000);
onboardled=1;
wait_us(1000000);
onboardled= 0;
std::cout<<test<<“\n”;
// 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
i am not sure how to set the pointer on the mpu6050 so we can read data from it
slook
(Daniel mi)
November 3, 2020, 11:16am
3
Can someone help me out?
All i get is: � � ␂ � ␆ ␃
slook
(Daniel mi)
November 3, 2020, 12:17pm
4
|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?
slook
(Daniel mi)
November 3, 2020, 2:42pm
5
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])
slook
(Daniel mi)
November 4, 2020, 1:41pm
8
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];
data[0]=0x11;
char data_write[1];
uint8_t whoam1 = 0x75;
data_write[0] = whoam1;
i2c.write(addr8bit, data_write, 1, 1);
wait_us(500);
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.
slook
(Daniel mi)
November 4, 2020, 3:36pm
10
i should buy me one i think
it is working with MPU6050IMU - Basic program to get the properly-scaled gyro and… | Mbed
i need to get it to work without that library somehow
JohnnyK
(Jan Kamidra)
November 4, 2020, 4:45pm
11
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()
{
printf("MbedOS v %d.%d.%d\r\n", MBED_MAJOR_VERSION, MBED_MINOR_VERSION, MBED_PATCH_VERSION);
i2c.frequency(400000);
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;
ThisThread::sleep_for(1s);
}
}
BR, Jan