Arduino Uno limit switch Interfacing and Programming

(Last Updated On: June 26, 2020)

limit switch

Description:

Arduino Uno limit switch Interfacing and Programming- This is a very detailed tutorial on how to use a limit switch with Arduino or Mega. This Tutorial explains everything from interfacing to the final testing. Limit Switches are one of the most commonly used electronic components. These switches are used for defining the limits, Left and Right, Upper and Down, etc. These switches are used in Plastic Injection Molding Machines, Solar Trackers, Garage Doors, Cabinets, Security systems, Accident detection system, and so on. For the complete circuit explanation and programming watch video tutorial.


Components and tools used in this project can be purchased from Amazon, the components purchase links are given below:

Arduino Uno:
Mega 2560:
limit switch:  Best deal “pack”:

*Please Note: These are affiliate links. I may make a commission if you buy the components through these links. I would appreciate your support in this way!

limit switch

Limit switches are one of the most commonly used electronic components, the limit_switches are used in a situation where we need to define the limits. Like for example, the movement of the Pneumatic cylinder can be controlled using the limit switch as demonstrated in the video. Limit switches are most commonly used in industries. If you look at the construction of the limit switch you will find that it’s just like the ordinary push-button but with a different design. Limit switches can be used in the same as the normal Pushbuttons are used. So if you don’t have a limit switch you can simply start with the push button. You can read my article on “Arduino Push Button Switch wiring and code “Beginners level”.


In the market, we have different types of switches like roller type limit switches, no matter which limit switch you use, the basic working principle is exactly the same. This limit switch can also be used with Mega, tinny, 8051 microcontrollers, pic microcontroller, plc, raspberry pi, etc.  for the limit switch interfacing watch this Video Tutorial “Click Here

 

Limit Switch Wiring with Arduino Uno:

Limit Switch
Limit Switch Wiring with Arduino

As you can see in the circuit diagram the limit switch wiring with the Arduino Uno or Mega is really simple. A Limit Switch is connected in series with a 10K ohm resistor. This is a Pull-Up resistor. In the normal condition, 5 Volt is connected with the Arduino, but when the limit switch is pressed the ground is connected with the Arduino’s pin number 2. In the same way, you can connect multiple limit switches. When the switch is closed it will connect ground with the digital pin 2 of the Arduino or Mega, or in simple words you can it will give 0 as the signal.

Limit Switch Arduino Programming:

Limit Switch Arduino Program Explanation:

I simply started off by defining a pin for the L Switch which is connected with the Arduino’s pin number 2. Next, I defined a pin for the Led which is connected with the Arduino’s pin number 13. Finally, I defined a flag of the type integer. this flag will be used to stop the unnecessary repetition of code.
int Lswitch = 2;
int led = 13;
int flag = 0;

Inside the void setup(), I activated the Serial communication and selected 9600 as the baud rate. I set the Limit Switch as the input using the pinMode() function and I set the LED as the output.

void setup()
{
Serial.begin(9600);
pinMode(Lswitch, INPUT);
pinMode(led, OUTPUT);
}

void loop()
{

This condition is used to check if the L switch is pressed and the flag value is 0, then print a message “door is closed” and change the status of the flag from 0 to 1. finally, a small delay of 20 milliseconds.
if( (digitalRead(Lswitch) == LOW) && (flag == 0) )
{
Serial.println(“door is closed”);
flag = 1;
delay(20);
}
The following condition is used to check if the L Switch is opened and if the flag value is 1 then print a message “door is opened”. Again change the flag status back to 0. Now, you can see the advantage of using the flag, each message is printed only one time.
if( (digitalRead(Lswitch) == HIGH) && (flag == 1) )
{
Serial.println(“door is opened”);
flag = 0;
delay(20);
}
if the flag value is 1 then the LED is turned ON for 1 second and then remains off for 1 second, this repeats again and again.
if ( flag == 1 )
{
digitalWrite(led, HIGH);
delay(1000);
digitalWrite(led, LOW);
delay(1000);
}

if ( flag == 1 )
{
digitalWrite(led, LOW);
}

digitalWrite(Lswitch, HIGH);

}



Watch Video Tutorial:

Recommended For You

About the Author: Engr Fahad

My name is Shahzada Fahad and I am an Electrical Engineer. I have been doing Job in UAE as a site engineer in an Electrical Construction Company. Currently, I am running my own YouTube channel "Electronic Clinic", and managing this Website. My Hobbies are * Watching Movies * Music * Martial Arts * Photography * Travelling * Make Sketches and so on...

1 Comment

  1. Hi Fahad,

    What a great presentation! Thank you for the video. You make it seem so simple in writing the codes. Like flow of water.

    I’m Tomi from Thailand and is always fascinated with motors control. Recently, I decided to indulge in wetting my feet in the Arduino lake.

    Actually, I was on the verge of giving up as my code comprehension is so discouraging until I stumbled upon your site. Eureka…! I found a Guru.

    I took up a challenge to build a simple mechanism to further improve the productivity at my workplace. Since the, I have been hunting for appropriate sketches for more than 2 weeks but not very fruitful.

    Highly appreciate if you could shine some light into my undertaking.

    Please allow me to explain my intends.

    1) Press Button A , a carriage (mounted on a linear actuator) travels forward for 180mm (about 4250 steps) at a speed of 2-2.5sec/100mm (delayMicroseconds(800)). As a contingency, in case of over travelled, there will be an installation of a limit switch-Y just after the stop position.

    2) The carriage will only travel back to homing position one second after Button-B is being triggered.

    3) Returning or Homing speed is controlled by a potentiometer. Between delayMicroseconds (400 to 1500)

    4) Home position is determined by steps and a backup limit switch-Z.

    5) LOOP back to (1)

    Hardware:-

    NEMA 17 bipolar Stepper motor + Linear motion lead screw slide actuator
    Arduino UNO R3
    A4988 Driver,Hi Fahad,
    Momentary button switch ….2 pcs
    Limit switch …………………….2 pcs
    12V DC 5A power supply unit
    10K Potentiometer

    I shall be keeping my fingers crossed and hoping for your favourable reply.

    Thanking you in advance and looking forward to hearing from you soon

    I may be contacted at;-
    a) tomigoh@gmail.com
    b) Whatsapp: +66 9913 15151

    Shukraan jazilaan lak

    Warmest Regards

    Tomi GOH

Leave a Reply