<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>linux gyro Archives | Microcontroller Tutorials</title>
	<atom:link href="https://www.teachmemicro.com/tag/linux-gyro/feed/" rel="self" type="application/rss+xml" />
	<link>https://www.teachmemicro.com/tag/linux-gyro/</link>
	<description>Microcontroller Tutorials and Resources</description>
	<lastBuildDate>Sun, 15 Oct 2023 20:48:38 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=7.0.2</generator>

<image>
	<url>https://www.teachmemicro.com/wp-content/uploads/2019/04/blue-icon-65x65.png</url>
	<title>linux gyro Archives | Microcontroller Tutorials</title>
	<link>https://www.teachmemicro.com/tag/linux-gyro/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>Beaglebone Black MPU6050 Interfacing: I2C Tutorial Part 2</title>
		<link>https://www.teachmemicro.com/beaglebone-black-mpu6050-i2c-tutorial-part-2/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=beaglebone-black-mpu6050-i2c-tutorial-part-2</link>
					<comments>https://www.teachmemicro.com/beaglebone-black-mpu6050-i2c-tutorial-part-2/#comments</comments>
		
		<dc:creator><![CDATA[Roland Pelayo]]></dc:creator>
		<pubDate>Sat, 06 May 2017 07:32:04 +0000</pubDate>
				<category><![CDATA[BeagleBone Tutorial]]></category>
		<category><![CDATA[i2c]]></category>
		<category><![CDATA[linux gyro]]></category>
		<category><![CDATA[mpu6050]]></category>
		<guid isPermaLink="false">https://www.teachmemicro.com/?p=790</guid>

					<description><![CDATA[<p>We covered the Beaglebone Black I2C basics in our last tutorial. This time, we'll try to communicate with a device through the I2C protocol. This device, the MPU6050, is a gyroscope + accelerometer integrated circuit that is very popular among makers and enthusiasts. Beaglebone Black MPU6050 Wiring Diagram First thing you need to do, of &#8230;</p>
<p>The post <a href="https://www.teachmemicro.com/beaglebone-black-mpu6050-i2c-tutorial-part-2/">Beaglebone Black MPU6050 Interfacing: I2C Tutorial Part 2</a> appeared first on <a href="https://www.teachmemicro.com">Microcontroller Tutorials</a>.</p>
]]></description>
										<content:encoded><![CDATA[<p>We covered the Beaglebone Black I2C basics in our <a href="https://www.teachmemicro.com/beaglebone-black-i2c-tutorial/">last tutorial</a>. This time, we'll try to communicate with a device through the I2C protocol. This device, the <a href="https://www.win-source.net/products/detail/tdk-invensense/mpu-6050.html">MPU6050</a>, is a gyroscope + accelerometer integrated circuit that is very popular among makers and enthusiasts.</p>
<p><span id="more-790"></span></p>
<h3>Beaglebone Black MPU6050 Wiring Diagram</h3>
<p>First thing you need to do, of course, is connect the MPU6050 to the Beaglebone Black. We will be using I2C-2 bus of the BBB (as per the previous tutorial) and so our SDA pin will be P9_20 while our SCL pin will be P9_19. Also, connect the MPU6050 VCC pin to the P9_4 (3.3 V VDD) and its ground pin to P9_2 (DGND). Here's the fritzing diagram:</p>
<p><img decoding="async" class="alignnone size-full wp-image-793" src="https://www.teachmemicro.com/wp-content/uploads/2017/05/Beaglebone-black-mpu6050_bb.jpg" alt="beaglebone black mpu6050 wiring" width="801" height="660" srcset="https://www.teachmemicro.com/wp-content/uploads/2017/05/Beaglebone-black-mpu6050_bb.jpg 801w, https://www.teachmemicro.com/wp-content/uploads/2017/05/Beaglebone-black-mpu6050_bb-300x247.jpg 300w, https://www.teachmemicro.com/wp-content/uploads/2017/05/Beaglebone-black-mpu6050_bb-768x633.jpg 768w" sizes="(max-width: 801px) 100vw, 801px" /></p>
<p>You can check if your connections are correct using the command <em>i2cdetect -r 2:</em></p>
<pre class="theme:shell-default-copy lang:sh decode:true"><pre><code class="language-cpp">root@arm:~/i2c-tut# i2cdetect -r 2
WARNING! This program can confuse your I2C bus, cause data loss and worse!
I will probe file /dev/i2c-2 using read byte commands.
I will probe address range 0x03-0x77.
Continue? [Y/n] y
     0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f
00:          -- -- -- -- -- -- -- -- -- -- -- -- --
10: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
20: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
30: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
40: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
50: -- -- -- -- UU UU UU UU -- -- -- -- -- -- -- --
60: -- -- -- -- -- -- -- -- 68 -- -- -- -- -- -- --
70: -- -- -- -- -- -- -- --
root@arm:~/i2c-tut#</code></pre></pre>
<p>You'll see that address 68 is filled out. That's the MPU6050!</p>
<h3>The MPU6050 Registers</h3>
<h4>Accelerometer and Gyroscope</h4>
<p>The MPU6050 has registers from 0x0D to 0x75. Each of these registers are 8-bits wide and have specific functions. To see all of these registers, download the <a href="https://www.invensense.com/wp-content/uploads/2015/02/MPU-6500-Register-Map2.pdf">MPU6050 register map</a>.</p>
<p>For the purpose of this tutorial, we'll be reading from the accelerometer and gyroscope registers. The accelerometer and gyroscope data are 16 bits wide and so data from each axis uses two registers. Here's what I'm talking about:</p>
<table>
<tbody>
<tr>
<th style="text-align: center;">Register</th>
<th style="text-align: center;">Address</th>
</tr>
<tr>
<td>ACCEL_XOUT_H</td>
<td>0x3B</td>
</tr>
<tr>
<td>ACCEL_XOUT_L</td>
<td>0x3C</td>
</tr>
<tr>
<td>ACCEL_YOUT_H</td>
<td>0x3D</td>
</tr>
<tr>
<td>ACCEL_YOUT_L</td>
<td>0x3E</td>
</tr>
<tr>
<td>ACCEL_ZOUT_H</td>
<td>0x3F</td>
</tr>
<tr>
<td>ACCEL_ZOUT_L</td>
<td>0x40</td>
</tr>
<tr>
<td>GYRO_XOUT_H</td>
<td>0x43</td>
</tr>
<tr>
<td>GYRO_XOUT_L</td>
<td>0x44</td>
</tr>
<tr>
<td>GYRO_YOUT_H</td>
<td>0x45</td>
</tr>
<tr>
<td>GYRO_YOUT_L</td>
<td>0x46</td>
</tr>
<tr>
<td>GYRO_ZOUT_H</td>
<td>0x47</td>
</tr>
<tr>
<td>GYRO_ZOUT_L</td>
<td>0x48</td>
</tr>
</tbody>
</table>
<p>We can check out the contents of these registers through <em>i2cdump:</em></p>
<pre class="theme:shell-default-copy lang:sh decode:true"><pre><code class="language-cpp">root@arm:~/i2c-tut# i2cdump 2 0x68
No size specified (using byte-data access)
WARNING! This program can confuse your I2C bus, cause data loss and worse!
I will probe file /dev/i2c-2, address 0x68, mode byte
Continue? [Y/n] y
     0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f    0123456789abcdef
00: ff 01 ff 2d af 0f 05 90 fb 0b 05 44 28 70 6e 94    .?.-???????D(pn?
10: c6 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00    ?...............
20: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00    ................
30: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00    ................
40: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00    ................
50: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00    ................
60: 00 00 00 00 00 00 00 00 00 00 00 40 00 00 00 00    ...........@....
70: 00 00 00 00 00 68 00 00 00 00 00 00 00 00 00 00    .....h..........
80: ff 01 ff 2d af 0f 05 90 fb 0b 05 44 28 70 6e 94    .?.-???????D(pn?
90: c6 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00    ?...............
a0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00    ................
b0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00    ................
c0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00    ................
d0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00    ................
e0: 00 00 00 00 00 00 00 00 00 00 00 40 00 00 00 00    ...........@....
f0: 00 00 00 00 00 68 00 00 00 00 00 00 00 00 00 00    .....h..........</code></pre></pre>
<p>Here you'll see that the registers on the table above have zero values. This is because we need to wake the MPU6050 up before it gives data.</p>
<h4>How to Wake Up the MPU6050</h4>
<p>To wake up the MPU6050, we need to write zero to the PWR_MGMT_1 register which is at address 0x6B. Here's how to do that:</p>
<pre class="theme:shell-default-copy lang:sh decode:true"><pre><code class="language-cpp">root@arm:~/i2c-tut# i2cset 2 0x68 0x6B 0
WARNING! This program can confuse your I2C bus, cause data loss and worse!
I will write to device file /dev/i2c-2, chip address 0x68, data address
0x6b, data 0x00, mode byte.
Continue? [Y/n] y</code></pre></pre>
<p>After that, we can use i2cdump again to check.</p>
<pre class="theme:shell-default-copy lang:default decode:true"><pre><code class="language-cpp">root@arm:~/i2c-tut# i2cdump 2 0x68
No size specified (using byte-data access)
WARNING! This program can confuse your I2C bus, cause data loss and worse!
I will probe file /dev/i2c-2, address 0x68, mode byte
Continue? [Y/n] y
     0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f    0123456789abcdef
00: ff 01 ff 2d af 0f 05 90 fb 0b 05 44 28 70 6e 94    .?.-???????D(pn?
10: c6 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00    ?...............
20: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00    ................
30: 00 00 00 00 00 00 00 00 00 00 01 2e 28 07 e4 d3    ..........?.(???
40: b4 f3 d0 ff e7 01 ec 00 68 00 00 00 00 00 00 00    ???.???.h.......
50: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00    ................
60: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 25    ...............%
70: 00 00 00 00 00 68 00 00 00 00 00 00 00 00 00 00    .....h..........
80: ff 01 ff 2d af 0f 05 90 fb 0b 05 44 28 70 6e 94    .?.-???????D(pn?
90: c6 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00    ?...............
a0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00    ................
b0: 00 00 00 00 00 00 00 00 00 00 01 2e 34 07 04 d2    ..........?.4???
c0: 94 f3 d0 ff 98 01 b2 00 4e 00 00 00 00 00 00 00    ???.???.N.......
d0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00    ................
e0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 03 fd    ..............??
f0: 00 00 00 00 00 68 00 00 00 00 00 00 00 00 00 00    .....h..........</code></pre></pre>
<p>The accelerometer and gyroscope registers now have values!</p>
<p>Try reorienting the MPU6050 and do the i2cdump again. You should have different values on the accelerometer and gyroscope registers.</p>
<h4>Getting Individual Values</h4>
<p>To get an individual register value, we can use the <em>i2cget</em> command. For example, I'll read the value of ACCEL_XOUT_H register (address 0x3B):</p>
<pre class="theme:shell-default-copy lang:default decode:true"><pre><code class="language-cpp">root@arm:~/i2c-tut# i2cget 2 0x68 0x3B
WARNING! This program can confuse your I2C bus, cause data loss and worse!
I will read from device file /dev/i2c-2, chip address 0x68, data address
0x3b, using read byte data.
Continue? [Y/n] y
0x30</code></pre></pre>
<p>The output 0x30 is the high byte of the MPU6050's acceleration in the x-axis. To get the low byte simply change the address:</p>
<pre class="theme:shell-default-copy lang:sh decode:true"><pre><code class="language-cpp">root@arm:~/i2c-tut# i2cget 2 0x68 0x3C
WARNING! This program can confuse your I2C bus, cause data loss and worse!
I will read from device file /dev/i2c-2, chip address 0x68, data address
0x3c, using read byte data.
Continue? [Y/n] y
0xf8</code></pre></pre>
<p>The low byte is 0xf8. This means the acceleration in the x-axis is 0x30f8 or 12, 536 in decimals.</p>
<h3>Scaling the MPU6050 Raw Data</h3>
<p>What we've read from the MPU6050 so far is raw data and needs to be converted to make sense. The acceleration data is measured in g (acceleration due to gravity) units while the gyroscope data is measured in degrees per second. The MPU6050 uses 2g and 250 deg/sec as default sensitivities.</p>
<p>Since each axes is 16 bytes and is signed, the raw values will range from -32,768 to 32, 767. This means acceleration in axes X ,Y and Z should have values within these ranges that represent the effect of gravity. For the default sensitivity which is 2g, the scaling factor should be around 16, 384 (32, 768/2).</p>
<p>In the same way, the X, Y and Z gyro data should have raw data readings from +250 to -250. According to the datasheet, the best scaling factor to use is 131.</p>
<p>However, even with scaling factors you may not get the exact acceleration and rotational speed since there will always be noise and sensor reading errors.</p>
<h4><strong>C++ Code</strong></h4>
<p>Following the discussion above, we can now write a C++ code that displays the accelerometer and gyroscope values:</p>
<pre class="theme:shell-default-copy lang:c++ decode:true"><pre><code class="language-cpp">/BBB-MPU6050.cpp
by OP from teachmemicro.com
*/

#include &lt;cstdlib&gt;
#include &lt;cstdio&gt;
#include &lt;iostream&gt;
#include &lt;stdexcept&gt;
#include &lt;cstring&gt;
#include &lt;string&gt;
#include &lt;memory&gt;
#include &lt;array&gt;

using namespace std;

int16_t accel_x;
int16_t accel_y;
int16_t accel_z;
int16_t gyro_x;
int16_t gyro_y;
int16_t gyro_z;

const char* deviceADDR = &quot; 0x68&quot;;
const char* PWR_MGMT_1 = &quot; 0x6B&quot;;
const char* ACCEL_X_OUT_H = &quot; 0x3B &quot;;
const char* ACCEL_X_OUT_L = &quot; 0x3C &quot;;
const char* ACCEL_Y_OUT_H = &quot; 0x3D &quot;;
const char* ACCEL_Y_OUT_L = &quot; 0x3E &quot;;
const char* ACCEL_Z_OUT_H = &quot; 0x3F &quot;;
const char* ACCEL_Z_OUT_L = &quot; 0x40 &quot;;
const char* GYRO_X_OUT_H = &quot; 0x43 &quot;;
const char* GYRO_X_OUT_L = &quot; 0x44 &quot;;
const char* GYRO_Y_OUT_H = &quot; 0x45 &quot;;
const char* GYRO_Y_OUT_L = &quot; 0x46 &quot;;
const char* GYRO_Z_OUT_H = &quot; 0x47 &quot;;
const char* GYRO_Z_OUT_L = &quot; 0x48 &quot;;

const char* cmdGet = &quot;i2cget -y 2&quot;;
const char* cmdSet = &quot;i2cset -y 2&quot;;

/*exec function that runs bash commands in c++
string exec(char* cmd) {
	string data;
	FILE * stream;
	const int max_buffer = 256;
	char buffer[max_buffer];
	strcat(cmd,&quot; 2&gt;&amp;1&quot;);
	stream = popen(cmd, &quot;r&quot;);

	if (stream) {
		while (!feof(stream))
			if (fgets(buffer, max_buffer, stream) != NULL) data.append(buffer);
			pclose(stream);
		}
	return data;
}

/*function that performs geti2c
string get(const char* reg1, const char* reg2){
	char str[100];
	string str2;

	strcpy(str, cmdGet);
	strcat(str, reg1);
	strcat(str, reg2);

	str2 = exec(str);
	return str2;
}

/*function that performs seti2c
void set(const char* reg1, const char* reg2, int value){
	char str[100];
	string str2;

	strcpy(str, cmdSet);
	strcat(str, reg1);
	strcat(str, reg2);
	strcat(str, to_string(value).c_str());

	str2 = exec(str);
}

int main(){
	set(deviceADDR, PWR_MGMT_1, 0);	//turn on the MPU6050
	while(true){
		accel_x = stoi(get(deviceADDR, ACCEL_X_OUT_H), nullptr, 16) &lt;&lt; 8 + stoi(get(deviceADDR, ACCEL_X_OUT_L), nullptr, 16);
		accel_y = stoi(get(deviceADDR, ACCEL_Y_OUT_H), nullptr, 16) &lt;&lt; 8 + stoi(get(deviceADDR, ACCEL_Y_OUT_L), nullptr, 16);
		accel_z = stoi(get(deviceADDR, ACCEL_Z_OUT_H), nullptr, 16) &lt;&lt; 8 + stoi(get(deviceADDR, ACCEL_Z_OUT_L), nullptr, 16);
		accel_x = accel_x / 16384;
		accel_y = accel_y / 16384;
		accel_z = accel_z / 16384;

		gyro_x = stoi(get(deviceADDR, GYRO_X_OUT_H), nullptr, 16) &lt;&lt; 8 + stoi(get(deviceADDR, GYRO_X_OUT_L), nullptr, 16);
		gyro_y = stoi(get(deviceADDR, GYRO_Y_OUT_H), nullptr, 16) &lt;&lt; 8 + stoi(get(deviceADDR, GYRO_Y_OUT_L), nullptr, 16);
		gyro_z = stoi(get(deviceADDR, GYRO_Z_OUT_H), nullptr, 16) &lt;&lt; 8 + stoi(get(deviceADDR, GYRO_Z_OUT_L), nullptr, 16);
		gyro_x = gyro_x / 131;
		gyro_y = gyro_y / 131;
		gyro_z = gyro_z / 131;

		cout &lt;&lt; &quot;X-acc: &quot; &lt;&lt; accel_x &lt;&lt; &quot; Y-acc: &quot; &lt;&lt; accel_y &lt;&lt; &quot; Z-acc: &quot; &lt;&lt; accel_z &lt;&lt; endl;
		cout &lt;&lt; &quot;X-gyro: &quot; &lt;&lt; gyro_x &lt;&lt; &quot; Y-gyro: &quot; &lt;&lt; gyro_y &lt;&lt; &quot; Z-gyro: &quot; &lt;&lt; gyro_z &lt;&lt; endl;

	}
	return 0;
}</code></pre></pre>
<p>You can now use your MPU6050 to create motion-based apps for the Beaglebone Black!</p>
<p>If you find this post useful, kindly post a comment below. Come back to this site for more updates!</p>
<p>The post <a href="https://www.teachmemicro.com/beaglebone-black-mpu6050-i2c-tutorial-part-2/">Beaglebone Black MPU6050 Interfacing: I2C Tutorial Part 2</a> appeared first on <a href="https://www.teachmemicro.com">Microcontroller Tutorials</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.teachmemicro.com/beaglebone-black-mpu6050-i2c-tutorial-part-2/feed/</wfw:commentRss>
			<slash:comments>6</slash:comments>
		
		
			</item>
	</channel>
</rss>

<!--
Performance optimized by W3 Total Cache. Learn more: https://www.boldgrid.com/w3-total-cache/?utm_source=w3tc&utm_medium=footer_comment&utm_campaign=free_plugin

Page Caching using Disk: Enhanced 

Served from: www.teachmemicro.com @ 2026-07-27 11:48:36 by W3 Total Cache
-->