I’m working on a project using the Artemis Thing Plus, and I’m trying to measure the distance to an object using a Taidacent ultrasonic sensor in PWM mode. The sensor outputs a high pulse on the TX pin after it’s triggered, and the pulse width corresponds to the distance of the object detected. Here are some details about my setup:
Sensor capabilities: It has a range of 3 cm to 450 cm, which translates to pulse widths of roughly 0.17 ms to 25.86 ms.
Goal: I need the Artemis to measure these pulse widths accurately to determine the object distance in real-time.
Given the Artemis Thing Plus’s capabilities, could anyone suggest the simplest way to capture this pulse width? Should I use pulseIn(), or is there a more efficient approach with the Artemis’s hardware timers or other methods?
That should work for most applications, unless you need the thing plus to do other stuff in-between, or if you notice you are missing some measurements (or if having every measurement is critical)…you could use interrupts and timers if pulseIn doesn’t suffice
Here’s a sample code:
const int TRIG_PIN = 7; // Example pin for trigger
const int ECHO_PIN = 8; // Example pin for echo (TX pin of the sensor)
void setup() {
pinMode(TRIG_PIN, OUTPUT);
pinMode(ECHO_PIN, INPUT);
Serial.begin(115200);
}
void loop() {
// Trigger the sensor
digitalWrite(TRIG_PIN, LOW);
delayMicroseconds(2);
digitalWrite(TRIG_PIN, HIGH);
delayMicroseconds(10);
digitalWrite(TRIG_PIN, LOW);
// Measure the pulse width
unsigned long pulseWidth = pulseIn(ECHO_PIN, HIGH);
// Convert pulse width to distance (adjust formula as per your sensor's specifications)
float distance = pulseWidth * 0.034 / 2;
Serial.print("Distance: ");
Serial.print(distance);
Serial.println(" cm");
delay(100); // Adjust delay as needed
}
I couldn’t get pulseIn to work but with the help of chatgpt I got the interrupt method to work.
See below.
Sam
const int TRIG_PIN = 6; // Pin for sending the trigger pulse to the sensor
const int ECHO_PIN = 7; // Pin to receive the echo pulse (TX pin of the sensor)
volatile unsigned long pulseStartTime = 0; // Stores the start time of the pulse
volatile unsigned long pulseEndTime = 0; // Stores the end time of the pulse
volatile bool pulseMeasured = false; // Flag to indicate pulse was measured
void setup() {
pinMode(TRIG_PIN, OUTPUT); // Set TRIG_PIN as output
pinMode(ECHO_PIN, INPUT); // Set ECHO_PIN as input
Serial.begin(115200); // Initialize Serial Monitor
// Ensure the trigger pin starts LOW
digitalWrite(TRIG_PIN, LOW);
// Attach interrupt on ECHO_PIN to detect both rising and falling edges
attachInterrupt(digitalPinToInterrupt(ECHO_PIN), measurePulse, CHANGE);
Serial.println(“Ultrasonic Sensor Program Initialized.”);
}
void loop() {
// Send a trigger pulse to start the measurement
digitalWrite(TRIG_PIN, LOW);
delayMicroseconds(2);
digitalWrite(TRIG_PIN, HIGH);
delayMicroseconds(10); // 10-microsecond pulse
digitalWrite(TRIG_PIN, LOW);
// Wait for the echo measurement to complete
if (pulseMeasured) {
// Calculate the duration of the pulse in microseconds
unsigned long pulseDuration = pulseEndTime - pulseStartTime;
// Convert the pulse duration to distance (using the speed of sound in air)
float distance = pulseDuration / 57.5; // Adjust this factor if needed for your sensor
// Print the measured distance to the Serial Monitor
Serial.print("Distance: ");
Serial.print(distance);
Serial.println(" cm");
// Reset the pulseMeasured flag for the next reading
pulseMeasured = false;
}
delay(70); // Ensure at least 70 ms between trigger pulses
}