loading

In this project i will show you how to build a simple 4 led tilt dimmer with a gyroscope and an arduino uno. There are 4 leds arranged in a "+" shape and they will light up more as you tilt the breadboard.

This won't involve any soldering, just basic breadboard assembly and basic arduino programming.

Step 1: Materials:

1) An Arduino Uno board and a USB cable. You can use a different board if you like but keep in mind that different boards have different pin configs, for example if you use an Arduino Mega the SDA and SCL pins are 20 and 21.

2) 4 leds, the leds should be identical , the color doesn't matter it's up to you :)

3) 4 identical resistors anywhere between 100 ohms and 1 K ohm, i recommend around 200

4) a breadboard

5) dupont wires

6) MPU-6050 gyro

7) U-shape jumper cables (optional). I've used these jumper cables because they look better on the breadboard, and the leds are more visible this way. You can find a box of 140 on ebay at about 4$. If you don't have these cables you can replace them with dupont wires.

Step 2: Assembly

1) Begin by placing the 4 leds on the breadboard in a "+" shape. The long pins of the leds are positive. I've placed the positive pins for the top and bottom leds on the right, and for the left and right leds below (see on the first picture.

2) Insert the four resistors in the breadboard.

3) Place the MPU6050 like in the picture

4) Insert the wires. The leds ground pins will go directly to the ground. The positive pins will go through a resistor into the arduino pins: pin 3 through a resistor to the front led, pin 5 through a resistor to the bottom led, and similar with pin 6 right led, pin 9 left led

The MPU6050 must be connected to ground and 5V+, after that connect SDA to A4 (analog 4), SCL to A5

I've also attached an fritzig schematic, if you want to make sure the connections are correct.

Step 3: The Code

   You will need two external libs I2CDev and MPU6050, i've attached them here, and i've posted below the source of the code. I've not written those libs it's not my merit :)

If you don't know how to install a library check this instructable:

Then copy paste or download my library and give it a try.

* I2CDev library source: https://github.com/jrowberg/i2cdevlib

Step 4: Improvements and Different Uses of the Gyro

This is the simplest project i've done with MPU6050, i can think of many derivatives from this idea:

- adding two or more leds for each direction, so the steeper the angel, the more leds will light up

- making a wearable that will warn you with a sound that you don't have a correct upright position

Those ugly conditions i think can be improved with some math (replace if's with some equations).

As a BONUS :) i've made a youtube video with another version of the project, i've added 3 leds for up, e for down, 2 for left and two for right.

If you want to check the video click here. I've also attached a picture of the breadboard above.

For those who are interested the code go here, and replace this line

#define SIMPLE_IMPLEMENTATION true

---------- with -----------

#define SIMPLE_IMPLEMENTATION false

The new led pinout is: front leds: 3,12,11, bottom leds: 5,6,7, left leds: 10, 4, right leds: 6, 9

In my other tutorial i've shown how the gyroscope can be used toflip the display on the computer when the display is physically rotated. The instructable is here.


If you liked the youtube videos, you can get more by subscribing to my channel here

<p>hello sir can you tell me how upload this code to arduino nano </p>
<p>Hello, you need to download and install the MPU6050 library and also I2CDev, i've attached them in the tutorial. Check this guide for installing a library into arduino ide: https://www.arduino.cc/en/guide/libraries</p>
<p>Hello sir,first of all thank your for the codes you provide. I want to ask how can I increase the brightness of the LED ? Where should i modify?</p>
Hello, you're wellcome. <br>For example if you're using <br>&quot;SIMPLE_IMPLEMENTATION = true;&quot;<br>you can modify the function:<br><br>void flashLEDsSimple(int x, int y, int z)<br>{<br> if (y &gt; 0) {<br> analogWrite(rightLed, y*4);<br> analogWrite(leftLed, 0); <br> } else {<br> analogWrite(leftLed, y*4*-1); <br> analogWrite(rightLed, 0); <br> }<br>......<br>and replace the constant &quot;4&quot; with a number a bit higher, so the steeper the angle the more intense PWM will be used. <br><br>Hope it helps :)
<p>Waoh!, thanks for the reply sir, really appreciate it. I'll give it a try first.</p>
<p>Hello again sir, i have enquiries, can i change the LED into 12v LED? If possible, where should i modify? Thank you.</p>
<p>You can, but some modifications are required. 12 v LED can't be powered directly from an arduino like the low voltage led's we used before, you have to use a NPN tranzistor and command the tranzistor through the arduino using a rezistor. You will also need a separate 12 volts power supply with the grounds connected to the 5 v arduino supply. So for each 12 v led you will need a NPN tranzistor and a rezistor. Adafruit has a tutorial here: https://learn.adafruit.com/rgb-led-strips/usage</p>
<p>Ok thank you sir. Apperciate it.</p>
<p>i cant compiling ..can help me?</p>
Copy and paste the error messages here please
<p>im getting this error :( any reason why?</p><p>warning: left shift count &gt;= width of type [enabled by default]</p><p> data[2] = ((packet[24] &lt;&lt; 24) + (packet[25] &lt;&lt; 16) + (packet[26] &lt;&lt; 8) + packet[27]);</p>
<br>Hmm, have you used the librares: I2Cdev.zip<br>MPU6050.zip i've attached to instructables or downloaded it from somewhere else ? Try to use my attached versions.
<p>i did indeed use your files but still gives me the error, really strange, any timps? tx</p>
<p>Hmm, it's from the gyro libraries, the only thing i've found about this error is on this forum :<a href="http://forum.arduino.cc/index.php?topic=371853.0">http://forum.arduino.cc/index.php?topic=371853.0 </a> </p><p>But that's not much of a help. </p><p>I haven't asked you what versiion is your Arduino IDE, is it the latest? 1.6.x ? and what kind of an arduino do you have? I've tested it on Arduino Uno, Arduino Pro mini. And also check that your library is included corectly, and not duplicated: https://learn.sparkfun.com/tutorials/installing-an-arduino-library</p>
What file and what line ?
<p>it doesn't say i think the library (i2C &amp; MPU) in not good or maybe i installed it wrong but i have tried everything :(</p><p>this is the full error </p><p>In file included from C:\Users\Florin\Downloads\arduino-nightly-windows\arduino-nightly\Test_Gyro_LED\Test_Gyro_LED.ino:44:0:</p><p>C:\Users\Florin\Documents\Arduino\libraries\MPU6050/MPU6050_6Axis_MotionApps20.h: In member function 'uint8_t MPU6050::dmpGetAccel(int32_t*, const uint8_t*)':</p><p>C:\Users\Florin\Documents\Arduino\libraries\MPU6050/MPU6050_6Axis_MotionApps20.h:580:31: warning: left shift count &gt;= width of type [enabled by default]</p><p> data[0] = ((packet[28] &lt;&lt; 24) + (packet[29] &lt;&lt; 16) + (packet[30] &lt;&lt; 8) + packet[31]);</p><p> ^</p><p>C:\Users\Florin\Documents\Arduino\libraries\MPU6050/MPU6050_6Axis_MotionApps20.h:580:52: warning: left shift count &gt;= width of type [enabled by default]</p><p> data[0] = ((packet[28] &lt;&lt; 24) + (packet[29] &lt;&lt; 16) + (packet[30] &lt;&lt; 8) + packet[31]);</p><p> ^</p><p>C:\Users\Florin\Documents\Arduino\libraries\MPU6050/MPU6050_6Axis_MotionApps20.h:581:31: warning: left shift count &gt;= width of type [enabled by default]</p><p> data[1] = ((packet[32] &lt;&lt; 24) + (packet[33] &lt;&lt; 16) + (packet[34] &lt;&lt; 8) + packet[35]);</p><p> ^</p><p>C:\Users\Florin\Documents\Arduino\libraries\MPU6050/MPU6050_6Axis_MotionApps20.h:581:52: warning: left shift count &gt;= width of type [enabled by default]</p><p> data[1] = ((packet[32] &lt;&lt; 24) + (packet[33] &lt;&lt; 16) + (packet[34] &lt;&lt; 8) + packet[35]);</p><p> ^</p><p>C:\Users\Florin\Documents\Arduino\libraries\MPU6050/MPU6050_6Axis_MotionApps20.h:582:31: warning: left shift count &gt;= width of type [enabled by default]</p><p> data[2] = ((packet[36] &lt;&lt; 24) + (packet[37] &lt;&lt; 16) + (packet[38] &lt;&lt; 8) + packet[39]);</p><p> ^</p><p>C:\Users\Florin\Documents\Arduino\libraries\MPU6050/MPU6050_6Axis_MotionApps20.h:582:52: warning: left shift count &gt;= width of type [enabled by default]</p><p> data[2] = ((packet[36] &lt;&lt; 24) + (packet[37] &lt;&lt; 16) + (packet[38] &lt;&lt; 8) + packet[39]);</p><p> ^</p><p>C:\Users\Florin\Documents\Arduino\libraries\MPU6050/MPU6050_6Axis_MotionApps20.h: In member function 'uint8_t MPU6050::dmpGetQuaternion(int32_t*, const uint8_t*)':</p><p>C:\Users\Florin\Documents\Arduino\libraries\MPU6050/MPU6050_6Axis_MotionApps20.h:604:30: warning: left shift count &gt;= width of type [enabled by default]</p><p> data[0] = ((packet[0] &lt;&lt; 24) + (packet[1] &lt;&lt; 16) + (packet[2] &lt;&lt; 8) + packet[3]);</p><p> ^</p><p>C:\Users\Florin\Documents\Arduino\libraries\MPU6050/MPU6050_6Axis_MotionApps20.h:604:50: warning: left shift count &gt;= width of type [enabled by default]</p><p> data[0] = ((packet[0] &lt;&lt; 24) + (packet[1] &lt;&lt; 16) + (packet[2] &lt;&lt; 8) + packet[3]);</p><p> ^</p><p>C:\Users\Florin\Documents\Arduino\libraries\MPU6050/MPU6050_6Axis_MotionApps20.h:605:30: warning: left shift count &gt;= width of type [enabled by default]</p><p> data[1] = ((packet[4] &lt;&lt; 24) + (packet[5] &lt;&lt; 16) + (packet[6] &lt;&lt; 8) + packet[7]);</p><p> ^</p><p>C:\Users\Florin\Documents\Arduino\libraries\MPU6050/MPU6050_6Axis_MotionApps20.h:605:50: warning: left shift count &gt;= width of type [enabled by default]</p><p> data[1] = ((packet[4] &lt;&lt; 24) + (packet[5] &lt;&lt; 16) + (packet[6] &lt;&lt; 8) + packet[7]);</p><p> ^</p><p>C:\Users\Florin\Documents\Arduino\libraries\MPU6050/MPU6050_6Axis_MotionApps20.h:606:30: warning: left shift count &gt;= width of type [enabled by default]</p><p> data[2] = ((packet[8] &lt;&lt; 24) + (packet[9] &lt;&lt; 16) + (packet[10] &lt;&lt; 8) + packet[11]);</p><p> ^</p><p>C:\Users\Florin\Documents\Arduino\libraries\MPU6050/MPU6050_6Axis_MotionApps20.h:606:50: warning: left shift count &gt;= width of type [enabled by default]</p><p> data[2] = ((packet[8] &lt;&lt; 24) + (packet[9] &lt;&lt; 16) + (packet[10] &lt;&lt; 8) + packet[11]);</p><p> ^</p><p>C:\Users\Florin\Documents\Arduino\libraries\MPU6050/MPU6050_6Axis_MotionApps20.h:607:31: warning: left shift count &gt;= width of type [enabled by default]</p><p> data[3] = ((packet[12] &lt;&lt; 24) + (packet[13] &lt;&lt; 16) + (packet[14] &lt;&lt; 8) + packet[15]);</p><p> ^</p><p>C:\Users\Florin\Documents\Arduino\libraries\MPU6050/MPU6050_6Axis_MotionApps20.h:607:52: warning: left shift count &gt;= width of type [enabled by default]</p><p> data[3] = ((packet[12] &lt;&lt; 24) + (packet[13] &lt;&lt; 16) + (packet[14] &lt;&lt; 8) + packet[15]);</p><p> ^</p><p>C:\Users\Florin\Documents\Arduino\libraries\MPU6050/MPU6050_6Axis_MotionApps20.h: In member function 'uint8_t MPU6050::dmpGetGyro(int32_t*, const uint8_t*)':</p><p>C:\Users\Florin\Documents\Arduino\libraries\MPU6050/MPU6050_6Axis_MotionApps20.h:637:31: warning: left shift count &gt;= width of type [enabled by default]</p><p> data[0] = ((packet[16] &lt;&lt; 24) + (packet[17] &lt;&lt; 16) + (packet[18] &lt;&lt; 8) + packet[19]);</p><p> ^</p><p>C:\Users\Florin\Documents\Arduino\libraries\MPU6050/MPU6050_6Axis_MotionApps20.h:637:52: warning: left shift count &gt;= width of type [enabled by default]</p><p> data[0] = ((packet[16] &lt;&lt; 24) + (packet[17] &lt;&lt; 16) + (packet[18] &lt;&lt; 8) + packet[19]);</p><p> ^</p><p>C:\Users\Florin\Documents\Arduino\libraries\MPU6050/MPU6050_6Axis_MotionApps20.h:638:31: warning: left shift count &gt;= width of type [enabled by default]</p><p> data[1] = ((packet[20] &lt;&lt; 24) + (packet[21] &lt;&lt; 16) + (packet[22] &lt;&lt; 8) + packet[23]);</p><p> ^</p><p>C:\Users\Florin\Documents\Arduino\libraries\MPU6050/MPU6050_6Axis_MotionApps20.h:638:52: warning: left shift count &gt;= width of type [enabled by default]</p><p> data[1] = ((packet[20] &lt;&lt; 24) + (packet[21] &lt;&lt; 16) + (packet[22] &lt;&lt; 8) + packet[23]);</p><p> ^</p><p>C:\Users\Florin\Documents\Arduino\libraries\MPU6050/MPU6050_6Axis_MotionApps20.h:639:31: warning: left shift count &gt;= width of type [enabled by default]</p><p> data[2] = ((packet[24] &lt;&lt; 24) + (packet[25] &lt;&lt; 16) + (packet[26] &lt;&lt; 8) + packet[27]);</p><p> ^</p><p>C:\Users\Florin\Documents\Arduino\libraries\MPU6050/MPU6050_6Axis_MotionApps20.h:639:52: warning: left shift count &gt;= width of type [enabled by default]</p><p> data[2] = ((packet[24] &lt;&lt; 24) + (packet[25] &lt;&lt; 16) + (packet[26] &lt;&lt; 8) + packet[27]);</p><p> ^</p>
What do you me one led light up at a time? I have two version in my code, one with led dimming and one led for each axys, and one with muliple led for each axys. The steeper the angle the more leds to light up for that axys. You would like to implement a delay between each led so that the change doesn't seem that abrupt?
In the multi led code you have leds lighting up the steeper the angle. Instead of all 3 lighting up at full tilt, is it possible to make it so only one led turns on per angle. Sort of like a single pixel moving up and down a line. Sorry if my description is confusing.
Sure it is, some refactorings must be done in &quot;flashLEDsMultiple&quot; function. Post your code here and i'll try to modify it to make a demo.
<p>Working great! Still pretty new to this but, how would I get one LED to light up at a time instead of creating a string? Played around with the HIGH and LOW with no luck.</p><p>I'm building a swing set that lights up according to the riders angle. Instead of single LED's I'm using an 8 ch relay board (Inputs work no differently than ledpins) that will power LED rope lights. I've also tweaked the code a bit to run lights one axis. &quot;u&quot; &amp; &quot;d&quot;</p>
<p>how can I add an certain angle to it to be active??</p>
<p>This is the led config:ledConfig leds[] = {<br> {3, 1, 'u', 5, 15}, <br> {12, 2, 'u', 16, 30},<br> {11, 3, 'u', 31, 45}, <br> {5, 1, 'd', 5, 15}, <br> {6, 2, 'd', 16, 30},<br> {7, 3, 'd', 31, 45}, <br> {6, 1, 'r', 5, 23}, <br> {9, 2, 'r', 24, 45},<br> {10, 1, 'l', 5, 23}, <br> {4, 2, 'l', 24, 45},<br>};</p><p>So let's say you want to add an extra led that will be lit after down angle will decrease between 45 and 60 degreeds you should add an element in the matrix like : {2, 4, 'd', 46, 60} where &quot;2&quot; is the led pin. </p><p>You should also replace &quot;for (int i=0; i&lt;10; i++) {&quot; the value &quot;10&quot; with an eleven (there are two occurences). And it should work.</p>
<p>Works Great !! Now i would like to control 3 LEDs so that instead of showing direction it results in say if x direction is more then 10 degrees then it Lights 1st led, if x direction is between angle 10 degrees to -10 degrees then it lights up 2nd led, and if angle is less than -10 degrees than it lights 3rd led, can it be done?? Thanks ;-) </p>
<p>Yep i already did that:) you can see that here: <iframe allowfullscreen="" frameborder="0" height="281" src="//www.youtube.com/embed/lYH1H_nWLz4" width="500"></iframe></p><p>You can read the last part of the tutorial to find out how to adapt it;) If you have more questions after reading that, go ahead and ask!</p>
<p>Thanks Bro, i just got it, its working fine now :-) Thank you so much !!!</p>
<p>No problem, glad to help :)</p>
<p>works Awsome I was able to make it with out any programming or Arduino Experience. now I would like to add more lights for different banks, pitchs, Yaw. Any chance of getting some advise on adding to code</p>
<p>I'm glad it worked out :) Well if you like to enhance the prototype first tell me exactly what wolud you like to do </p>
<div><div><div><div><div><div><div><div><div><div><div><div><div><div><div><div><div><div><div><div><div><div><div><div><div><div><blockquote>danionescu</blockquote><p>Thanks for getting back to me. I was wondering if the Gy 521 with Arduino UNO R3 can work with more lights. I was thinking bank 10,20,30,45. Pitch 3 different degrees up and down. Yaw 2 different degrees each way. If you could write code or tell me how to write it. </p></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div>
<p> I've modified the code, but i have not fully tested yet. The source is: <a href="https://bitbucket.org/danionescu/arduino/src/bdeb3ee79afaaad26282fc7c8b089a99440afd39/giroscope_led_controll/giroscope_led_controll.ino?at=default&fileviewer=file-view-default" rel="nofollow">https://bitbucket.org/danionescu/arduino/src/bdeb3...</a></p><p>You have to change the line: #define SIMPLE_IMPLEMENTATION true</p><p>to false. </p><p> The led pinout is in the matrix: ledConfig leds[] = {</p><p> {3, 1, 'u', 5, 15}, </p><p> {12, 2, 'u', 16, 30},</p><p> {11, 3, 'u', 31, 45}, </p><p> {5, 1, 'd', 5, 15}, </p><p> {6, 2, 'd', 16, 30},</p><p> {7, 3, 'd', 31, 45}, </p><p> {6, 1, 'r', 5, 23}, </p><p> {9, 2, 'r', 24, 45},</p><p> {10, 1, 'l', 5, 23}, </p><p> {4, 2, 'l', 24, 45},</p><p>};</p><p> Where the first parameter is led pin, the third means (up, down, right, left), the forth minAngle, the fifth maxAngle.</p><p> I'll upload an youtube video next weekend, but you can work with the existing code i guess. If you have questions i'l be happy to answer them.</p><p>Hope it helped somehow:) </p>
<p>not sure if i am doing this right. I tried changing the code to what you typed in the comment but am getting error when i verify code.&quot;exit status 1 macro names must be identifiers&quot;</p>
<p>What is the second parameter</p>
<p>The second parameter in the array is the led position inside the group, but it's not actually used in the sketch.</p>
<p>Here is the video: https://www.youtube.com/watch?edit=vd&amp;v=lYH1H_nWLz4</p>
<p>I will try to write the code maybe during the weekend, it shouldn't be that complicated.</p>
<p>GYRO.ino:108:24: note: in expansion of macro 'F'<br>Error compiling.</p><p>I got this.</p>
Are you using arduino ide 1.6 (latest) ?
<p>yes sir.</p>
<p>Works good</p>
Looks nice. I've just got an idea to use a led matrix (MAX7219) to move a dot (led) in any direction on the matrix
<p>If you're using an ArduinoMega SDA is pin 20, SCL is pin21. if you want to test the MPU6050 directly(without my code) you can the library: <a href="https://github.com/jrowberg/i2cdevlib" rel="nofollow">https://github.com/jrowberg/i2cdevlib</a></p><p>After cloning it copy the Arduino/MPU6050 folder under your libraries and try the example: <strong>MPU6050_DMP6</strong></p>
Board*
So hi I'm Samuel I previously got different code working by plugging in the int pin at the far right could you have forgotten it otherwise my beard or mpu might be broken
<p>help, the code wont work it keep on giving the error 1 that the initialization failed help!</p>
<p>I think the wiring might be incorrect, the gyroscope damaged or the arduino sda/scl pins damaged. I've personally tried many times with different arduinos and it works.What kind of arduino do you have? Have you modified the code? And are you sure you have MPU6050 i'm asking this because there are many variants? </p>
<p>Thank you so much. I've been looking for a tutorial that actually lets you use the data from the MPU6050 to make some sort of output besides a little 3D graphic on a screen. Keep it up!</p>
<p>You're wellcome. I'm planning to do more:) If you find some part of the tutorial hard to understand, please tell me and i will make improvements.</p>
<p>Looks great! I love arduinos!</p>
<p>Thanks, it's my first instructable :)</p>

About This Instructable

11,578views

56favorites

License:

Bio: I'm an electronics enthusiast, passionate about science, and programming. I am a web developer but i like the challenges involved with building things from ...
More by danionescu:Alarm System Security Put to the Test How to chart your instructable statistics Automated Windows Shades 
Add instructable to: