Arduino Nano - Using Micro Switches

Published

Introduction: Arduino Nano - Using Micro Switches

About: Omar Silva-Zapata is co-founder of Robo-Geek Inc.

In this tutorial we will learn how to connect a micro-switch to turn an LED on and off.

This tutorial is designed to be used with the Arduino Nano compatible Robo-Geek kits only.

Step 1: How Micro-switches Work

Micro-switches are incredible useful devices. A switch allows the user to change the state of a circuit. Think of the light switch in your house - it allows you to turn a light and on off. In this tutorial we will be turning and LED light on-off instead.

How the micro-switch in your kit work? These simple micro-switches are normally open - meaning OFF if not pushed and will close (ON) when you push the button.

The micro-switch has 4 legs and are connected in groups of. In order to work with a micro-switch you only need to connect to pins.

If you would like to learn a little more, check this video:

Step 2: The Circuit

Before working on this circuit, make sure you disconnect the USB cable from your laptop.

The wiring diagram for this circuit is displayed with a Fritzing diagram with a picture of the same circuit using similar color coding.

Important: Please note that the cables in your kit may be different colors so use this reference as a guide.

Two resistors are required. One for the LED and the other for the micro-switch.

1. Connect the GND pin to the blue line in the breadboard. This will give you a common ground for your circuit.

2. Connect one leg of your resistors to the ground and the other leg to one of the LED and one leg of the micro-switch respectively.

3. Connect 5V to the second leg of the micro-switch.

4. Connect Pin 12 to the other leg of the LED.

5. Connect Pin 10 to the leg where you connected the resistor for the micro-switch.

Step 3: The Code

The code of this tutorial was slightly modified from the Examples code from the Arduino IDE.

To make the code to work:

1. Create a new project by selecting New from the File menu.

2. Delete the code that comes by default.

3. Copy paste the code below

4. Check connections circuits from prior Step.

5. Connect the USB and upload your program.

const int buttonPin = 10; // the number of the pushbutton pin
const int ledPin = 12; // the number of the LED pin

// variables will change: int buttonState = 0; // variable for reading the pushbutton status

void setup() { // initialize the LED pin as an output: pinMode(ledPin, OUTPUT); // initialize the pushbutton pin as an input: pinMode(buttonPin, INPUT); }

void loop() { // read the state of the pushbutton value: buttonState = digitalRead(buttonPin);

// check if the pushbutton is pressed. // if it is, the buttonState is HIGH: if (buttonState == HIGH) { // turn LED on: digitalWrite(ledPin, HIGH); } else { // turn LED off: digitalWrite(ledPin, LOW); } }

Step 4: Troubleshooting

When troubleshooting:

1. Check if you have any error uploading the program. If so then check your program or the settings in Arduino.

2. If you have no errors uploading the program, then we must check the circuit. Double check you have wired the circuit properly - do this only while the circuit is disconnected. Do not move cables or components while your circuit is powered.

3. Check the orientation of the LED diode. Refer to this tutorial to ensure the LED is working:

https://www.instructables.com/id/Arduino-Nano/

4. Check that the micro-switch is connected properly.

5. Still not working? Take a break. Sometimes when we are tired, we don't see obvious things. Once rested, come back repeat steps above and try again.

6. Rarely you may have a defective component or wired but it's possible. If you are sure all the other steps were done properly, replace the micro-switch with another micro-switch and test again.

If everything else fails and you feel you are really stuck, please e-mail us at info@robo-geek.ca with evidence you purchase the kit from Robo-Geek and pictures of your circuit or the error you are getting.

If you want to add a comment to this tutorial, remember ensure to read Instructables Be nice policy before commenting.

Step 5: Challenges

If you would like to challenge yourself to do more, we suggest:

1. Using 3 micro-switches and 3 different color LEDs, create a program to turn each LED with its respective micro-switch

2. Using micro-switch and 3 LEDs create a program that when micro-switch is pressed, all 3 LEDs will turn.

Step 6: Further Reading

Congratulations in completing this tutorial.

If you want to learn more about micro-switches, we highly recommended reading the comprehensive tutorial by Lady Ada:

http://www.ladyada.net/learn/arduino/lesson5.html

Share

    Recommendations

    • Creative Misuse Contest

      Creative Misuse Contest
    • Clocks Contest

      Clocks Contest
    • Oil Contest

      Oil Contest

    Discussions