Shop Lights Project

Hello,

Just discovered Arduino and got my first one last week. Have no coding or electronics experience, but have put in some time and come up with my first project. This is exciting, wont have to run romex, mess with 2way and 3way switch wiring, and still have light control at each entry.
Pressing any of the buttons changes the state of the relay. Some de-bouncing in there, works really well set up like in the attachment. When installed in the shop,the buttons will have cable runs of 2ft, 18ft, and 25ft. Im going to open up a cell charger (5v 2.1a) and use that for power to the device and relay.
The image shows an UNO, but Im thinking Nano for this, seems like a waste for the UNO.

My questions for you guys:
1, Anything obvious in the code that could be improved regarding reliability?
2, Any changes need to be made to the circuit for the longer cable runs, regarding reliability?
3, Can any NO momentary switch be used? Having a lot of trouble finding suitable buttons, was thinking of using the buttons those guys use on the arcade cabinets. Those would have to be pretty robust to stand up the abuse of gameplay. Any suggestions for switches?

Note, Im sure the wiring to the relay is misrepresented in the attachment, (I dont understand the symbol). But it works nice on my desk, only has VCC, in, and ground (plus the 120vac outputs). Currently controlling a lamp. The lamp stayed on most of the day today, and we didnt loose any magic smoke!

Thanks
k

// momentary button, push and relay closes 
// push again and relay opens

int switchPin = 12;
int relayPin = 7;
boolean lastButton = LOW;
boolean currentButton = LOW;
boolean relayOn = true;

void setup()
{
  pinMode(switchPin, INPUT);
  pinMode(relayPin, OUTPUT);
}

boolean debounce(boolean last)
{
  boolean current = digitalRead(switchPin);
  if (last != current)
  {
    delay(5);
    current = digitalRead(switchPin);
  }
  return current;
}

void loop()
{
  currentButton = debounce(lastButton);
  if (lastButton == LOW && currentButton == HIGH)
  {
    relayOn = !relayOn;
  }
  lastButton = currentButton;
  
  digitalWrite(relayPin, relayOn);

}

Basically you don't wire buttons like that you wire them between input and ground and enable the internal pull up resistors or have a lower value external pull up like a 1K.

You don't drive a relay like that you need a transistor and a diode across the coil.

Hi,
Welcome to the forum.

I would suggest you forget fritzy and hand draw your circuit diagrams and send pictures.

You cannot connect a relay coil directly to your UNO, the output cannot supply enough current, also the way you have your relay connected to the output is also wrong.

google arduino output transistor relay

See what turns up.
Also do you have a DMM to help trouble shoot any problems?

Tom… :slight_smile:

Ok, you guys are telling me that I cant wire the relay this way and cant wire the buttons this way. . . But i have done just that and it works.
Are you trying to say that it wont be reliable after some time, or the UNO will eventually fail, or are you speaking to best practices? Maybe the relays that Im using have some of the functionality you mention built in?
I want this thing to work, and keep working, so Im all ears here. As well, Ive just spent the last 2 hours reading about transistors, and, well Im clearly missing something. If the transistor is for protection, can I just power the relay directly from the power supply instead of the UNO 5v pin?

Grumpy, Im unclear about

you wire them between input and ground and enable the internal pull up resistors or have a lower value external pull up like a 1K.

Ive read a dozen tutorials and watched as many videos, and thought thats what I have here. Pin 12 is the input, I did have a 10k resistor on the (-) leg of each button, but found that it would work without them, and that the resistor specs didnt seem to make any difference, so ended up with just the one on the “input bus” I like to call it. But with your suggestion I have swapped in a 1k. Cant see any difference though, as it still works the same.
So, I dont understand how what you said is different than what I have. It would be great if you could elaborate.

Tom, Thanks for the welcome! I do have an 87-5 that I use for solar power projects. I just took a measurement and got a max of 41ma to the relay. That was powered from a 5v PC USB port.
Check these pics (please excuse the sloppiness, my male to female leads have not arrived yet).

Note in the photos there are four buttons. The installation will only have three.

Thanks for the help guys.

Hi,.

Now you can see why we don't like fritzy, your setup is nothing like the diagram You have a 4 relay driver board that you did not tell us about, that makes the difference, it has the necessary transistor/opto driver for the relays.

You are using the 5V from the UNO to power the relay board, this will be okay while powered from the USB socket.

However if you use a power pack plugged into the DC socket you will have problems.

When using the DC socket you will be using the internal 5V regulator, it may have problems supplying enough current to drive the relays, especially if all 4 are on at the same time.

Tom..... :)

Hi Tom, yes I definitely left out some pertinent info there. . . Sorry, I didn't realize there was a difference between a relay and a relay board (with all the goods already on it) until right after I posted my last reply.

To be clear, the relay board that will go into service is the single channel version of the one in my photos. Only one relay total will be used in the shop lighting project, with 3 buttons sending signals. There are 5 light fixtures drawing around 1a each.

Hopefully GrumpyM (or someone) can help me understand what the problem is with my button wiring.

So, any comment on the wire lengths I mentioned in the OP. A little concerned that the one button on the end of 25ft of wire might give an issue.

Button suggestions?

k

The fact your buttons work for the moment is irrelevant, they will not continue to do so especially on long wires.See this http://www.thebox.myzen.co.uk/Tutorial/Inputs.html

Some comments:

1A per fixture is pretty beefy, even at 110V…

You might want to put a pull up resistor between the relay out put and 5V. THis will keep the relay off while the Arduino is off/starting. I assume it’s one of those useless opto relay drivers you now see everywhere which turn off the relay when powered… (Which Chinese designed it is a retard…)

Using the 5V of the Arduino is fine as long as you don’t power it all via the DC jack. If you power it via USB for a charger you’re fine.

The debounce routine is a bit basic and blocking. Also 5ms is not a hole lot especially on long cables. For buttons I’m a fan of using a library. There are a lot, just pick one. It makes the code cleaner. Yes, there is overhead but it’s just so simple. I’m using Bounce2 most of the time.

Because of the long cables I would use a pretty low value pull up (or down) resistor. And I think I would use a series resistor for the input as well to be sure it’s protected from any kind of spike. Something like:

                   [5V]
                    |
                    |
                   [1k]
                    |
[input]----[10k]----|----[switch]

And you can indeed use push buttons like you did now. But you can also use regular light witches and connect each to a pin. If any of the switches change state you change the relay state. So the button orientation doesn’t match the relay state. And you can simply turn on/off the light with any of the buttons.

#include <Bounce2.h>

const byte SwitchPins[] = {10, 11, 12};
const byte RelayPin = 7;

Bounce switches[sizeof(SwitchPins)];
const byte DebounceInterval = 20; //ms

void setup(){
  //setup the switches
  for(byte i = 0; i < sizeof(SwitchPins); i++){
    pinMode(SwitchPins, INPUT);
    switches.interval(DebounceInterval);
    switches[i].attach(SwitchPins[i]);
  }
  //setup the relay
  digitalWrite(RelayPin, HIGH); //turn it off
  pinMode(RelayPin, OUTPUT);
}

void loop(){
  //loop all switches
  for(byte i = 0; i < sizeof(SwitchPins); i++){
    //if a switch changed
    if(switches[i].update()){
      togglePin(RelayPin); //toggle relay
    }
  }
}

//easy acces to toggle a pin
inline void togglePin(int pin){
  digitalWrite(pin, !digitalRead(pin));
}

It it were me,
I would use doorbell buttons, or motion sensors.
I would run 24VAC for all the long wire runs, only because I have so many transformers from old work.
my doorbell has worked for over 25 years with the same transformer.

I am never certain that specific use devices will work. a phone charger has internal limits on power.
then use AC rated opto-couplers

BTW, the NANO has MORE ability than an UNO, so, if you feel that wasting an UNO would occur, then you have 2 additional analog pins you are wasting by using a NANO.

relays, like all coils, work by creating a magnetic field when voltage is passed thru the wire.
the field is established or not. depending on the voltage, length of wire, and so on.
when the voltage is lost, the magnetic field collapses and the energy is returned to the wire in a maelstrom of collapsing magnetic filed and a huge antenna length of wire.

although it does not spike your electronics to the point of failure every time. the fact that it could means that we know it and the simple fix is to add a 10 cent diode to eliminate damage to our electronical bits.

think of it as an athletic supporter. you may have worn one for season after season in school, and it was never needed.
but that one time…

Thanks for the info guys.

Some stuff:

septillion, I must be doing something stupid... Again. When uploading your code I get the following:

Arduino: 1.6.7 (Windows 7), Board: "Arduino/Genuino Uno"

C:\Users\admin\Documents\Arduino\admin_1pushbutton_on_off_relay_control2\admin_1pushbutton_on_off_relay_control2.ino: In function 'void setup()':

admin_1pushbutton_on_off_relay_control2:13: error: invalid conversion from 'const byte* {aka const unsigned char*}' to 'uint8_t {aka unsigned char}' [-fpermissive]

     pinMode(SwitchPins, INPUT);

                              ^

In file included from sketch\admin_1pushbutton_on_off_relay_control2.ino.cpp:1:0:

C:\Program Files (x86)\Arduino\hardware\arduino\avr\cores\arduino/Arduino.h:125:6: error:   initializing argument 1 of 'void pinMode(uint8_t, uint8_t)' [-fpermissive]

 void pinMode(uint8_t, uint8_t);

      ^

admin_1pushbutton_on_off_relay_control2:14: error: request for member 'interval' in 'switches', which is of non-class type 'Bounce [3]'

     switches.interval(DebounceInterval);

              ^

exit status 1
invalid conversion from 'const byte* {aka const unsigned char*}' to 'uint8_t {aka unsigned char}' [-fpermissive]

Invalid library found in C:\Program Files (x86)\Arduino\libraries\Bounce2-master: C:\Program Files (x86)\Arduino\libraries\Bounce2-master
Invalid library found in C:\Program Files (x86)\Arduino\libraries\Bounce2-master: C:\Program Files (x86)\Arduino\libraries\Bounce2-master

  This report would have more information with
  "Show verbose output during compilation"
  enabled in File > Preferences.

Got the library from the first link on this page: http://playground.arduino.cc/Code/Bounce

Is that not the right one?

GrumpyM, nice site there, lots of good info. Read the page you linked and then some. You said pullup, in your first post and I read for some reason pulldown. Got ya now. Same thing septillion is suggesting. Opposite of what I have, and the majority of tutorials I read online have.

dave in Jersey, good info, thanks for the input. Driving through NJ was the one thing that caused me to immediately go and get my first GPS back in the day. . .

Hi,

You might want to put a pull up resistor between the relay out put and 5V.

I hope not if the relay output is 110Vac.

Tom.... :)