Introduction: Gyroscope Led Control With Arduino

Picture of 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:

Picture of 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

Picture of 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

Picture of 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<br>

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.

Comments

RecepK4 (author)2017-11-28

Hi my friend,

My project is that assemble the gyro and arduino to bicycle helmet. When I assemble it ,i will solve my right and left signal problem. When i rotate my head 15 agree to left , the led will open. So, drivers will be understood what direction i will go.

Thank you for sharing the code . It help me so much to do my project. However, I struggle with a possibility of giving wrong decision. It means that when i want to turn right but i rotate my head15 degree to left. After that I understand my decision wrong and I changed my brain right but i wait the time to signal close for left.

How can I change your code according to my project. At the below , You can see my updated code

/**

* Simple implementation false

*

* means that for pitch/yaw there only be

* a single led for each direction. The led will get more bright

* as the direction increases

*

* Simple implementation true

* means that for pitch/yaw there will be multiple led's that light up one by one

*

* The accelerometer/gyro is a MPU6050, it should be wired to the SDA and SCL pins

*/

#define SIMPLE_IMPLEMENTATION false

const int frontLed = 3;

const int bottomLed = 5;

const int rightLed = 6;

const int leftLed = 9;

long int lastPrintTime;

typedef struct

{

byte pin;

byte positionInsideGroup;

char thePosition; // Left, Right, Up, Down

byte minAngle;

byte maxAngle;

} ledConfig;

ledConfig leds[] = {

{3, 1, 'u', 5,15},

{12, 2, 'u', 16, 30},

{11, 3, 'u', 5, 15},

{5, 1, 'd', 5, 15},

{6, 2, 'd', 16, 30},

{7, 3, 'd', 31, 45},

{8 , 1, 'r', 5, 23},

{9, 2, 'r', 24, 45},

{10, 1, 'l', 5, 23},

{4, 2, 'l', 24, 45},

};

#include "I2Cdev.h"

#include "MPU6050_6Axis_MotionApps20.h"

#if I2CDEV_IMPLEMENTATION == I2CDEV_ARDUINO_WIRE

#include "Wire.h"

#endif

MPU6050 mpu;

bool dmpReady = false; // set true if DMP init was successful

uint8_t mpuIntStatus; // holds actual interrupt status byte from MPU

uint8_t devStatus; // return status after each device operation (0 = success, !0 = error)

uint16_t packetSize; // expected DMP packet size (default is 42 bytes)

uint16_t fifoCount; // count of all bytes currently in FIFO

uint8_t fifoBuffer[64]; // FIFO storage buffer

// orientation/motion vars

Quaternion q; // [w, x, y, z] quaternion container

VectorInt16 aa; // [x, y, z] accel sensor measurements

VectorInt16 aaReal; // [x, y, z] gravity-free accel sensor measurements

VectorInt16 aaWorld; // [x, y, z] world-frame accel sensor measurements

VectorFloat gravity; // [x, y, z] gravity vector

float euler[3]; // [psi, theta, phi] Euler angle container

float ypr[3]; // [yaw, pitch, roll] yaw/pitch/roll container and gravity vector

volatile bool mpuInterrupt = false; // indicates whether MPU interrupt pin has gone high

void setup()

{

#if I2CDEV_IMPLEMENTATION == I2CDEV_ARDUINO_WIRE

Wire.begin();

TWBR = 24; // 400kHz I2C clock (200kHz if CPU is 8MHz)

#elif I2CDEV_IMPLEMENTATION == I2CDEV_BUILTIN_FASTWIRE

Fastwire::setup(400, true);

#endif

Serial.begin(9600);

while (!Serial); // wait for Leonardo enumeration, others continue immediately

Serial.println(F("Initializing I2C devices..."));

mpu.initialize();

Serial.println(F("Testing device connections..."));

Serial.println(mpu.testConnection() ? F("MPU6050 connection successful") : F("MPU6050 connection failed"));

Serial.println(F("Initializing DMP..."));

devStatus = mpu.dmpInitialize();

mpu.setXGyroOffset(220);

mpu.setYGyroOffset(76);

mpu.setZGyroOffset(-85);

mpu.setZAccelOffset(1788); // 1688 factory default for my test chip

if (devStatus == 0) {

// turn on the DMP, now that it's ready

Serial.println(F("Enabling DMP..."));

mpu.setDMPEnabled(true);

Serial.println(F("Enabling interrupt detection (Arduino external interrupt 0)..."));

attachInterrupt(0, dmpDataReady, RISING);

mpuIntStatus = mpu.getIntStatus();

Serial.println(F("DMP ready! Waiting for first interrupt..."));

dmpReady = true;

packetSize = mpu.dmpGetFIFOPacketSize();

} else {

Serial.print(F("DMP Initialization failed (code "));

Serial.print(devStatus);

Serial.println(F(")"));

}

if (SIMPLE_IMPLEMENTATION) {

initializeLEDsSimple();

} else {

initializeLEDsMultiple();

}

lastPrintTime = millis();

}

void loop()

{

if (!dmpReady) return;

mpuInterrupt = false;

mpuIntStatus = mpu.getIntStatus();

fifoCount = mpu.getFIFOCount();

if ((mpuIntStatus & 0x10) || fifoCount == 1024) {

mpu.resetFIFO();

Serial.println(F("FIFO overflow!"));

} else if (mpuIntStatus & 0x02) {

while (fifoCount < packetSize) {

fifoCount = mpu.getFIFOCount();

}

mpu.getFIFOBytes(fifoBuffer, packetSize);

fifoCount -= packetSize;

mpu.dmpGetQuaternion(&q, fifoBuffer);

mpu.dmpGetGravity(&gravity, &q);

mpu.dmpGetYawPitchRoll(ypr, &q, &gravity);

int x = ypr[0] * 180/M_PI;

int y = ypr[1] * 180/M_PI;

int z = ypr[2] * 180/M_PI;

Serial.print(y);

Serial.print("\t");

Serial.println(z);

if (SIMPLE_IMPLEMENTATION) {

flashLEDsSimple(x, y, z);

} else {

flashLEDsMultiple(x, y, z);

}

}

}

void initializeLEDsSimple()

{

Serial.print("int simple");

pinMode(frontLed, OUTPUT);

pinMode(bottomLed, OUTPUT);

pinMode(rightLed, OUTPUT);

pinMode(leftLed, OUTPUT);

}

void initializeLEDsMultiple()

{ Serial.print("int multiple");

for (int i=0; i<10; i++) {

Serial.println(leds[i].pin);

pinMode(leds[i].pin, OUTPUT);

}

delay(3000);

}

void flashLEDsSimple(int x, int y, int z)

{

Serial.print("led simple");

if (y > 0) {

analogWrite(rightLed, y*4);

analogWrite(leftLed, 0);

} else {

analogWrite(leftLed, y*4*-1);

analogWrite(rightLed, 0);

}

if (z > 0) {

analogWrite(bottomLed, z*4);

analogWrite(frontLed, 0);

} else {

analogWrite(frontLed, z*4*-1);

analogWrite(bottomLed, 0);

}

}

void flashLEDsMultiple(int x, int y, int z)

{

//Serial.print(z);Serial.print(",");Serial.print(leds[i].thePosition);Serial.print(",");Serial.println(leds[i].minAngle);

bool modified = false;

if (z < 0 && leds[0].thePosition == 'u' && abs(z) > leds[0].minAngle) {

for (int b=1; b <= 5; b++){

digitalWrite(leds[0].pin, HIGH);

delay(500);

digitalWrite(leds[0].pin, LOW);

delay(500);

}

modified = true;

}

if (!modified) {

digitalWrite(leds[0].pin, LOW);

}

if (z > 0 && leds[3].thePosition == 'd' && abs(z) > leds[3].minAngle) {

for (int a=1; a <= 5; a++){

digitalWrite(leds[3].pin, HIGH);

delay(500);

digitalWrite(leds[3].pin, LOW);

delay(500);

}

modified = true;

}

if (!modified) {

digitalWrite(leds[3].pin, LOW);

}

}

void dmpDataReady()

{

mpuInterrupt = true;

}

danionescu (author)RecepK42017-12-06

So you want to turn 15 degrees right and signal right or turn it 15 degrees left and signal left am i correct so far? If not can you please rephrase , and if i am right, what did it go wrong with the project? I don't understand what do you mean by "struggle with the possibility of wrong decision"

Alex Sap (author)2017-10-03

Hi my friend I want to make the same project by using an arduino genuino 101 and a neopixel ring. I am not so experienced so I would appreciate any advice. Do you think that I can use your code with some modifications? Thanks in advance!

danionescu (author)Alex Sap2017-10-21

Hey, if you're interested i've finished the project you wore asking about. The video is here: https://youtu.be/MFQ2PecTw8g and the code here: https://github.com/danionescu0/arduino/blob/master/projects/neopixel_ring_gyroscope/neopixel_ring_gyroscope.ino

danionescu (author)Alex Sap2017-10-04

Hmm, you have to modifiy the code a bit more, i think i'll buy one of those strips and make a new instructable

Meanwhile you can chech these links:
https://learn.adafruit.com/florabrella/test-the-neopixel-strip
https://www.hackster.io/zanycadencedev/getting-started-with-arduino-and-neopixels-013360

Alex Sap (author)danionescu2017-10-04

Thank you very much my friend for the reply! I 'll give it a try and we 'll see :) !

elliotts17 (author)2017-10-21

Hello. And thank you for posting this great project. I would like to create a similar project but to have a spinning top that lights up LEDs in the direction of the turn and also shows tilt. I would also like it too be self contained so it can be mobile. Any idea where to start?

danionescu (author)elliotts172017-10-21

Thanks. What do you mean by a spinning top? can you give me more details, maybe a picture with a sketch? Does it involve a gyroscope too ?

AlexR385 made it! (author)2017-10-07

Hello, help my to modify the code so that the led worked in fade mode.

That would be three led group from the different sides (back top left right.) worked in a fade mode.

danionescu (author)AlexR3852017-10-08

Hello. Are you planning to use a led matrix and if so what kind of matrix?

napster007 (author)2017-06-15

I am having Error "no match for operator! (!serial)" Am I missing library?

napster007 (author)napster0072017-06-15

GyroscopeLedControll:77: error: no match for 'operator!' in '!Serial'

danionescu (author)napster0072017-06-18

Stragnge, what kind of arduino board are you using ?

JPTUDELFT (author)2017-05-16

This is truely great! I am a teacher on secundary school design class. Would you agree if I use this tutorial for one of my lessons? I hope to hear from you.

Greetz Pim

danionescu (author)JPTUDELFT2017-05-16

Thanks! I agree, these tutorials are here to help people, so go ahead:)
If you need more info or details just give me a message.

teddypapa (author)2017-02-28

hello sir can you tell me how upload this code to arduino nano

danionescu (author)teddypapa2017-03-02

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

FredieJ (author)2017-02-17

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?

danionescu (author)FredieJ2017-02-18

Hello, you're wellcome.
For example if you're using
"SIMPLE_IMPLEMENTATION = true;"
you can modify the function:

void flashLEDsSimple(int x, int y, int z)
{
if (y > 0) {
analogWrite(rightLed, y*4);
analogWrite(leftLed, 0);
} else {
analogWrite(leftLed, y*4*-1);
analogWrite(rightLed, 0);
}
......
and replace the constant "4" with a number a bit higher, so the steeper the angle the more intense PWM will be used.

Hope it helps :)

FredieJ (author)danionescu2017-02-20

Waoh!, thanks for the reply sir, really appreciate it. I'll give it a try first.

FredieJ (author)FredieJ2017-02-24

Hello again sir, i have enquiries, can i change the LED into 12v LED? If possible, where should i modify? Thank you.

danionescu (author)FredieJ2017-02-24

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

FredieJ (author)danionescu2017-02-27

Ok thank you sir. Apperciate it.

AzwanT1 (author)2017-02-16

i cant compiling ..can help me?

danionescu (author)AzwanT12017-02-16

Copy and paste the error messages here please

FlorinB19 (author)2017-02-14

im getting this error :( any reason why?

warning: left shift count >= width of type [enabled by default]

data[2] = ((packet[24] << 24) + (packet[25] << 16) + (packet[26] << 8) + packet[27]);

danionescu (author)FlorinB192017-02-15


Hmm, have you used the librares: I2Cdev.zip
MPU6050.zip i've attached to instructables or downloaded it from somewhere else ? Try to use my attached versions.

FlorinB19 (author)danionescu2017-02-15

i did indeed use your files but still gives me the error, really strange, any timps? tx

danionescu (author)FlorinB192017-02-16

Hmm, it's from the gyro libraries, the only thing i've found about this error is on this forum :http://forum.arduino.cc/index.php?topic=371853.0

But that's not much of a help.

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

danionescu (author)FlorinB192017-02-15

What file and what line ?

FlorinB19 (author)danionescu2017-02-15

it doesn't say i think the library (i2C & MPU) in not good or maybe i installed it wrong but i have tried everything :(

this is the full error

In file included from C:\Users\Florin\Downloads\arduino-nightly-windows\arduino-nightly\Test_Gyro_LED\Test_Gyro_LED.ino:44:0:

C:\Users\Florin\Documents\Arduino\libraries\MPU6050/MPU6050_6Axis_MotionApps20.h: In member function 'uint8_t MPU6050::dmpGetAccel(int32_t*, const uint8_t*)':

C:\Users\Florin\Documents\Arduino\libraries\MPU6050/MPU6050_6Axis_MotionApps20.h:580:31: warning: left shift count >= width of type [enabled by default]

data[0] = ((packet[28] << 24) + (packet[29] << 16) + (packet[30] << 8) + packet[31]);

^

C:\Users\Florin\Documents\Arduino\libraries\MPU6050/MPU6050_6Axis_MotionApps20.h:580:52: warning: left shift count >= width of type [enabled by default]

data[0] = ((packet[28] << 24) + (packet[29] << 16) + (packet[30] << 8) + packet[31]);

^

C:\Users\Florin\Documents\Arduino\libraries\MPU6050/MPU6050_6Axis_MotionApps20.h:581:31: warning: left shift count >= width of type [enabled by default]

data[1] = ((packet[32] << 24) + (packet[33] << 16) + (packet[34] << 8) + packet[35]);

^

C:\Users\Florin\Documents\Arduino\libraries\MPU6050/MPU6050_6Axis_MotionApps20.h:581:52: warning: left shift count >= width of type [enabled by default]

data[1] = ((packet[32] << 24) + (packet[33] << 16) + (packet[34] << 8) + packet[35]);

^

C:\Users\Florin\Documents\Arduino\libraries\MPU6050/MPU6050_6Axis_MotionApps20.h:582:31: warning: left shift count >= width of type [enabled by default]

data[2] = ((packet[36] << 24) + (packet[37] << 16) + (packet[38] << 8) + packet[39]);

^

C:\Users\Florin\Documents\Arduino\libraries\MPU6050/MPU6050_6Axis_MotionApps20.h:582:52: warning: left shift count >= width of type [enabled by default]

data[2] = ((packet[36] << 24) + (packet[37] << 16) + (packet[38] << 8) + packet[39]);

^

C:\Users\Florin\Documents\Arduino\libraries\MPU6050/MPU6050_6Axis_MotionApps20.h: In member function 'uint8_t MPU6050::dmpGetQuaternion(int32_t*, const uint8_t*)':

C:\Users\Florin\Documents\Arduino\libraries\MPU6050/MPU6050_6Axis_MotionApps20.h:604:30: warning: left shift count >= width of type [enabled by default]

data[0] = ((packet[0] << 24) + (packet[1] << 16) + (packet[2] << 8) + packet[3]);

^

C:\Users\Florin\Documents\Arduino\libraries\MPU6050/MPU6050_6Axis_MotionApps20.h:604:50: warning: left shift count >= width of type [enabled by default]

data[0] = ((packet[0] << 24) + (packet[1] << 16) + (packet[2] << 8) + packet[3]);

^

C:\Users\Florin\Documents\Arduino\libraries\MPU6050/MPU6050_6Axis_MotionApps20.h:605:30: warning: left shift count >= width of type [enabled by default]

data[1] = ((packet[4] << 24) + (packet[5] << 16) + (packet[6] << 8) + packet[7]);

^

C:\Users\Florin\Documents\Arduino\libraries\MPU6050/MPU6050_6Axis_MotionApps20.h:605:50: warning: left shift count >= width of type [enabled by default]

data[1] = ((packet[4] << 24) + (packet[5] << 16) + (packet[6] << 8) + packet[7]);

^

C:\Users\Florin\Documents\Arduino\libraries\MPU6050/MPU6050_6Axis_MotionApps20.h:606:30: warning: left shift count >= width of type [enabled by default]

data[2] = ((packet[8] << 24) + (packet[9] << 16) + (packet[10] << 8) + packet[11]);

^

C:\Users\Florin\Documents\Arduino\libraries\MPU6050/MPU6050_6Axis_MotionApps20.h:606:50: warning: left shift count >= width of type [enabled by default]

data[2] = ((packet[8] << 24) + (packet[9] << 16) + (packet[10] << 8) + packet[11]);

^

C:\Users\Florin\Documents\Arduino\libraries\MPU6050/MPU6050_6Axis_MotionApps20.h:607:31: warning: left shift count >= width of type [enabled by default]

data[3] = ((packet[12] << 24) + (packet[13] << 16) + (packet[14] << 8) + packet[15]);

^

C:\Users\Florin\Documents\Arduino\libraries\MPU6050/MPU6050_6Axis_MotionApps20.h:607:52: warning: left shift count >= width of type [enabled by default]

data[3] = ((packet[12] << 24) + (packet[13] << 16) + (packet[14] << 8) + packet[15]);

^

C:\Users\Florin\Documents\Arduino\libraries\MPU6050/MPU6050_6Axis_MotionApps20.h: In member function 'uint8_t MPU6050::dmpGetGyro(int32_t*, const uint8_t*)':

C:\Users\Florin\Documents\Arduino\libraries\MPU6050/MPU6050_6Axis_MotionApps20.h:637:31: warning: left shift count >= width of type [enabled by default]

data[0] = ((packet[16] << 24) + (packet[17] << 16) + (packet[18] << 8) + packet[19]);

^

C:\Users\Florin\Documents\Arduino\libraries\MPU6050/MPU6050_6Axis_MotionApps20.h:637:52: warning: left shift count >= width of type [enabled by default]

data[0] = ((packet[16] << 24) + (packet[17] << 16) + (packet[18] << 8) + packet[19]);

^

C:\Users\Florin\Documents\Arduino\libraries\MPU6050/MPU6050_6Axis_MotionApps20.h:638:31: warning: left shift count >= width of type [enabled by default]

data[1] = ((packet[20] << 24) + (packet[21] << 16) + (packet[22] << 8) + packet[23]);

^

C:\Users\Florin\Documents\Arduino\libraries\MPU6050/MPU6050_6Axis_MotionApps20.h:638:52: warning: left shift count >= width of type [enabled by default]

data[1] = ((packet[20] << 24) + (packet[21] << 16) + (packet[22] << 8) + packet[23]);

^

C:\Users\Florin\Documents\Arduino\libraries\MPU6050/MPU6050_6Axis_MotionApps20.h:639:31: warning: left shift count >= width of type [enabled by default]

data[2] = ((packet[24] << 24) + (packet[25] << 16) + (packet[26] << 8) + packet[27]);

^

C:\Users\Florin\Documents\Arduino\libraries\MPU6050/MPU6050_6Axis_MotionApps20.h:639:52: warning: left shift count >= width of type [enabled by default]

data[2] = ((packet[24] << 24) + (packet[25] << 16) + (packet[26] << 8) + packet[27]);

^

danionescu (author)2016-08-04

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?

jakegellinger (author)danionescu2016-08-04

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.

danionescu (author)jakegellinger2016-08-04

Sure it is, some refactorings must be done in "flashLEDsMultiple" function. Post your code here and i'll try to modify it to make a demo.

jakegellinger (author)2016-08-03

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.

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. "u" & "d"

EduardoA81 (author)2016-06-01

how can I add an certain angle to it to be active??

danionescu (author)EduardoA812016-06-02

This is the led config:ledConfig leds[] = {
{3, 1, 'u', 5, 15},
{12, 2, 'u', 16, 30},
{11, 3, 'u', 31, 45},
{5, 1, 'd', 5, 15},
{6, 2, 'd', 16, 30},
{7, 3, 'd', 31, 45},
{6, 1, 'r', 5, 23},
{9, 2, 'r', 24, 45},
{10, 1, 'l', 5, 23},
{4, 2, 'l', 24, 45},
};

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 "2" is the led pin.

You should also replace "for (int i=0; i<10; i++) {" the value "10" with an eleven (there are two occurences). And it should work.

AkashM15 (author)2016-05-28

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 ;-)

danionescu (author)AkashM152016-05-28

Yep i already did that:) you can see that here:

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!

AkashM15 (author)danionescu2016-05-28

Thanks Bro, i just got it, its working fine now :-) Thank you so much !!!

danionescu (author)AkashM152016-05-28

No problem, glad to help :)

GaryK56 (author)2016-03-31

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

danionescu (author)GaryK562016-03-31

I'm glad it worked out :) Well if you like to enhance the prototype first tell me exactly what wolud you like to do

GaryK56 (author)danionescu2016-04-05
danionescu

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.

danionescu (author)GaryK562016-04-11

I've modified the code, but i have not fully tested yet. The source is: https://bitbucket.org/danionescu/arduino/src/bdeb3...

You have to change the line: #define SIMPLE_IMPLEMENTATION true

to false.

The led pinout is in the matrix: ledConfig leds[] = {

{3, 1, 'u', 5, 15},

{12, 2, 'u', 16, 30},

{11, 3, 'u', 31, 45},

{5, 1, 'd', 5, 15},

{6, 2, 'd', 16, 30},

{7, 3, 'd', 31, 45},

{6, 1, 'r', 5, 23},

{9, 2, 'r', 24, 45},

{10, 1, 'l', 5, 23},

{4, 2, 'l', 24, 45},

};

Where the first parameter is led pin, the third means (up, down, right, left), the forth minAngle, the fifth maxAngle.

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.

Hope it helped somehow:)

GaryK56 (author)danionescu2016-05-11

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."exit status 1 macro names must be identifiers"

GaryK56 (author)danionescu2016-04-24

What is the second parameter

danionescu (author)GaryK562016-04-24

The second parameter in the array is the led position inside the group, but it's not actually used in the sketch.

danionescu (author)GaryK562016-04-13

Here is the video: https://www.youtube.com/watch?edit=vd&v=lYH1H_nWLz4

danionescu (author)GaryK562016-04-05

I will try to write the code maybe during the weekend, it shouldn't be that complicated.

About This Instructable

14,505views

57favorites

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:Gyroscope Fun With Neopixel RingRC Car Hack With Android and Arduino How to Build a Text to Speech IOT Speaker
Add instructable to: