Raspberry Pi Pico Tips and Tricks

Tuesday 11 April 2023

Sensing vibration with a Raspberry Pi Pico

Vibration sensors are designed to convert mechanical movement into a signal that can be measured.

I've written this short explanation as part of the much larger book 'Raspberry Pi Pico Tips and Tricks'. You can download it for free from here.

The underlying principles behind vibration sensors vary depending on the type of sensor, but they generally rely on the conversion of mechanical energy (vibrations) into some other form of energy that can be measured and analysed. The most common types of vibration sensors are based on the following principles:

  • Piezoelectricity: Piezoelectric sensors rely on the effect which occurs when certain materials, such as quartz, generate an electrical charge in response to applied mechanical stress.
  • Capacitance: Accelerometers and displacement sensors often use the principle of capacitance, where changes in the capacitance between two electrodes is proportional to changes in the distance between the electrodes. When the sensor is subjected to a vibration, the distance between the electrodes changes, and this change in distance can be used to measure the vibration.
  • Inductance: Inductive sensors measure the change in inductance in a coil caused by the movement of a ferromagnetic core inside the coil. When the ferromagnetic core is subjected to a vibration, the inductance changes, and this change can be used to measure the vibration.
  • Magnetic field: Magnetic sensors work when changes in a magnetic field is generated by the movement of a ferromagnetic material inside a sensor.
  • Light: Optical sensors use the principle of light and optics, where changes in the light that is transmitted or reflected through the sensor are proportional to changes in the position of a reflecting surface inside the sensor.

In each of these cases, the output signal from the sensor is proportional to the vibration being measured, and the signal can be analysed to determine the frequency, amplitude, and other characteristics of the vibration.

There are several types of movement that vibration sensors can be measure including:

  • Acceleration
  • Velocity
  • Displacement

Each type of vibration sensor has its own strengths and weaknesses, and the choice of sensor will depend on the specific application, including the type of vibration to be measured, the frequency range and the operating environment.

Piezoelectric vibration sensor

A piezoelectric vibration sensor works by converting mechanical energy (vibrations) into electrical energy. The sensor is made up of a piezoelectric material, such as quartz, which has the ability to generate an electrical charge when subjected to mechanical stress.

Vibration Sensor Connection

When a vibration is applied to the piezoelectric sensor, the piezoelectric material deforms and generates an electrical charge proportional to the magnitude of the vibration. This electrical charge can be measured and used to determine the frequency and amplitude of the vibration.

The piezoelectric sensor can be used in a wide range of applications, including measuring vibrations in machinery, detecting structural movements in buildings, and measuring the impact of shock or drop events. The sensors can also be used to monitor and control the vibrations of musical instruments or in anti-tampering systems.

Piezoelectric sensors are generally rugged, reliable, and highly sensitive, making them a popular choice for many applications that require the measurement of vibration or impact

The unit that we will test comes in two parts. There is an interface board which incorporates two screw terminals which attach to the sensor proper.

Piezoelectric vibration sensor

The interface board will connect to the Pico as an analogue device with the addition of a power supply connection that can be used at 5 or 3.3V.

Connecting everything up

We will want to connect;

  • + on the interface board to the 3V3 pin (36) on the Pico
  • - on the interface board to the Ground pin (38) on the Pico
  • S on the interface board to ADC0 pin (31) on the Pico

The power and ground pins are fairly self explanatory, and because the vibration sensor’s interface board has an analogue output, we will apply signal output (S) to one of our Analogue to Digital Converter (ADC) pins. In this case ADC0 on pin 31 (GPIO26).

Vibration Sensor Connection

Connecting the interface board to the Pico practically can be achieved in a number of ways. But because the connection is relatively simple we can build a minimal configuration that will plug directly onto the pins using Dupont header connectors and jumper wire.

Vibration Sensor Connection

Code

The following code takes 20 readings from the analogue connection on GPIO26 (ADC0) in quick succession. It then discards the maximum and minimum values in the set and averages the remainder. This is done so that the sensor can get a ‘reading’ of a vibration that represents a value over a slightly longer period of time than a single reading. This is useful since the sensor can respond so quickly to a movement, that it may be possible for low frequency vibrations or impacts to not get a true representation

from machine import ADC, Pin
import time

# Sensor reading connection 
adc = ADC(Pin(26))

# Number of repetitions per reading
n = 20

while True:
    reading_group = []
    
    for x in range(n):
        new_value = adc.read_u16()
        reading_group.append(new_value)
        time.sleep(.01)
    
    # Get rid of the outliers
    reading_group.remove(max(reading_group))
    reading_group.remove(min(reading_group))
    
    # Get the average
    vibration = sum(reading_group)/ len(reading_group)
    
    print(vibration)

To test the code we can run it with the sensor taped to a suitably ‘vibrat-y’ device. I tried both a electric knife and a battery powered jig-saw. Both produced good results. Ultimately I have a plan in mind to attach them to my water pump to tell how long it is operating for and to the aeration pump on my septic system (that runs continuously) so that I can be alerted if it fails or if part of it fails (like a diaphragm) which would alter the nature of the vibration.

Don't forget, if you're looking for the book 'Raspberry Pi Pico Tips and Tricks'. You can download it for free (or donate if you wish) from here.

No comments:

Post a Comment