I bought this and all is working fine. I have tried the write command and it works. But read command is not working is this correct ?
#include "mbed.h"
#include "PCF8574.h"
PCF8574 io(p9,p10,0x40);
int main() {
int a;
while (1){
if ( io.read() == 0x65)
{
io.write(0x64);
}}
}
Basically if a button is pressed at 5th pin a led should glow at 4th pin.. I have tried connecting the other pin of the button to +3.3v and ground, but nothing seem to work.
I bought this and all is working fine. I have tried the write command and it works. But read command is not working is this correct ?
<<code>>
#include "mbed.h"
#include "PCF8574.h"
PCF8574 io(p9,p10,0x40);
int main() {
int a;
while (1){
if ( io.read() == 0x65)
{
io.write(0x64);
}}
}
<</code>>
Basically if a button is pressed at 5th pin a led should glow at 4th pin.. I have tried connecting the other pin of the button to +3.3v and ground, but nothing seem to work.
Do you have pull-ups or pull-downs connected to the inputs of the PCF8574?
Lerche
You mean at the 5th pin ? Then no I have not connected any pull up or pull down. Do I have to connect ?
P.S. I have connected 2.5k pull up to SDA and SCL.
<<quote Lerche>>
Do you have pull-ups or pull-downs connected to the inputs of the PCF8574?
Lerche
<</quote>>
You mean at the 5th pin ? Then no I have not connected any pull up or pull down. Do I have to connect ?
P.S. I have connected 2.5k pull up to SDA and SCL.
The 5th pin of the chip, or the 5th bi-directional port-pin?
Please draw a simple schematic.
That would be much easier to understand.
What do you mean by 5th pin?
The 5th pin of the chip, or the 5th bi-directional port-pin?
Please draw a simple schematic.
That would be much easier to understand.
The 5th pin of the chip, or the 5th bi-directional port-pin?
Please draw a simple schematic.
That would be much easier to understand.
Yes its the 5th bi-directional port-pin
So if i press the button at P5, the led should glow at P4. I need to know what should be connected at A.
Thanks
<<quote Lerche>>
What do you mean by 5th pin?
The 5th pin of the chip, or the 5th bi-directional port-pin?
Please draw a simple schematic.
That would be much easier to understand.
<</quote>>
Yes its the 5th bi-directional port-pin
{{http://i.imgur.com/spT3Q.png}}
So if i press the button at P5, the led should glow at P4. I need to know what should be connected at A.
Thanks
Have you put pull-down/pull-up on the other pins as well?
This is probably necessary, as when reaqding the input, those pins must not hang.
Put 100k pull-down on all of the inputs, then this should take care of it.
Lerche
{{/media/uploads/Lerche/pcf_1.gif}}
Here's the schematic I'd use.
Have you put pull-down/pull-up on the other pins as well?
This is probably necessary, as when reaqding the input, those pins must not hang.
Put 100k pull-down on all of the inputs, then this should take care of it.
Lerche
You dont really need a pullup on input pins. The PCF8574 has internal weak pullups. However, you do need to write a '1' to every bit that you want to read. The weak pullup will then allow you to read a '1' when the switch is open or read a '0' when the switch is closed. When you have previously written a '0' to a port pin you will always read back a '0' no matter what the switch state is or whether or not you have an external pull up installed.
I would also mask and test the specific bit on P5 rather that just compare with a hardcoded value. That way you can later add more switches or LEDs without changing the test.
Christian is right about the series R for the LED, you need that to limit the max current.
You dont really need a pullup on input pins. The PCF8574 has internal weak pullups. However, you do need to write a '1' to every bit that you want to read. The weak pullup will then allow you to read a '1' when the switch is open or read a '0' when the switch is closed. When you have previously written a '0' to a port pin you will always read back a '0' no matter what the switch state is or whether or not you have an external pull up installed.
I would also mask and test the specific bit on P5 rather that just compare with a hardcoded value. That way you can later add more switches or LEDs without changing the test.
<<code>>
if ( (io.read() & 0x20) == 0x00) {
// P5 switch pressed
}
<</code>>
Christian is right about the series R for the LED, you need that to limit the max current.
The breadboarded circuit is as given by Christian Lerche, with a resistor added in series with the LED but without the 100k resistors on the unused inputs.
I've posted sample code and a video of a working circuit at http://romillys-robots.blogspot.com/2012/03/pcf8574-i2c-and-mbed.html
The breadboarded circuit is as given by Christian Lerche, with a resistor added in series with the LED but without the 100k resistors on the unused inputs.
The breadboarded circuit is as given by Christian Lerche, with a resistor added in series with the LED but without the 100k resistors ion the unused inputs.
Thanks it worked :)
<<quote romilly>>
I've posted sample code and a video of a working circuit at http://romillys-robots.blogspot.com/2012/03/pcf8574-i2c-and-mbed.html
The breadboarded circuit is as given by Christian Lerche, with a resistor added in series with the LED but without the 100k resistors ion the unused inputs.
<</quote>>
Thanks it worked :)
Please login to post comments.