element14 Community
element14 Community
    Register Log In
  • Site
  • Search
  • Log In Register
  • Members
    Members
    • Benefits of Membership
    • Achievement Levels
    • Members Area
    • Personal Blogs
    • Feedback and Support
    • What's New on element14
  • Learn
    Learn
    • Learning Center
    • eBooks
    • STEM Academy
    • Webinars, Training and Events
    • More
  • Technologies
    Technologies
    • 3D Printing
    • FPGA
    • Industrial Automation
    • Internet of Things
    • Power & Energy
    • Sensors
    • More
  • Challenges & Projects
    Challenges & Projects
    • Design Challenges
    • element14 presents
    • Project14
    • Arduino Projects
    • Raspberry Pi Projects
    • More
  • Products
    Products
    • Arduino
    • Dev Tools
    • Manufacturers
    • Raspberry Pi
    • RoadTests & Reviews
    • Avnet Boards Community
    • More
  • 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
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
  • Events
  • Mentions
  • Sub-Groups
  • Tags
  • More
  • Cancel
  • New
Blog Post Actions
  • Subscribe by email
  • More
  • Cancel
  • Share
  • Subscribe by email
  • More
  • Cancel
Group Actions
  • Group RSS
  • More
  • Cancel
Engagement
  • Author Author: zst123
  • Date Created: 10 Dec 2020 11:21 AM Date Created
  • Views 308 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.

 

 

 

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.

 

After connections, your setup should look like this

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

 

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”.)

 

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.

 

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/

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

 

Click the top right button to change the platform.

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

 

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/

 

 

Enter your own project name and click on Import.

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

 

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

 

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

 

Verify that the board LED is blinking.

—

 

Now, replace with our code from above.

 

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
Upload Preview

 

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.

 

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
Upload Preview

 

Hard vs Soft Taps

 

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.

 

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.

Anonymous
Element14

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 © 2022 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

  • Facebook
  • Twitter
  • linkedin
  • YouTube