element14 Community
element14 Community
    Register Log In
  • Site
  • Search
  • Log In Register
  • About Us
  • Community Hub
    Community Hub
    • What's New on element14
    • Feedback and Support
    • Benefits of Membership
    • Personal Blogs
    • Members Area
    • Achievement Levels
  • Learn
    Learn
    • Ask an Expert
    • eBooks
    • element14 presents
    • Learning Center
    • Tech Spotlight
    • STEM Academy
    • Webinars, Training and Events
    • Learning Groups
  • Technologies
    Technologies
    • 3D Printing
    • FPGA
    • Industrial Automation
    • Internet of Things
    • Power & Energy
    • Sensors
    • Technology Groups
  • Challenges & Projects
    Challenges & Projects
    • Design Challenges
    • element14 presents Projects
    • Project14
    • Arduino Projects
    • Raspberry Pi Projects
    • Project Groups
  • Products
    Products
    • Arduino
    • Avnet Boards Community
    • Dev Tools
    • Manufacturers
    • Multicomp Pro
    • Product Groups
    • Raspberry Pi
    • RoadTests & Reviews
  • Store
    Store
    • Visit Your Store
    • Choose another store...
      • Europe
      •  Austria (German)
      •  Belgium (Dutch, French)
      •  Bulgaria (Bulgarian)
      •  Czech Republic (Czech)
      •  Denmark (Danish)
      •  Estonia (Estonian)
      •  Finland (Finnish)
      •  France (French)
      •  Germany (German)
      •  Hungary (Hungarian)
      •  Ireland
      •  Israel
      •  Italy (Italian)
      •  Latvia (Latvian)
      •  
      •  Lithuania (Lithuanian)
      •  Netherlands (Dutch)
      •  Norway (Norwegian)
      •  Poland (Polish)
      •  Portugal (Portuguese)
      •  Romania (Romanian)
      •  Russia (Russian)
      •  Slovakia (Slovak)
      •  Slovenia (Slovenian)
      •  Spain (Spanish)
      •  Sweden (Swedish)
      •  Switzerland(German, French)
      •  Turkey (Turkish)
      •  United Kingdom
      • Asia Pacific
      •  Australia
      •  China
      •  Hong Kong
      •  India
      •  Korea (Korean)
      •  Malaysia
      •  New Zealand
      •  Philippines
      •  Singapore
      •  Taiwan
      •  Thailand (Thai)
      • Americas
      •  Brazil (Portuguese)
      •  Canada
      •  Mexico (Spanish)
      •  United States
      Can't find the country/region you're looking for? Visit our export site or find a local distributor.
  • Translate
  • Profile
  • Settings
Experimenting with Vibration Sensors
  • Challenges & Projects
  • Design Challenges
  • Experimenting with Vibration Sensors
  • More
  • Cancel
Experimenting with Vibration Sensors
Blog Experimenting with Vibration Sensors - Characterize RPM of Spinning Devices #3 (Initial Setup & Tapping Experiment)
  • Blog
  • Forum
  • Documents
  • Polls
  • Files
  • Events
  • Mentions
  • Sub-Groups
  • Tags
  • More
  • Cancel
  • New
  • Share
  • More
  • Cancel
Group Actions
  • Group RSS
  • More
  • Cancel
Engagement
  • Author Author: zst123
  • Date Created: 10 Dec 2020 11:21 AM Date Created
  • Views 1029 views
  • Likes 3 likes
  • Comments 0 comments
Related
Recommended

Experimenting with Vibration Sensors - Characterize RPM of Spinning Devices #3 (Initial Setup & Tapping Experiment)

zst123
zst123
10 Dec 2020

Experiment 1 (Initial Setup & Tapping Experiment)

 

Objective

This experiment is suitable for beginners. I will walk through how to set up the vibration sensor to:

 

1. Turn on a red LED when vibration is detected

2. Plot a graph on a PC to observe the vibrations

 

Microcontroller Setup

To make it simple for beginners, I will be coding using Mbed which is a framework for ARM microcontrollers and very similar to the Arduino environment.

The Mbed has an online compiler through the cloud. You must register and login to their website to use the service, but there is no need to install any software.

 

I am using the Nucleo Board (NUCLEO-H743ZI2) which is Mbed-compatible. There are many powerful boards supported and you can also use other Mbed boards.

 

image

 

 

Pin Connections

Make these connections.

 

Sensor Pin

Nucleo Board

Description

Pin 1 (Red)

3V3

Power input

Pin 2 (White)

A0

Sensor Output

Pin 3 (Black)

GND

Ground

 

If you haven't read my previous post Experimenting with Vibration Sensors - Characterize RPM of Spinning Devices #2 (Introduction & Unboxing),

I have broken out the pins into a standard 2.54mm header.

image

 

After connections, your setup should look like this

As for the sensor, I mounted it firmly to the table using clear tape for now.

image

 

Code for Microcontroller

We shall use the following Mbed code.

    #include "mbed.h"

    AnalogIn sensor(A0); 
    DigitalOut green_led(LED1);
    DigitalOut red_led(LED3);

    int main() {    
        const uint16_t lower_threshold = 32768 - 7000;
        const uint16_t upper_threshold = 32768 + 7000;
        
        while(1) {
            uint16_t reading = sensor.read_u16();
            
            if (lower_threshold < reading && reading < upper_threshold) {
                green_led = true;
                red_led = false;
            } else {
                green_led = false;
                red_led = true;
            }
            printf("%d\n", reading);
            
            ThisThread::sleep_for(100ms);
       }
    }

I have also published it on my Github repo. You can check for updates there.

 

Code Explanation

 

It is a continuous loop which will do the following task indefinitely:

 

1. Sample the vibration sensor using the ADC

2. Check if the vibration detected is about a certain threshold, then toggle an LED

3. Send the vibration sensor values to the PC using the serial port (printf)

 

Sensor Threshold Explanation

When there is no vibration, the ADC reading will be exactly centered at 50%. (From the datasheet, Table 1B states that the offset voltage is “1/2 power supply voltage”.)

image

 

Depending on the direction of vibration, the voltage can go up or down in one event. Hence, we need to check for 2 thresholds, an upper and lower threshold.

image

 

Note that this board has a 16-bit ADC, where 50% is 32768. The thresholds are set around this nominal value.

 

Guide for Mbed programming

 

First create an account and login to the Mbed IDE website using your account.

  • https://ide.mbed.com/


Go to the NUCLEO-H743ZI2 platform page and click on “Add to your Mbed Compiler”

  • https://os.mbed.com/platforms/ST-Nucleo-H743ZI2/

image

Once it is confirmed that the platform has been added, go back to the IDE.

image

 

Click the top right button to change the platform.

image

A window will pop up with all your boards. Select NUCLEO-H743ZI2.

image

 

Let’s first import an example Blinky program to ensure our board works properly.

 

Open up the mbed-os-example-blinky page and click on “Import into Compiler”.

  • https://os.mbed.com/teams/mbed-os-examples/code/mbed-os-example-blinky/

 

image

 

Enter your own project name and click on Import.

image

The project will be seen in the Workspace and you can open up main.cpp.

image

 

On the top, click on Compile. It will download the hex file.

image

 

Connect the Nucleo board and a NUCLEO drive will appear. Drag & drop the hex file into the NUCLEO drive to program it.

image

 

Verify that the board LED is blinking.

image

—

 

Now, replace with our code from above.

image

 

Compile and drag & drop onto the drive again.

 

We are ready to do our first test!

 

First Test Results

 

I have created the code above, that when the amount of vibration reaches a threshold value that we have set, the Red LED will turn on

 

Here, by tapping on the sensor, we can confirm the behavior as the LED is seen toggling on and off.

You don't have permission to edit metadata of this video.
Edit media
x
image
Upload Preview
image

 

In my code, the microcontroller will also write the values to the PC using the serial port.

 

It is possible to visualise the values by doing a plot of the values over time.

The most simple one is to use the Arduino Serial Plotter tool.

image

 

Tapping will cause a short vibration. This is the graph of how the sensor behaves when tapping on the sensor.

You don't have permission to edit metadata of this video.
Edit media
x
image
Upload Preview
image

 

Hard vs Soft Taps

image

 

Problems faced

 

However, we now face a small problem… If you have noticed, I have added a delay of 100ms in the code. This is because if I reduce the delay, it will be too fast and the Arduino Serial Plotter will start to lag.

In the next blog post, I will code my own graph plotter program using Python to display extremely fast data. In the future experiments, it is necessary to get rid of the delay to plot a smoother graph and also do more accurate analysis.

 

Conclusion

 

In this post, I demonstrated how we can use the Mbed online compiler to quickly get the project code up and running.

We learnt that this sensor is very sensitive and we can distinguish the hard taps and soft taps. The graph can be plotted on the PC.

image

 

Although I have faced a small setback in plotting the graph, I will solve it in the next experiment! Please leave a comment if you would like to suggest anything I should try.

  • Sign in to reply
element14 Community

element14 is the first online community specifically for engineers. Connect with your peers and get expert answers to your questions.

  • Members
  • Learn
  • Technologies
  • Challenges & Projects
  • Products
  • Store
  • About Us
  • Feedback & Support
  • FAQs
  • Terms of Use
  • Privacy Policy
  • Legal and Copyright Notices
  • Sitemap
  • Cookies

An Avnet Company © 2025 Premier Farnell Limited. All Rights Reserved.

Premier Farnell Ltd, registered in England and Wales (no 00876412), registered office: Farnell House, Forge Lane, Leeds LS12 2NE.

ICP 备案号 10220084.

Follow element14

  • X
  • Facebook
  • linkedin
  • YouTube