stm32 projects

SW-420 Vibration Sensor and Buzzer with STM32F103C8T6 using STM32CubeIDE

Introduction

SW-420 Vibration Sensor and Buzzer with STM32F103C8T6 using STM32CubeIDE- The SW-420 Vibration Sensor is a widely used sensor that can detect vibrations and movements. It is commonly used in various applications such as earthquake detection, and industrial machinery monitoring. In this article, we will explore how to interface the SW-420 Vibration Sensor and Buzzer with STM32F103C8T6 microcontroller using STM32CubeIDE, a powerful integrated development environment.




Project overview:

In this project, we will create a simple vibration alert system using STM32F103C8T6. To detect vibrations, we will use the SW-420 vibration sensor, and for the alert, we will use a buzzer. For programming, we will use STM32CubeIDE. When a vibration is detected, the buzzer will turn on for a few seconds and then turn off.

Components Link:

Product Source Source
STM32F103C8T6 Amazon AliExpress
Buzzer Amazon AliExpress
ST-Link V2 Amazon AliExpress
Breadboard Amazon AliExpress
Jumper Wires Amazon AliExpress

*Please Note: These are affiliate links. I may make a commission if you buy the components through these links. I would appreciate your support in this way!



About STM32F103C8T6:

SW-420 Vibration Sensor with STM32F103C8T6

STM32F103C8T6 microcontrollers are widely used in various applications due to their high performance, low power consumption, and extensive peripheral set. They are suitable for a range of applications, from simple home automation projects to complex industrial controls.

Feature Specification of STM32F103C8T6
Core ARM Cortex-M3
Operating Frequency Up to 72 MHz
Flash Memory 64 KB
RAM 20 KB
Supply Voltage 2.0V to 3.6V
I/O Pins 37 GPIOs
Timers Three 16-bit timers, one PWM timer
ADC 2x 12-bit, 10 channels, up to 1 MSPS
DAC Not available
Communication Interfaces 2x I2C, 3x USART, 2x SPI, 1x CAN
PWM Channels Available through timers
DMA Channels 7x DMA Channels
Operating Temperature -40°C to 85°C
Package Type LQFP48
Energy Features Low-power modes (Sleep, Stop, Standby)
Security Features Not available
Debugging JTAG and Serial Wire Debug (SWD)
USB USB 2.0 Full-speed device
CRC Calculation Unit

Yes



What is the SW-420 Vibration Sensor?

SW-420 Vibration Sensor with STM32F103C8T6

The SW-420 Vibration Sensor is a module that consists of a spring-based vibration sensor and a LM393 dual comparator chip. The sensor detects vibrations and converts them into electrical signals, which can then be processed by a microcontroller. It has a wide voltage range and is compatible with various microcontrollers, including the STM32F103C8T6.

Feature Specification of SW-420 Vibration Sensor
Operating Voltage Typically 3.3V to 5V
Digital Output Logic High when vibration is detected
Sensitivity Adjustable via onboard potentiometer
Current Consumption Very low, typically in the microampere range
Output Type Digital signal (High/Low)
Operating Temperature Range Usually -10°C to 70°C
Dimensions Small, compact size (varies by manufacturer)
Mounting Typically through-hole or with mounting holes on PCB
Detection Threshold Adjustable, dependent on potentiometer setting
Response Time Fast, typically milliseconds
Durability Designed for long-term use
Application Scope

Ideal for alarm systems, motion detection, automation, etc.



About ST-Link V2

SW-420 Vibration Sensor with STM32F103C8T6

The ST-Link V2 is a debugger and programmer for the STM8 and STM32 microcontroller families. It’s manufactured by STMicroelectronics, a leading company in the semiconductor market. The ST-Link V2 provides an affordable and easy-to-use solution for debugging and programming applications.

Here are some key features and aspects of the ST-Link V2:

Compatibility: It supports the full range of STM8 and STM32 microcontrollers for development purposes. This includes support for the entire STM32 family, which is quite popular among embedded systems developers.

Interface Support: The device interfaces with the computer via USB and connects to the target microcontroller through various types of connectors, including JTAG and SWD (Serial Wire Debug) interfaces.

Debugging Capabilities: It offers extensive debugging features. This includes not only basic functionalities like run, halt, and step control but also more advanced features such as setting breakpoints, watchpoints, and examining the state of the microcontroller’s memory and registers.

Programming: The ST-Link V2 can be used for programming the microcontroller’s flash memory. It’s quite efficient in flashing a wide range of devices, and the programming process is usually straightforward and fast.

Software Support: It is compatible with several development environments and software tools, including ST’s own Integrated Development Environments (IDEs) like STM32CubeIDE and other popular tools such as Keil MDK, IAR EWARM, and AC6 SW4STM32.

Portability and Size: The device is quite compact and portable, making it a convenient tool for development and field debugging.

Firmware Upgrades: ST frequently releases firmware upgrades for the ST-Link V2, ensuring it stays up-to-date with the latest microcontrollers and features.

Cost-Effective: One of the key attractions of the ST-Link V2 is its affordability. It provides professional-level debugging and programming capabilities at a fraction of the cost of more expensive tools.

Clones and Variants: There are numerous clones and variants of the ST-Link V2 available in the market, some of which offer similar functionalities at a lower price. However, it’s important to be cautious with these versions regarding compatibility and support.




Creating Project in STM32CubeIDE:

open your STM32CubeIDE. Click on “File” in the menu, then choose “New,” and under “New,” click on “STM32 Project.”

SW-420 Vibration Sensor with STM32F103C8T6

In the “Target Selection” window, search for the specific STM32 microcontroller you are using. I am using the STM32F103C8T6, so I entered this in the “Commercial Part Number” field. Select your STM32 board and click “Next.”

SW-420 Vibration Sensor with STM32F103C8T6

In the “Setup STM32 Project” window, choose a name for your project. I selected “vibrationDetection” and left the other options as default. After that, click “Finish.”

SW-420 Vibration Sensor with STM32F103C8T6

In the “Pinout and Configuration” window, click on the “System Core” tab. Under “System Core,” click on “SYS,” and choose “Serial Wire” as the debug mode.

SW-420 Vibration Sensor with STM32F103C8T6

I have connected the SW-420 Vibration Sensor to PB9, so I will select PB9 as an input GPIO. 

SW-420 Vibration Sensor with STM32F103C8T6

Additionally, I connected a buzzer to PB8, so I selected PB8 as an output GPIO.

SW-420 Vibration Sensor with STM32F103C8T6

After selecting the GPIO pins, click on the “Save” button to generate the code.”

SW-420 Vibration Sensor with STM32F103C8T6

After generating the code, a file named ‘main.c’ will be created.

SW-420 Vibration Sensor with STM32F103C8T6

If you are using a buzzer module with STM32F103C8T6, then instead of this configuration and code, we use PWM and frequency configuration, which we will discuss in detail in the next article.



Circuit Diagram:

SW-420 Vibration Sensor with STM32F103C8T6

The VCC and GND pins of the SW-420 Vibration Sensor are connected with the STM32F103C8T6 5v and GND pins. The D0 pin of the SW-420 Vibration Sensor is connected with the STM32F103C8T6 PB9 pin.

The red wire of the 5V buzzer is connected with the STM32F103C8T6 5v pin. The ground pin of the 5V buzzer is connected with the collector of the 2n2222 NPN transistor. The Base of the transistor is connected with the STM32F103C8T6 PB8 pin through a 10K-ohm resistor. The emitter of the transistor is connected with the ground pin of the STM32F103C8T6. So, that’s all about the connections.

SW-420 Vibration Sensor with STM32F103C8T6 Program:

Copy the below code and paste it into the while section.

SW-420 Vibration Sensor with STM32F103C8T6



Stm32CubeIDE Project Uploading to STM32:

Before uploading the SW-420 vibration sensor and buzzer project, ensure to connect the ST-Link V2 with the STM32F103C8T6 microcontroller. The connection diagram is provided below:

SW-420 Vibration Sensor with STM32F103C8T6

Connect the SWDIO (Serial Wire Debug Input/Output) pin of the ST-Link V2 to the SWDIO pin on the STM32F103C8T6 microcontroller. This allows bidirectional communication between the ST-Link V2 and the microcontroller.

Connect the SWCLK (Serial Wire Clock) pin of the ST-Link V2 to the SWCLK pin on the STM32F103C8T6 microcontroller. This clock signal synchronizes data transmission between the two devices.

Establish a common ground reference by connecting the GND pin of the ST-Link V2 to the GND pin on the STM32F103C8T6 microcontroller.

Provide power to the STM32 microcontroller by connecting the 3.3V pin of the ST-Link V2 to the 3.3V power supply on the STM32F103C8T6 board.

Once the connection between Jab ST-Link V2 and STM32F103C8T6 is complete, connect the ST-Link V2 to your PC or laptop, and then click on the “build” button.

SW-420 Vibration Sensor with STM32F103C8T6

Once the build processing is complete, click on the green play button. After clicking the play button, the “Edit Launch Configuration Properties” window will open.

SW-420 Vibration Sensor with STM32F103C8T6

In this window, navigate to the “Debugger” menu and select “Debug Probe” as “ST-Link (OpenOCD).”

SW-420 Vibration Sensor with STM32F103C8T6

Then, click on the “Show Generator Options” and choose “Software System Reset” as the reset mode.

SW-420 Vibration Sensor with STM32F103C8T6

SW-420 Vibration Sensor with STM32F103C8T6

Once you’ve made these selections, click on the “Apply” button to save all the changes. Finally, press the “OK” button to upload the program.

SW-420 Vibration Sensor with STM32F103C8T6

When you see the message “shutdown command invoked” on your console, disconnect the ST-Link V2 from your PC or laptop. Then, reconnect the STM32F103C8T6 to your PC, laptop, or any other 5V power source. Afterward, proceed to practically test your project.

SW-420 Vibration Sensor with STM32F103C8T6



Conclusion

In this article, we have explored how to interface the SW-420 Vibration Sensor and Buzzer with STM32F103C8T6 microcontroller using STM32CubeIDE. We discussed the basics of the SW-420 Vibration Sensor, the setup process for STM32CubeIDE, and the steps to interface the sensor and buzzer with the microcontroller. By following these guidelines and customizing the code to your specific needs, you can effectively incorporate the SW-420 Vibration Sensor and buzzer into your projects.

Remember to refer to the datasheet and documentation of the SW-420 Vibration Sensor and the STM32F103C8T6 microcontroller for detailed specifications and additional information.

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button