Yamilett Estrada-Reyes
Published

Displacement vs. Time Graph Using an Ultrasonic Sensor

Use an ultrasonic sensor to make a Displacement vs. Time graph on Google Sheets for a pendulum, specifically, a speed bag.

BeginnerFull instructions provided2 hours527
Displacement vs. Time Graph Using an Ultrasonic Sensor

Things used in this project

Hardware components

Argon
Particle Argon
×1
Jumper wires (generic)
Jumper wires (generic)
×4
Ultrasonic Sensor - HC-SR04 (Generic)
Ultrasonic Sensor - HC-SR04 (Generic)
×1
USB-A to Micro-USB Cable
USB-A to Micro-USB Cable
×1
Breadboard (generic)
Breadboard (generic)
×1
Portable Charger
×1

Software apps and online services

Particle Build Web IDE
Particle Build Web IDE
Google Sheets
Google Sheets

Hand tools and fabrication machines

Tape, Velcro® Stick On Tape/Strip
Tape, Velcro® Stick On Tape/Strip
optional*

Story

Read more

Schematics

The Wiring Diagram:

- GND is connected to the “Gnd” pin
- D3 is connected to the “Echo” pin
- D7 is connected to “Trig” pin
- VUSB is connected to “Vcc” pin (pwr supply)

Code

ultrasonic-code.ino

C/C++
#define triggerPin D7 // pwm/input
#define echoPin D3 //output

double rest = 0.0;

double startMillis = 0.0;
double currentMillis;
double elapsedMillis;

double duration, distance;

const double marginOfError = 0.3; // in inches
const double speedOfSound = 0.0135; // in inches

void setup() {
    pinMode(triggerPin, OUTPUT); // send pulses out from the Argon
    pinMode(echoPin, INPUT); // receives the pulses from the device
}

double getTime() { 
    currentMillis = millis();
    elapsedMillis = (double)((currentMillis - startMillis) / 1000.000); // in seconds
    return elapsedMillis;
}

double getDistance() { // returns distance from the sensor, NOT from equilibrium
    digitalWrite(triggerPin, LOW); // makes sure the trigger pin is clear
    delayMicroseconds(2); 
    digitalWrite(triggerPin, HIGH); // generates an ultrasonic sound wave 
    delayMicroseconds(10);       
    digitalWrite(triggerPin, LOW);
    duration = pulseIn(echoPin, HIGH); // measures the duration of a pulse in   microseconds
    distance = (duration / 2) * speedOfSound; // duration = time it takes for the pulse to go out + bounce back.
    return distance;
}

void loop() {
    if(startMillis == 0.0) {
        startMillis = millis();
    }
    if(rest == 0.0) { 
       rest = getDistance() - marginOfError;
    } 
    double displacement = (getDistance() - rest) * 1.000;
    double timeElapsed = getTime() * 1.000;
    char data[256];
    snprintf(data, sizeof(data), "{\"displacement\":%f, \"time\":%f}", displacemwnt, timeElapsed);
    Particle.publish("motion-detected", data, PRIVATE);
    delay(200);
}

Credits

Yamilett Estrada-Reyes

Yamilett Estrada-Reyes

3 projects • 2 followers

Comments

Add projectSign up / Login