Problem interfacing ultrasonic sensor (HC-SR04) with expander (pcf8574)

Hi!
I wonder how to connect ultrasonic sensor to arduino using an expander. The main problem is that the library “PCF8574.h” does not support the function pulseIn, which is necessary to get the value of time. Is it posible to do this? Maybe by using pulseIn function with address of the exact pin, but I don’t now how. Please help me :slight_smile:
Here is my code:

#define trig 2
#define echo 1

#include <Wire.h>    // Required for I2C communication
#include "PCF8574.h" // Required for PCF8574

/* PCF8574 instance */
PCF8574 expander;

void setup() {
   /* Setup serial for debug */
  Serial.begin(9600);
  delay(3000);
   /* Start I2C bus and PCF8574 instance */
  expander.begin(0x20);
    /* Setup some PCF8574 pins for demo */
  expander.pinMode(trig, OUTPUT);
  expander.pinMode(echo, INPUT);
 }
void loop() {
 int time, dist;
 expander.digitalWrite(trig, HIGH);
 delayMicroseconds(1000);
 expander.digitalWrite(trig, LOW);

// ?? THIS LINE ??
 time = expander.pulseIn(echo, HIGH);
// OR MAYBE LIKE THIS 
// time = pulseIn(0x20,HIGH);

 dist = (time/2)/29.1;
 Serial.println(dist);
 }

The PCF8574 is not very sutiable for this for several reasons. First of all it will not source much current only sink it so you can not connect it directly.
Then as it is an I2C interface you can not read the input very fast so it is no good for fine timing.
All in all forget it.

ok, thanks for your help. So I must to connect it directly to arduino.

Yes that is the best way.

Tel me, what can of pcf8574 module are you using? Are you using the generic kind that simply expend to an 8 bit bus + 1 interrupt, or are you using the lcd version?

And, could you try to use an interrupt on your arduino to capture the rising and falling edge of the ECHO pin, instead of the pulsin methode. Cause seriously, using the pulsing methode is as effective as blinking with a delay. :wink: