How to Use the SW-18010P Vibration Sensor with STM32: Full Wiring and Code Guide
This tutorial explains the SW-18010P vibration sensor’s features, pinout, and operating principle, then provides detailed hardware wiring, LED initialization, sensor initialization, interrupt handling, and a complete STM32F103C8T6 firmware example that lights a three‑color LED based on vibration detection.
1. Vibration Module Overview
1.1 Model Introduction
Several vibration‑sensor modules exist (e.g., 801S, SW‑520D, SW‑420), but this guide focuses on the SW‑18010P. Connect VCC and GND to power the module; the onboard indicator LED lights when vibration is detected and turns off when vibration stops. Sensitivity can be adjusted with the potentiometer by rotating it to the left or right.
1.2 Working Parameters and Pinout
The SW‑18010P is a sealed spring‑type, non‑directional vibration switch that can be triggered from any angle. Under normal use its switch life can reach 200 000 cycles. Pin definitions are:
VCC : 3.3 V or 5 V power supply
GND : Ground
DO : Digital output, can be connected to any GPIO pin
AO : Analog output (not used in this example)
2. SW‑18010P Operating Principle
When stationary the module is in an open‑circuit OFF state. Upon receiving sufficient external force or acceleration, the contacts close briefly, producing an ON state. The DO pin outputs a high level when there is no vibration and a low level when the vibration exceeds the set threshold. The module’s response time to small vibrations is very short, so it cannot directly drive a relay.
3. Communication Diagram
The goal is to control a three‑color LED: green lights (red off) when there is no vibration, and red lights (green off) when vibration is detected. This setup can be used for anti‑theft alarms in museums or display cases.
4. Practical Implementation
4.1 Hardware Wiring
The hardware required for this tutorial includes:
Microcontroller: STM32F103C8T6
Vibration module: SW‑18010P
Three‑color LED module
Programmer: ST‑LINK V2
Typical connections are:
VCC → 5 V
GND → GND
DO → STM32 pin A4 (red LED control)
DO → STM32 pin A5 (green LED control)
Additional LED pins: A7 → Red LED, G → Green LED, GND → GND
4.2 LED Initialization
void led_init(void)
{
GPIO_InitTypeDef gpio_init_struct;
LED1_GPIO_CLK_ENABLE(); /* LED1 clock enable */
LED3_GPIO_CLK_ENABLE(); /* LED3 clock enable */
gpio_init_struct.Pin = LED1_GPIO_PIN;
gpio_init_struct.Mode = GPIO_MODE_OUTPUT_PP;
gpio_init_struct.Pull = GPIO_PULLUP;
gpio_init_struct.Speed = GPIO_SPEED_FREQ_HIGH;
HAL_GPIO_Init(LED1_GPIO_PORT, &gpio_init_struct);
gpio_init_struct.Pin = LED3_GPIO_PIN;
HAL_GPIO_Init(LED3_GPIO_PORT, &gpio_init_struct);
LED1(0); /* turn off LED1 */
LED3(0); /* turn off LED3 */
}LED header file (simplified):
#ifndef _LED_H
#define _LED_H
#include "sys.h"
/* Pin definitions */
#define LED1_GPIO_PORT GPIOA
#define LED1_GPIO_PIN GPIO_PIN_5
#define LED1_GPIO_CLK_ENABLE() do{ __HAL_RCC_GPIOA_CLK_ENABLE(); }while(0)
#define LED3_GPIO_PORT GPIOA
#define LED3_GPIO_PIN GPIO_PIN_7
#define LED3_GPIO_CLK_ENABLE() do{ __HAL_RCC_GPIOA_CLK_ENABLE(); }while(0)
/* Macros to control LEDs */
#define LED1(x) do{ x ? HAL_GPIO_WritePin(LED1_GPIO_PORT, LED1_GPIO_PIN, GPIO_PIN_SET) : HAL_GPIO_WritePin(LED1_GPIO_PORT, LED1_GPIO_PIN, GPIO_PIN_RESET); }while(0)
#define LED3(x) do{ x ? HAL_GPIO_WritePin(LED3_GPIO_PORT, LED3_GPIO_PIN, GPIO_PIN_SET) : HAL_GPIO_WritePin(LED3_GPIO_PORT, LED3_GPIO_PIN, GPIO_PIN_RESET); }while(0)
#define LED1_TOGGLE() do{ HAL_GPIO_TogglePin(LED1_GPIO_PORT, LED1_GPIO_PIN); }while(0)
#define LED3_TOGGLE() do{ HAL_GPIO_TogglePin(LED3_GPIO_PORT, LED3_GPIO_PIN); }while(0)
void led_init(void);
#endif4.3 Vibration Module Initialization
void sw_init(void)
{
GPIO_InitTypeDef gpio_initstruct;
SW_CLK(); /* enable IO clock */
gpio_initstruct.Pin = SW_PIN; /* DO pin */
gpio_initstruct.Mode = GPIO_MODE_IT_FALLING; /* falling‑edge trigger */
gpio_initstruct.Pull = GPIO_NOPULL; /* no pull‑up/down */
HAL_GPIO_Init(SW_GPIO, &gpio_initstruct);
HAL_NVIC_SetPriority(EXTI4_IRQn, 2, 0); /* priority */
HAL_NVIC_EnableIRQ(EXTI4_IRQn); /* enable EXTI */
}4.4 Handling Vibration Interrupts
uint16_t get_swflag(void)
{
return swflag;
}
void set_swflag(uint16_t value)
{
swflag = value;
}
void EXTI4_IRQHandler(void)
{
HAL_GPIO_EXTI_IRQHandler(GPIO_PIN_4);
__HAL_GPIO_EXTI_CLEAR_IT(GPIO_PIN_4);
}
void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin)
{
if (GPIO_Pin == SW_PIN)
{
if (HAL_GPIO_ReadPin(SW_GPIO, SW_PIN) == GPIO_PIN_RESET) // vibration detected
{
swflag = 1;
}
}
}4.5 Main Function
int main(void)
{
HAL_Init();
sys_stm32_clock_init(RCC_PLL_MUL9); // 72 MHz
delay_init(72);
led_init();
sw_init();
while (1)
{
if (get_swflag()) // vibration detected
{
HAL_GPIO_WritePin(LED1_GPIO_PORT, LED1_GPIO_PIN, GPIO_PIN_SET); // red on
HAL_GPIO_WritePin(LED3_GPIO_PORT, LED3_GPIO_PIN, GPIO_PIN_RESET); // green off
delay_ms(500);
set_swflag(0);
}
else // no vibration
{
HAL_GPIO_WritePin(LED3_GPIO_PORT, LED3_GPIO_PIN, GPIO_PIN_SET); // green on
HAL_GPIO_WritePin(LED1_GPIO_PORT, LED1_GPIO_PIN, GPIO_PIN_RESET); // red off
}
}
}4.6 Running Result
When the board is idle, the green LED stays on and the red LED is off. Tapping or shaking the breadboard causes the sensor to detect vibration, turning the green LED off and the red LED on.
5. Conclusion
As vibration‑sensor technology continues to evolve, future modules will offer higher sensitivity, lower power consumption, and broader application scenarios, ranging from smart home safety to industrial monitoring. This guide provides a solid foundation for integrating the SW‑18010P into embedded projects.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Liangxu Linux
Liangxu, a self‑taught IT professional now working as a Linux development engineer at a Fortune 500 multinational, shares extensive Linux knowledge—fundamentals, applications, tools, plus Git, databases, Raspberry Pi, etc. (Reply “Linux” to receive essential resources.)
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
