• ultrasonic sensor arduino distance measuring

    ultrasonic sensor arduino distance measuring Displayed on 4 Digit 7 segment

    In this Arduino Tutorial we will learn how the HC-SR04 Ultrasonic Sensor works and how to use it with the Arduino Board. You can watch the following video or read the written tutorial below.

    The HC-SR04 Ultrasonic Module has 4 pins, Ground, VCC, Trig and Echo. The Ground and the VCC pins of the module needs to be connected to the Ground and the 5 volts pins on the Arduino Board respectively and the trig and echo pins to any Digital I/O pin on the Arduino Board.

      • VCC - 5V
      • GND - GND
      • Echo  - A0
    • Trig  -  A1

    Schema

    Code for 4 Digit 7 segment (common anode)

    #define TRIGGER_PIN A1
    #define ECHO_PIN A0
    #define USONIC_DIV 58.0
    #define MEASURE_SAMPLE_DELAY 5
    #define MEASURE_SAMPLES 15
    #define MEASURE_DELAY 15
    int aPin = 2;
    int bPin = 3;
    int cPin = 4;
    int dPin = 5;
    int ePin = 12;
    int fPin = 7;
    int gPin = 8;
    int GND1 = 6;
    int GND2 = 11;
    int GND3 = 10;
    int GND4 = 9;
    int j;
    int num;
    int dig1 = 0;
    int dig2 = 0;
    int dig3 = 0;
    int dig4 = 0;
    int DTime = 4;
    void setup()
    {
    pinMode(aPin, OUTPUT);
      pinMode(bPin, OUTPUT);
      pinMode(cPin, OUTPUT);
      pinMode(dPin, OUTPUT);
      pinMode(ePin, OUTPUT);
      pinMode(fPin, OUTPUT);
      pinMode(gPin, OUTPUT);
      pinMode(GND1, OUTPUT);
      pinMode(GND2, OUTPUT);
      pinMode(GND3, OUTPUT);
      pinMode(GND4, OUTPUT);
       pinMode(TRIGGER_PIN, OUTPUT);
       pinMode(ECHO_PIN, INPUT);
       digitalWrite(TRIGGER_PIN, LOW);
    }
    
    void loop()
    {
      delay(MEASURE_DELAY);
      long distance = measure();
      j = distance;
      num = distance;
        dig1 = num / 1000;
        num = num - (dig1 * 1000);
        dig2 = num / 100;
        num = num - (dig2 * 100);
        dig3 = num / 10;
        dig4 = num - (dig3 * 10);
    
    
        for (int i = 1; i <= 50; i++)
        {
    
          if (j >= 1000)
          {
            digitalWrite( GND4, HIGH);    //digit 4
            pickNumber(dig4);
            delay(DTime);
            digitalWrite( GND4, LOW);
    
            digitalWrite( GND3, HIGH);    //digit 3
            pickNumber(dig3);
            delay(DTime);
            digitalWrite( GND3, LOW);
    
            digitalWrite( GND2, HIGH);   //digit 2
            pickNumber(dig2);
            delay(DTime);
            digitalWrite( GND2, LOW);
    
            digitalWrite( GND1, HIGH);   //digit 1
            pickNumber(dig1);
            delay(DTime);
            digitalWrite( GND1, LOW);
          }
          else if  (j >= 100)
          {
            digitalWrite( GND4, HIGH);    //digit 4
            pickNumber(dig4);
            delay(DTime);
            digitalWrite( GND4, LOW);
    
            digitalWrite( GND3, HIGH);    //digit 3
            pickNumber(dig3);
            delay(DTime);
            digitalWrite( GND3, LOW);
    
            digitalWrite( GND2, HIGH);   //digit 2
            pickNumber(dig2);
            delay(DTime);
            digitalWrite( GND2, LOW);
    
            digitalWrite( GND1, LOW);   //digit 1
            pickNumber(dig1);
            delay(DTime);
            digitalWrite( GND1, LOW);
          }
          else if  (j >= 10)
          {
            digitalWrite( GND4, HIGH);    //digit 4
            pickNumber(dig4);
            delay(DTime);
            digitalWrite( GND4, LOW);
    
            digitalWrite( GND3, HIGH);    //digit 3
            pickNumber(dig3);
            delay(DTime);
            digitalWrite( GND3, LOW);
    
            digitalWrite( GND2, LOW);   //digit 2
            pickNumber(dig2);
            delay(DTime);
            digitalWrite( GND2, LOW);
    
            digitalWrite( GND1, LOW);   //digit 1
            pickNumber(dig1);
            delay(DTime);
            digitalWrite( GND1, LOW);
          }
    
          else if  (j >= 0)
          {
            digitalWrite( GND4, HIGH);    //digit 4
            pickNumber(dig4);
            delay(DTime);
            digitalWrite( GND4, LOW);
    
            digitalWrite( GND3, LOW);    //digit 3
            pickNumber(dig3);
            delay(DTime);
            digitalWrite( GND3, LOW);
    
            digitalWrite( GND2, LOW);   //digit 2
            pickNumber(dig2);
            delay(DTime);
            digitalWrite( GND2, LOW);
    
            digitalWrite( GND1, LOW);   //digit 1
            pickNumber(dig1);
            delay(DTime);
            digitalWrite( GND1, LOW);
          }
      }
    }
    long measure()
    {
      long measureSum = 0;
      for (int i = 0; i < MEASURE_SAMPLES; i++)
      {
        delay(MEASURE_SAMPLE_DELAY);
        measureSum += singleMeasurement();
      }
      return measureSum / MEASURE_SAMPLES;
    }
    
    long singleMeasurement()
    {
      long duration = 0;
      // Measure: Put up Trigger...
      digitalWrite(TRIGGER_PIN, HIGH);
      // ... wait for 11 µs ...
      delayMicroseconds(11);
      // ... put the trigger down ...
      digitalWrite(TRIGGER_PIN, LOW);
      // ... and wait for the echo ...
      duration = pulseIn(ECHO_PIN, HIGH);
      return (long) (((float) duration / USONIC_DIV) * 10.0);
    }
    
    void pickNumber(int x) {
      switch (x) {
        case 0: zero(); break;
        case 1: one(); break;
        case 2: two(); break;
        case 3: three(); break;
        case 4: four(); break;
        case 5: five(); break;
        case 6: six(); break;
        case 7: seven(); break;
        case 8: eight(); break;
        case 9: nine(); break;
      }
    }
    
    
    
    void zero()
    {
      digitalWrite( aPin, LOW);
      digitalWrite( bPin, LOW);
      digitalWrite( cPin, LOW);
      digitalWrite( dPin, LOW);
      digitalWrite( ePin, LOW);
      digitalWrite( fPin, LOW);
      digitalWrite( gPin, HIGH);
    }
    
    void one()
    {
      digitalWrite( aPin, HIGH);
      digitalWrite( bPin, LOW);
      digitalWrite( cPin, LOW);
      digitalWrite( dPin, HIGH);
      digitalWrite( ePin, HIGH);
      digitalWrite( fPin, HIGH);
      digitalWrite( gPin, HIGH);
    }
    
    void two()
    {
      digitalWrite( aPin, LOW);
      digitalWrite( bPin, LOW);
      digitalWrite( cPin, HIGH);
      digitalWrite( dPin, LOW);
      digitalWrite( ePin, LOW);
      digitalWrite( fPin, HIGH);
      digitalWrite( gPin, LOW);
    }
    
    void three()
    {
      digitalWrite( aPin, LOW);
      digitalWrite( bPin, LOW);
      digitalWrite( cPin, LOW);
      digitalWrite( dPin, LOW);
      digitalWrite( ePin, HIGH);
      digitalWrite( fPin, HIGH);
      digitalWrite( gPin, LOW);
    }
    
    void four()
    {
      digitalWrite( aPin, HIGH);
      digitalWrite( bPin, LOW);
      digitalWrite( cPin, LOW);
      digitalWrite( dPin, HIGH);
      digitalWrite( ePin, HIGH);
      digitalWrite( fPin, LOW);
      digitalWrite( gPin, LOW);
    }
    
    void five()
    {
      digitalWrite( aPin, LOW);
      digitalWrite( bPin, HIGH);
      digitalWrite( cPin, LOW);
      digitalWrite( dPin, LOW);
      digitalWrite( ePin, HIGH);
      digitalWrite( fPin, LOW);
      digitalWrite( gPin, LOW);
    }
    
    void six()
    {
      digitalWrite( aPin, LOW);
      digitalWrite( bPin, HIGH);
      digitalWrite( cPin, LOW);
      digitalWrite( dPin, LOW);
      digitalWrite( ePin, LOW);
      digitalWrite( fPin, LOW);
      digitalWrite( gPin, LOW);
    }
    
    void seven()
    {
      digitalWrite( aPin, LOW);
      digitalWrite( bPin, LOW);
      digitalWrite( cPin, LOW);
      digitalWrite( dPin, HIGH);
      digitalWrite( ePin, HIGH);
      digitalWrite( fPin, HIGH);
      digitalWrite( gPin, HIGH);
    }
    
    void eight()
    {
      digitalWrite( aPin, LOW);
      digitalWrite( bPin, LOW);
      digitalWrite( cPin, LOW);
      digitalWrite( dPin, LOW);
      digitalWrite( ePin, LOW);
      digitalWrite( fPin, LOW);
      digitalWrite( gPin, LOW);
    }
    
    void nine()
    {
      digitalWrite( aPin, LOW);
      digitalWrite( bPin, LOW);
      digitalWrite( cPin, LOW);
      digitalWrite( dPin, LOW);
      digitalWrite( ePin, HIGH);
      digitalWrite( fPin, LOW);
      digitalWrite( gPin, LOW);
    }
    

    Code for 4 Digit 7 segment (common cathode)

    #define TRIGGER_PIN A1
    #define ECHO_PIN A0
    #define USONIC_DIV 58.0
    #define MEASURE_SAMPLE_DELAY 5
    #define MEASURE_SAMPLES 15
    #define MEASURE_DELAY 15
    int aPin = 2;
    int bPin = 3;
    int cPin = 4;
    int dPin = 5;
    int ePin = 12;
    int fPin = 7;
    int gPin = 8;
    int GND1 = 6;
    int GND2 = 11;
    int GND3 = 10;
    int GND4 = 9;
    int j;
    int num;
    int dig1 = 0;
    int dig2 = 0;
    int dig3 = 0;
    int dig4 = 0;
    int DTime = 4;
    void setup()
    {
    pinMode(aPin, OUTPUT);
      pinMode(bPin, OUTPUT);
      pinMode(cPin, OUTPUT);
      pinMode(dPin, OUTPUT);
      pinMode(ePin, OUTPUT);
      pinMode(fPin, OUTPUT);
      pinMode(gPin, OUTPUT);
      pinMode(GND1, OUTPUT);
      pinMode(GND2, OUTPUT);
      pinMode(GND3, OUTPUT);
      pinMode(GND4, OUTPUT);
       pinMode(TRIGGER_PIN, OUTPUT);
       pinMode(ECHO_PIN, INPUT);
       digitalWrite(TRIGGER_PIN, HIGH);
    }
    
    void loop()
    {
      delay(MEASURE_DELAY);
      long distance = measure();
      j = distance;
      num = distance;
        dig1 = num / 1000;
        num = num - (dig1 * 1000);
        dig2 = num / 100;
        num = num - (dig2 * 100);
        dig3 = num / 10;
        dig4 = num - (dig3 * 10);
    
    
        for (int i = 1; i <= 50; i++)
        {
    
          if (j >= 1000)
          {
            digitalWrite( GND4, LOW);    //digit 4
            pickNumber(dig4);
            delay(DTime);
            digitalWrite( GND4, HIGH);
    
            digitalWrite( GND3, LOW);    //digit 3
            pickNumber(dig3);
            delay(DTime);
            digitalWrite( GND3, HIGH);
    
            digitalWrite( GND2, LOW);   //digit 2
            pickNumber(dig2);
            delay(DTime);
            digitalWrite( GND2, HIGH);
    
            digitalWrite( GND1, LOW);   //digit 1
            pickNumber(dig1);
            delay(DTime);
            digitalWrite( GND1, HIGH);
          }
          else if  (j >= 100)
          {
            digitalWrite( GND4, LOW);    //digit 4
            pickNumber(dig4);
            delay(DTime);
            digitalWrite( GND4, HIGH);
    
            digitalWrite( GND3, LOW);    //digit 3
            pickNumber(dig3);
            delay(DTime);
            digitalWrite( GND3, HIGH);
    
            digitalWrite( GND2, LOW);   //digit 2
            pickNumber(dig2);
            delay(DTime);
            digitalWrite( GND2, HIGH);
    
            digitalWrite( GND1, HIGH);   //digit 1
            pickNumber(dig1);
            delay(DTime);
            digitalWrite( GND1, HIGH);
          }
          else if  (j >= 10)
          {
            digitalWrite( GND4, LOW);    //digit 4
            pickNumber(dig4);
            delay(DTime);
            digitalWrite( GND4, HIGH);
    
            digitalWrite( GND3, LOW);    //digit 3
            pickNumber(dig3);
            delay(DTime);
            digitalWrite( GND3, HIGH);
    
            digitalWrite( GND2, HIGH);   //digit 2
            pickNumber(dig2);
            delay(DTime);
            digitalWrite( GND2, HIGH);
    
            digitalWrite( GND1, HIGH);   //digit 1
            pickNumber(dig1);
            delay(DTime);
            digitalWrite( GND1, HIGH);
          }
    
          else if  (j >= 0)
          {
            digitalWrite( GND4, LOW);    //digit 4
            pickNumber(dig4);
            delay(DTime);
            digitalWrite( GND4, HIGH);
    
            digitalWrite( GND3, HIGH);    //digit 3
            pickNumber(dig3);
            delay(DTime);
            digitalWrite( GND3, HIGH);
    
            digitalWrite( GND2, HIGH);   //digit 2
            pickNumber(dig2);
            delay(DTime);
            digitalWrite( GND2, HIGH);
    
            digitalWrite( GND1, HIGH);   //digit 1
            pickNumber(dig1);
            delay(DTime);
            digitalWrite( GND1, HIGH);
          }
      }
    }
    long measure()
    {
      long measureSum = 0;
      for (int i = 0; i < MEASURE_SAMPLES; i++)
      {
        delay(MEASURE_SAMPLE_DELAY);
        measureSum += singleMeasurement();
      }
      return measureSum / MEASURE_SAMPLES;
    }
    
    long singleMeasurement()
    {
      long duration = 0;
      // Measure: Put up Trigger...
      digitalWrite(TRIGGER_PIN, LOW);
      // ... wait for 11 µs ...
      delayMicroseconds(11);
      // ... put the trigger down ...
      digitalWrite(TRIGGER_PIN, HIGH);
      // ... and wait for the echo ...
      duration = pulseIn(ECHO_PIN, LOW);
      return (long) (((float) duration / USONIC_DIV) * 10.0);
    }
    
    void pickNumber(int x) {
      switch (x) {
        case 0: zero(); break;
        case 1: one(); break;
        case 2: two(); break;
        case 3: three(); break;
        case 4: four(); break;
        case 5: five(); break;
        case 6: six(); break;
        case 7: seven(); break;
        case 8: eight(); break;
        case 9: nine(); break;
      }
    }
    
    
    
    void zero()
    {
      digitalWrite( aPin, HIGH);
      digitalWrite( bPin, HIGH);
      digitalWrite( cPin, HIGH);
      digitalWrite( dPin, HIGH);
      digitalWrite( ePin, HIGH);
      digitalWrite( fPin, HIGH);
      digitalWrite( gPin, LOW);
    }
    
    void one()
    {
      digitalWrite( aPin, LOW);
      digitalWrite( bPin, HIGH);
      digitalWrite( cPin, HIGH);
      digitalWrite( dPin, LOW);
      digitalWrite( ePin, LOW);
      digitalWrite( fPin, LOW);
      digitalWrite( gPin, LOW);
    }
    
    void two()
    {
      digitalWrite( aPin, HIGH);
      digitalWrite( bPin, HIGH);
      digitalWrite( cPin, LOW);
      digitalWrite( dPin, HIGH);
      digitalWrite( ePin, HIGH);
      digitalWrite( fPin, LOW);
      digitalWrite( gPin, HIGH);
    }
    
    void three()
    {
      digitalWrite( aPin, HIGH);
      digitalWrite( bPin, HIGH);
      digitalWrite( cPin, HIGH);
      digitalWrite( dPin, HIGH);
      digitalWrite( ePin, LOW);
      digitalWrite( fPin, LOW);
      digitalWrite( gPin, HIGH);
    }
    
    void four()
    {
      digitalWrite( aPin, LOW);
      digitalWrite( bPin, HIGH);
      digitalWrite( cPin, HIGH);
      digitalWrite( dPin, LOW);
      digitalWrite( ePin, LOW);
      digitalWrite( fPin, HIGH);
      digitalWrite( gPin, HIGH);
    }
    
    void five()
    {
      digitalWrite( aPin, HIGH);
      digitalWrite( bPin, LOW);
      digitalWrite( cPin, HIGH);
      digitalWrite( dPin, HIGH);
      digitalWrite( ePin, LOW);
      digitalWrite( fPin, HIGH);
      digitalWrite( gPin, HIGH);
    }
    
    void six()
    {
      digitalWrite( aPin, HIGH);
      digitalWrite( bPin, LOW);
      digitalWrite( cPin, HIGH);
      digitalWrite( dPin, HIGH);
      digitalWrite( ePin, HIGH);
      digitalWrite( fPin, HIGH);
      digitalWrite( gPin, HIGH);
    }
    
    void seven()
    {
      digitalWrite( aPin, HIGH);
      digitalWrite( bPin, HIGH);
      digitalWrite( cPin, HIGH);
      digitalWrite( dPin, LOW);
      digitalWrite( ePin, LOW);
      digitalWrite( fPin, LOW);
      digitalWrite( gPin, LOW);
    }
    
    void eight()
    {
      digitalWrite( aPin, HIGH);
      digitalWrite( bPin, HIGH);
      digitalWrite( cPin, HIGH);
      digitalWrite( dPin, HIGH);
      digitalWrite( ePin, HIGH);
      digitalWrite( fPin, HIGH);
      digitalWrite( gPin, HIGH);
    }
    
    void nine()
    {
      digitalWrite( aPin, HIGH);
      digitalWrite( bPin, HIGH);
      digitalWrite( cPin, HIGH);
      digitalWrite( dPin, HIGH);
      digitalWrite( ePin, LOW);
      digitalWrite( fPin, HIGH);
      digitalWrite( gPin, HIGH);
    }
    
    « DHT11 Humidity and Temperature Sensor Displayed on 4 Digit 7 segment12V 30W DIY TDA2030A Dual Track Power Amplifier Board Kit »
    Partager via Gmail Delicious Technorati Yahoo! Google Bookmarks Blogmarks Pin It

  • Comments

    No comments yet

    Suivre le flux RSS des commentaires


    Add comment

    Name / User name:

    E-mail (optional):

    Website (optional):

    Comment: