Using Raspberry/Arduino to detect limit switch status

Discussion in 'Embedded Systems and Microcontrollers' started by knro, Oct 15, 2015.

  1. knro

    Thread Starter New Member

    Nov 28, 2011
    15
    0
    This is a followup to my thread in the project's forum on electric winch connections. As you can see from the circuit diagram, 230VAC passes through a NC limit switch to feed the motor. Is there a way to detect the status of the limit switch (Connected/Open) using Raspberry PI or Arduino?
     
  2. shteii01

    AAC Fanatic!

    Feb 19, 2010
    3,398
    497
    Sure. Set one of the pins to be input. Connect output of the switch to this pin. Run a program that contains the loop that monitors the pin (pretty simple, probably about 10 or less lines of code). In programming it is called Polling. The program constantly polling the pin to see if its status has changed. We are talking micro or milli seconds here, it is very very very fast, the program checks the pin tens or even hundreds of times in a second. If you don't need to check the pin so very often, you can adjust the program.

    It is my understanding that R. PI is more of a mini computer. A huge overkill for such simple task.

    Make sure switch output is compatible with whatever uC you use. For example at work we use PLC that use 24 VDC system. So our limit switches are the 24 VDC, switch open and PLC sees 0 volts, switch closed and PLC sees 24 volts DC.

    Arduino people designed their hardware for 3.3 VDC and 5 VDC. If you use commercial hardware, you may need additional circuit to step down the sensor output to the level that uC can safely receive. Should not be hard.
     
    Last edited: Oct 15, 2015
  3. MikeML

    AAC Fanatic!

    Oct 2, 2009
    5,450
    1,066
    Do not hook 240Vac to the pin of an Arduino or Pi!

    You need galvanic isolation between the line side and the Arduino side. This is usually accomplished using a small transformer with isolated windings.

    I have used an inexpensive surplus cell-phone charger (can have a real iron transformer or a switch-mode type supply). Use the 5V side to tell if the 240V is present or not...

    My local thrift shops have hundreds of obsolete cell phone chargers which they practically give away...
     
    JohnInTX and knro like this.
  4. knro

    Thread Starter New Member

    Nov 28, 2011
    15
    0
    The charger is a brilliant idea! I have a few around and will adapt them to check the status of the limit switch! Thanks a lot!!
     
  5. ISB123

    Well-Known Member

    May 21, 2014
    1,239
    527
    240V small oven bulb and a phototransistor placed in heatshrink tubing should to the job.


    Or try this:

    Use NPN transistor.
    example.png
     
    Last edited: Oct 16, 2015
  6. knro

    Thread Starter New Member

    Nov 28, 2011
    15
    0
    Hmm this is also an interesting solution, but I'm not sure of connecting mains to a circuit. What's the purpose of the capacitor? and why 470uF?
     
  7. ISB123

    Well-Known Member

    May 21, 2014
    1,239
    527
    Mains are not connected to circuit its just has a wire wrapped around (insulated)240V AC wire. Since AC creates EMF when current flows trough the conductor.You should be able to wrap the wire around the mains cable which in turn is going to act as "antenna" picking up the EMF and turning the transistor on every positive cycle which means that transistor is going to switch at 50Hz to prevent that you need to use a 470-1000uF capacitor on base to smooth it out into flat DC.

    You wont need base resistor on transistor since generated current is going to be very small.All parts on base section of transistor are for simulation purposes and are not needed except the capacitor and the diode but I doubt that you are going to need it.

    Capture.PNG
     
    Last edited: Oct 16, 2015
  8. JohnInTX

    Moderator

    Jun 26, 2012
    2,348
    1,029
  9. knro

    Thread Starter New Member

    Nov 28, 2011
    15
    0
    Ok back folks! So I implemented the phone charger idea and used voltage divider to connect it it to Raspberry PI GPIO (5v to 3.3v), and it worked! Except for one annoying issue. When I turn the charger on, I get ON signal on the GPIO, however when I turn it off, it takes about 2-3 seconds before the signals changes from ON to OFF. I think there is perhaps a capacitor in the charger getting discharged when it is turned off?

    If that's the case, how to fix it? Do I just remove the capacitor or there is more complex circuitry I have to deal with?
     
  10. mcgyvr

    AAC Fanatic!

    Oct 15, 2009
    4,771
    971
    I would have just added a second limit switch or rewired the existing one to pull the pin to ground..
     
  11. MikeML

    AAC Fanatic!

    Oct 2, 2009
    5,450
    1,066
    Parallel the output of the charger with 4.7K, or 1K, or 470Ω.
     
    knro likes this.
  12. knro

    Thread Starter New Member

    Nov 28, 2011
    15
    0
    So use a bleeder resistor, of course! The different resistors will have different discharge times?
     
  13. knro

    Thread Starter New Member

    Nov 28, 2011
    15
    0
    Even the 470Ω resistor results in about 1.2 seconds delay. I tried 100Ω and there is very little delay but it gets very hot, 220Ω delay is acceptable and it doesn't get too hot. I'm using 0.25 W resistors. Maybe removing output capacitor from charger is another option?
     
  14. MikeML

    AAC Fanatic!

    Oct 2, 2009
    5,450
    1,066
    Apparently, the charger you happened to use must have a large capacitor that charges to 1.4 times the line voltage in the first part of the SMPS. The energy stored in that capacitor is ∝ to CV^2, so would have four times the energy than the same charger used here in the US at a line voltage of 120Vac, cause I never noticed such a long delay.

    I have several of the old 5V cell phone chargers around here. I'll test some of them to see how long it takes for their output to fall off.
     
  15. MikeML

    AAC Fanatic!

    Oct 2, 2009
    5,450
    1,066
    I tested several SMPS 5V cell-phone chargers (Samsung, Nokia, Canon) I have in my junk box, using a line voltages of 120Vac 60Hz and 240Vac 60Hz. With a 510Ω "bleeder" resistor across their output, the measured time delay of the output voltage decay after the input was interrupted ranged from 1.4s to 3.4s. An older-style iron-transformer one with filtered (but unregulated) output was about 0.7s.

    As I suspected, there are two parts to the turn-off delay. The first part holds the output voltage constant after the ac goes off; the second part looks like an RC delay of the output filter capacitor. For the Samsung cell charger, with 120Vac input, part one was 1s, while the TC of part 2 is 0.4s. With 240Vac input, part one went to 3.4s; while part 2 stayed at 0.4s.

    I had previously used this trick to make a remote low-voltage LED indicator to tell me when my 240V deep-well water pump is running, and there the delay on cessation of the visual indication was not even noticeable.... I am also using a wall-wart to drive a 12Vdc running-time meter where the input comes from a 240Vac circuit.
     
    knro likes this.
  16. knro

    Thread Starter New Member

    Nov 28, 2011
    15
    0
    Thanks Mike!! I think I can live with the ~1.4 seconds delay now since the limit switch is already off and all that sensor does is commanding the relays to turn off as well, so this delay is OK for my application. Thanks a lot for the feedback!
     
Loading...