Arduino
Il primo sketch mostrerà come comandare le uscite dell’integrato PCF8575.
Per visualizzare lo stato delle singole uscite, ho collegato un LED ad ognuna di esse. L’integrato non è in grado di fornire (modalità source) sufficiente corrente per alimentare un LED, è quindi necessario un collegamento in modalità sink, ovvero con la corrente che “entra” nel PIN:

In questo caso quindi il LED sarà acceso quando il PIN sarà a massa, ovvero a livello logico 0.
Vediamo come comandare le uscite:
- scriviamo, partendo dall’uscita P7 fino all’uscita P0, lo stato desiderato (0 oppure 1), ad esempio: 10110000;
- facciamo lo stesso per le uscite dalla P17 alla P10: 10110000 01011101;
- (per comodità) convertiamo le due parti in esadecimale: B0 5D;
- inviamo i due valori trovati all’integrato.
Il bus I2C consente di collegare più dispositivi; perché i comandi arrivino al dispositivo corretto, è necessario introdurre il concetto di indirizzo. Ogni dispositivo I2C ha un indirizzo di fabbrica; per il chip PCF8575 tale indirizzo è 0×20 (32 in decimale). Collegando a massa o a Vcc i PIN A0, A1 e A2 è possibile modificare tale indirizzo:
Se ad esempio colleghiamo A0 a massa e A1, A2 a Vcc, l’indirizzo sarà 0100110 ovvero 0×26. La modifica dell’indirizzo di fabbrica consente di collegare più integrati dello stesso tipo ad un singolo bus I2C.
Una volta stabilito l’indirizzo, possiamo inviare i comandi con il seguente codice:
#include <Wire.h> #define PCF8575_ADDRESS 0x20 ... Wire.beginTransmission(PCF8575_ADDRESS); Wire.write(0xB0); Wire.write(0x5D); Wire.endTransmission(); |
Utilizzando gli stessi comandi, ho preparato uno sketch che accende a rotazione i 16 LED:
Nella prossima pagina vedremo un esempio di utilizzo di entrambi i moduli…










I get :error:
PCF8575_Led_rolling_Demo.cpp: In function ‘void loop()’:
PCF8575_Led_rolling_Demo.cpp:24:8: error: ‘class TwoWire’ has no member named ‘write’
PCF8575_Led_rolling_Demo.cpp:25:8: error: ‘class TwoWire’ has no member named ‘write’
I able to compile by change it for
Wire.write(low);
Wire.write(high);
to
Wire.send(low);
Wire.send(high);
but still now a blink from any of the LED’s. Are you using any resistor? I”ve tryed it using just the pcf8575 anf an arduino board, no RTC module. Is that doable and if so what I’m I doing wring?
I get :error:
PCF8575_Led_rolling_Demo.cpp: In function ‘void loop()’:
PCF8575_Led_rolling_Demo.cpp:24:8: error: ‘class TwoWire’ has no member named ‘write’
PCF8575_Led_rolling_Demo.cpp:25:8: error: ‘class TwoWire’ has no member named ‘write’
I able to compile by change it for
Wire.write(low);
Wire.write(high);
to
Wire.send(low);
Wire.send(high);
but still now a blink from any of the LED’s. Are you using any resistor? I”ve tryed it using just the pcf8575 anf an arduino board, no RTC module. Is that doable and if so what I’m I doing wrong?
Hi
You’re probably using an older (< 1.0) version of Arduino IDE… anyway you don’t need a RTC module for this example, could you post your connections so I can check?
Cool demo. I have some questions http://arduino.cc/forum/index.php/topic,100097.msg750764.html#msg750764
Hello!
I’m trying to build a circuit with a RTC and a PCF8575 .
It happens that the RTC does not tell the correct time.
There must be some conflict.
The code I’m using to test is this:
#include // Librarie from Henning Karlsen
#include
DS1307 rtc(20, 21); // Indica em quais pinos o RTC está conectado.
static byte tabela1[] = {1,2,4,8,16,32,64,128};
static byte tabela2[] = {128,64,32,16,8,4,2,1};
long millis_antes = 0;
void set(byte b0,byte b1)
{
Wire.beginTransmission(0×20);
Wire.write(b0);
Wire.write(b1);
Wire.endTransmission();
}
void setup()
{
Serial.begin(38400);
rtc.halt(false); // Inicia o funcionamento do RTC.
Wire.begin();
//Todos os pinos OFF
set(0,0);
}
void loop()
{
if(millis() – millis_antes > 5000)
{
millis_antes = millis();
Serial.print(“Horario: “);
Serial.println(rtc.getTimeStr(FORMAT_LONG));
}
for(int i = 0; i 0; i–)
{
set(tabela1[i],tabela2[i]); // Apenas o pino 1 ON.
delay(100);
}
set(255,255); // Todos os pinos ON
delay(200);
}
The LEDs blink correctly, but the time printed on the serial monitor is:
Horario: 27:85:85
I have tested your code and the same thing occurs.
Any suggestions on what might cause the problem?
Tank you.
Fernando Garcia
Sorry for english.
The code does not appear correctly.
Here’s a link to it.
https://gist.github.com/21dfdc057cfbd1cc44a9/5e7c42002b4025b9d48a21896d81c9a6bd74cbba
Best regards.
Hi Fernando,
are you using an Arduino Mega?