Hello, I have been really stuck with this issue for a few weeks now. Hopefully someone here can help me!
I’m currently using an Arduino Nano 33 Iot on Arduino Cloud IoT. Due to the complexity of my project, I would require more output pins and hence, got a PCF8574 extension board to get more pins. I’m using a switch as a control of the output in “Things” on Arduino Iot Cloud. When the switch is turned on, the variable “LED” will be true, and I want the PCF8574 to turn on the relay and when the switched is off, “LED” variable becomes false, thus turning off the relay.
I could achieve this with the GPIO pins from Arduino Nano 33 IoT itself, but from the PCF8574 I’m unable to achieve it. I’m thinking its due to the sinking of the current from the LED into my PCF8574 because my LED turns on without me turning on the relay, but how do I prevent this and still make it work like how I wanted it to?
Sketch generated by the Arduino IoT Cloud Thing "Shorting wires"
https://create.arduino.cc/cloud/things/117eb328-8ff3-4e25-a895-7c41783028ee
Arduino IoT Cloud Variables description
The following variables are automatically generated and updated when changes are made to the Thing
bool lED;
bool lED2;
Variables which are marked as READ/WRITE in the Cloud Thing will also have functions
which are called when their values are changed from the Dashboard.
These functions are generated with the Thing and added at the end of this sketch.
*/
#include "thingProperties.h"
#include <Wire.h>
#include "PCF8574.h"
#include "Arduino.h"
PCF8574 pcf8574(0x20);
void setup() {
// Initialize serial and wait for port to open:
Wire.begin();
Serial.begin(9600);
pinMode(2, OUTPUT);
pcf8574.pinMode(P0, OUTPUT);
//Wire.begin();
//Wire.beginTransmission(0x20);
//Wire.write(0xFF); // Set all pins as inputs initially
//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();
}
void onLEDChange() {
if (lED) {
pcf8574.digitalWrite(P0, LOW);
delay(500);
//Wire.beginTransmission(0x20);
//Wire.write(0xFE);
//Wire.endTransmission();
//delay(500);
}
else {
pcf8574.digitalWrite(P0, HIGH);
delay(500);
//Wire.beginTransmission(0x20);
//Wire.write(0xFF);
//Wire.endTransmission();
//delay(500);
}
}
void onLED2Change() {
if (lED2) {
digitalWrite(6, HIGH); //Pin 6 of arduino turns on relay
} else {
digitalWrite(6, LOW); //Pin 6 of arduino turns off relay
}
}
You don't have a common ground between the Nano/PCF circuit and the relay/transistor circuit. At least that's not clear from your wiring diagram. A real schematic diagram might be clearer. Hand-drawn on paper and photographed would be fine.
The PCF output may not be able to source enough current to fully switch on the NPN transistor sufficiently to energise the relay coil. PCF outputs can sink a good amount of current when set to LOW/0, but can only source a tiny current when set to HIGH/1. That tiny current may be enough to partially switch on the NPN and make the led glow, but not enough to energise the relay.
To fix 2, you could try using an n-channel MOSFET instead of the NPN, or you could try a PNP as a high-side switch to the relay.
For 1, apologies for the lack of information on my wiring diagram! For my Nano, PCF, relay and transistors, I connected all of them to a common ground on my breadboard, and the ground comes from my Nano. Hopefully that is what you meant?
Thank you for the detailed explanation, I would not have thought of all these much less use a MOSFET or PNP!
I would like to understand further on MOSFET and PNP after doing my own readings,
regarding the programming of my PCF, since the PCF sink high current when set to LOW and source little amounts of current when set to HIGH, when should I output a LOW and HIGH for my PCF to energise the relay?
Good. Where the ground "comes from" is arbitrary. The important thing is that both halves of the circuit share the same ground.
You can do it either way. With HIGH output, an n-channel MOSFET should get switched on. MOSFETs are switched on/off by voltage, unlike BJTs which are switched on/off by current.
Alternatively you can use a LOW output to energise the relay by using a PNP as a high-side switch.
The diagram in your post above shows that you do indeed need to do some reading on transistors! The current will not flow along the red arrows you drew, that's not how NPN work. NPN are switched on by a current flowing from the base to the emitter, not from the collector to the base, which is how PNP work.
EDIT: it would help if you could describe what kind of relay you are using. It is a bare relay and you have wired the transistor & led up as shown on your breadboard, or is it a relay module which contains those things? If it's the former, it's very important to include a flyback diode across the relay coil, otherwise other components can be damaged. A relay module will have the flyback diode.
I see, thank you for this! I will go take a look and try both of them out!
The diagram in your post above shows that you do indeed need to do some reading on transistors! The current will not flow along the red arrows you drew, that's not how NPN work. NPN are switched on by a current flowing from the base to the emitter, not from the collector to the base, which is how PNP work.
You are right! I'm sorry I wasn't sure what was going through my head when I was doing this.
EDIT: it would help if you could describe what kind of relay you are using. It is a bare relay and you have wired the transistor & led up as shown on your breadboard, or is it a relay module which contains those things? If it's the former, it's very important to include a flyback diode across the relay coil, otherwise other components can be damaged. A relay module will have the flyback diode.
[/quote]
I'm using a SONGLE SRS-5V-DC SL relay, yup I do have a flyback diode. Due to illustrating my circuit connection, I did not include the flyback diode in the diagram. I have attached a photo of it!
There's one thing I do not understand as well is, I used a digital multimeter and measure the pins of each of my PCF and they are all at 3.3V. Though in my code, i only specified P0 to be an output, all of the pins P1 - P7 could still light up my LED. Would there be anything wrong with me code?
Ummm... why isn't the Nano plugged into the breadboard? It should be the first thing you plug into the breadboard when you build a circuit. Unlike Uno and Mega, Nano is designed to be breadboard compatible!
Due to your inaccurate illustration, I wasted my time warning you about the importance of the flyback diode, which you already new
You need to do some more reading about how the PCF chip works.
Typically, an Arduino digital pin has 4 "modes". There is INPUT (the default), INPUT_PULLUP, OUTPUT with HIGH and OUTPUT with LOW. (Your particular model of Nano may also have INPUT_PULLDOWN.)
The PCF chip's pins have only 2 modes. They are equivalent to INPUT_PULLUP (the default) and OUTPUT with LOW.
If you connect an LED and resistor between an Arduino pin and ground and set the mode to INPUT_PULLUP, the led will glow, because of the internal pull-up resistor which has been enabled. The value of the internal pull-up resistor is quite high, so only a small current will flow and the LED will glow only weakly. The same thing is happening with the PCF pins.
Ummm... why isn't the Nano plugged into the breadboard? It should be the first thing you plug into the breadboard when you build a circuit. Unlike Uno and Mega, Nano is designed to be breadboard compatible!
Hahah you are right, when i first started out I was learning it as I go and I could look at the pin outs at the underside of the board, hence I didn't plug into the breadboard. I have always planned to do it and have done it!
Due to your inaccurate illustration, I wasted my time warning you about the importance of the flyback diode, which you already new
I'm sorry for that! Thank you though for checking on it still!
You need to do some more reading about how the PCF chip works.
Typically, an Arduino digital pin has 4 "modes". There is INPUT (the default), INPUT_PULLUP, OUTPUT with HIGH and OUTPUT with LOW. (Your particular model of Nano may also have INPUT_PULLDOWN.)
The PCF chip's pins have only 2 modes. They are equivalent to INPUT_PULLUP (the default) and OUTPUT with LOW.
If you connect an LED and resistor between an Arduino pin and ground and set the mode to INPUT_PULLUP, the led will glow, because of the internal pull-up resistor which has been enabled. The value of the internal pull-up resistor is quite high, so only a small current will flow and the LED will glow only weakly. The same thing is happening with the PCF pins.
Do you mean that the PCF has a high internal pull-up resistor? I might have read that somewhere but i was confused about the information.
I have never tried the mode INPUT_PULLUP before as setting it as OUTPUT felt more intuitive to me and given my current project context.