如果您對 PCF8574 模組還不熟悉,建議您先參閱
「實做 PCF8574 模組的輸出入功能(一)」 http://pizgchen.blogspot.com/2019/03/arduino-pcf8574.html
接線
Arduino PCF8574 模組
VCC VCC
GND GND
A4 SDA
A5 SCL
將 2 個 PCF8574 模組串接,並調整第二個模組的跳線帽,第一個模組的位址是 0x38,第二個模組的位址是 0x39,如下圖。
第一個模組
第二個模組
兩個模組串接
實作範例一
這個實驗用 Arduino 讓第一個模組的 pin0 腳位和第二個模組的 pin7 腳位 LED 每秒閃一次,所以您還應該在那 2 個腳位設置 LED 電路,然後您可以看到閃爍的 LED,並在 Arduino 的序列視窗看到腳位的輸出狀態。
程式碼
#include "HCPCF8574.h" //Include the HCPCF8574 library
#define I2C_ADD1 0x38 //I2C address of the first PCF8574 device
#define I2C_ADD2 0x39 //I2C address of the second PCF8574 device
HCPCF8574 Port1(I2C_ADD1); //Create an instance of the library to control the first device
HCPCF8574 Port2(I2C_ADD2); //Create an instance of the library to control the second device
void setup()
{
Serial.begin(9600);
Port1.init(); //Initiliase the first PCF8574 (all pins are set to high)
Port2.init(); //Initiliase the second PCF8574 (all pins are set to high)
Port1.pinMode(0, OUTPUT); //Set digital pin 0 on the first device to an output
Port2.pinMode(7, OUTPUT); //Set digital pin 7 on the second device to an output
}
void loop()
{
Serial.println("Port1 pin0 & Port2 pin7 is HIGH");
Port1.pinWrite(0, HIGH); //Set digital pin 0 on the first device high
Port2.pinWrite(7, HIGH); //Set digital pin 7 on the second device high
delay(1000); //Wait 1 second
Serial.println("Port1 pin0 & Port2 pin7 is LOW");
Port1.pinWrite(0, LOW); //Set digital pin 0 on the first device low
Port2.pinWrite(7, LOW); //Set digital pin 7 on the second device low
delay(1000); //Wait another second
}
實作範例二
這個實驗用 Arduino 讀取 2 組 PCF8574AT 模塊的 P0~P7 腳位信號,所以您還應該在那些腳位總共設置 16 個按鈕電路,然後可以在 Arduino 的序列視窗顯示按鈕的開關狀態。
程式碼
#include "HCPCF8574.h" //Include the HCPCF8574 library
#define I2C_ADD1 0x38 //I2C address of the first PCF8574 device
#define I2C_ADD2 0x39 //I2C address of the second PCF8574 device
HCPCF8574 Port1(I2C_ADD1); //Create an instance of the library to control the first device
HCPCF8574 Port2(I2C_ADD2); //Create an instance of the library to control the second device
void setup()
{
Serial.begin(9600);
Port1.init(); //Initiliase the first PCF8574 (all pins are set to high)
Port2.init(); //Initiliase the second PCF8574 (all pins are set to high)
for (int i=0; i<8 ; i++) {
Port1.pinMode(i, INTPUT); //Set digital pin 0 on the first device to an output
Port2.pinMode(i, INTPUT); //Set digital pin 7 on the second device to an output
}
}
void loop()
{
boolean State10 = Port1.pinRead(0); //Read the port1 of digital pin 0
boolean State11 = Port1.pinRead(1); //Read the port1 of digital pin 1
boolean State12 = Port1.pinRead(2); //Read the port1 of digital pin 2
boolean State13 = Port1.pinRead(3); //Read the port1 of digital pin 3
boolean State14 = Port1.pinRead(4); //Read the port1 of digital pin 4
boolean State15 = Port1.pinRead(5); //Read the port1 of digital pin 5
boolean State16 = Port1.pinRead(6); //Read the port1 of digital pin 6
boolean State17 = Port1.pinRead(7); //Read the port1 of digital pin 7
boolean State20 = Port2.pinRead(0); //Read the port2 of digital pin 0
boolean State21 = Port2.pinRead(1); //Read the port2 of digital pin 1
boolean State22 = Port2.pinRead(2); //Read the port2 of digital pin 2
boolean State23 = Port2.pinRead(3); //Read the port2 of digital pin 3
boolean State24 = Port2.pinRead(4); //Read the port2 of digital pin 4
boolean State25 = Port2.pinRead(5); //Read the port2 of digital pin 5
boolean State26 = Port2.pinRead(6); //Read the port2 of digital pin 6
boolean State27 = Port2.pinRead(7); //Read the port2 of digital pin 7
Serial.print(State10); Serial.print(State11); Serial.print(State12); Serial.print(State13);
Serial.print(State14); Serial.print(State15); Serial.print(State16); Serial.print(State17);
Serial.println("Port2=");
Serial.print(State20); Serial.print(State21); Serial.print(State22); Serial.print(State23);
Serial.print(State24); Serial.print(State25); Serial.print(State26); Serial.print(State27);
delay(1000); //Wait another second
}
採購資訊
PCF8574AT I/O擴展模塊 https://goods.ruten.com.tw/item/show?21908917689628
可輸入類比訊號嗎?
回覆刪除