Debian Codegraph @

docs/programming/MPU6050.md

raw symbols SCIP
1 ## MPU6050 sensor
2
3 ![Screenshot](../images/ide/mpumonitor.png)
4
5 Read values(Ax) from a MPU6050 accelerometer and dump them to the serial port.
6
7 !!! tip "examples/C/MPU6050_UART_DEMO.c"
8 ```python
9 /*
10 Scan the I2C bus for MPU6050 and send results via UART.
11 */
12
13 #include <avr/kp.h> // Include file for I/O operations
14
15
16 #define REG_CONTROL 0xF4
17 #define CMD_TEMP 0x2E
18
19 #define REG_RESULT_AX 0x3B
20
21 int main (void)
22 {
23
24 uint8_t addresses[20], found, res[10]={0,0,0,0,0,0,0,0,0,0},i=0;
25
26 uint8_t accel_range_commands[] = {0x1B, 0<<3};
27 uint8_t gyro_range_commands[] = {0x1C, 0<<3}; // gyro range
28 uint8_t power_on_commands[] = {0x6B, 0x00}; // Power on
29 char mystring[10];
30
31 i2c_init();
32 uart_init(38400);
33
34
35 for(;;)
36 {
37
38 found = i2c_scan(&addresses[0]); // i2c scan will store the addresses in `addresses`, and return total found sensors.
39
40 for(i=0;i<found;i++){
41 uart_send_byte_ascii(addresses[i]); // send address
42 uart_send_byte(','); // send comma.
43
44 //MPU6050 detected at 104 (0x68). read values from ax and send over UART
45 if(addresses[i] == 0x68){
46 // write to 0x68 (bmp180 address) , 0xF4 and 0x2E
47 i2c_write(0x68 , accel_range_commands, 2); // Gyro Range . 250
48 i2c_write(0x68 , gyro_range_commands, 2); // Accelerometer Range. 2
49 i2c_write(0x68 , power_on_commands, 2); // Power ON
50 delay_ms(10);
51 for(;;){
52 // read 2 bytes from the result register for Ax
53 i2c_read(0x68, REG_RESULT_AX, &res[0],2); itoa((res[0]<<8)|(res[1]), mystring, 10); // Convert to decimal string
54 uart_send_string(mystring);
55 uart_send_byte('\n');
56
57 //uart_send_byte_ascii(res[0]); // send MSB
58 //uart_send_byte(',');
59 //uart_send_byte_ascii(res[1]); // send LSB
60 //uart_send_byte('\n');
61 delay_ms(100);
62 }
63
64 }
65
66 }
67 if(found)
68 uart_send_byte('\n');
69 delay_ms(500);
70
71 }
72
73 return 0;
74 }
75 ```
76
77 ## Serial Monitor
78
79 Raw values are combined into a 16 bit signed integer and `itoa` function is used to convert them
80 to a string before dumping into the serial port.
81
82 Click the gauge button, and select ASCII mode to view values in a gauge. you can also use the data logger