Using Arduino and Seeed Studio Grove ultrasonic ranger we can easily build a distance meter. This project shows how to do that where distance value is printed (in centimeters and inches) on Grove LCD (I2C 1602 LCD display) and Arduino IDE serial monitor.
To see how to interface the Arduino with Grove I2C LCD, visit this post:
Interfacing Arduino with Seeed Studio Grove LCD
About Grove Ultrasonic Ranger:
The Grove ultrasonic ranger (ultrasonic distance sensor) is an ultrasonic transducer that uses ultrasonic waves to measure the distance. This sensor can measure distances from 3cm to 350cm with an accuracy up to 2mm.
Compared with traditional ultrasonic sensor module HC-SR04, the Grove ultrasonic distance sensor has an integrated single-chip microcomputer, it requires only one I/O pin. Another difference is that HC-SR04 only supports 5V systems, while the Grove ultrasonic distance sensor supports both 5V and 3.3V systems. This means it can be directly connected to any Raspberry pi I/O pin.
Grove Ultrasonic ranger features:
- 3.3V/5V compatible, wide supply voltage range: 3.2v~5.2v
- One I/O pin is required
- Wide measurement range: 3cm ~ 350cm
- Low cost and easy to use through Grove connector (cable), plug and play
Grove Ultrasonic ranger hardware overview:
The following image shows a basic hardware overview of the Grove ultrasonic ranger module.
Hardware Required:
- Arduino/Genuino UNO board —> Board details —> ATmega328P datasheet
- Grove base shield —> official page
- Grove ultrasonic ranger —> official page
- Grove LCD screen (1602 I2C LCD)
- 2 x Grove 4-pin cable
Arduino with Grove ultrasonic ranger and 1602 LCD circuit:
The following image shows the connection of the 3 Grove modules (base shield, 16×2 LCD and ultrasonic ranger module) using 4-pin Grove cables.
Note that the base shield is directly mounted to the Arduino UNO board.
The Grove 1602 LCD display is connected to base shield I2C port where:
GND pin is connected to Arduino GND,
VCC pin is connected to Arduino +5V pin,
SDA is connected to Arduino analog pin 4 (A4),
SCL is connected to Arduino analog pin 5 (A5).
The Grove ultrasonic ranger (ultrasonic distance sensor) is connected to base shield port D2, internally it’s connected as follows:
GND pin is connected to Arduino GND,
VCC pin is connected to Arduino +5V pin,
NC is a not connected pin (it looks as it is connected to pin D3),
SIG (signal) pin is connected to Arduino digital pin 2.
Arduino with Grove ultrasonic ranger and 1602 LCD code:
Seeed Studio provides a nice open source library for their LCD module which can be installed from Arduino IDE library manager (Sketch —> Include Library —> Manage Libraries …, in the search box write “grove lcd” and install the one from Seeed Studio).
Or it can be installed manually, first download library compressed file from the following link:
Grove LCD display Library —> direct link
They also provides a library for the Grove ultrasonic distance sensor which can be installed also through library manager (in the search box write “grove ultrasonic” ) or manually, download links are below:
Seeed Studio ultrasonic ranger library —> direct link
After the download, go to Arduino IDE —> Sketch —> Include Library —> Add .ZIP Library … and browse for the .zip file (previously downloaded).
The same thing for the 2nd library file.
In the Arduino code there are 3 libraries which are included as shown below:
1 2 3 | #include <Wire.h> // include Arduino Wire library #include "rgb_lcd.h" // include Seeed Studio LCD library #include "Ultrasonic.h" // include Seeed Studio ultrasonic ranger library |
Definition of the Grove ultrasonic ranger data pin connection and the initialization of its library:
1 2 3 4 | // define ultrasonic ranger data pin #define RANGERPIN 2 // initialize ultrasonic library Ultrasonic ultrasonic(RANGERPIN); |
Rest of code is described through comments.
Full Arduino code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 | /************************************************************************* * * Interfacing Arduino with Seeed Studio Grove LCD (I2C 16x2 LCD) * and Grove ultrasonic ranger. * Distance value is printed in centimeters and inches on * the 1602 LCD and Arduino IDE serial monitor. * This is a free software with NO WARRANTY. * https://simple-circuit.com/ * ************************************************************************/ #include <Wire.h> // include Arduino Wire library #include "rgb_lcd.h" // include Seeed Studio LCD library #include "Ultrasonic.h" // include Seeed Studio ultrasonic ranger library // initialize LCD library rgb_lcd lcd; // define ultrasonic ranger data pin #define RANGERPIN 2 // initialize ultrasonic library Ultrasonic ultrasonic(RANGERPIN); void setup() { // open serial communication Serial.begin(9600); // initialize the LCD with 16 columns and 2 rows: lcd.begin(16, 2); lcd.print("Distance:"); } // main loop void loop() { char _buffer[7]; int centimeters; int inches; // get distance in centimeters centimeters = ultrasonic.MeasureInCentimeters(); delay(250); // wait 250 milliseconds between readings // get distance in inches inches = ultrasonic.MeasureInInches(); delay(250); // wait 250 milliseconds between readings // print distance in cm sprintf( _buffer, "%03u cm", centimeters ); lcd.setCursor(10, 0); // move cursor to position 10, 1st row lcd.print(_buffer); // print distance (in cm) on LCD Serial.print("\r\nDistance: "); Serial.println(_buffer); // print distance (in cm) on serial monitor // print distance in inches sprintf( _buffer, "%03u in", inches ); lcd.setCursor(10, 1); // move cursor to position 10, 2nd row lcd.print(_buffer); // print distance (in inches) on LCD Serial.print(" "); Serial.println(_buffer); // print distance (in inches) on serial monitor } // end of example code. |
Discover more from Simple Circuit
Subscribe to get the latest posts sent to your email.