Introduction
In this tutorial, we will learn how to use a gyro module to control LEDs on an Arduino board. This is an intermediate-level exercise that may pose some challenges, so it’s essential to provide detailed questions if you encounter difficulties. Let’s dive into the steps involved in this tutorial.
Connecting the Gyro Sensor and LEDs
Before diving into the code, let’s first understand how the gyro sensor and LEDs are connected for this project.
The LEDs are connected in the same way as in the previous exercise, so if you haven’t done that exercise yet, I recommend watching that video first.
The gyro sensor is connected using a breadboard. The connection diagram for the sensor is as follows:
VCC is connected to 3.3 volts.
GND is connected to ground.
SCL and SDA are connected to the leftmost part of the board, labeled as SCL1 and SDA1.
Understanding the Code
Now, let’s take a look at the code. Since the code in its entirety is quite long, I have provided an empty version of it in the description of the video. This way, you don’t need to write all of it by hand at the start.
Simply use the empty version and, at the end of the video, you will find the solution code as well. This allows you to compare your code with my solution.
But before you can run the code, make sure to install the corresponding library. Without the library, your code will not work as expected.
Controlling LEDs with the Gyro Sensor
The purpose of this project is to control LEDs based on the tilt of the gyro sensor. Each direction of tilt corresponds to turning on a specific LED.
The gyro sensor measures the tilt of the sensor and provides this information to the Arduino board. The Arduino board then uses this data to determine which LED to turn on.
Introduction: MPU6050 Library for Arduino
The MPU6050 library, which is contained in the mpu656050.zip file, is a crucial component for controlling LEDs on Arduino boards. In this tutorial, we will explore how to add this library to your Arduino code and utilize its functions to control LEDs based on data from a gyro module. By following this tutorial, you will gain a better understanding of integrating libraries into your Arduino projects and learn how to use the MPU6050 library effectively.
Adding the Library to Your Code
To begin, it is essential to know how to add libraries to your Arduino code. If you encounter any difficulty with this process, it is advised to review the very first video tutorial that explains how to add libraries. Familiarizing yourself with this step is fundamental before proceeding to more complex tasks. In the case of this tutorial, the MPU6050 library is utilized in Lesson 16 of a book. Therefore, ensure you have access to the library code section provided in Lesson 16.
Controlling LEDs Based on Tilt Direction
To start our exploration of the MPU6050 library, we will focus on controlling LEDs based on the tilt direction. First, we need to add code that displays the tilt direction. In order to do this, you can use the following code:
“`html
Serial.print(“Tilt Direction: “);
Serial.print(ACX);
“`
By executing this code, the tilt direction will be displayed on the serial monitor. The values obtained from the gyro module will be displayed, allowing us to observe the tilt direction in real-time. After adding this code, upload the program and open the serial monitor. You can find the serial monitor button located on the top right corner of the screen.
Interpreting Tilt Values
When the setup is uploaded, you will notice that the tilt values vary depending on the orientation of your Arduino board. When the board is flat, the tilt values typically range from -100 to -200 on the ACX axis. However, when tilted in another direction, for example, vertically, the values can reach around +2000. On the other hand, if tilted in the opposite direction, such as horizontally, the values can be as high as -5000 or -6000.
Displaying Tilt in Y Direction
Now, let’s extend our understanding by displaying the tilt in the Y direction as well. To achieve this, simply copy the same code used for the X direction and modify it as follows:
“`html
Serial.print(“X Tilt: “);
Serial.print(ACX);
Serial.print(” , Y Tilt: “);
Serial.print(ACY);
“`
After adding this code, both the X and Y tilt values will be visible on the serial monitor. The tilt in the Y direction will be represented by the ACY variable. By closely observing the values displayed, you will notice that a negative value, such as -3000, indicates one direction, while a positive value like 3000 or 4000 suggests the opposite direction.
The MPU6050 library is a vital resource for controlling LEDs on Arduino boards. By adding this library to your code and utilizing its functions, you can effectively control LEDs based on data received from a gyro module. This tutorial has provided an overview of how to integrate the MPU
Gyro module to control LEDs on Arduino Tutorial
Setting up the Arduino
To control LEDs using a gyro module on an Arduino, we need to first set up the necessary components. Begin by declaring Pin 4 and Pin 6 as outputs in the setup section of the code.
For Pin 4, we use the condition “if ACX is smaller than -2000” to determine if the module is tilted in a specific direction. If the condition is met, the LED connected to Pin 4 turns on. To ensure that the LED turns off when the module is not tilted, we add the line “digitalWrite(4, LOW)” right before the condition. This completes the setup for Pin 4.
Controlling Pin 4
To test the functionality of Pin 4, we can tilt the gyro module in the specified direction. If the LED connected to Pin 4 turns on, it means that the code is working correctly. However, it is important to note that there is a one-second delay in the code, indicated by “delay(1000)”.
Controlling Pin 6
The process for controlling Pin 6 is similar to that of Pin 4. Start by declaring Pin 6 as an output using the line “pinMode(6, OUTPUT)”. Next, copy the two lines used for Pin 4 and modify the condition by checking if ACX is greater than 1000. This means that when the module is tilted in the opposite direction, the LED connected to Pin 6 should turn on.
Just like with Pin 4, it is necessary to turn off the LED connected to Pin 6 before turning it on in the correct condition. The line “digitalWrite(6, LOW)” accomplishes this.
Controlling Pins 5 and 7
To control LEDs connected to Pins 5 and 7, we follow a similar process. Firstly, declare Pins 5 and 7 as outputs in the setup section of the code. This can be done by adding the lines “pinMode(5, OUTPUT)” and “pinMode(7, OUTPUT)”.
With Pins 5 and 7 set as outputs, we can copy the previous four lines of code and modify them accordingly. Replace “4” and “6” with “5” and “7” respectively. This will enable the LEDs connected to these pins to turn on and off based on the tilt of the gyro module.
With the code set up for controlling the various LEDs using the gyro module, you can now test the functionality of each LED. Tilt the module in the desired directions and observe if the corresponding LEDs turn on and off accordingly. This tutorial provides a basic understanding of how to use a gyro module to control LEDs on an Arduino, and can be a starting point for more complex projects.
Gyro Module to Control LEDs on Arduino Tutorial
Step 1: Wiring the Gyro Module
First, we need to connect the gyro module to the Arduino board. Ensure that you have the necessary components and follow these steps:
1. Connect the VCC pin of the gyro module to the 5V pin on the Arduino.
2. Connect the GND pin of the gyro module to the GND pin on the Arduino.
3. Connect the SDA pin of the gyro module to the A4 pin on the Arduino.
4. Connect the SCL pin of the gyro module to the A5 pin on the Arduino.
Step 2: Uploading the Code
Now that we have the gyro module connected, let’s upload the code to control the LEDs. Open the Arduino IDE and follow these steps:
1. Define the LED pins that you will be using. For example, if you are using pins 5 and 7, declare them as variables:
“`html
Int ledPin5 = 5;
Int ledPin7 = 7;
“`
2. Initialize the gyro module by adding the necessary libraries and setting it up:
“`html
#include
#include
#include
Adafruit_L3GD20_Unified gyro = Adafruit_L3GD20_Unified(20);
Void setup() {
Serial.begin(9600);
Gyro.begin();
}
“`
3. Now, let’s write the loop function to control the LEDs based on the gyro readings:
“`html
Void loop() {
Sensors_event_t event;
Gyro.getEvent(&event);
Int gyroY = event.gyro.y;
If (gyroY 2000) {
DigitalWrite(ledPin5, HIGH);
} else {
DigitalWrite(ledPin5, LOW);
}
Delay(100);
}
“`
Step 3: Testing the LEDs
At this point, you can test the functionality of the LEDs controlled by the gyro module. The LED on pin 7 should turn on when the gyro reading for the y-axis is negative. The LED on pin 5 should turn on when the gyro reading for the y-axis is greater than 2000.
It’s worth mentioning that due to video brightness, it might be challenging to visually determine which LED is on. To overcome this, you can remove the pin nodes from the circuit temporarily to observe the LED status more clearly.
In this tutorial, we learned how to connect a gyro sensor and LEDs to an Arduino board. We also explored the code required to control the LEDs based on the tilt measured by the sensor.
Remember to refer to the connection diagram and use the provided empty code version and solution code to ensure successful execution of your project.
Completing this tutorial should give you a solid understanding of how to use a gyro module to control LEDs on an Arduino board. Remember to take note of any difficulties you encounter and provide specific questions for further clarification. Stay curious and