Go Down

Topic: Need advice before I plug this in. (Read 726 times) previous topic - next topic

ericscheidt

I'm pretty new to electronics and am trying to build a simple way to control the on/off state of a lamp connected to a power switch tail when it senses a magnet being drawn near. Theoretically I think this should work, but I would like someone to look it over before I fry my UNO. Should there be diodes and resistors protecting the UNO from the Tail? Is a 9v battery recommended? Ideally how I want this to work is when a magnet closes the reed switch I want the lamp to turn on and off really quick and stay off until the magnet is pulled away and brought near it again. Basically the lamp would work like a one shot strobe. Here's my code and I've attached a fritzing diagram.

#define LED 13    // Pin for the LED
#define BUTTON 2  // The input pin where the
                   // pushbutton is connected
int val = 0;      // variable stores value of input

void setup() {
   pinMode(LED, OUTPUT);
   pinMode(BUTTON, INPUT);
}

void loop() {
   val = digitalRead(BUTTON);
   
   // check if there was a transition
   if (val == HIGH) {
     digitalWrite(LED, HIGH);
     delay(10);
     digitalWrite(LED,LOW);
   }
   
   while(digitalRead(BUTTON)==HIGH); {}   
 
}

LarryD

#1
Jul 07, 2013, 06:24 am Last Edit: Jul 07, 2013, 06:43 am by LarryD Reason: 1
1. Add this to setup()
 digitalWrite(BUTTON,HIGH);         //Enable the pull-up resistor   EDIT: in this case not needed

2. Note: you have no switch de-bounce code so this may cause a problem.

3. Also: Will the power switch tail operate at 10ms of a second?   EDIT: NO/MAYBE,      Operate times: 15 ms max actuate






The way you have it in your schematic isn't the same as how you have it wired up!

LarryD

You don't need to connect pin 3 of the Power Switch Tail to GND
The way you have it in your schematic isn't the same as how you have it wired up!

ericscheidt

Yeah, I was worried that the power switching relay wouldn't operate within that short of an interval. In reality up to 50ms would probably work fine for my purposes.

Do you know how many milliseconds after a magnetic reed switch has been pulled/released it takes for things to settle to a reliably read a state? I could probably introduce a delay within in my code to debounce.

Also, most importantly, I'm using a DIY Power Switch tail from Sparky's Widgets. It still operates at 5v and is pretty much identical to the Power Switch Tail. It has three connections (VCC,CTRL GND) I have 5V connected to VCC, Ground to GND, and am using DIgital Pin 13 to send a signal to CTRL. I have read that when a relay's coil de-energizes it can output more voltage than what was supplied to it. I think someone said that adding a diode somewhere is enough to soak up the surge. The relay unit should have some built in protection but am not 100% sure. Is there a way I could add some additional protection just in case and if so how?
Thanks!

1ChicagoDave

#4
Jul 07, 2013, 07:27 am Last Edit: Jul 07, 2013, 07:49 am by 1ChicagoDave Reason: 1
Someone correct me if I'm reading it wrong, but from the schematic here...

http://www.powerswitchtail.com/Documents/PST%20II%20product%20insert,%20Aug%202012.pdf
...it looks like you need a positive & negative connection (complete, closed circuit) to activate the PST.

From the code and diagram in OP, it looks like the PST is being sent two positive signals; one is connection to 5v, and other is from pin 13.

I think you want to connect pin 2 on PST to Arduino ground, pin 1 of PST to pin 13 (LED Pin) on Arduino, and can leave pin 3 on PST unconnected. This way, when Arduino sends HIGH signal to PST, it completes the circuit & activates relay to turn on a/c signal.

Never mind. Sparky's uses a transistor instead of connecting directly as in PowerSwitchTail version.
If this is the one your using (http://www.sparkyswidgets.com/portfolio-item/relaypcb/)...then yes, it looks like you had/have it hooked up correctly. You do have to connect +5v, GND, and positive (high) signal from Arduino to activate it.

Also, it looks like Sparky's has the diode & resistor built-in. So you should be fine connecting directly to Arduino output.

Quote

Also, most importantly, I'm using a DIY Power Switch tail from Sparky's Widgets.

Yes, that was pretty important information. :-)

LarryD

#5
Jul 07, 2013, 07:48 am Last Edit: Jul 07, 2013, 08:26 am by LarryD Reason: 1
Quote
I think you want to connect pin 2 on PST to Arduino ground, pin 1 of PST to pin 13 (LED Pin) on Arduino

Yes

Or leave it as is and change to:
    digitalWrite(LED, LOW);  //If pin PST pin 1 is +5V and pin 2 is connected to
    delay(10);
    digitalWrite(LED,HIGH);  //If pin PST pin 1 is +5V and pin 2 is connected to

Try this:
Code: [Select]

#define LED 13    // Pin for the LED
#define BUTTON 2  // The input pin where the
//                   reed is connected

boolean closed = false;  //used to disable flashing
int val = 0;             //variable stores value of input

//******
void setup() {
 pinMode(LED, OUTPUT);
 pinMode(BUTTON, INPUT);
}                   // End of setup()

//******
void loop() {
 val = digitalRead(BUTTON);

 // check if there was a transition
 if (val == HIGH && closed==false)
 {                //When the reed is first closed flash LED
   //digitalWrite(LED, LOW);          //If pin PST pin 1 is +5V and pin 2 is connected to
   digitalWrite(LED, HIGH);             //If you are using sparkyswidgets
   delay(100);
   //digitalWrite(LED,HIGH);          //If pin PST pin 1 is +5V and pin 2 is connected to
   digitalWrite(LED, LOW);             //If you are using sparkyswidgets
   closed = true; //Disables the LED from
                  //flashing while the reed is closed
 }

 if (val == LOW)
 {                
   closed = false; //When the reed is open enable flashing
   delay(10);      //Wait for contact bounce to finish
 }

}                   // End of loop()
The way you have it in your schematic isn't the same as how you have it wired up!

LarryD

#6
Jul 07, 2013, 07:56 am Last Edit: Jul 07, 2013, 08:02 am by LarryD Reason: 1
Around in circles we go.

If you are using http://www.sparkyswidgets.com/portfolio-item/relaypcb/
no protection is needed .
Then change back to:

  digitalWrite(LED, HIGH);
  delay(100);
   delay(50);
  digitalWrite(LED,LOW);
The way you have it in your schematic isn't the same as how you have it wired up!

ericscheidt

Yeah that's the one I was using or for even beteer reference here https://www.youtube.com/watch?v=-fRkUMgqHq0. I'm a little worried because I uploaded some earlier version of my code on my UNO (everything was up and working), refined my code a little more and went to upload it again and now noticed the bootloader on my arduino is hosed. It could be unrelated  and coincidental (The UNO I have been using is old and beat up) but am now worried my wiring may have caused it. Wondering if there is something fundamentally wrong with my circuit that could damage a microcontroller? Thanks Larry that code looks fantastic! I'll try that.


LarryD

#8
Jul 07, 2013, 08:15 am Last Edit: Jul 07, 2013, 08:28 am by LarryD Reason: 1
If you are using http://www.sparkyswidgets.com/portfolio-item/relaypcb/
You do need to wire pin 3 on the PST to GND on the Arduino.  

The diode across the relay is there already.
The way you have it in your schematic isn't the same as how you have it wired up!

ericscheidt

#9
Jul 07, 2013, 10:32 am Last Edit: Jul 07, 2013, 10:34 am by ericscheidt Reason: 1
Well I tried again on a brand new UNO and everything started behaving very erratically. I used the code you revised and the moment I plugged it in it started switching the relay very fast and was only stopped when I moved a magnet closer upon which the entire arduino shut itself down. I'm using a reed switch that is normally open. Which is weird. Also the 9v battery started heating up pretty fast. I feel that there is something with my wiring scheme that is grossly incorrect. I have tried rewiring this 10 times and everything looks good. Any ideas? I don't want to fry this one too.

LarryD

#10
Jul 07, 2013, 05:46 pm Last Edit: Jul 07, 2013, 08:18 pm by LarryD Reason: 1
1. Show us a picture of your wired up circuit, and one of the component layout for the PST.
2. K1 on the PST may be drawing too much current, what is it's resistance? (do not want to draw too much current from the Arduino 5V line)
3. What is the battery voltage/current under load?

Quote
I plugged it in it started switching the relay very fast

This is because D13 (LED) flashes at power-up.
Maybe use D12 instead.
The way you have it in your schematic isn't the same as how you have it wired up!

ericscheidt

OK, I think I figured this out. My reed switch/resitor was laid out incorrectly. I think I was also shorting 9v straight into a digital pin configured as an input (woops!). i rewired everything according to the fritzing diagram i have attached and now everything works great! Is there anything else that looks like a bad idea? Thanks for all the help, I really appreciate it.

Go Up