3 Pin Ultrasonic Sensor

This code reads a value from an ultrasonic sensor with 3 pins: ground, voltage, and signal. The signal pin plugs into a digital pin on the Arduino, and the code changes the pin mode as needed to both send and received data to the sensor via one wire.

Supported Products

  • Parallax PING))) Ultrasonic Sensor

Decleration Code

const int pingPin = 7;             // Update this to the pin your sensor is plugged into!

EveryUpdate

// establish variables for duration of the ping,
// and the distance result in inches and centimeters:
long duration, inches, cm;

// The PING))) is triggered by a HIGH pulse of 2 or more microseconds.
// Give a short LOW pulse beforehand to ensure a clean HIGH pulse:
pinMode(pingPin, OUTPUT);
digitalWrite(pingPin, LOW);
delayMicroseconds(2);
digitalWrite(pingPin, HIGH);
delayMicroseconds(5);
digitalWrite(pingPin, LOW);

// The same pin is used to read the signal from the PING))): a HIGH
// pulse whose duration is the time (in microseconds) from the sending
// of the ping to the reception of its echo off of an object.
pinMode(pingPin, INPUT);
duration = pulseIn(pingPin, HIGH, 100);             // Max 100 microsecond read

// Convert the duration value from 0 to 100 to the Embrio range of 0.0 to 1.0
Output_Activation = transform(duration, 0, 100, 0.0, 1.0);

No required source files.

To use this node in your Embrio project, either add it from the Library toolbar or copy the XML in the below box and paste it into a node screen.

View Comments (0)