Introduction: Gyroscope Led Control With Arduino
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
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
Attachments
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.
111 Comments
Question 6 weeks ago on Introduction
what should I do if I want to add 4 buzzer outputs and how do you make the tilt angles on each side readable on the serial monitor? Thank you for your help
Question 2 years ago
hello I wanted to ask you if it was possible to increase the sensitivity of the gy 521 sensor if yes which values should be changed thanks
Answer 2 years ago
Hmm i found this link if it helps: https://howtomechatronics.com/tutorials/arduino/arduino-and-mpu6050-accelerometer-and-gyroscope-tutorial/
Question 2 years ago on Introduction
Si può usare con esp32
Mi daresti una mano grazie
Answer 2 years ago
Hello, can you please post in English? Thanks
Reply 2 years ago
good morning I wanted to ask you if it was possible to replace the relays with the led in. more I asked her how it is possible to vary the sensitivity of the 6050 mpu thanks in advance
Reply 2 years ago
Morning
Do you mean you want to replace the led's with relays? sure you can do that, but be sure you use relay board. If you're using just relays varoius components must be added like a resistor, a transitor and a diode but the relay board contains them all.
For your second question the MPU6050 doesn't have variable sensitivity it just reports what it senses, please tell me what are you trying to achieve so i may offer a workaround:)
Reply 2 years ago
I want to use a card with 8 relays I have to create a self-leveling system for campers the gyroscope must manage the relays which in turn will manage the solenoid valves and a hydraulic pump
Reply 2 years ago
That's a very interesting project, but it's substantially more complex that my LED's project. You would have to know each actuator(valve) position how much is it opened and closed, and use some geometry to model the 3 dimensional rectangle.
Also the controll might be through PID (https://www.norwegiancreations.com/2016/08/the-seesaw-part-2-basic-pid-theory-and-arduino-implementation/).
Check out this simpler project: http://www.diva-portal.org/smash/get/diva2:916229/FULLTEXT01.pdf
Well don't have enough knowledge to take this project on my own, but i gave you some hints which might get you started :)
Have fun with the project!
Reply 2 years ago
this is the scheme of operation
4 years ago
Is this correct?
Reply 4 years 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.
Reply 4 years ago
Thanks to the respond man!
-------
I follow the instuctions but there are two pinouts in #6 ?
ButtomLed and RightLed
Is that correct?
Reply 4 years 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
Reply 4 years ago
But the instruction you said is this... How about the other pinouts?
Reply 4 years 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
Reply 4 years ago
Thanks a lot sir!
Reply 4 years ago
You're wellcome. hope this helps
Reply 4 years ago
is not declared?
Reply 4 years ago
Remowe all html tags from the code, you have a <p> tag there it won't work.
You can download the clean code from my repository: https://github.com/danionescu0/arduino/tree/master/projects/giroscope_led_controll