Hi!
I would like to use the PCF8574 mod to use additional i/o ports, to imitate the switches (practically, you have to pull down the probe to GND) of a BTaudio module, but this code (NEXT - PLAY - PREW - VOLUME) does not work (Relay is work fini)
It works fine on Arduino pins, but not on PCF8574 pins (P0-P7).
Do you have an idea for a solution?
// NEXT - PLAY - PREW - VOLUME
void button(int pin, int msec) {
pinMode(pin, OUTPUT);
digitalWrite(pin, LOW);
delay(msec);
pinMode(pin, INPUT);
digitalWrite(pin, LOW);
}
// RELAY
void relay(int pin, int level) {
digitalWrite(pin, level);
}
Do you find the PCF8574 with the I2C scanner, if not you have a wiring mistook or a bad part. If so you cannot control the outputs like you do on the Arduino. The outputs sink enough current to drive a LED but source very little. To use the pins as an input you need to program thes as '1' then read them. If this does not get you through it post an annotated schematic showing all connections, power, ground and power source(s). Note a frizzy drawing is a wire layout picture and does not substitute for a schematic. Links to the parts would help but Azon etc are useless.
The wiring is fine because the BTSATE is working properly.
Button 3 of the audio module should be at 3.3 V by default (but in this case it is at 4.8 V if I connect it to the pcf8574). It should go down to GND, but this does not happen and the next, play, prew functions do not work.
If I transfer the connections to the Arduino Uno's own pins, everything works fine.
So I think the PCF8574 module can't handle it properly or something else needs to be written in the source code.
#include "Arduino.h"
#include "PCF8574.h"
// Set i2c address
PCF8574 pcf8574(0x21);
// XY-WRBT Bluetooth Hangmodul
#define pinNext P0 // NEXT & VOL +
#define pinPlay P1 // PLAY
#define pinPrew P2 // BACK & VOL -
#define BTSTATE P3 // Relay modul
void setup()
{
Serial.begin(115200);
delay(1000);
Serial.print("Init pcf8574...");
if (pcf8574.begin()) {
Serial.println("OK");
} else {
Serial.println("KO");
}
}
void loop()
{
if(Serial.available()) {
char Android=Serial.read();
if(Android=='K') { // Next
button(pinNext,100);
Serial.print("Next");
Serial.print(";");
}
if (Android=='E') { // Prew
button(pinPrew,100);
Serial.print("Prew");
Serial.print(";");
}
if (Android=='M') { // Play-Pause
button(pinPlay,100);
Serial.print("Play-Pause");
Serial.print(";");
}
if (Android=='F') { // Volume +
button(pinNext,1000);
Serial.print("Volume +");
Serial.print(";");
}
if (Android=='L') { // Volume -
button(pinPrew,1000);
Serial.print("Volume -");
Serial.print(";");
}
if (Android=='P') { // Program
pcf8574.digitalWrite(BTSTATE, HIGH);
MODE = "PROG";
Serial.print(MODE);
Serial.print(";");
}
if (Android=='S') { // State
pcf8574.digitalWrite(BTSTATE, LOW);
MODE = "STATE";
Serial.print(MODE);
Serial.print(";");
}
}
}
void button(int pin, int ptm) {
pcf8574.pinMode(pin, OUTPUT);
pcf8574.digitalWrite(pin, LOW);
delay(ptm);
pcf8574.pinMode(pin, INPUT);
pcf8574.digitalWrite(pin, LOW);
}
You are aware that a PCF8574 differs from an Arduino pin in that it can really only sink current?. If you attempt to source something, you get a maximum of about 100uA.
The contacts of the Audio module are at 3.3 V during normal use, when I press one of the buttons, it pulls the current pin down to GND, so it steps a song, etc.
Connection to PCF8574: +5V, GND, P0, P1, P2.
If I connect it, the contacts will be at around 4.8 V and will not go to GND or they will all go to GND.
It looks like you have the ideal situation for the PCF8574 in that you need to sink a connected pin to ground.
If I connect it, the contacts will be at around 4.8 V and will not go to GND or they will all go to GND.
Try the Adafruit library. There are examples where the pin is set to an OUTPUT and set LOW so as to sink current and ground the connected external pin.
Have you actually constructed you prototype?
Have you tried the example codes to see how it operates?
Have you got 4K7 or 10K pullup resistors on the SDA and SCL lines?
How have you got the Address pins configured?
The pasted sketch works if I rewrite the min and move it to the Arduiono pins 4,5,6 and delete the pcf8574. part. So, the connections are good.
The problem is that the pins of the pcf8574 module do not have 3.3 V, but 4.8 V.
These 2 lines should release 4.8 V, as Pin 3,4,5 does.
If you need 3.3 volts on the pcf8574, then connect its Vcc to 3.3 volts and not 5 volts. Its output pins have a very high resistance pull up resistor to Vcc active when the pins are not explicitly forced to LOW.
I have plugged PCF8574 modul to Arduino 3.3 V. On the Pins (P0-P2) measured 3.8 V.
GND to Ar. GND (Blue)
VCC to Ar. 3.3V (Red)
SDA to Ar. A4 (Orange)
SCL to Ar. A5 (Yellow)
When i add to setup this some lines, the P2 is work (pinPrew), but only this. But, P0 and P1 not work.
Try to experiment with some Leds. Use a current limiting resistor for each (around 220 to 1k ohms). Wire the leds between Vcc and the PCF8574 port pins.
2: Try to understand how a PCF8574 works. It has only 2 states equivalent to an Arduino's Input pullup and Output LOW. That is it if you are using the port for output. If you are using for input, first ensure that an Output Low is not active on that port.
To set a port low, which in your case switches a device on, simply try say pcf8574.digitalWrite(P0, LOW).
To set a port high (weak high) try say pcf8574.pinMode(P0, INPUT)