Distance sensor
Overview
An example of using Seeed Studio’s Grove series of sensors. The distance measured by the ultrasonic sensor is displayed on the LCD. Ultrasonic sensors are usually input to digital pins, but since LCD’s Switch1 and Grove&5V’s Din1 use the same pins, they are connected to UART pins and UART pins are used as digital pins.
Leaf to use
Type | Name | Q’ty |
---|---|---|
AZ62 | Connector Cover | 1 |
AI04 | LCD | 1 |
AX04 | Spacer | 1 |
AX06 | Grove&5V | 1 |
AP01 | AVR MCU | 1 |
AZ01 | USB | 1 |
AV01 | CR2032 | 1 |
Grove – Ultrasonic Distance Sensor | 1 | |
CR2032 coin cell battery | 1 | |
M2*18mm screw | 2 |
Assembly
Assemble the leaves as shown in the figure below.
Source code
- The LCD library and ultrasonic control library “Ultrasonic.h” are required for writing. Please include them beforehand. For more information, see here.
- Write the following program in the Arduino IDE.
//=====================================================================
// Leafony Platform sample sketch
// Platform : Grove + Ultrasonic
// Processor : ATmega328P (3.3V /8MHz)
// Application : Ultrasonic Ranger demo
//
// Leaf configuration
// (1) AI04 LCD
// (2) AP01 AVR MCU
// (3) AX01 Grove&5V + Grove - Ultrasonic Ranger (UART pinに接続)
// ※ Ultrasonic RangerはGrove&5VのUARTに接続する
// (4) AZ01 USB
//
// (c) 2019 Trillion-Node Study Group
// Released under the MIT license
// https://opensource.org/licenses/MIT
//
// Rev.00 2019/08/01 First release
//=====================================================================
//Grove - Ultrasonic Rangerを使用したデモ
//LCDに超音波センサーから取得した対象物からの距離を表示
//=====================================================================
//use libraries
//ST7032 - Arduino LiquidCrystal compatible library
//https://github.com/tomozh/arduino_ST7032
//Grove_Ultrasonic_Ranger
//https://github.com/Seeed-Studio/Grove_Ultrasonic_Ranger/
//=====================================================================
#include <Wire.h>
#include <ST7032.h>
// LCD
ST7032 lcd;
char strMessage[8];
#include "Ultrasonic.h"
Ultrasonic ultrasonic(A1);
void setup() {
Serial.begin(115200);
//LCD Initialize
lcd.begin(8, 2);
lcd.setContrast(30);
lcd.clear();
lcd.print(" Hello!");
lcd.setCursor(0, 1);
delay(1000);
lcd.begin(8, 2);
lcd.setContrast(30);
lcd.clear();
lcd.print("12345678");
lcd.setCursor(0, 1);
lcd.print("87654321");
delay(500);
lcd.clear();
lcd.blink();
int i;
for (i=0 ; i<8 ;i++)
{
lcd.setCursor(i, 0);
delay(100);
}
for (i=0 ; i<8 ;i++)
{
lcd.setCursor(i, 1);
delay(100);
}
lcd.noBlink();
}
void loop() {
long RangeInCentimeters;
lcd.clear();
lcd.print("Distance");
RangeInCentimeters = ultrasonic.MeasureInCentimeters();
sprintf(strMessage,"%5d cm",RangeInCentimeters);
lcd.setCursor(0, 1);
lcd.print(strMessage);
delay(250);
}
Last modified 15.03.2021