Welcome to Hackster!
Hackster is a community dedicated to learning hardware, from beginner to pro. Join us, it's free!
Lisleapex Blog
Published © Apache-2.0

Tilt Sensor (tilt Switch) Module-Arduino Tutorial

This post shows how to use the tilt sensor module with Arduino.

BeginnerWork in progress2 hours158
Tilt Sensor (tilt Switch) Module-Arduino Tutorial

Things used in this project

Hardware components

Arduino UNO
Arduino UNO
×1
VBBMicro 'Arduino UNO Avatar'
Virtual Breadboard VBBMicro 'Arduino UNO Avatar'
×1
Tilt Sensor, SPST-NC
Tilt Sensor, SPST-NC
×1
LED (generic)
LED (generic)
×1
Resistor 220 ohm
Resistor 220 ohm
×1
Jumper wires (generic)
Jumper wires (generic)
×1

Software apps and online services

Arduino IDE
Arduino IDE

Hand tools and fabrication machines

10 Pc. Jumper Wire Kit, 5 cm Long
10 Pc. Jumper Wire Kit, 5 cm Long

Story

Read more

Schematics

pin wiring

Code

Untitled file

Arduino
int ledPin = 12;
int sensorPin = 4;
int sensorValue;
int lastTiltState = HIGH; // the previous reading from the tilt sensor
// the following variables are long's because the time, measured in miliseconds,
// will quickly become a bigger number than can be stored in an int.
long lastDebounceTime = 0; // the last time the output pin was toggled
long debounceDelay = 50; // the debounce time; increase if the output flickers
void setup(){
 pinMode(sensorPin, INPUT);
 digitalWrite(sensorPin, HIGH);
 pinMode(ledPin, OUTPUT);
 Serial.begin(9600);
}
void loop(){
 sensorValue = digitalRead(sensorPin);
 // If the switch changed, due to noise or pressing:
 if (sensorValue == lastTiltState) {
 // reset the debouncing timer
 lastDebounceTime = millis();
 }
 if ((millis() - lastDebounceTime) > debounceDelay) {
 // whatever the reading is at, it's been there for longer
 // than the debounce delay, so take it as the actual current state:
 lastTiltState = sensorValue;
 }
 digitalWrite(ledPin, lastTiltState);

 Serial.println(sensorValue);
 delay(500);
}

Credits

Lisleapex Blog
26 projects • 0 followers
Fast Delivery of High-Quality Electronic Components
Contact

Comments

Please log in or sign up to comment.