Arduino-Based Tilt and Distance Measurement System with ADXL335 and Ultrasonic Sensor
View and Edit Project In Cirkit Designer
Circuit Documentation
Summary
This circuit is designed to measure the angle of a joint in human bones using an ADXXL335 accelerometer and an ultrasonic sensor. The data is processed by an Arduino UNO and displayed on a 16x2 LCD. The circuit also includes a trimmer potentiometer for adjusting the LCD contrast.
Component List
ADXXL335 Accelerometer
- Description: Measures acceleration in three axes (X, Y, Z).
- Pins: VCC, X-OUT, Y-OUT, Z-OUT, GND
Arduino UNO
- Description: Microcontroller board based on the ATmega328P.
- Pins: UNUSED, IOREF, Reset, 3.3V, 5V, GND, Vin, A0, A1, A2, A3, A4, A5, SCL, SDA, AREF, D13, D12, D11, D10, D9, D8, D7, D6, D5, D4, D3, D2, D1, D0
16X2 LCD
- Description: Alphanumeric display module with 16 columns and 2 rows.
- Pins: VSS, VDD, V0, RS, RW, E, D1, D0, D2, D3, D4, D6, D5, D7, A, K
Trimmer Potentiometer
- Description: Adjustable resistor used for setting the LCD contrast.
- Pins: leg1, wiper, leg2
- Properties: Resistance: 10k Ohms
Ultrasonic Sensor
- Description: Measures distance using ultrasonic waves.
- Pins: +VCC, Trigger, Echo, GND
Wiring Details
ADXXL335 Accelerometer
- VCC connected to A0 of Arduino UNO
- X-OUT connected to A1 of Arduino UNO
- Y-OUT connected to A2 of Arduino UNO
- Z-OUT connected to GND of Arduino UNO
- GND connected to 3.3V of Arduino UNO
Arduino UNO
- A0 connected to VCC of ADXXL335
- A1 connected to X-OUT of ADXXL335
- A2 connected to Y-OUT of ADXXL335
- GND connected to Z-OUT of ADXXL335, leg1 of Trimmer Potentiometer, VSS, D5, and K of 16X2 LCD
- 3.3V connected to GND of ADXXL335
- 5V connected to +VCC of Ultrasonic Sensor, leg2 of Trimmer Potentiometer, VDD, and A of 16X2 LCD
- D12 connected to D6 of 16X2 LCD
- D11 connected to D4 of 16X2 LCD
- D9 connected to Echo of Ultrasonic Sensor
- D8 connected to Trigger of Ultrasonic Sensor
- D5 connected to E of 16X2 LCD
- D4 connected to RW of 16X2 LCD
- D3 connected to RS of 16X2 LCD
- D2 connected to V0 of 16X2 LCD
16X2 LCD
- VSS connected to GND of Arduino UNO
- VDD connected to 5V of Arduino UNO
- V0 connected to D2 of Arduino UNO
- RS connected to D3 of Arduino UNO
- RW connected to D4 of Arduino UNO
- E connected to D5 of Arduino UNO
- D4 connected to D11 of Arduino UNO
- D6 connected to D12 of Arduino UNO
- D5 connected to GND of Arduino UNO
- D7 connected to wiper of Trimmer Potentiometer
- A connected to 5V of Arduino UNO
- K connected to GND of Arduino UNO
Trimmer Potentiometer
- leg1 connected to GND of Arduino UNO
- wiper connected to D7 of 16X2 LCD
- leg2 connected to 5V of Arduino UNO
Ultrasonic Sensor
- +VCC connected to 5V of Arduino UNO
- Trigger connected to D8 of Arduino UNO
- Echo connected to D9 of Arduino UNO
- GND connected to GND of Arduino UNO
Code Documentation
#include <ADXL335.h>
#include <HCSR04.h>
#include <UnoWiFiDevEd.h>
#include <LiquidCrystal.h>
#define ADC_ref 5 // ADC reference Voltage
#define zero_x 1.799
#define zero_y 1.799
#define zero_z 1.799
#define echoPin 9
#define trigPin 8
#define selectSwitch 1
#define sensitivity_x 0.4
#define sensitivity_y 0.4
#define sensitivity_z 0.4
unsigned int value_x;
unsigned int value_y;
unsigned int value_z;
float xv;
float yv;
float zv;
float angle;
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
void setup() {
analogReference(ADC_ref);
pinMode(selectSwitch, INPUT);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
lcd.clear();
lcd.begin(16, 2);
lcd.print("Tilt & Distance");
lcd.setCursor(0, 1);
lcd.print(" Measurement ");
delay(3000);
lcd.clear();
}
void loop() {
int distance, duration;
value_x = analogRead(A0);
value_y = analogRead(A1);
value_z = analogRead(A2);
xv = (value_x / 1024.0 * ADC_ref - zero_x) / sensitivity_x;
yv = (value_y / 1024.0 * ADC_ref - zero_y) / sensitivity_y;
zv = (value_z / 1024.0 * ADC_ref - zero_z) / sensitivity_z;
angle = atan2(-yv, -zv) * 57.2957795 + 180;
if (digitalRead(selectSwitch) == HIGH) {
lcd.setCursor(0, 0);
lcd.print("Tilt: ");
lcd.print(angle);
lcd.print(" deg");
digitalWrite(trigPin, HIGH);
delayMicroseconds(1000);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = (duration / 2) / 29.1;
if (distance >= 400 || distance <= 0) {
lcd.setCursor(0, 1);
lcd.print("Out of Range");
} else {
lcd.setCursor(0, 1);
lcd.print("Distance: ");
lcd.print(distance);
lcd.print(" cm ");
}
delay(1000);
lcd.clear();
} else {
lcd.setCursor(0, 0);
lcd.print("X=");
lcd.print(xv);
lcd.print(" Y=");
lcd.print(yv);
lcd.setCursor(0, 1);
lcd.print(" Z= ");
lcd.print(zv);
delay(1000);
}
}
This code reads data from the ADXXL335 accelerometer and the ultrasonic sensor, calculates the tilt angle and distance, and displays the results on the 16x2 LCD. The LCD displays either the tilt angle and distance or the X, Y, Z values from the accelerometer based on the state of the select switch.