TechTorch

Location:HOME > Technology > content

Technology

Measuring Traveled Distance with Arduino: A Comprehensive Guide

January 25, 2025Technology3643
Measuring Traveled Distance with Arduino: A Comprehensive Guide Introd

Measuring Traveled Distance with Arduino: A Comprehensive Guide

Introduction

Measuring distance traveled is a common requirement in various projects, from robotics to outdoor navigation. The Arduino microcontroller is an excellent choice for such tasks, offering numerous methods to achieve accurate distance measurements. In this article, we will explore three primary techniques: Wheel Encoders, GPS Modules, and Ultrasonic Sensors. Each method has its unique advantages and is best suited for different scenarios.

1. Using a Wheel Encoder

Wheel encoders are widely used in robotics to measure the rotation of wheels, which can be converted into distance traveled. This method is ideal for closed-loop systems where the wheel's circumference and number of rotations are known.

Components Needed:

An Arduino board Wheel encoder (incremental or absolute) A motor driver (if controlling motors) Cables and breadboard

Basic Steps:

Connecting the Encoder: Connect the encoder to the Arduino according to its specifications, typically using digital pins. Code the Arduino: Use the following sample code to read the encoder values and calculate the distance.
const int encoderPinA  2; // Pin for encoder A
const int encoderPinB  3; // Pin for encoder B
volatile int encoderValue  0; // Variable to store encoder value
float wheelCircumference  0.2f; // Example: 20 cm
void setup() {
  (9600);
  pinMode(encoderPinA, INPUT);
  pinMode(encoderPinB, INPUT);
  attachInterrupt(digitalPinToInterrupt(encoderPinA), updateEncoder, RISING);
}
void loop() {
  float distance  encoderValue / (360.0f / wheelCircumference); // Assuming 360 ticks per revolution
  ("Distance: ");
  (distance);
  (" cm");
  delay(1000); // Update every second
}
void updateEncoder() {
  encoderValue  ;
}

2. Using a GPS Module

GPS modules are suitable for outdoor applications, especially when measuring distances over larger areas. They provide accurate real-time coordinates, which can be used to calculate distances.

Components Needed:

An Arduino board A GPS module (e.g., NEO-6M) Jumper wires

Basic Steps:

Connecting the GPS Module: Connect the GPS module to the Arduino typically using Serial pins. Code the Arduino: Use the following sample code to read GPS coordinates and calculate the distance.
#include TinyGPS.h
#include SoftwareSerial.h
TinyGPSPlus gps;
SoftwareSerial ss(4, 3); // RX, TX
float lastLat  0.0;
float lastLng  0.0;
void setup() {
  (9600);
  (9600);
}
void loop() {
  while (ss.available()  0) {
    int parsed  gps.encode(());
    if (parsed ! TinyGPSPlus::OK) {
      continue;
    }
    float currentLat  ;
    float currentLng  gps.location.lng;
    float distance  calculateDistance(lastLat, lastLng, currentLat, currentLng);
    ("Distance: ");
    (distance);
    (" meters");
    lastLat  currentLat;
    lastLng  currentLng;
  }
}
float calculateDistance(float lat1, float lat2, float lng1, float lng2) {
  const float R  6371e3; // Radius of Earth in meters
  float phi1  lat1 * PI / 180;
  float phi2  lat2 * PI / 180;
  float deltaPhi  (lat2 - lat1) * PI / 180;
  float deltaLambda  (lng2 - lng1) * PI / 180;
  float a  sin(deltaPhi / 2) * sin(deltaPhi / 2)   cos(phi1) * cos(phi2) * sin(deltaLambda / 2) * sin(deltaLambda / 2);
  float c  2 * atan2(sqrt(a), sqrt(1 - a));
  return R * c; // Distance in meters
}

3. Using an Ultrasonic Sensor

Ultrasonic sensors are ideal for short-range measurements, such as measuring the distance to an object in front of the Arduino. They are commonly used in applications where high precision is not required, but quick and easy distance measurements are needed.

Components Needed:

An Arduino board HC-SR04 ultrasonic sensor Jumper wires

Basic Steps:

Connecting the Ultrasonic Sensor: Connect the sensor to the Arduino. Code the Arduino: Use the following sample code to measure distance.
const int trigPin  9;
const int echoPin  10;
void setup() {
  (9600);
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
}
void loop() {
  long duration  0;
  float distance  0.0;
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);
  duration  pulseIn(echoPin, HIGH);
  distance  duration * 0.034 / 2; // Distance in cm
  ("Distance: ");
  (distance);
  (" cm");
  delay(1000);
}

Conclusion

Choose the method that best suits your project requirements. Wheel encoders are ideal for measuring distance in robotics, GPS modules are suitable for outdoor applications, and ultrasonic sensors are good for short-range measurements. Each method offers different levels of accuracy and practicality, making them suitable for a wide range of applications.