I've recently posted a tutorial about this project on YouTube explaining everything you can read on this article. You can watch it right below.
IntroductionHave 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.
ExplanationTilt and motion sensors 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:
- Accelerometer-based sensors- measure acceleration due to gravity to determine tilt and inclination.
- Gyroscope-based sensors- detect angular rotation, often combined with accelerometers in IMUs (Inertial Measurement Units).
- 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.
Inside this tiny component, there’s a metal ball or spring 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.
SponsorIf 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.
ProjectsNow let’s move to the practical part: building the shake sensor LED project.
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:
- Arduino UNO
- I/O Expansion Shield
- Digital RGB LED Module
- Shake Sensor
- Jumper Wires
After you grab all components, connect them like this:
- Attach the I/O Expansion Shield to your Arduino UNO.
- Connect the RGB LED module to port 2.
- Connect the shake sensor to port 3.
That’s it! It’s a simple plug-and-play setup.
You can also follow the wiring diagram to assist you.
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 LED_PIN 2
#define SHAKE_SENSOR_PIN 3
Adafruit_NeoPixel pixel = Adafruit_NeoPixel(1, LED_PIN, NEO_GRB + NEO_KHZ800);
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) {
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() {
switch(option) {
case 0:
pixel.setPixelColor(0, pixel.Color(0, 0, 0));
break;
case 1:
pixel.setPixelColor(0, pixel.Color(255, 0, 0));
break;
case 2:
pixel.setPixelColor(0, pixel.Color(0, 255, 0));
break;
case 3:
pixel.setPixelColor(0, pixel.Color(0, 0, 255));
break;
case 4:
pixel.setPixelColor(0, pixel.Color(255, 255, 0));
break;
case 5:
pixel.setPixelColor(0, pixel.Color(255, 0, 255));
break;
case 6:
pixel.setPixelColor(0, pixel.Color(0, 255, 255));
break;
case 7:
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 below).
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:
- Flight control systems
- Wireless communication
- PCB design
- 3D printing and mechanical structure
- 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.
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!


_ztBMuBhMHo.jpg?auto=compress%2Cformat&w=48&h=48&fit=fill&bg=ffffff)










Comments