Portenta Ultrasonic RangeFinder HC-SR04

Portenta Ultrasonic RangeFinder HC-SR04

Has anyone got a RangeFinder working with the Portenta?

Here is my code, but I just get zero's. I am testing if a 1 Kohm resistor is needed between the Echo Pin and the Portenta.

int myTriggerPin = 7;
int myEchoPin = 6;
unsigned long myDuration;

void setup() {
  Serial.begin(115200);
  pinMode(myTriggerPin, OUTPUT);
  pinMode(myEchoPin, INPUT_PULLDOWN);
}

void loop() {
  digitalWrite(myTriggerPin, LOW);
  delayMicroseconds(10); 
  digitalWrite(myTriggerPin, HIGH);
  delayMicroseconds(10); 
  digitalWrite(myTriggerPin, LOW);
  myDuration = pulseIn(myEchoPin, HIGH, 40000UL);
  Serial.println(myDuration);
  delay(10);
}


I filed an issue about pulseIn() on the mbed github, but Arduino proved pulseIn works. I expect I am messing something up.

Is the Portenta a 5V board? The HC-SR04 operating voltage is 5V.

Nope, but it has a 5V output pin. I have also tried a 1 k ohm resistor at the echo pin, but still get zeros. The rangeFinders work fine with other boards. (Yes, I am capable of really stupid errors, that is why I repeat things from scratch many times. )

Yes the 5V pin should be used thanks @groundFungus. Still nothing working with the Portenta and this range finder. Anyone else have any suggestions.

Wondering if other pins would be better to try.

I tried it again and this code worked fine.





// Note: VCC on RangeFinder must go to +5V on Portenta
//         GND on rangefinder goes to GND on Portenta 
         
int myTriggerPin = D6;  // Trigger on RangeFinder
int myEchoPin = D7;     // Echo on Rangefinder  
unsigned long myDuration;

void setup() {
  Serial.begin(115200);
  pinMode(myTriggerPin, OUTPUT);
  pinMode(myEchoPin, INPUT_PULLDOWN);
}

void loop() {
  digitalWrite(myTriggerPin, LOW);
  delayMicroseconds(10); 
  digitalWrite(myTriggerPin, HIGH);
  delayMicroseconds(10); 
  digitalWrite(myTriggerPin, LOW);
  myDuration = pulseIn(myEchoPin, HIGH, 40000UL);
  Serial.println("Duration us: "+ String(myDuration));
  delay(200);
  if (myDuration > 2000    ){       // raw data from 200 to 16000                                          
                                        // where  2000 raw = ~35cm,  4000 raw = ~80cm                                    
       digitalWrite(LEDB, LOW);    // LEDB Blue LED on if far 
    } else { 
        digitalWrite(LEDB, HIGH);   // LEDB Blue LED off if close or nothing
    }  
}