Grove - Ultrasonic Ranger

From Wiki 来自痴汉的爱
Jump to: navigation, search


Ultrasonic Ranger.jpg



This Grove - Ultrasonic sensor is a non-contact distance measurement module which works at 42KHz, suitable for projects that require middle distance measurement.

Model: SEN10737P


  • Operating voltage: 3.3/5.0VDC
  • Operating current: 15mA
  • Ultrasonic frequency: 42kHz
  • Measuring range: 3-400cm
  • Resolution: 1cm
  • Output: PWM
  • Suli-compatible Library

Note: More details about Suli-compatible Library, please refer to Suli


With Arduino

Here we demonstrate how to use Grove - Ultrasonic Ranger to obtain distance message.

Connect Serial LCD to D4/D5 pins of Grove - Base Shield and connect Ultrasonic Ranger to D7.


Download the UltrasonicRanger Library and SerialLCD Library from Github. For this libraries, rather than from Arduino library files, they are from other pages. You need to install them to Arduino Library. Please refer to the course"How to install library" of wiki page to learn the specific steps.

open code from the path:File->Examples->UltrasonicRanger->examples->UltraDisOnSeeedSerialLcd and upload.

// Function: Measure the distance to obstacles in front and display the
//                         result on seeedstudio serialLcd. Make sure you installed the
//                         serialLCD, SoftwareSerial and Ultrasonic library.        
//        Hardware: Grove - Ultrasonic Ranger, Grove - Serial LCD
//        Arduino IDE: Arduino-1.0
//        Author:         LG                
//        Date:          Jan 17,2013
//        Version: v1.0 modified by FrankieChu
//        by


#include <SoftwareSerial.h>
#include <SerialLCD.h>
#include "Ultrasonic.h"

SerialLCD slcd(11,12);
Ultrasonic ultrasonic(7);
void setup()
void loop()
        long RangeInCentimeters;
        RangeInCentimeters = ultrasonic.MeasureInCentimeters();
        slcd.print("The distance:");
        slcd.setCursor(0,1) ;
        slcd.setCursor(5,1) ;

Also we shot a video about it. Find it in the Resources! It can give you a more straightforward instruction.

With TI LaunchPad

Sensing the Distance (Ultrasonic Ranger Sensor)

This example shows how to measure the distance to obstacles using the ultrasonic sensor and display the value on a 4-Digital-Display (centimeters).


  Ultrasonic-Ranger to 4-digit-display
 Measure the distance to obstacles in front and display the value on 
 The circuit:
 * Ultrasonic Ranger attached to SPI plug on Grove Base BoosterPack
 * one side pin (either one) to ground
 * the other side pin to +VCC
 * LED anode (long leg) attached to RED_LED
 * LED cathode (short leg) attached to ground
 * Note:  
 This example code is in the public domain. 
#include "TM1637.h" 
#include "Ultrasonic.h"
/* Macro Define */
#define CLK               40                  /* 4-digital display clock pin */
#define DIO               39                 /* 4-digital display data pin */
#define BLINK_LED         RED_LED            /* blink led */
#define ULTRASONIC_PIN    38                  /* pin of the Ultrasonic Ranger */

/* Global Variables */
TM1637 tm1637(CLK, DIO);                  /* 4-digital display object */
Ultrasonic ultrasonic(ULTRASONIC_PIN);    /* Ultrasonic Ranger object */
int distance = 0;                         /* variable to store the distance to obstacles in front */
int blink_interval = 0;                   /* led delay time */
int8_t bits[4] = {0};                     /* array to store the single bits of the value */

/* the setup() method runs once, when the sketch starts */
void setup() {
    /* Initialize 4-digital display */
    /* declare the red_led pin as an OUTPUT */
    pinMode(RED_LED, OUTPUT);

/* the loop() method runs over and over again */
void loop() {   
    distance = ultrasonic.MeasureInCentimeters();   /* read the value from the sensor */   
    memset(bits, 0, 4);                             /* reset array when we use it */
    for(int i = 3; i >= 0; i--) {
        /* get single bits of the analog value */
        bits[i] = distance % 10;
        distance = distance / 10;  
        tm1637.display(i, bits[i]);                 /* display by 4-digital display */

With Raspberry Pi

First step,you should connect Ultrasonic to Port D4. Run the program below. It will print distance information on you terminal,as the figure below.

GrovePi+ Ultrasonic Ranger Sensor.jpg
GrovePi+ Ultrasonic Ranger Sensor terminal.jpg
# GrovePi + Grove Ultrasonic Ranger

from grovepi import *

# Connect the Grove Ultrasonic Ranger to digital port D4

ultrasonic_ranger = 4

while True:
        # Read distance value from Ultrasonic
        print ultrasonicRead(ultrasonic_ranger)

    except TypeError:
        print "Error"
    except IOError:
        print "Error"

Run The Program

  • Find the path to the file(According to your own path)
   cd GrovePi/Software/Python/
  • Run Program
   sudo python


Personal tools