I concluded that the optimal way for using a two way switch or a relay was to connect the common wire to a digital data pin on an arduino with a 100 ohm resistor in between, then connect the NC to ground and the NO to 5V.
That way when the relay has no current flowing through it the input goes Ground -> resistor -> digital in. And when it has it goes 5V -> resistor -> digital in.
On page 2 dlloyd suggests using the resistor between the 5V and data only and not on the gnd side.
What's the reason for not having a resistor between gnd and data ?
Should I rewire?
Thnx for any reply.
EDIT
PS: I had initially seen ppl only using Common and NO on the relay then using a 10K resisotr for pulldown but I don't see a reason to so that when I have the extra wire for ground.
PPS: I should mention the distance between my relay and the arduino is about 1.5 meters and the wire I use is 23awg solid core (a piece of cat6 I had lying around)
Not sure how your question relates to that tutorial. For inputs I have written this. http://www.thebox.myzen.co.uk/Tutorial/Inputs.html
A resistor is only used in line with an input by paranoid people who are worried that if by mistake you make that pin an output AND you set the output low AND you push the button then and only then you could end up damaging the pin. So three stupid things before breakfast you are protected from.
I was pretty sure it was fine as is but wasn't sure about how sensitive an arduino is. Thinking gnd was to be connected straight to input without a resistor between or else ... idk lol. Didn't see any issues with it being there either. I asked to be sure since the thing will be on 24/7. From reading the adafruit tutorial I know the extra resistor is not strictly needed.
I don't see why some suggest using a pulldown/up resistor in combination with a relay though. Only using common and NO.
After reading your tutorial:
Since a relay is basically break before make, should I ignore the input for a few cycles after a state change is detected then read the new state after that?
How does a floating input behave most of the time? Will I see a fast flutter between on and off or will it linger in it's previous state for a few microseconds?
Should I spend my time looking up relay brand and check how long the switching takes?
I haven't seen any weird behavior yet but it's only be up for 2 days.
The microcontroller will detect a fast flutter between on and off when an input isn't connected to anything. To get rid of this unwanted behaviour you will need a pulldown or pullup resistor.
I'm using the G6RN-1A-DC24 relay. it's rated at 3ms - 6 ms max release engage.
That includes the charging, the actual time the pin is floating would be a fraction of that I think.
Unless a floating input causes damage, I'm just going to ignore the state for 6-10 ms once it changes, read it again after that and transmit that value. That's the solution for which I don't need to rewire anything.
If someone sees fault in that please reply.
Thnx.
PS:Like I said, as of now I haven't seen any flutter happening though. I'm willing to take this is coincidental.
the actual time the pin is floating would be a fraction of that I think.
So lets say the floating time is 1mS, in that time the controller can execute 16000 operations, so that is a long time in computer terms for it to float.
Use a pull up resistor, the internal ones cost you nothing, just activate them.
Phoenixxl:
How does a floating input behave most of the time? Will I see a fast flutter between on and off or will it linger in it's previous state for a few microseconds?
It will do whatever it likes, depending on many factors - for low power battery operation its a no-no since
the floating input can consume of the order of mA, whereas pulled up it consumes < 1nA
A digital input drives an CMOS gate and holding a CMOS gate at half-way point turns on
both the n-FET and the p-FET, creating a shoot-through situation (although at ~1mA or so, not the 100A
you get with big MOSFET motor drivers!)
Think of a floating input as a voltage sensor with uncontrolled drift driving a high gain amplifier (CMOS
gate in the forbidden region).
In the old days people did a lot of clever analog tricks using the CD4000 series CMOS logic gates as they
worked up to 15V.
First, thank you all for taking the time to reply.
My current setup is something like this:
From reading @Grumpy_Mike 's tutorial page concerning floating pins and break before make 2 way switches, ignoring the NC connector makes more sense now. I read over the adafruit tutorial too quickly, and think now they should point out under their figure 5.4 the reason for not doing this is not because 2 way switches are "too expensive".
That said , is this the better or "ideal setup" in my case?
Using the original setup I also tried initializing the pins with input_pullup as @Grumpy_Mike suggested. That works too..
So which should I go for? The first (current) setup but with internal pullup enabled or the second?
I have the needed materials to do any and have seen the light
I will rewire the chinese take-away box adorning my wall if needed. sigh. So beautiful. cough
Thnx.
EDIT:
Just enabling the internal pullup and leaving the rest as is things would look like this per input?
Should I not disconnect the 5V (to NC) connector in this case? Or is the internal resistor just there to fill the gap "break before make" makes?
You can disconnect or leave it, no difference. For a digital signal a SPST switch is all you want as the time
delay during break-before-make is considerable more than a logic transistion time (measured in ns, not ms)
1 to 10 state changes per flip, which seems to be the expected behavior when not using the internal pullup. bleh...
Would it be possible this is a badly manufactured board ?
I'll disconnect the 5V wire to check.
EDIT:
07:23
No 5V connected + internal pullup -> flutter while switching. Not after, not before just while.
No 5V connected + no internal pullup -> flutter the whole time , as expected.
Going for external pullup now. Seeing what that gives.
Trial by fire as they say. ha!
EDIT:
08:13
Same issue when using an external pullup resistor.
Fluttering while switching.. state changes between 1-10 times while switching..
Trying pulldown now. sigh
EDIT:
08:39
PullDown .. nada.
The relay works when going from HIGH to LOW ( I think , taking pullup into account)
But flutters when going the other way. Will try some more stuff.
Used internal pullup + pushbutton instead of a relay. Things work fine. bleh. Will try NO-common.
you are experiencing mechanical contact bounce. This will also happen with a pushbutton. You need to ignore any state changes after you observed the first change. Bounce time it probably given in the relay's datasheet, typical values are up to a few milliseconds
I have tried everything. Ironically, the only way I get no fast switching between states is by connecting it like I originally did.
@olf2012
If fluttering will happen for a few milliseconds due to mechanical contact bounce anyway I will go with my original idea, like you also suggest. Once I detect a change, wait 1- 10 milliseconds, read again.
If the pin floating for a few milliseconds, while the relay is between states, will not kill my arduino I will fix this in software.
I do not need to know on the millisecond when my garage door is open/closed or the garage light is on.
I was already done though. I used a counter going down each loop. That way I don't have the overflow issue after 50 days.
If they ever make a faster arduino and I decide to use it I'll have to change my program
150 cycles is about 10 ms in my currently running program which should be plenty.
If I needed something more precise I would use something else though. In this case this should be fine.
Thank you again for providing it to me it was very nice of you to do so. I had already finished myself. I already made a similar one to yours (millis() based though, not clock time. with an extra check on rollover) in a previous project too, I wanted to put a minimal delay when flipping relays. Had I not made any of it I would surely have used yours.