pulseIn only working with HC-SR04 when connected to A0 pin of Portenta Breakout

Hey, I've been working with an HC-SR04 sensor connected to a Portenta breakout with a Portenta H7 mounted. Whenever I attach the echo pin of the HC-SR04 to anything but A0, I get a continuous stream of 0's (Many a second, hard to count the rate exactly). When I connect to A0, I get the expected result in centimeters (Still many times a second), and I can measure variable distances.

I've tried adding delays, using INPUT_PULLIN, adding a longer timeout in the pulseIn, but nothing happened. I added a while loop that looped until non zero output, but got no output whatsoever. I would think the sensor is giving no output, but the code works fine with A0, which makes me think that the sensor has to be giving output.

I've tested all of the GPIO pins, All Analog Pins, and all PWM pins but no luck. I've also tried connecting to pins directly on the H7. Any ideas as to why only A0 would work and how to work with the other pins?

#include <Arduino_PortentaBreakout.h>
// I've tested many other echoes, but A0 is the only working echo - A6 works as trigger but not as echo
const int trigPin = A6;
const int echoPin = A0;

float duration, distance;

void setup() {
  Serial.begin(9600);
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
}

void loop() {
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);
  duration = pulseIn(echoPin, HIGH);
  distance = (duration*.0343)/2;
  Serial.print("Distance: ");
  Serial.println(distance);
}

As you failed to provide a wiring diagram I simply assume you connected the sensor to the Portenta as shown on this page.
If you done that you may have fried most of your Portenta pins with a 5V voltage. Hope that the short pulse was taken by the flyback diodes and the Portenta still lives.
Use at least a voltage divider, better a level converter!

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.