Using Arduino IoT Cloud to control PCF8574 as output

I'm currently using an Arduino Nano 33 Iot, however, due to my project being large scale I would require more GPIO pins for output control of my LED lights. I have decided to use the PCF8574 expansion module and it will be connected to a npn transistor to control my relay, however, when I turn on/off my LED light (Arduino Things), the PCF8574 doesn't output the voltage and current to turn on my relay.

My code below:

  bool lED;
  bool lED2;
  bool lED3;
  bool lED4;
  bool lED5;

#include "thingProperties.h"
#include <Wire.h>

int PCF_Output = 0x20;
byte PCF8574OUTPUT = B00000000; //sets a variable to be used for logic operator

void setup() {
  // Initialize serial and wait for port to open:
  Wire.begin();
  
  Serial.begin(9600);
  pinMode(2, OUTPUT);
  pinMode(3, OUTPUT);
  pinMode(6, OUTPUT);
  pinMode(7, OUTPUT);
  pinMode(9, OUTPUT);
 
  pinMode(A0, OUTPUT);
  pinMode(A1, OUTPUT);
  

  Wire.beginTransmission(PCF_Output);
  Wire.write(0x00);
  Wire.endTransmission();
  
  delay(1500); // This delay gives the chance to wait for a Serial Monitor without blocking if none is found
  

  // Defined in thingProperties.h
  initProperties();

  // Connect to Arduino IoT Cloud
  ArduinoCloud.begin(ArduinoIoTPreferredConnection);

  setDebugMessageLevel(2);
  ArduinoCloud.printDebugInfo();
}

void loop() {
  ArduinoCloud.update();

// int voltage_2 = analogRead(A1);
// core_2_Value = voltage_2 * (5.0/1024.0);
 
 // delay(10);
}

void onLEDChange()  {
  Serial.println(lED);
  if (lED) {
    digitalWrite(A0, HIGH);
    
    Wire.beginTransmission(PCF_Output);
    Wire.write(0x01);
    Wire.endTransmission();
    
    delay(500);
    
  }
  else {
    digitalWrite(A0, LOW);
    
    Wire.beginTransmission(PCF_Output);
    Wire.write(0x00);
    Wire.endTransmission();
    
    delay(500);
  }
}

void onLED2Change()  {
  if (lED2){
    digitalWrite(6,HIGH);
  }else {
    digitalWrite(6,LOW);
  }
  }

image

The onLEDChange() is linked to Core 1 LED variable in Things, by toggling the LED I would like my PCF8574 to turn either on and off when the LED is true and false respectively.

Hi @ntu-spgroup_fyp. Have you tried running a simple sketch that only controls the PCF8574 directly, without any Arduino IoT Cloud code at all in the sketch?

If so, did it work as expected?

If not, I recommend you try that first before introducing the significant additional complexity of Arduino IoT Cloud into the system.

Hi @ptillisch , thanks for the advice! I went to create a simple LED light up between the P0 and P1 of from the PCF8574 without any controls, I have realised the code of my PCF8574 doesn't run within the void loop (). I'm not sure whether if I'm doing anything wrong?

#include "thingProperties.h"
#include "Arduino.h"
#include <Wire.h>

int PCF_Output = 0x20;

void setup() {

  Wire.begin();
  
  Serial.begin(9600);
  
  Wire.beginTransmission(0x20);
  Wire.write(0xFF);
  Wire.endTransmission();
  
  delay(1500); // This delay gives the chance to wait for a Serial Monitor without blocking if none is found
  
  // Defined in thingProperties.h
  initProperties();

  // Connect to Arduino IoT Cloud
  ArduinoCloud.begin(ArduinoIoTPreferredConnection);

  setDebugMessageLevel(2);
  ArduinoCloud.printDebugInfo();
}

void loop() {
  ArduinoCloud.update();

  Wire.beginTransmission(0x20);
  Wire.write(0xFE);
  Wire.endTransmission();
    
  delay(500);
  
  Wire.beginTransmission(0x20);
  Wire.write(0xFD);
  Wire.endTransmission();
    
  delay(500);
}

I did some testing and found that the LED lights up only when the PCF8574 is set low (eg 0). So i initialised it to high (0xFF) to off it and to only turn P0 (0xFE) on and P1 (0xFD) on within the loop(). However, it is not working as i intended it to.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.