Use a microswitch with an Arduino

FunduinoPublished on

Eine LED über einen Mikroschalter schalten

Mikroschalter melden einer Steuerung, dass etwas eine Endlage erreicht hat. In 3D-Druckern und CNC-Maschinen sitzen sie als Endstop an den Achsenenden.

In diesem Einsteigerprojekt liest der Mikrocontroller den Zustand eines Mikroschalters ein und schaltet danach eine LED. Der Schalter hat drei Anschlüsse – NC, NO und COM –, deren Bedeutung die Anleitung zuerst erklärt.

Pinout of the microswitch with NC, NO, and COM
NC, NO, and COM on the microswitch

You need this

Mini slide switch - breadboard-compatible, 2.54 mm pitch Mini slide switch - breadboard-compatible, 2.54 mm pitch 0,74 € *Item no. F23106707About the product — Mini slide switch - breadboard-compatible, 2.54 mm pitch
Funduino UNO R3 Microcontroller - ATmega328P, 16 MHz, for Arduino Funduino UNO R3 Microcontroller - ATmega328P, 16 MHz, for Arduino 11,12 € *Item no. F23107480About the product — Funduino UNO R3 Microcontroller - ATmega328P, 16 MHz, for Arduino
830-point Breadboard - solderless breadboard for Arduino 830-point Breadboard - solderless breadboard for Arduino 2,90 € *Item no. FUN-1010553About the product — 830-point Breadboard - solderless breadboard for Arduino
Breadboard Cables Male-to-Male - 40 Pieces, 20 cm Breadboard Cables Male-to-Male - 40 Pieces, 20 cm 1,74 € *Item no. FUN-1010558About the product — Breadboard Cables Male-to-Male - 40 Pieces, 20 cm
LED 5 mm - 100 Pieces, 5 Colors, 2 V LED 5 mm - 100 Pieces, 5 Colors, 2 V 1,97 € *Item no. F23107316About the product — LED 5 mm - 100 Pieces, 5 Colors, 2 V
Resistor Assortment 100 Pieces - 5 Values 100 Ω to 10 kΩ, 0.5 W Resistor Assortment 100 Pieces - 5 Values 100 Ω to 10 kΩ, 0.5 W 1,72 € *Item no. F23107216About the product — Resistor Assortment 100 Pieces - 5 Values 100 Ω to 10 kΩ, 0.5 W

All 6 parts together 20,19 € *Ready to ship immediately · Delivery time: 1-3 business days

* All prices include statutory VAT, plus shipping costs.

The microswitch

Microswitches are used wherever a controller needs to detect that something has reached its end position - for example, as an endstop on a 3D printer.

The three terminals mean:

  • NC = Normally Closed, closed at rest
  • NO = Normally Open, open at rest
  • COM (C) = common terminal, the data line here
Pinout of the microswitch with NC, NO, and COM
NC, NO, and COM on the microswitch

Build the circuit

The LED is connected to Pin 6, and the microswitch to Pin 7. The LED needs a 220-ohm series resistor.

When pressed, the microcontroller receives a LOW signal, i.e. a digital “0”. When not pressed, it receives a HIGH signal, i.e. a “1”.

To ensure the input reads HIGH when not pressed, it needs a pull-up resistor. You can enable the microcontroller’s internal resistor in the program with pinMode(Sensor, INPUT_PULLUP).

Circuit diagram with microswitch and LED on the Arduino
The circuit diagram
Microswitch and LED set up on a breadboard
The setup on the breadboard
1 / 2

The program

The program reads the state of the switch and turns the LED on accordingly. The LED lights up as long as the switch is pressed.

Switch LED via the microswitch
int Sensor = 7;
int Wert;
int LED = 6;
void setup()
{
  pinMode(Sensor, INPUT);
  pinMode(LED, OUTPUT);
}
void loop ()
{
  Wert = digitalRead(Sensor);
  if (Wert == 0)
  {
    // the microswitch was pressed
    digitalWrite (LED, HIGH);
  }
  else
  {
    // the microswitch was released
    digitalWrite (LED, LOW);
  }
}
Rate the instructions

Comments

Ask a question or share an experience

Contact us if something doesn’t work. Please include your board and the error message.

We review every comment before publication. Your email address remains with us.