How to Use Tilt Sensors With Arduino - Lesson #12

by lucascreator in Circuits > Arduino

27 Views, 1 Favorites, 0 Comments

How to Use Tilt Sensors With Arduino - Lesson #12

instructables-cover.png

Learn how to use the tilt (shake) sensors with Arduino.

Supplies

20251026_103026.jpg

1 x DFRobot MindPlus Arduino Coding Kit

1 x DFRobot I/O Expansion Shield

1 x DFRobot Digital RGB LED Module

1 x DFRobot Digital Shake Sensor

1 x Arduino UNO

Jumper wires (generic)

YouTube Tutorial

How to Use Tilt Sensors with Arduino - Lesson #12

I've recently posted a tutorial about this project on YouTube explaining everything you can read on this article. You can watch it right above.

Introduction

Have you ever wondered how Arduino can detect a shake and respond instantly?

In today’s lesson, we’re going to make a fun project where a simple shake sensor changes the color of an RGB LED every time you move it.

Welcome to lesson #12 of the Arduino for Beginners series!

By the end of this tutorial, you’ll not only understand how a vibration-based tilt sensor works but also build an interactive RGB LED system that reacts to motion.

Sounds fun? Let’s get started.

Explanation

20251024_112406.jpg
16-4.jpg
10.jpg

Tilt and motion sensors (photo 1) are used everywhere, from airplanes and smartphones to smartwatches and industrial robots. They allow devices to "feel" their orientation or movement, helping systems react to motion in real time.

There are several kinds of tilt and motion sensors:

  1. Accelerometer-based sensors - measure acceleration due to gravity to determine tilt and inclination.
  2. Gyroscope-based sensors - detect angular rotation, often combined with accelerometers in IMUs (Inertial Measurement Units).
  3. Vibration-based tilt sensors - use mechanical motion to detect movement without complex electronics.

In this lesson, we’ll use the vibration-based tilt sensor, often called a shake sensor (photo 2).

Inside this tiny component, there’s a metal ball or spring (photo 3) that connects two internal pins whenever the sensor moves. When the sensor is at rest, the circuit is open. When shaken, the ball (or spring) temporarily bridges the connection, closing the circuit and generating a digital signal that Arduino can detect.

Different models detect movement in one direction or multiple directions. The one included in the MindPlus Arduino Coding Kit (the one I’m using here) detects motion in one direction, but it’s very reliable and has strong anti-shock capability (perfect for simple interactive projects like this LED color switcher).

When combined with an RGB LED, it becomes a great way to visualize motion detection. Each shake will make your LED flash a new color, creating a dynamic light display that responds to touch and movement.

Sponsor

20250609_162406.jpg
20250609_162454.jpg
20250609_162534.jpg
20250609_162545.jpg

If you’re curious about the MindPlus Arduino Coding Kit, it’s an excellent all-in-one set for beginners.

It includes sensors, jumper wires, an LCD display, and much more - basically everything you need to learn Arduino through hands-on experiments.

This kit is provided by DFRobot, the sponsor of this educational series.

DFRobot is one of the world’s leading open-source hardware providers, offering products ranging from simple modules and sensors to AI-powered microcontrollers and IoT development boards.

I personally recommend their kits because they’re affordable, high-quality, and well-supported (ideal for both students and makers just getting started).

Big thanks to DFRobot for supporting this series and helping make STEM education accessible to everyone.

Project

20251026_103327.jpg
20251026_103051.jpg
17-1.jpg
17-2.jpg
17-3.jpg
18.png

Now let’s move to the practical part: building the shake sensor LED project (photo 1).

In this setup, the shake sensor will act as a switch. Every time you move or tap it, the RGB LED will change to the next color in a sequence.

You’ll need (photo 2)

  1. Arduino UNO
  2. I/O Expansion Shield
  3. Digital RGB LED Module
  4. Shake Sensor
  5. Jumper Wires

After you grab all components, connect them like this:

  1. Attach the I/O Expansion Shield to your Arduino UNO (photo 3).
  2. Connect the RGB LED module to port 2 (photo 4).
  3. Connect the shake sensor to port 3 (photo 5).

That’s it! It’s a simple plug-and-play setup.

You can also follow the wiring diagram to assist you (last photo)

Code

20-1.png
20-2.png
20251026_103444.jpg
20251026_103454.jpg
20251026_103501.jpg

Once the hardware is connected, open the Arduino IDE and copy today’s code from the GitHub repository.

Now let’s walk through how it works.

We start by defining the pins and variables and setting up the shake sensor as INPUT_PULLUP.

#include <Adafruit_NeoPixel.h>

// Define pins
#define LED_PIN 2
#define SHAKE_SENSOR_PIN 3
Adafruit_NeoPixel pixel = Adafruit_NeoPixel(1, LED_PIN, NEO_GRB + NEO_KHZ800);

// Variables
int option = 0;
int lastShakeState = HIGH;
int currentShakeState;

void setup() {
pinMode(SHAKE_SENSOR_PIN, INPUT_PULLUP);
pixel.begin();
pixel.show();
updateLED();
}

In the loop, we constantly read the sensor’s state:

currentShakeState = digitalRead(SHAKE_SENSOR_PIN);

When the sensor detects a shake, the state changes from HIGH to LOW - that’s our trigger.

Each time this happens, we increase a variable called option by one unit and call the updateLED() function.

if (lastShakeState == HIGH && currentShakeState == LOW) {
// Increment option, wrap around from 7 to 0
option = (option + 1) % 8;
updateLED();
delay(50);
}

The updateLED writes the RGB values to the LED module, and pixel.show() displays the change. The result is a clean, colorful transition every time you move or shake the sensor.

void updateLED() {
// Set LED color based on option value
switch(option) {
case 0: // OFF
pixel.setPixelColor(0, pixel.Color(0, 0, 0));
break;
case 1: // RED
pixel.setPixelColor(0, pixel.Color(255, 0, 0));
break;
case 2: // GREEN
pixel.setPixelColor(0, pixel.Color(0, 255, 0));
break;
case 3: // BLUE
pixel.setPixelColor(0, pixel.Color(0, 0, 255));
break;
case 4: // RED + GREEN = YELLOW
pixel.setPixelColor(0, pixel.Color(255, 255, 0));
break;
case 5: // RED + BLUE = MAGENTA
pixel.setPixelColor(0, pixel.Color(255, 0, 255));
break;
case 6: // GREEN + BLUE = CYAN
pixel.setPixelColor(0, pixel.Color(0, 255, 255));
break;
case 7: // RED + GREEN + BLUE = WHITE
pixel.setPixelColor(0, pixel.Color(255, 255, 255));
break;
}
pixel.show();
}

If you had trouble understanding this code, I wrote down a complete tutorial for beginners about Arduino programming. You can check it out here.

Once you’ve understood the code, upload it to your Arduino. Then gently shake the sensor, and you’ll see the LED instantly changing colors (refer to the photos above).

Announcement

course-content.png

If you enjoyed this project, I’ve got something exciting coming soon.

I’m preparing a masterclass on how to build your own Arduino-powered drone from scratch!

This upcoming course won’t be for complete beginners as we’ll dive into:

  1. Flight control systems
  2. Wireless communication
  3. PCB design
  4. 3D printing and mechanical structure
  5. And much more.

By the end, you’ll have a working drone you can actually fly - and a solid understanding of how it works from the inside out.

If that sounds like something you’d love to build, join the waiting list to be notified when enrollment opens.

Conclusion

15.jpg

And that’s it for today’s lesson!

You just learned how vibration-based sensors work and built an interactive project that turns motion into colorful light.

This project may look simple, but it introduces a key concept in physical computing: using sensors to make electronics react to the real world.

In the next lesson, we’ll scale things up and create our first big project: an automatic city lights model. This will be a great opportunity to combine what you’ve learned so far.

Make sure to follow me so you won’t miss it.

Until then, check out this another article. I'm sure it’ll help you take your Arduino skills to the next level.

Thanks for reading, and happy making!