.. note::
Hello, welcome to the SunFounder Raspberry Pi & Arduino & ESP32 Enthusiasts Community on Facebook! Dive deeper into Raspberry Pi, Arduino, and ESP32 with fellow enthusiasts.
**Why Join?**
- **Expert Support**: Solve post-sale issues and technical challenges with help from our community and team.
- **Learn & Share**: Exchange tips and tutorials to enhance your skills.
- **Exclusive Previews**: Get early access to new product announcements and sneak peeks.
- **Special Discounts**: Enjoy exclusive discounts on our newest products.
- **Festive Promotions and Giveaways**: Take part in giveaways and holiday promotions.
👉 Ready to explore and create with us? Click [|link_sf_facebook|] and join today!
.. _2.1.2_c:
2.1.2 Micro Switch
=======================
Introduction
--------------------
In this project, we will learn how to use Micro Switch. A Micro Switch is a small, very sensitive switch which requires minimum compression to activate. Because they are reliable and sensitive, micro switches are often used as a safety device.
They are used to prevent doors from closing if something or someone is in the way and other applications similar.
Required Components
------------------------------
In this project, we need the following components.
.. image:: ../img/2.1.2component.png
It's definitely convenient to buy a whole kit, here's the link:
.. list-table::
:widths: 20 20 20
:header-rows: 1
* - Name
- ITEMS IN THIS KIT
- LINK
* - Raphael Kit
- 337
- |link_Raphael_kit|
You can also buy them separately from the links below.
.. list-table::
:widths: 30 20
:header-rows: 1
* - COMPONENT INTRODUCTION
- PURCHASE LINK
* - :ref:`cpn_gpio_board`
- |link_gpio_board_buy|
* - :ref:`cpn_breadboard`
- |link_breadboard_buy|
* - :ref:`cpn_wires`
- |link_wires_buy|
* - :ref:`cpn_resistor`
- |link_resistor_buy|
* - :ref:`cpn_led`
- |link_led_buy|
* - :ref:`cpn_micro_switch`
- \-
* - :ref:`cpn_capacitor`
- |link_capacitor_buy|
Schematic Diagram
-----------------
Connect the left pin of the Micro Switch to GPIO17, and two LEDs to
pin GPIO22 and GPIO27 respectively. Then when you press and release the
move arm of the Micro Switch, you can see the two LEDs light up alternately.
.. image:: ../img/image305.png
.. image:: ../img/micro_Schematic.png
Experimental Procedures
-----------------------
**Step 1:** Build the circuit.
.. image:: ../img/2.1.4fritzing.png
**Step 2**: Go to the folder of the code.
.. raw:: html
.. code-block::
cd ~/raphael-kit/c/2.1.2
**Step 3**: Compile.
.. raw:: html
.. code-block::
gcc 2.1.2_MicroSwitch.c -lwiringPi
**Step 4**: Run the executable file above.
.. raw:: html
.. code-block::
sudo ./a.out
While the code is running, press the Micro Switch, then the yellow LED lights up; release the moving arm, the red LED turns on.
.. note::
If it does not work after running, or there is an error prompt: \"wiringPi.h: No such file or directory\", please refer to :ref:`install_wiringpi`.
**Code**
.. code-block:: c
#include
#include
#define microPin 0
#define led1 3
#define led2 2
int main(void)
{
// When initialize wiring failed, print message to screen
if(wiringPiSetup() == -1){
printf("setup wiringPi failed !");
return 1;
}
pinMode(microPin, INPUT);
pinMode(led1, OUTPUT);
pinMode(led2, OUTPUT);
while(1){
// micro switch high, led1 on
if(digitalRead(microPin) == 1){
digitalWrite(led1, LOW);
digitalWrite(led2, HIGH);
printf("LED1 on\n");
}
// micro switch low, led2 on
if(digitalRead(microPin) == 0){
digitalWrite(led2, LOW);
digitalWrite(led1, HIGH);
printf(".....LED2 on\n");
}
delay(500);
}
return 0;
}
**Code Explanation**
.. code-block:: c
if(digitalRead(slidePin) == 1){
digitalWrite(led1, LOW);
digitalWrite(led2, HIGH);
printf("LED1 on\n");
}
When the moving arm of the micro switch is released, the left pin is connected to the right pin; at this time, a high level will be read on GPIO17, and then LED1 will be on and LED2 will be off.
.. code-block:: c
if(digitalRead(slidePin) == 0){
digitalWrite(led2, LOW);
digitalWrite(led1, HIGH);
printf(".....LED2 on\n");
}
When the move arm is pressed, the left pin and the middle pin are connected. At this point a low level will be read on GPIO17, then turns LED2 on and LED1 off.
Phenomenon Picture
------------------
.. image:: ../img/2.1.2micro_switch.JPG