# Gemma M0 Vibration Sensor Motion Alarm

## Overview

![](https://cdn-learn.adafruit.com/assets/assets/000/051/889/medium800/sensors_P3110526_2000.jpg?1520801298)

https://youtu.be/wuzA4dvtgkI

You can catch enemy spies in the act of touching your stuff by building a motion alarm with a vibration switch, Gemma M0, and a piezo buzzer! Any time it is moved enough to trigger the vibration switch, it will sound the alarm!!

### Adafruit GEMMA M0 - Miniature wearable electronic platform

[Adafruit GEMMA M0 - Miniature wearable electronic platform](https://www.adafruit.com/product/3501)
The **Adafruit Gemma M0** is a super small microcontroller board, with just enough built-in to create many simple projects. It may look small and cute: round, about the size of a quarter, with friendly alligator-clip sew pads. But do not be fooled! The Gemma M0 is incredibly...

In Stock
[Buy Now](https://www.adafruit.com/product/3501)
[Related Guides to the Product](https://learn.adafruit.com/products/3501/guides)
![Pink polished fingers holding a Adafruit GEMMA M0 - Miniature wearable electronic platform. ](https://cdn-shop.adafruit.com/product-videos/640x480/3501-05.jpg)

### Fast Vibration Sensor Switch (Easy to trigger)

[Fast Vibration Sensor Switch (Easy to trigger)](https://www.adafruit.com/product/1766)
The "poor man's" accelerometer/motion sensor! These spring-vibration switches are **high sensitivity** non-directional vibration induced trigger switches. Inside is a very soft spring coiled around a long metal pin. When the switch is moved, the spring touches the...

Out of Stock
[Buy Now](https://www.adafruit.com/product/1766)
[Related Guides to the Product](https://learn.adafruit.com/products/1766/guides)
![Plastic cylinder with two wires sticking out](https://cdn-shop.adafruit.com/640x480/1766-00.jpg)

### Small Enclosed Piezo w/Wires

[Small Enclosed Piezo w/Wires](https://www.adafruit.com/product/1740)
This small (14mm diameter) piezo element is nicely enclosed so you can attach easily. Piezo elements convert vibration to voltage or voltage to vibration. That means you can use this as a buzzer for making beeps, tones, and alerts, AND you can use it as a sensor to detect fast movements like...

In Stock
[Buy Now](https://www.adafruit.com/product/1740)
[Related Guides to the Product](https://learn.adafruit.com/products/1740/guides)
![Small Enclosed Piezo with two Wires](https://cdn-shop.adafruit.com/640x480/1740-02.jpg)

### 3 x AAA Battery Holder with On/Off Switch and 2-Pin JST

[3 x AAA Battery Holder with On/Off Switch and 2-Pin JST](https://www.adafruit.com/product/727)
This battery holder connects 3 AAA batteries together in series for powering all kinds of projects. We spec'd these out because the box is slim, and 3 AAA's add up to about 3.3-4.5V, a very similar range to Lithium Ion/polymer (Li-Ion) batteries and have an on-off switch. That makes...

In Stock
[Buy Now](https://www.adafruit.com/product/727)
[Related Guides to the Product](https://learn.adafruit.com/products/727/guides)
![Front angled shot of 3 x AAA battery holder with on-off switch and 2-pin JST PH connector.](https://cdn-shop.adafruit.com/640x480/727-07.jpg)

### Alkaline AAA batteries - 3 pack

[Alkaline AAA batteries - 3 pack](https://www.adafruit.com/product/3520)
Battery power for your portable project! These batteries are good quality at a good price, and work fantastic with any of the kits or projects in the shop that use AAA's. This is a pack of **3 AAA batteries**.  
  
These batteries are Alkaline (MnO2) chemistry, with a...

Out of Stock
[Buy Now](https://www.adafruit.com/product/3520)
[Related Guides to the Product](https://learn.adafruit.com/products/3520/guides)
![Angled shot of 3 PKcell AAA batteries. ](https://cdn-shop.adafruit.com/640x480/3520-00.jpg)

### USB cable - 6" A/MicroB

[USB cable - 6" A/MicroB](https://www.adafruit.com/product/898)
This here is your standard A-microB USB cable, for USB 1.1 or 2.0. Perfect for connecting a PC to your NETduino, Arduino Leonardo, Raspberry Pi (among other things).  
  
Approximately 6.5" long from end to end, its a little more compact than our classic 3' long cable  
<br...></br...>

In Stock
[Buy Now](https://www.adafruit.com/product/898)
[Related Guides to the Product](https://learn.adafruit.com/products/898/guides)
![USB Cable - 6" A/Micro B](https://cdn-shop.adafruit.com/640x480/898-01.jpg)

You'll also need three M3 x 8mm screws and four nuts to attach parts to the Gemma M0.

![](https://cdn-learn.adafruit.com/assets/assets/000/051/902/medium800/sensors_screws.jpg?1520804543)

# Gemma M0 Vibration Sensor Motion Alarm

## Make the Alarm

## How It Works

A vibration switch is a very small cylindrical sensor that has a stiff piece of metal surrounded by a spring. The spring normally doesn't touch the stiff leg, so the circuit is open. When it is moved, the spring will vibrate a bit, touching the stiff leg, and closing the circuit. You can see the copper leg with its coiled spring surrounding the silver colored leg in this cutaway photograph.

![](https://cdn-learn.adafruit.com/assets/assets/000/051/891/medium800/sensors_components_1766-01.jpg?1520801645)

By connecting one leg to analog pin **A0** and the other to **GND** , we can&nbsp;code the Gemma M0 so that it monitors the analog value of the **A0** input pin — when the spring vibrates it will send the analog pin to ground, and so the analog voltage value will decrease. When the value goes below a certain threshold, the code will send a pulse width modulated (PWM) tone signal to the piezo buzzer on **D0** to make it sound the alarm.

![](https://cdn-learn.adafruit.com/assets/assets/000/051/892/medium800/sensors_GemmaM0_Motion_Alarm_Fritzing.jpg?1520802978)

## Assembly

The Gemma M0 can't make any sounds on it's own, so let's connect the piezo buzzer to it so we can hear it!

![](https://cdn-learn.adafruit.com/assets/assets/000/051/872/medium800/sensors_IMG_1474_2k.jpg?1520755332)

## Bending Legs

We'll use the two M3 screws and nuts to connect the buzzer's legs to the&nbsp; **D0&nbsp;** and&nbsp; **GND&nbsp;** pads of the Gemma M0.

Use pliers (or your fingers) to bend the legs down, carefully, making sure not to break them off.

While the legs will bend easily to their new position, trying to move them back to their original position may be more stress than they can take.

Each leg can fit nicely into a small groove molded into the plastic case.

![sensors_IMG_1475_2k.jpg](https://cdn-learn.adafruit.com/assets/assets/000/051/873/medium640/sensors_IMG_1475_2k.jpg?1520755360)

![sensors_IMG_1476_2k.jpg](https://cdn-learn.adafruit.com/assets/assets/000/051/874/medium640/sensors_IMG_1476_2k.jpg?1520755371)

![sensors_IMG_1477_2k.jpg](https://cdn-learn.adafruit.com/assets/assets/000/051/875/medium640/sensors_IMG_1477_2k.jpg?1520755380)

## Make Feet

Use the pliers or the screws themselves to form small hook-like feet at the end of each leg, as shown here, so they will connect to&nbsp; **D0&nbsp;** and&nbsp; **GND.&nbsp;** (You can check the other side of the board to see the pad names on the silkscreen.)

![](https://cdn-learn.adafruit.com/assets/assets/000/051/876/medium800/sensors_IMG_1487_2k.jpg?1520755411)

It doesn’t matter which leg of the buzzer goes to which pad, it is not polarized.

Thread the nuts on and tighten them, being careful that the legs don't touch any copper pads on the board other than their respective pin assignments.

![](https://cdn-learn.adafruit.com/assets/assets/000/051/890/medium800/sensors_P3110518_2000.jpg?1520801502)

## Fast Vibration Sensor

OK, now that the piezo is connected, we'll add the vibration switch.

We’ll connect the vibration switch to **A0** with the thicker, silver colored leg. Use a screw and nut to connect it to the Gemma M0 as shown.

You'll use an extra nut to connect the thin wire of the vibration sensor to the same **GND** screw that's being used for the piezo buzzer.

![](https://cdn-learn.adafruit.com/assets/assets/000/051/893/medium800/sensors_P3110523_2000.jpg?1520803071)

![](https://cdn-learn.adafruit.com/assets/assets/000/051/894/medium800/sensors_P3110520_2000.jpg?1520803080)

![](https://cdn-learn.adafruit.com/assets/assets/000/051/895/medium800/sensors_P3110521_2000.jpg?1520803089)

![](https://cdn-learn.adafruit.com/assets/assets/000/051/896/medium800/sensors_P3110522_2000.jpg?1520803100)

![](https://cdn-learn.adafruit.com/assets/assets/000/051/898/medium800/sensors_P3110526_2000.jpg?1520803137)

That’s all there is to it — next we’ll program it!

# Gemma M0 Vibration Sensor Motion Alarm

## Code the Alarm with CircuitPython

## CircuitPython Prep

Follow this guide [https://learn.adafruit.com/adafruit-gemma-m0/circuitpython](../../../../adafruit-gemma-m0/circuitpython)&nbsp;to get started with coding the Gemma M0 in CircuitPython. Install the latest release version of CircuitPython on the board. You may also want to install the Mu editor&nbsp;[https://learn.adafruit.com/adafruit-gemma-m0/installing-mu-editor](../../../../adafruit-gemma-m0/installing-mu-editor)&nbsp;for your coding needs.

Once you can successfully code in Mu and upload to the board, return here.

## Alarm Code

The alarm code will do two fundamental things: measure the analog voltage value on pin **A0** to see when it drops below a certain threshold, and use PWM (pulse width modulation) to play alarm tones on the piezo buzzer over pin **D0.**

Copy the code below, paste it into Mu, and then save it to your Gemma M0 as **code.py**.

https://github.com/adafruit/Adafruit_Learning_System_Guides/blob/main/Gemma/GemmaM0_Vibration_Switch_Motion_Alarm/code.py

Test the alarm by touching or tapping it. As soon as it senses a bit of movment,&nbsp; **BEEP BEEP BEEP BEEP BEEP!!!**

# Gemma M0 Vibration Sensor Motion Alarm

## Use the Alarm

Now, you’re ready to deploy the alarm. Hook up the battery pack, set the alarm in your bag or secret hollow book, and then turn it on. When your overly curious enemy rummages around in your things, the piercing alarm will let you catch them in the act!

![](https://cdn-learn.adafruit.com/assets/assets/000/051/900/medium800/sensors_P3110527_2000.jpg?1520803323)

https://youtu.be/wuzA4dvtgkI


## Guide Products

### Adafruit GEMMA M0 - Miniature wearable electronic platform

[Adafruit GEMMA M0 - Miniature wearable electronic platform](https://www.adafruit.com/product/3501)
The **Adafruit Gemma M0** is a super small microcontroller board, with just enough built-in to create many simple projects. It may look small and cute: round, about the size of a quarter, with friendly alligator-clip sew pads. But do not be fooled! The Gemma M0 is incredibly...

In Stock
[Buy Now](https://www.adafruit.com/product/3501)
[Related Guides to the Product](https://learn.adafruit.com/products/3501/guides)
### Fast Vibration Sensor Switch (Easy to trigger)

[Fast Vibration Sensor Switch (Easy to trigger)](https://www.adafruit.com/product/1766)
The "poor man's" accelerometer/motion sensor! These spring-vibration switches are **high sensitivity** non-directional vibration induced trigger switches. Inside is a very soft spring coiled around a long metal pin. When the switch is moved, the spring touches the...

Out of Stock
[Buy Now](https://www.adafruit.com/product/1766)
[Related Guides to the Product](https://learn.adafruit.com/products/1766/guides)
### Small Enclosed Piezo w/Wires

[Small Enclosed Piezo w/Wires](https://www.adafruit.com/product/1740)
This small (14mm diameter) piezo element is nicely enclosed so you can attach easily. Piezo elements convert vibration to voltage or voltage to vibration. That means you can use this as a buzzer for making beeps, tones, and alerts, AND you can use it as a sensor to detect fast movements like...

In Stock
[Buy Now](https://www.adafruit.com/product/1740)
[Related Guides to the Product](https://learn.adafruit.com/products/1740/guides)
### 3 x AAA Battery Holder with On/Off Switch and 2-Pin JST

[3 x AAA Battery Holder with On/Off Switch and 2-Pin JST](https://www.adafruit.com/product/727)
This battery holder connects 3 AAA batteries together in series for powering all kinds of projects. We spec'd these out because the box is slim, and 3 AAA's add up to about 3.3-4.5V, a very similar range to Lithium Ion/polymer (Li-Ion) batteries and have an on-off switch. That makes...

In Stock
[Buy Now](https://www.adafruit.com/product/727)
[Related Guides to the Product](https://learn.adafruit.com/products/727/guides)
### Alkaline AAA batteries - 3 pack

[Alkaline AAA batteries - 3 pack](https://www.adafruit.com/product/3520)
Battery power for your portable project! These batteries are good quality at a good price, and work fantastic with any of the kits or projects in the shop that use AAA's. This is a pack of **3 AAA batteries**.  
  
These batteries are Alkaline (MnO2) chemistry, with a...

Out of Stock
[Buy Now](https://www.adafruit.com/product/3520)
[Related Guides to the Product](https://learn.adafruit.com/products/3520/guides)
### USB cable - 6" A/MicroB

[USB cable - 6" A/MicroB](https://www.adafruit.com/product/898)
This here is your standard A-microB USB cable, for USB 1.1 or 2.0. Perfect for connecting a PC to your NETduino, Arduino Leonardo, Raspberry Pi (among other things).  
  
Approximately 6.5" long from end to end, its a little more compact than our classic 3' long cable  
<br...></br...>

In Stock
[Buy Now](https://www.adafruit.com/product/898)
[Related Guides to the Product](https://learn.adafruit.com/products/898/guides)

## Related Guides

- [Sewable NeoPixels](https://learn.adafruit.com/flora-rgb-smart-pixels.md)
- [Best Beginner Boards for Teachers](https://learn.adafruit.com/best-beginner-boards-for-teachers.md)
- [NeoPixel NanoRing](https://learn.adafruit.com/neopixel-nanoring-gemma.md)
- [Glowing Scale Armor](https://learn.adafruit.com/glowing-scale-armor.md)
- [Clue Coffee Scale](https://learn.adafruit.com/clue-coffee-scale.md)
- [Chatty Light-Up Circuit Playground Express Mask](https://learn.adafruit.com/chatty-light-up-cpx-mask.md)
- [GEMMA M0 Case](https://learn.adafruit.com/gemma-m0-case.md)
- [The Tilt Trumpet](https://learn.adafruit.com/the-tilt-trumpet.md)
- [CircuitPython Snow Globe](https://learn.adafruit.com/circuitpython-snow-globe.md)
- [Adabot Operation Game](https://learn.adafruit.com/adabot-operation-game.md)
- [Makey Paper Craft ](https://learn.adafruit.com/makey-paper-craft.md)
- [Clockwork Goggles](https://learn.adafruit.com/gemma-m0-clockwork-goggles.md)
- [BLE Synth with the Feather nRF52840 and Circuit Playground Bluefruit](https://learn.adafruit.com/ble-synth-with-the-feather-nrf52840-and-circuit-playground-bluefruit.md)
- [Which CircuitPython Board is Right for You?](https://learn.adafruit.com/choose-your-circuitpython-board.md)
- [FruitBox Sequencer: Musically Delicious Step Pattern Generator ](https://learn.adafruit.com/circuitpython-fruitbox-sequencer-musically-delicious-step-pattern-generator.md)
- [Pico W PiCowBell Case](https://learn.adafruit.com/pico-w-picowbell-case.md)
