Example Code for Arduino-PWM Output

Last revision 2025/12/17

Hardware Preparation

  • DFRduino UNO R3 (or similar) x 1
  • Water-proof Ultrasonic Sensor (ULS) x1
  • Connectors

Software Preparation

Wiring Diagram

SEN0300 Water-proof Ultrasonic Sensor (ULS) Connection Diagram

Other Preparation Work

The internal probe surface is regarded as the detecting starting point by default. If you take the plane of the bell-mouth as starting point, then the distance will be the detected distance minus 40mm.

Sample Code

* **************************************************** 
* @brief Water-proof Ultrasonic Sensor (ULS)

 * @copyright	[DFRobot](https://www.dfrobot.com), 2016
 * @copyright	GNU Lesser General Public License

* @author [Xiaoyu]([email protected])
* @version  V1.0
* @date  2019-03-11
  
* GNU Lesser General Public License.
* All above must be included in any redistribution
* ****************************************************/
#include <SoftwareSerial.h>
char col;// for storing the data read from the serial port
unsigned long duration;
int datapin = 10;
int val = 0;
void setup() {
        Serial.begin(57600);     //Enable the serial port and set band rate to 57600 bps
        pinMode(LED_BUILTIN, OUTPUT);
        pinMode(datapin, INPUT);
        digitalWrite(LED_BUILTIN, HIGH);
}

void loop(){
        digitalWrite(LED_BUILTIN, HIGH);
        delay(57); 
        digitalWrite(LED_BUILTIN, LOW);
        delay(4);                         //give level signal          
        duration = pulseIn(datapin, HIGH);//read the high level pulse on the pin, the maximum pulse interval is 55ms; assign the result to the variable "duration"
        delay(3);
        digitalWrite(LED_BUILTIN, HIGH);//Pull up the signal pin 
        duration = duration/ 58.00; //convert time to distance    
        Serial.print(duration);//serial port print data
        Serial.println("cm");
        Serial.println("=========== ");        
        } 

Result

SEN0300 Water-proof Ultrasonic Sensor (ULS) Read PWM Output

Was this article helpful?

TOP