[SOLVED] HIGH and LOW Questions

Hello everyone.

I recently purchased an Adruino UNO to play around with IoT and for use in learning a bit about PLCs.

I bought an 8-relay module to get the feel for things.

I wrote a small sketch just to get used to how things are done and to test out the relay board. What I am seeing though is kind of confusing. I think I misunderstand the initialization or something, not sure.

When the sketch starts running relay 2 starts in the on state (energized) which I confirmed by checking the contacts. When I measure DC voltage from ground to relay 2 (on Pin 7) it shows 0VDC, however, the LED light on the board used to indicated relay state is on. When relay 1 had completed its cycle voltage from ground to Pin 7 goes to 4.7 VDC, the LED indicator on the relay board turns off and the relay de-energizes.

I have GND on the UNO going to the ground pin on the relay board and 5V from the UNO going to VCC on the relay board.

I am not clear on what I am doing wrong or if I am just totally messed up (completely possible) but I would expect the initial state of relay 2 to be off.

I KNOW I am looking at this wrong in some way because it just doesn’t make sense but for the life of me I can’t figure out what the missing piece is.

No matter which relays I use or which pins, it does not change.

Can someone help out and get me straightened out?

int relayOne = 6;
int relayTwo = 7;

int relayOn = 2000;
int relayOff = 2000;

void setup() {
  // put your setup code here, to run once:

  pinMode(relayOne, OUTPUT);
  pinMode(relayTwo, OUTPUT);

}

void loop() {
  // put your main code here, to run repeatedly:

  for (int i=1; i<=3; i++) {

  digitalWrite(relayOne, HIGH);
  delay(relayOn);
  digitalWrite(relayOne, LOW);
  delay(relayOff);
    
  }

  digitalWrite(relayTwo, HIGH);
  delay(relayOn);
  digitalWrite(relayTwo, LOW);
  delay(relayOff);
  
}

Many of the relay boards have active low inputs, which means a low turns the relay on, and a high turns it off. If you can post a link to the specific relay board that you have, someone will be able to confirm exactly what you have, and how to wire it to an arduino, getting the ground and power connections correct for proper isolation of the relays from the arduino can be a bit confusing.

david_2018:
Many of the relay boards have active low inputs, which means a low turns the relay on, and a high turns it off. If you can post a link to the specific relay board that you have, someone will be able to confirm exactly what you have, and how to wire it to an arduino, getting the ground and power connections correct for proper isolation of the relays from the arduino can be a bit confusing.

David, thank you for your reply. I went to get the link to the board and in doing so read some about it, particularly the questions asked by the customers. It is definitely an Active Low board. I guess I should have done that before posting, it just never occurred to me that was the case.

Anyway, thank you so much for your reply. Now, on to more experimenting.

8-Relay Module

BTW
dlloyd

  void setup() 
  {
  // put your setup code here, to run once:

  digitalWrite(relayOne,HIGH); // relay OFF
  pinMode(relayOne, OUTPUT);

  digitalWrite(relayTwo,HIGH); //relay OFF
  pinMode(relayTwo, OUTPUT);
  }
. . .
  digitalWrite(relayOne, LOW);
  delay(relayOn);
  digitalWrite(relayOne, HIGH);
  delay(relayOff);

Thank you for that Larry. I tried setting the initial state at one point, BUT, I set it to LOW. When the result did not change I interpreted that as not working and moved on.

Getting these relays behaving in a normal fashion and doing various things has ended up being quite educational.

You need to get over the idea that HIGH always means on and LOW always means off. These are just arbitrary pin states.

I think it’s very helpful to define variables that describe the pin states:

byte relayOnState = LOW;
byte relayOffState = HIGH;

...

digitalWrite(relayPin, relayOnState);

By defining these variables, your code becomes self documenting. It’s clear what the digitalWrite() call does without needing a comment. This also makes it easy to alter the configuration of your sketch by only changing two lines of code should you one day switch to using an active high relay.

Well, I am just starting out, so lots to learn. However, it had not occurred to me that one would set the pin states as a variable. I will have to give that a try.