Need support? support@freenove.com
Chapter 10 Ultrasonic Ranging
The following is the code:
1
2
3
4
5
6
7
8
9
10
11
12
#include "UltrasonicRanging.h"
void setup() {
Serial.begin(115200);
setupSonar();
}
void loop() {
float dist = getSonar();
Serial.printf("dist: %.2fcm\r\n", dist);
delay(500);
}
Initialize the pins of the ultrasonic module.
Get the data value of the ultrasonic module, the unit is cm.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#ifndef _ULTRASONICRANGING_h
#define _ULTRASONICRANGING_h
#if defined(ARDUINO) && ARDUINO >= 100
#include "Arduino.h"
#else
#include "WProgram.h"
#endif
#define PIN_TRIG 32 //Ultrasonic trig pin
#define PIN_ECHO 12 //Ultrasonic echo pin
void setupSonar();
float getSonar();
#endif