sparkfun / SparkFun_SX1509_Arduino_Library

Arduino library for the SX1509 16-I/O GPIO expander.
44 stars 38 forks source link

Digital outputs start high after initialization #7

Closed diosdog closed 2 years ago

diosdog commented 4 years ago

According to the SX1509 datasheet, digital outputs default to high on initialization. This is the opposite of what's typically expected for Arduino outputs. With the current library setup, it appears impossible to avoid this, as performing a digitalWrite prior to setting a pin as an output changes the pullup/pulldown settings instead of the RegData register value. Ideally, there would be a way to specify the initial value prior to enabling an output. I see several possible solutions:

  1. Support an optional extra argument to pinMode to specify starting output level.
  2. Add a separate function to specify default output level, to be called prior to setting a pin as an output.
  3. Check the pullup/pulldown state to determine initial output state, allowing setting of initial output level by setting pullup/pulldown state prior to pin direction change.
  4. Change to default low on startup by always writing bit in RegData to low whenever about to set a pin as an output, to match Arduino behavior (however, this might cause unexpected behavior for previous scripts using the library)
Bascy commented 2 years ago

I think this is already solved in commit a7c931753588d99679b6e1644b3bf8dd68da7d3f using your #1 suggestion

nseidle commented 2 years ago

Thanks @Bascy - I thought so. Please re-open if needed.