Building an Angle Measurement Device using an Arduino Nano is exciting and helpful in the world of DIY projects and electronics. An I2C LCD display, an ADXL335 accelerometer, and an Arduino Nano are three crucial parts of this project. These parts work together to create a small, accurate, and easy-to-use tool that can measure and show angles instantly anywhere.
This tool can be applied in many different fields, such as robotics and do-it-yourself projects where accurate tilt or inclination measurement is necessary. We’ll lead you through the entire process of building your angle measuring device in this post, including how to add a reset button to set it to zero degrees.
Circuit Diagram
Components Required
- Arduino Nano
- ADXL335 Accelerometer
- I2C LCD Display (16×2)
- Push Button (Reset Switch)
- Breadboard and Jumper Wires
- Power Supply (USB cable or battery pack)
Understanding the Components
Arduino Nano
The Arduino Nano is a small, breadboard-friendly variant of the Arduino Uno. It offers similar functionality in a more compact package, making it perfect for this portable project. The Nano features:
- ATmega328P microcontroller
- 32KB of flash memory
- 2KB of SRAM
- 22 I/O pins (14 digital, 8 analog)
- A mini-USB connector for programming and power
ADXL335 Accelerometer (GY-61)
The ADXL335 accelerometer measures acceleration in three axes: X, Y, and Z. It outputs Analog voltages proportional to the acceleration sensed by the sensor. This data can calculate the tilt angles relative to each axis. The ADXL335 features:
- Measurement range: ±3 g
- Low power consumption
- Analog voltage outputs
- Small size (4mm × 4mm × 1.45mm)
I2C LCD Display Module
An I2C LCD display simplifies the process of displaying data from the Arduino. Instead of using multiple pins, the I2C protocol uses two wires: SDA (data line) and SCL (clock line). The features include:
- 16 characters by 2 lines
- I2C interface using the PCF8574 I/O expander
- Adjustable contrast via a potentiometer
- Backlight control
Circuit Diagram and Connections
Here is how to connect each component:
Connecting the ADXL335 Accelerometer
- VCC to 3.3V on Arduino Nano
- GND to GND on Arduino Nano
- X to A7 on Arduino Nano
- Y to A6 on Arduino Nano
- Z to A3 on Arduino Nano
Connecting the I2C LCD Display
- VCC to 5V on Arduino Nano
- GND to GND on Arduino Nano
- SDA to A4 on Arduino Nano
- SCL to A5 on Arduino Nano
Connecting the Reset Switch
- One terminal to GND
- Another terminal to D7 (digital pin 7) on Arduino Nano
Uploading the Code
Installing Necessary Libraries
Before diving into the code, make sure you have the necessary libraries installed. You will need:
- LiquidCrystal_I2C.h for the LCD display
You can install these libraries through the Arduino IDE Library Manager by navigating to Sketch > Include Library > Manage Libraries and searching for the library names.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 | #include <Wire.h> #include <LiquidCrystal_I2C.h> // Initialize the I2C LCD LiquidCrystal_I2C lcd(0x27, 16, 2); // Change the address 0x27 to match your LCD's address // ADXL335 pin definitions const int xPin = A7; const int yPin = A6; const int zPin = A3; // Reset button pin const int resetPin = 7; // Variables for initial zeroing float initialRoll = 0.0; void setup() { // Initialize the serial communication Serial.begin(9600); // Initialize the LCD lcd.init(); lcd.backlight(); // Display the opening screen lcd.setCursor(0, 0); lcd.print("Electro Gadget"); delay(2000); lcd.clear(); // Initialize the reset button pinMode(resetPin, INPUT_PULLUP); } void loop() { // Check if the reset button is pressed if (digitalRead(resetPin) == LOW) { // Read accelerometer values int xRaw = analogRead(xPin); int yRaw = analogRead(yPin); int zRaw = analogRead(zPin); // Convert raw values to voltage float xVoltage = (xRaw / 1023.0) * 5.0; float yVoltage = (yRaw / 1023.0) * 5.0; float zVoltage = (zRaw / 1023.0) * 5.0; // Convert voltage to g-force (assuming 0g at 1.65V and 330mV/g sensitivity) float xG = (xVoltage - 1.65) / 0.330; float yG = (yVoltage - 1.65) / 0.330; float zG = (zVoltage - 1.65) / 0.330; // Calculate initial roll angle in degrees initialRoll = atan2(yG, zG) * 180.0 / PI; } // Read accelerometer values int xRaw = analogRead(xPin); int yRaw = analogRead(yPin); int zRaw = analogRead(zPin); // Convert raw values to voltage float xVoltage = (xRaw / 1023.0) * 5.0; float yVoltage = (yRaw / 1023.0) * 5.0; float zVoltage = (zRaw / 1023.0) * 5.0; // Convert voltage to g-force (assuming 0g at 1.65V and 330mV/g sensitivity) float xG = (xVoltage - 1.65) / 0.330; float yG = (yVoltage - 1.65) / 0.330; float zG = (zVoltage - 1.65) / 0.330; // Calculate current roll angle in degrees float currentRoll = atan2(yG, zG) * 180.0 / PI; // Adjust the roll angle based on the initial zeroing float adjustedRoll = currentRoll - initialRoll; // Print adjusted roll angle to the LCD lcd.setCursor(0, 0); lcd.print("Angle: "); lcd.print(adjustedRoll, 1); lcd.print((char)223); // Degree symbol // Optional: Print adjusted roll angle to the serial monitor for debugging Serial.print("Angle: "); Serial.println(adjustedRoll); // Delay before next reading delay(500); } |
The code will read analog values from the ADXL335 accelerometer, convert these values into angles, and display them on the I2C LCD display. It also includes functionality to reset the angle measurements using a push button.
Calibrating Angle Measurement Device
Calibration is a critical step to ensure accurate angle measurements. Follow these steps to calibrate your device:
- Place the device on a flat, level surface.
- Press the reset button to set the current position as 0 degrees.
- The calibration process resets the baseline values for the X, Y, and Z axes to zero, allowing the device to measure angles accurately from this reference point.
Testing the Angle Measurement Device
Power Up the Device: Connect the Arduino Nano to a USB power source or a battery pack.
Observe Readings: The I2C LCD will display the angle measurements for the X, Y, and Z axes in real time.
Tilt the Device: Move the device in different directions and observe the corresponding changes in angle on the LCD display.
Applications and Further Enhancements
Applications
This angle measurement device can be used in a variety of applications, including:
Robotics: To measure the tilt of a robotic arm or platform.
DIY Projects: As an inclinometer for various home improvement tasks.
Gadget Building: To create custom gadgets that require tilt sensing.
Possible Enhancements
Wireless Communication: Add a Bluetooth or Wi-Fi module to transmit angle data wirelessly.
Data Logging: Store angle data on an SD card for further analysis.
Advanced Calibration: Implement a more sophisticated calibration process to improve accuracy.
Video Output
Conclusion
Building an angle measurement device using an Arduino Nano and ADXL335 accelerometer is an excellent project for learning about accelerometers and microcontrollers. With the added reset switch, you can easily calibrate the device to ensure accurate measurements. This project can be expanded and modified for various applications, making it a versatile and practical tool in your electronics toolkit.