What's new
PinoyTechnician

This is a sample guest message. Register a free account today to become a member! Once signed in, you'll be able to participate on this site by adding your own topics and posts, as well as connect with other members through your own private inbox!

REFERENCE SR04 Distance Sensor (SONAR) Arduino Library

aybyd

Registered
Joined
Jun 29, 2014
Messages
7
Reaction score
2
Points
1
Hi, just want to share again one of my own coded library for Distance Sensor (SR04 SONAR) for Arduino.

Header file: SR04.h

PHP:
//HC-SR04 Sonar Sensor Library
//Created by: Aybyd Romero (c)2014
//Public License
//You can reuse and distribute this copy of HC-SR04 Sonar sensor free of charge. You may retain my name.
//http://www.micropik.com/PDF/HCSR04.pdf

#ifndef SR04_h
#define SR04_h

#if ARDUINO >= 100
#include "Arduino.h"
#else
#include "WProgram.h"
#endif

class SR04
{

  public:
    SR04(int trig, int ech);
    void begin(void);
    long getCentimeter();

  private:
    int TRIGGER;
    int ECHO;

};

#endif

Source file: SR04.cpp

PHP:
#include "SR04.h"

SR04::SR04(int trig, int ech)
{
  TRIGGER = trig;
  ECHO = ech;
}

void SR04::begin(void) {
  pinMode(TRIGGER, OUTPUT);
  pinMode(ECHO, INPUT);
}

long SR04::getCentimeter() {
  long  distance, duration;
  digitalWrite(TRIGGER, LOW);
  delayMicroseconds(2);
  digitalWrite(TRIGGER, HIGH);
  delayMicroseconds(10);
  digitalWrite(TRIGGER, LOW);
  duration = pulseIn(ECHO, HIGH);
  return distance = duration / 58;
}

Sample usage:

PHP:
#include "SR04.h"


SR04 sensor(14, 15); //Create a wrapper class for HC-SR04 Sensor. SR04 sensor(TRIGGER PIN, ECHO PIN)

int tankHeight = 40; // This the height of the tank in cm.
int sensorClearance = 5;
int lowLevel = 35; // Indicate water low level to trigger the pump to load more water. This is get by subtracting sensorClearance to tankHeigth. We can set this to any number to trigger pump
int waterHeight;
int waterPercentage;
int level;

int leds[] = {2, 3, 4, 5, 6}; //LED Pin assignment for indicator

void setup() {
  Serial.begin (9600);
  sensor.begin();
  //Loop over led pins and set it as OUTPUT
  for (int led = 0; led < 5; led++) {
    pinMode(leds[led], OUTPUT);
  }
}

void loop() {

  level = sensor.getCentimeter() - sensorClearance; // Get sensor readings in cm minus the clearance from sensor distance over the top edge of tank.
  waterHeight = tankHeight - sensorClearance;

  //  <--This part is for debugging purpose only, to display water level, distance of water from the sensor and percentage -->
  Serial.print("Distance: ");
  Serial.print(level);
  Serial.println("cm");
  level = waterHeight - level; // Since our sensor for level is over the top, we need to subtract the tank height to get water level.
  waterPercentage = ((float)level / (float)waterHeight) * 100.0; // Converting it to Percent by using simple Calculus.
  Serial.print("Water Level: ");
  Serial.print(waterPercentage);
  Serial.println("%");
  //<-- To this line -->


//This function is to map the water level to led indicator
  int ledLevel = map(level, 0, tankHeight - sensorClearance, 0, 5);
  for (int thisLed = 0; thisLed < 5; thisLed++) {
    if (thisLed < ledLevel) {
      digitalWrite(leds[thisLed], HIGH);
    }
    else {
      digitalWrite(leds[thisLed], LOW);
    }
    delay(50);
  }
}

Here is the image of the SONAR Sensor
Happy sensing... :D
hcsr04-stm32f4xx.jpg
 
Back
Top