Example Code for Arduino-PWM Trigger (Analog Output)

Last revision 2026/01/22

This article provides example code for using Arduino's PWM trigger to achieve analog outputs, explaining voltage-distance proportionality for enhanced distance measurement capabilities.

Hardware Preparation

  • Arduino board: Quantity1
  • URM37 V5.0 ultrasonic sensor (SKU:SEN0001): Quantity1
  • Jumper wires: Quantity several

Software Preparation

Wiring Diagram

Connection Diagram

Other Preparation Work

  1. Ensure the sensor's serial level is set to TTL mode (default) using the button method described in Getting Started section.
  2. Connect the hardware as per the wiring diagram.

Sample Code

// # Editor     : roker
// # Date       : 05.03.2018
// # Product name: URM V5.0 ultrasonic sensor
// # Product SKU : SEN0001
// # Version     : 1.0
// # Description:
// # The Sketch for scanning 180 degree area 2-800cm detecting range
// # The sketch for using the URM37 PWM trigger pin mode from DFRobot
// #   and writes the values to the serialport
// # Connection:
// #       Vcc (Arduino)    -> Pin 1 VCC (URM V5.0)
// #       GND (Arduino)    -> Pin 2 GND (URM V5.0)
// #       Pin 5 (Arduino)  -> Pin 6 COMP/TRIG (URM V5.0)
// #       Pin A0 (Arduino) -> Pin 7 DAC (URM V5.0)
int URTRIG = 5;         // trigger pin
int sensorPin = A0;     // select the input pin for the potentiometer
int sensorValue = 0;    // variable to store the value coming from the sensor
unsigned int DistanceMeasured = 0;
void setup()
{
  //Serial initialization
  Serial.begin(9600);                        // Sets the baud rate to 9600
  pinMode(URTRIG, OUTPUT);                   // A low pull on pin COMP/TRIG
  digitalWrite(URTRIG, HIGH);                // Set to HIGH
  delay(500);
  Serial.println("Init the sensor");
}
void loop()
{
  Serial.print("Distance=");
  digitalWrite(URTRIG, LOW);
  digitalWrite(URTRIG, HIGH);              
  delay(200);
  sensorValue = analogRead(sensorPin);
  
  sensorValue = sensorValue * 1.1; //  (sensorValue * 5000 / 1024 ) / 4.125 = sensorValue * 1.1 , calculate the voltage value ADC collected and divide it by 4.125mV /cm.
  Serial.print(sensorValue);
  Serial.println("cm");
}

Result

The distance is calculated from the analog output voltage. Note: the error of the distance got by calculating output analog voltage is bigger than that of the distance by other ways.

Was this article helpful?

TOP