Gyroscope Led Control With Arduino

26,186

60

101

About: 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 scratch.

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.

Teacher Notes

Teachers! Did you use this instructable in your classroom?
Add a Teacher Note to share how you incorporated it into your lesson.

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

Source code here: https://github.com/danionescu0/arduino/tree/master...

Or copy-paste it from below:

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

Step 5: A Recent Add-on to This Tutorial, a Neopixel Ring Driven by a Gyroscope

You can find the code here if you're interested about that.

1 Person Made This Project!

Recommendations

  • Made with Math Contest

    Made with Math Contest
  • Multi-Discipline Contest

    Multi-Discipline Contest
  • Robotics Contest

    Robotics Contest

101 Discussions

0
None
danionescuEmengTV

Reply 5 months ago

It's a problem with the instructable editor, i'm sorry. It adds some <br> an <p> tags, please delete the <br> and all will be fine.

0
None
EmengTVdanionescu

Reply 5 months ago

Thanks to the respond man!
-------
I follow the instuctions but there are two pinouts in #6 ?
ButtomLed and RightLed
Is that correct?

received_671834729942322.jpeg
0
None
danionescuEmengTV

Reply 5 months ago

Well, i see your sketch it's different from mine, try to use mine to see if it works i don't know how you modified it:)
Mine looks like this:
const int frontLed = 3;
const int bottomLed = 5;
const int rightLed = 6;
const int leftLed = 9;
Double check with the code i provided in the tutorial

0
None
EmengTVdanionescu

Reply 5 months ago

But the instruction you said is this... How about the other pinouts?

received_345314313001292.jpeg
0
None
danionescuEmengTV

Reply 5 months ago

That's the pins coresponding to the led's. The code you just need to ensure the simple implementation is set to false
#define SIMPLE_IMPLEMENTATION false
If you need to add more then 10 led's you need to modify the sketch substantially

0
None
EmengTVdanionescu

Reply 5 months ago

Sorry sir but.. can you help me again... The

is not declared?

received_343364429698397.jpeg
0
None
EmengTVdanionescu

Reply 5 months ago

Is this code is for 10 LEDs?
Because i need to try more LED

0
None
chasetheduke

Question 7 months ago

I'm a beginner in writing code but is there a possible way to make this same code work off of the xy movements instead of the gyro. I am making a 3D printer and wanted to have LEDs light up when the head moves around.

1 answer
0
None
danionescuchasetheduke

Reply 7 months ago

Hello. I think you need the accelerometer values of the device. MPU6050 has a gyro and a accelerometer built in. The accelerometer will report the accelerations on X, Y and Z axes.
To get the values from each axys use : mpu.getAcceleration(&ax, &ay, &az); Where mpu is your library instance and ax, ay and az are values you have previously initialized.

0
None
NartA2

9 months ago

Hello, i have done everything literally the way you did it. everything is working and the montoring is showing the values change. However, the LEDS are not working.
Do resistors have to be all similar in value?
I am currently using 1 k ohm resistors

3 replies
0
None
danionescuNartA2

Reply 9 months ago

Yes they should be similar values. To check if the wiring is good and the leds work well with the resistors upload the blink sketch, and modify the led pin. If it's all ok it should blink if not, the wiring is bad, or the led are busted, or the resistors have a higher value then needed. This is how you calculate the value for the resistor: http://www.ohmslawcalculator.com/led-resistor-calculator

0
None
NartA2danionescu

Reply 9 months ago

thank ykmour for your reply, it eventually worked with similar 1 k ohm resistors