En el panel superior, aseguré la pantalla LCD, el interruptor de encendido y el botón, y luego los conecté a la PCB. Por último inserté la batería de 9V en el conector y fijé el panel superior con las tuercas.
Y eso es todo, el proyecto del medidor de rango de Arduino está hecho, pero lo que queda en este video es explicar cómo funciona el programa, así que echemos un vistazo al código de Arduino.
Medidor de rango Arduino y código fuente de nivel de burbuja digital
Como el código es un poco más largo, para una mejor comprensión, publicaré el código fuente del programa en secciones con descripción para cada sección. Y al final de este artículo publicaré el código fuente completo.
Entonces, primero debemos incluir la biblioteca Wire.h para la comunicación I2C del acelerómetro, así como la biblioteca LiquidCrystal.h para controlar la pantalla LCD. Luego necesitamos definir la pantalla LCD, la dirección I2C del módulo del acelerómetro MPU6050, los pines del sensor ultrasónico, así como algunas variables necesarias para el programa a continuación.
#include<Wire.h> // I2C communication library
#include <LiquidCrystal.h> // includes the LiquidCrystal Library
LiquidCrystal lcd(7, 6, 5, 4, 3, 2); // Creates an LCD object. Parameters: (rs, enable, d4, d5, d6, d7)
const int MPU = 0x68; // I2C address of the MPU6050 accelerometer
#define trigPin 8
#define echoPin 9
#define selectButton 10
int16_t AcX, AcY, AcZ;
long duration;
float distance;
int program = 0;
float d = 0;
float d1 = 0;
float d2 = 0;
float area = 0;
int axis = 0;
int angle = 0;
int unitSelect = 0;
String unit = "cm";
Code language: Arduino (arduino)
En la sección de configuración, debemos inicializar tanto la interfaz I2C del acelerómetro como la pantalla de cristal líquido, así como definir los modos de pin para el disparador del sensor ultrasónico y los pines de eco, y el pin del botón.
void setup() {
// Initialize interface to the MPU6050
Wire.begin();
Wire.beginTransmission(MPU);
Wire.write(0x6B);
Wire.write(0);
Wire.endTransmission(true);
lcd.begin(16, 2); // Initializes the interface to the LCD screen
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(selectButton, INPUT_PULLUP);
}
Code language: Arduino (arduino)
En la sección de bucle principal tenemos un interruptor de instrucciones a través del cual podemos alternar entre los diferentes programas de nuestro dispositivo. En el primero, o caso número 0, seleccionamos la unidad de medida. Con la función lcd.print(), imprimimos el texto en la pantalla LCD y, con las declaraciones if, alternamos entre las cuatro unidades de medida.
switch (program) { // Switch between different programs
case 0: // Select unit of measurement
lcd.setCursor(0, 0); // Sets the location at which subsequent text written to the LCD will be displayed
lcd.print("Select Unit: ");
lcd.setCursor(13, 0);
lcd.print(unit);
lcd.print(" ");
delay(10);
// If button is pressed - change unit
if (digitalRead(selectButton) == 0) {
if (unitSelect == 0) {
unit = "in";
unitSelect = 1;
}
else if (unitSelect == 1) {
unit = "m";
unitSelect = 2;
}
else if (unitSelect == 2) {
unit = "ft";
unitSelect = 3;
}
else if (unitSelect == 3) {
unit = "cm";
unitSelect = 0;
}
// If button is held longer then half a second - change program
delay(500);
if (digitalRead(selectButton) == 0) {
program = 1;
lcd.clear();
delay(500);
}
}
break;
Code language: Arduino (arduino)
Debemos notar aquí que la variable unitSelect toma acción en la función personalizada getDistance(), donde realmente dice a qué unidad debemos convertir la unidad básica cm que estamos obteniendo del sensor ultrasónico.
/ Converting the units
if (unitSelect == 1) {
distance = distance; // cm to cm
unit = "cm";
}
else if (unitSelect == 2) {
distance = distance * 0.393701; // cm to in
unit = "in";
}
else if (unitSelect == 3) {
distance = distance * 0.01; // cm to m
unit = "m";
}
else if (unitSelect == 0) {
distance = distance * 0.0328; // cm to ft
unit = "ft";
}
Code language: Arduino (arduino)
Para seleccionar la unidad de medida y entrar también en el primer programa del dispositivo solo tenemos que mantener pulsado el botón durante más de medio segundo.
// If button is held longer then half a second - change program
delay(500);
if (digitalRead(selectButton) == 0) {
program = 1;
lcd.clear();
delay(500);
}
}
break;
Code language: Arduino (arduino)
En el caso número 1 o en el programa de medición de distancia primero obtenemos la distancia al objeto más cercano usando la función personalizada getDistance().
case 1: // Distance measuring program
distance = getDistance(); // Distance to the nearest object
lcd.setCursor(0, 0);
lcd.print("Dist: ");
lcd.print(distance); // Prints the distance value from the sensor
lcd.print(" ");
lcd.setCursor(14, 0);
lcd.print(unit);
delay(10);
lcd.setCursor(0, 1);
lcd.print("d:");
lcd.setCursor(8, 1);
lcd.print("d:");
delay(200);
// Save distance 1
if (digitalRead(selectButton) == 0) {
if (d == 0) {
lcd.setCursor(0, 1);
lcd.print("d: ");
lcd.setCursor(2, 1);
lcd.print(distance);
d = 1;
delay(100);
}
// Save distance 2
else if (d == 1) {
lcd.setCursor(8, 1);
lcd.print("d: ");
lcd.setCursor(10, 1);
lcd.print(distance);
d = 0;
delay(100);
}
// If button is held longer then half sencond change program
delay(500);
if (digitalRead(selectButton) == 0) {
program = 2;
d = 0;
lcd.clear();
delay(500);
}
}
break;
Code language: Arduino (arduino)
Veamos cómo funciona esta función.
//===== getDistance - Custom Function
float getDistance() {
// Clears the trigPin
digitalWrite(trigPin, LOW);
// Sets the trigPin on HIGH state for 10 micro seconds
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
// Reads the echoPin, returns the sound wave travel time in microseconds
duration = pulseIn(echoPin, HIGH);
// Calculating the distance
distance = duration * 0.034 / 2; // distance in cm
// Converting the units
if (unitSelect == 1) {
distance = distance; // cm to cm
unit = "cm";
}
else if (unitSelect == 2) {
distance = distance * 0.393701; // cm to in
unit = "in";
}
else if (unitSelect == 3) {
distance = distance * 0.01; // cm to m
unit = "m";
}
else if (unitSelect == 0) {
distance = distance * 0.0328; // cm to ft
unit = "ft";
}
return distance;
}
Code language: Arduino (arduino)
Aquí, usando el pin disparador, le decimos al sensor que genere una onda de sonido ultrasónica.
Luego, usando el pin de eco y la función pulseIn(), medimos la duración de la onda que viajó desde el sensor hasta el objeto y de regreso. Teniendo en cuenta la velocidad del sonido y el tiempo de viaje, podemos calcular fácilmente la distancia. Así que imprimimos la distancia medida junto con algún texto en la pantalla LCD y usando las declaraciones "si", si presionamos el botón, imprimimos o guardamos las dos últimas medidas.
El siguiente es el programa para calcular el área en el que estamos utilizando métodos similares. Necesitamos tomar dos medidas perpendiculares y simplemente multiplicarlas para obtener el área del cuadrado que forman.
case 2: // Area measuring program
distance = getDistance();
lcd.setCursor(0, 0);
lcd.print("Area: ");
lcd.print(area); // Prints the calculated area from the two measurements
lcd.setCursor(12, 0);
lcd.print(unit); // Prints the selected unit and the square sign below
lcd.print("^2");
delay(200);
if ( d == 0) {
lcd.setCursor(0, 1);
lcd.print("d1: ");
lcd.setCursor(3, 1);
lcd.print(distance);
delay(200);
}
else if (d == 1) {
lcd.setCursor(9, 1);
lcd.print("d2: ");
lcd.setCursor(12, 1);
lcd.print(distance);
delay(200);
}
else if (d == 2) {
lcd.setCursor(6, 0);
lcd.print(area);
delay(200);
}
// Save distance 1
if (digitalRead(selectButton) == 0) {
if (d == 0) {
lcd.setCursor(0, 1);
lcd.print("d1: ");
lcd.setCursor(3, 1);
lcd.print(distance);
d = 1;
d1 = distance;
delay(100);
}
// Save distance 2
else if (d == 1) {
lcd.setCursor(9, 1);
lcd.print("d2: ");
lcd.setCursor(12, 1);
lcd.print(distance);
d = 2;
d2 = distance;
area = d1 * d2; // Calculate the area
delay(100);
}
else if (d == 2) {
lcd.clear();
d = 0;
area = 0;
delay(100);
}
// If button is held longer then half sencond change program
delay(500);
if (digitalRead(selectButton) == 0) {
program = 3;
d = 0;
lcd.clear();
delay(500);
}
}
break;
Code language: Arduino (arduino)
El último caso, es el programa de medición de ángulos. Aquí necesitamos leer los datos del acelerómetro, que en realidad es la fuerza del campo gravitatorio de la Tierra en tres ejes diferentes, X, Y y Z. El valor de cada eje se almacena en 2 registros, por lo que necesitamos leer un total de 6 registros y combínelos para obtener el valor correcto.
case 3: // Angle measuring program
// Read the accelerometer data
Wire.beginTransmission(MPU);
Wire.write(0x3B); // Start with register 0x3B (ACCEL_XOUT_H)
Wire.endTransmission(false);
Wire.requestFrom(MPU, 6, true); // Read 6 registers total, each axis value is stored in 2 registers
AcX = Wire.read() << 8 | Wire.read(); // X-axis value
AcY = Wire.read() << 8 | Wire.read(); // Y-axis value
AcZ = Wire.read() << 8 | Wire.read(); // Z-axis value
if ( axis == 0) {
// Calculating the Pitch angle (rotation around Y-axis)
angle = atan(-1 * AcX / sqrt(pow(AcY, 2) + pow(AcZ, 2))) * 180 / PI;
lcd.setCursor(0, 0);
lcd.print("Pitch");
}
else if (axis == 1) {
// Calculating the Roll angle (rotation around X-axis)
angle = atan(-1 * AcY / sqrt(pow(AcX, 2) + pow(AcZ, 2))) * 180 / PI;
lcd.setCursor(0, 0);
lcd.print("Roll ");
}
lcd.setCursor(0, 1);
lcd.print("Angle: ");
lcd.print(abs(angle));
lcd.print(" ");
lcd.setCursor(10, 1);
lcd.print("deg");
delay(200);
// Change axis
if (digitalRead(selectButton) == 0) {
if (axis == 0) {
axis = 1;
delay(100);
}
// Save distance 2
else if (axis == 1) {
axis = 0;
delay(100);
}
// If button is held longer then half sencond change program
delay(500);
if (digitalRead(selectButton) == 0) {
program = 0;
lcd.clear();
delay(500);
}
}
break;
Code language: Arduino (arduino)
Una vez que tenemos los valores X,Y y Z del acelerómetro podemos calcular el ángulo de Pitch, o rotación alrededor del eje Y, así como el ángulo de Roll, o rotación alrededor del eje X, usando las dos ecuaciones. Puede encontrar más detalles sobre estas ecuaciones en la nota de aplicación de Freescale Semiconductor, Detección de inclinación mediante un acelerómetro de tres ejes . Entonces, después de eso, imprimimos los valores junto con algún texto en la pantalla LCD y, al presionar el botón, cambiamos lo que mostramos en la pantalla LCD, ya sea el ángulo de cabeceo o de balanceo.
Aquí está el código fuente completo para este medidor de rango Arduino y proyecto de nivel de burbuja:
/*
DIY Digital Range Measurer and Spirit Level
by Dejan Nedelkovski,
www.HowToMechatronics.com
*/
#include<Wire.h> // I2C communication library
#include <LiquidCrystal.h> // includes the LiquidCrystal Library
LiquidCrystal lcd(7, 6, 5, 4, 3, 2); // Creates an LCD object. Parameters: (rs, enable, d4, d5, d6, d7)
const int MPU = 0x68; // I2C address of the MPU6050 accelerometer
#define trigPin 8
#define echoPin 9
#define selectButton 10
int16_t AcX, AcY, AcZ;
long duration;
float distance;
int program = 0;
float d = 0;
float d1 = 0;
float d2 = 0;
float area = 0;
int axis = 0;
int angle = 0;
int unitSelect = 0;
String unit = "cm";
void setup() {
// Initialize interface to the MPU6050
Wire.begin();
Wire.beginTransmission(MPU);
Wire.write(0x6B);
Wire.write(0);
Wire.endTransmission(true);
lcd.begin(16, 2); // Initializes the interface to the LCD screen
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(selectButton, INPUT_PULLUP);
}
void loop() {
switch (program) { // Switch between different programs
case 0: // Select unit of measurement
lcd.setCursor(0, 0); // Sets the location at which subsequent text written to the LCD will be displayed
lcd.print("Select Unit: ");
lcd.setCursor(13, 0);
lcd.print(unit);
lcd.print(" ");
delay(10);
// If button is pressed - change unit
if (digitalRead(selectButton) == 0) {
if (unitSelect == 0) {
unit = "in";
unitSelect = 1;
}
else if (unitSelect == 1) {
unit = "m";
unitSelect = 2;
}
else if (unitSelect == 2) {
unit = "ft";
unitSelect = 3;
}
else if (unitSelect == 3) {
unit = "cm";
unitSelect = 0;
}
// If button is held longer then half a second - change program
delay(500);
if (digitalRead(selectButton) == 0) {
program = 1;
lcd.clear();
delay(500);
}
}
break;
case 1: // Distance measuring program
distance = getDistance(); // Distance to the nearest object
lcd.setCursor(0, 0);
lcd.print("Dist: ");
lcd.print(distance); // Prints the distance value from the sensor
lcd.print(" ");
lcd.setCursor(14, 0);
lcd.print(unit);
delay(10);
lcd.setCursor(0, 1);
lcd.print("d:");
lcd.setCursor(8, 1);
lcd.print("d:");
delay(200);
// Save distance 1
if (digitalRead(selectButton) == 0) {
if (d == 0) {
lcd.setCursor(0, 1);
lcd.print("d: ");
lcd.setCursor(2, 1);
lcd.print(distance);
d = 1;
delay(100);
}
// Save distance 2
else if (d == 1) {
lcd.setCursor(8, 1);
lcd.print("d: ");
lcd.setCursor(10, 1);
lcd.print(distance);
d = 0;
delay(100);
}
// If button is held longer then half sencond change program
delay(500);
if (digitalRead(selectButton) == 0) {
program = 2;
d = 0;
lcd.clear();
delay(500);
}
}
break;
case 2: // Area measuring program
distance = getDistance();
lcd.setCursor(0, 0);
lcd.print("Area: ");
lcd.print(area); // Prints the calculated area from the two measurements
lcd.setCursor(12, 0);
lcd.print(unit); // Prints the selected unit and the square sign below
lcd.print("^2");
delay(200);
if ( d == 0) {
lcd.setCursor(0, 1);
lcd.print("d1: ");
lcd.setCursor(3, 1);
lcd.print(distance);
delay(200);
}
else if (d == 1) {
lcd.setCursor(9, 1);
lcd.print("d2: ");
lcd.setCursor(12, 1);
lcd.print(distance);
delay(200);
}
else if (d == 2) {
lcd.setCursor(6, 0);
lcd.print(area);
delay(200);
}
// Save distance 1
if (digitalRead(selectButton) == 0) {
if (d == 0) {
lcd.setCursor(0, 1);
lcd.print("d1: ");
lcd.setCursor(3, 1);
lcd.print(distance);
d = 1;
d1 = distance;
delay(100);
}
// Save distance 2
else if (d == 1) {
lcd.setCursor(9, 1);
lcd.print("d2: ");
lcd.setCursor(12, 1);
lcd.print(distance);
d = 2;
d2 = distance;
area = d1 * d2; // Calculate the area
delay(100);
}
else if (d == 2) {
lcd.clear();
d = 0;
area = 0;
delay(100);
}
// If button is held longer then half sencond change program
delay(500);
if (digitalRead(selectButton) == 0) {
program = 3;
d = 0;
lcd.clear();
delay(500);
}
}
break;
case 3: // Angle measuring program
// Read the accelerometer data
Wire.beginTransmission(MPU);
Wire.write(0x3B); // Start with register 0x3B (ACCEL_XOUT_H)
Wire.endTransmission(false);
Wire.requestFrom(MPU, 6, true); // Read 6 registers total, each axis value is stored in 2 registers
AcX = Wire.read() << 8 | Wire.read(); // X-axis value
AcY = Wire.read() << 8 | Wire.read(); // Y-axis value
AcZ = Wire.read() << 8 | Wire.read(); // Z-axis value
if ( axis == 0) {
// Calculating the Pitch angle (rotation around Y-axis)
angle = atan(-1 * AcX / sqrt(pow(AcY, 2) + pow(AcZ, 2))) * 180 / PI;
lcd.setCursor(0, 0);
lcd.print("Pitch");
}
else if (axis == 1) {
// Calculating the Roll angle (rotation around X-axis)
angle = atan(-1 * AcY / sqrt(pow(AcX, 2) + pow(AcZ, 2))) * 180 / PI;
lcd.setCursor(0, 0);
lcd.print("Roll ");
}
lcd.setCursor(0, 1);
lcd.print("Angle: ");
lcd.print(abs(angle));
lcd.print(" ");
lcd.setCursor(10, 1);
lcd.print("deg");
delay(200);
// Change axis
if (digitalRead(selectButton) == 0) {
if (axis == 0) {
axis = 1;
delay(100);
}
// Save distance 2
else if (axis == 1) {
axis = 0;
delay(100);
}
// If button is held longer then half sencond change program
delay(500);
if (digitalRead(selectButton) == 0) {
program = 0;
lcd.clear();
delay(500);
}
}
break;
}
}
//===== getDistance - Custom Function
float getDistance() {
// Clears the trigPin
digitalWrite(trigPin, LOW);
// Sets the trigPin on HIGH state for 10 micro seconds
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
// Reads the echoPin, returns the sound wave travel time in microseconds
duration = pulseIn(echoPin, HIGH);
// Calculating the distance
distance = duration * 0.034 / 2; // distance in cm
// Converting the units
if (unitSelect == 1) {
distance = distance; // cm to cm
unit = "cm";
}
else if (unitSelect == 2) {
distance = distance * 0.393701; // cm to in
unit = "in";
}
else if (unitSelect == 3) {
distance = distance * 0.01; // cm to m
unit = "m";
}
else if (unitSelect == 0) {
distance = distance * 0.0328; // cm to ft
unit = "ft";
}
return distance;
}
Code language: Arduino (arduino)
Eso sería todo, espero que hayas disfrutado este proyecto Arduino y hayas aprendido algo nuevo. No dude en hacer cualquier pregunta en la sección de comentarios a continuación.