.. 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.2.6_c: 2.2.6 Speed Sensor Module =============================== Introduction ------------------ In this project, we will learn the use of the speed sensor module. A Speed Sensor Module is a type of tachometer that is used to measure the speed of a rotating object like a motor. Required Components ------------------------------ In this project, we need the following components. .. image:: ../img/2.2.6component.png :width: 700 :align: center 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_speed_sensor` - \- Schematic Diagram ----------------------- .. image:: ../img/2.2.6circuit.png :width: 400 :align: center Experimental Procedures ------------------------------ **Step 1:** Build the circuit. .. image:: ../img/2.2.6fritzing.png :width: 700 :align: center **Step 2:** Change directory. .. raw:: html .. code-block:: cd ~/raphael-kit/c/2.2.6/ **Step 3:** Compile. .. raw:: html .. code-block:: gcc 2.2.6_speed_sensor_module.c -lwiringPi **Step 4:** Run. .. raw:: html .. code-block:: sudo ./a.out After the code runs, the green LED will light up. If you place an obstacle in the gap of the speed sensor module, the "light blocked" will be printed on the screen and the red LED will be lit. Remove the obstacle and the green LED will light up again. .. 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 speedPin 0 #define Gpin 2 #define Rpin 3 void LED(int color) { pinMode(Gpin, OUTPUT); pinMode(Rpin, OUTPUT); if (color == 0){ digitalWrite(Rpin, HIGH); digitalWrite(Gpin, LOW); } else if (color == 1){ digitalWrite(Rpin, LOW); digitalWrite(Gpin, HIGH); } } void Print(int x){ if ( x == 0 ){ printf("Light was blocked\n"); } } int main(void){ if(wiringPiSetup() == -1){ //when initialize wiring failed,print messageto screen printf("setup wiringPi failed !"); return 1; } pinMode(speedPin, INPUT); int temp; while(1){ //Reverse the input of speedPin if ( digitalRead(speedPin) == 0 ){ temp = 1; } if ( digitalRead(speedPin) == 1 ){ temp = 0; } LED(temp); Print(temp); } return 0; } **Code Explanation** .. code-block:: c void LED(int color) { pinMode(Gpin, OUTPUT); pinMode(Rpin, OUTPUT); if (color == 0){ digitalWrite(Rpin, HIGH); digitalWrite(Gpin, LOW); } else if (color == 1){ digitalWrite(Rpin, LOW); digitalWrite(Gpin, HIGH); } } Set a ``LED()`` function to control the 2 LEDs, the parameter of this function is ``color``. When ``color`` is 0, set ``Rpin`` to ``HIGH`` (light up the red LED) and ``Gpin`` to ``LOW`` (turn off the green LED); when ``color`` is 1, then light up the green LED and turn off the red LED. .. code-block:: c while(1){ //Reverse the input of speedPin if ( digitalRead(speedPin) == 0 ){ temp = 1; } if ( digitalRead(speedPin) == 1 ){ temp = 0; } LED(temp); Print(temp); } When you place an obstacle in the gap of the speed sensor module, ``speedPin`` is low level (0), then call ``LED(1)`` function to light up the green LED and "Light was blocked!" is printed. **Phenomenon Picture** ----------------------- .. image:: ../img/2.2.6photo_interrrupter.JPG :width: 500 :align: center