Hi all!
I'm coming back to Arduino and I'm reviewing my first project in where, with a lot of helps from others on the web, I managed to code a program in where 4 temp sensors are read and, depending on the average temp of a certain amounts of readings, 4 relays are opened or closed.
Right now i'm using this code to understand basic concepts which I didn't back then (I focused on the results instead of understanding the logic) and with that in mind i'm commenting all lines once I understand what are they doing.
So, I'm kind of confused here:
for (int i = 0; i < 4; i++) {
pinMode(relayPin[i], INPUT_PULLUP); //Sets the relay pins as pullups
pinMode(relayPin[i], OUTPUT); // defaults HIGH, relay OFF
}
First, being these relays, are the pullup needed? Would floating affect the relays if I didn't set the pins to a known state? I thought pullup were only needed when reading, not when used ad outputs. What would happen if I didn't use them?? Would the relays open and close randomly?
Second, having in mind using pullup instead of pulldown goes "against" logic, setting HIGH as Open and LOW as Closed, shouldn't it better to use pulldown?
Third, if I turn on the pullups with INPUT_PULLUP, wouldn't using OUTPUT overwrite this?
and if it doesn't, why there's no OUTPUT_PULLUP instruction? (This kinds of refers back to my first question about using pullup for outputs.)
Outputs cannot float. They are either HIGH (source current) or LOW (sink current). If you see a pull up or pull down resistor on an output, the resistor is probably there to keep the pin in a known state while the pin is an input (default state) during reset. The pull down on a MOSFET gate is an example of that use.
Thank you for the answers.
Let me copy the entire code to avoid any misunderstanding
const byte tempPin[] = {A1, A2, A5, A4}; //defining the LM35 temp sensors input pins as constant byte to avoid changing it. Value from 0 to 255
const byte relayPin[] = {6, 7, 8, 9}; //defining the relay output pins as constant byte to avoid changing it. Value from 0 to 255
// hysteresis = upperLimit - lowerLimit
const byte lowerLimit = 28;
const byte upperLimit = 32;
float tempC[4]; //creating the 4 temp inputs inside an array. Float to deal with decimals
const int numReadings = 25; // defining the number of readings to create an average with
word printInterval = 1000; // 1 second
unsigned long lastPrintTime = 0;
void setup()
{
analogReference(INTERNAL); //Using the internal 1.1v reference for better resolution on the analog pin readings
Serial.begin(115200); // Setting 115200 baud ratio for communication
for (int i = 0; i < 4; i++) { //starting with i = 0 and as long as i is less than 4, go through the loop and then add 1 to i value
pinMode(relayPin[i], INPUT_PULLUP); //Sets the relay pins as pullups. This is to avoid floating state for the pins.
pinMode(relayPin[i], OUTPUT); // sets the relay pins as outputs, defaults state HIGH, relay OFF
}
}
void loop()
{
// readings and control
for (int i = 0; i < 4; i++) {
float raw_temp = analogRead(tempPin[i]) / 9.31;
tempC[i] += (raw_temp - tempC[i]) / numReadings;
if (tempC[i] < lowerLimit) {
digitalWrite(relayPin[i], LOW); //relay ON
}
else if (tempC[i] > upperLimit) {
digitalWrite(relayPin[i], HIGH); // relay OFF
}
}
if (millis() - lastPrintTime >= printInterval) {
for (int i = 0; i < 4; i++) {
Serial.print("tempC");
Serial.print(i + 1);
Serial.print(" ");
Serial.println(tempC[i]);
}
Serial.println();
lastPrintTime = millis(); // reset print timer
}
}
Am I using the input_pullup for nothing, then? Because that is stated in the setup, so there's no reset involved.
Output then doesn't have a floating state and I shouldn't use it?
pinMode(relayPin[i], INPUT_PULLUP); //Sets the relay pins as pullups. This is to avoid floating state for the pins.
pinMode(relayPin[i], OUTPUT); // sets the relay pins as outputs, defaults state HIGH, relay OFF
Using the pinMode command, you can't have both. INPUT_PULLUP is overwritten by OUTPUT.
How are the relays wired? From Arduino Pin to Ground? How many relays are there? How much current does each on require?
When using the type of relay board that energizes the relay on a LOW input, you should always set the port pins HIGH before pinModing to OUTPUT so there is not a very short ON time before the pin is set to OUTPUT.
digitalWrite(relay1,HIGH);
digitalWrite(relay2,HIGH);
pinMode(relay1,OUTPUT);
pinMode(relay2,OUTPUT);
outsider:
When using the type of relay board that energizes the relay on a LOW input, you should always set the port pins HIGH before pinModing to OUTPUT so there is not a very short ON time before the pin is set to OUTPUT.
digitalWrite(relay1,HIGH);
digitalWrite(relay2,HIGH);
pinMode(relay1,OUTPUT);
pinMode(relay2,OUTPUT);
I see, thanks, although you are saying that the HIGH/LOW Open/Closed status relationship depends on the relay board?
I thought that depended on the Arduino Board and Code.
adwsystems:
pinMode(relayPin[i], INPUT_PULLUP); //Sets the relay pins as pullups. This is to avoid floating state for the pins.
pinMode(relayPin[i], OUTPUT); // sets the relay pins as outputs, defaults state HIGH, relay OFF
Using the pinMode command, you can't have both. INPUT_PULLUP is overwritten by OUTPUT.
How are the relays wired? From Arduino Pin to Ground? How many relays are there? How much current does each on require?
1 module of 4 relays opto wired like this:
So basically I should remove the INPUT_PULLUP from there because it has no purpose?
I see, thanks, although you are saying that the HIGH/LOW Open/Closed status relationship depends on the relay board?
The relays may need to be driven HIGH or LOW in order to be energised. Which it is depends on the design of the relay board. It is common for them to be energised when their input is LOW.
The suggested way to initialise such relays is to set their inputs to HIGH before setting the pinMode() thus ensuring that they are not briefly energised when the pinMode() is applied.
Of course, all of this depends on how the relay inputs are wired and how their outputs are wired. If the relays have changeover outputs then you may decide to have the relays start energised and deal with the outputs appropriately. That, however, will mean that when the Arduino is turned off the normally closed relay contacts will pass current. Whether this is desirable depends on the application.
digitalWrite(pin,HIGH) does the same thing as pinMode(pin,INPUT_PULLUP) with an input pin, you're OK as is, but I would use the former in your case, just looks better IMHO.
outsider:
digitalWrite(pin,HIGH) does the same thing as pinMode(pin,INPUT_PULLUP) with an input pin, you're OK as is, but I would use the former in your case, just looks better IMHO.
Great to know, although it's only needed for inputs and not for outputs, right?
Thanks
When the Aduino starts up, all pins are inputs by default, if you want, say, pin 4 to be an input, you don't have to set it as input, just hook up and go. But if you want to use the the built in pullup resistor you have to pinMode(4, INPUT_PULLUP), before INPUT_PULLUP was invented you had to digitalWrite(4,HIGH). Now, if you want an output pin to be HIGH when your program starts, you have to first set the pin HIGH, THEN pinMode(pin,OUTPUT), other wise the pin will be LOW when the program starts, turning on a LOW true relay or something until the program gets around to turning it off by writing the pin HIGH.