{"id":52372,"date":"2026-11-01T10:51:03","date_gmt":"2026-11-01T10:51:03","guid":{"rendered":"http:\/\/siratek.net\/index.php\/tutorials\/tilt-switch\/"},"modified":"2026-11-01T10:51:03","modified_gmt":"2026-11-01T10:51:03","slug":"tilt-switch","status":"publish","type":"siratek_tutorial","link":"https:\/\/siratek.net\/index.php\/tutorials\/tilt-switch\/","title":{"rendered":"Detecting Movement with a Tilt Sensor"},"content":{"rendered":"<body><p><\/p>\n<h2>What it does<\/h2>\n<p>A tilt sensor is a switch that is operated by gravity instead of a finger. Inside the small metal cylinder is a rolling ball; tip the sensor past a certain angle and the ball rolls onto two contacts and closes the circuit.<\/p>\n<p>That makes it a push button that presses itself when the thing it is mounted on moves. It suits anything that needs to notice being disturbed: a parcel alarm, a drawer monitor, a toy that reacts when you pick it up, a machine that must stop if it is knocked over.<\/p>\n<p>What it is <em>not<\/em> is an angle sensor. It tells you tilted or not tilted, with no reading in between. For an actual angle you want the MPU-6050.<\/p>\n<h2>Before you begin<\/h2>\n<h3>Required parts and tools<\/h3>\n<ul>\n<li>An Arduino Uno or compatible board.<\/li>\n<li>One tilt sensor module.<\/li>\n<li>Three jumper wires.<\/li>\n<\/ul>\n<h3>The board Siratek sells<\/h3>\n<p>The module on the Siratek product page has <strong>three pins, marked <code>DO<\/code>, <code>GND<\/code> and <code>VCC<\/code><\/strong>, a blue adjustment screw, two small indicator LEDs, and an LM393 comparator chip.<\/p>\n<p>That tells you several useful things before you wire anything.<\/p>\n<ul>\n<li>The output is <strong>digital only<\/strong>: tilted or not. There is no analog pin on this board.<\/li>\n<li>One LED shows power and the other shows the output state, so you can see the sensor working without a single line of code.<\/li>\n<li>The comparator means the output is a clean switched signal, not a bouncing contact passed straight through, though the ball inside can still chatter as it rolls.<\/li>\n<li>The blue screw adjusts the comparator\u2019s threshold. On a simple tilt switch there is little to adjust, and it is usually best left where it is. If the output never changes, that screw is the first thing to look at.<\/li>\n<\/ul>\n<h3>Which way means tilted<\/h3>\n<p>The sensor\u2019s resting orientation decides everything, and the output may read HIGH when level and LOW when tilted, or the other way round. This varies between boards.<\/p>\n<p>Do not guess it. The first sketch below prints the raw state, so you can tip the sensor and see which reading means what. Thirty seconds of watching saves an afternoon of inverted logic.<\/p>\n<h3>Safety notes<\/h3>\n<p>Low voltage throughout, and nothing here can hurt you. Wire it with the USB cable unplugged as usual.<\/p>\n<h2>Pin reference<\/h2>\n<table>\n<thead>\n<tr>\n<th>Pin or label<\/th>\n<th>Connect to<\/th>\n<th>Purpose<\/th>\n<th>Notes<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td><code>VCC<\/code><\/td>\n<td>Uno <code>5V<\/code><\/td>\n<td>Power<\/td>\n<td>The module has its own regulation and indicators<\/td>\n<\/tr>\n<tr>\n<td><code>GND<\/code><\/td>\n<td>Uno <code>GND<\/code><\/td>\n<td>Common ground<\/td>\n<td><\/td>\n<\/tr>\n<tr>\n<td><code>DO<\/code><\/td>\n<td>Uno <code>D2<\/code><\/td>\n<td>Digital output, tilted or not<\/td>\n<td>Read with plain <code>INPUT<\/code><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>Read this with <code>pinMode(pin, INPUT)<\/code>, not <code>INPUT_PULLUP<\/code>. The module drives the line itself, so it needs no help holding a level, unlike a bare push button.<\/p>\n<figure class=\"siratek-tutorial-figure\"><img decoding=\"async\" src=\"http:\/\/siratek.net\/wp-content\/uploads\/2026\/11\/tilt-switch-wiring.svg\" alt=\"A tilt sensor module with VCC to 5V, GND to ground and its digital output on pin 2\" loading=\"lazy\"><figcaption>Read it with plain INPUT. The board drives the pin both ways.<\/figcaption><\/figure>\n<h2>Wiring steps<\/h2>\n<ol>\n<li>Unplug the USB cable.<\/li>\n<li>Connect <code>GND<\/code> to a <code>GND<\/code> pin on the Uno.<\/li>\n<li>Connect <code>VCC<\/code> to <code>5V<\/code>.<\/li>\n<li>Connect <code>DO<\/code> to digital pin <code>2<\/code>.<\/li>\n<li>Plug the USB cable back in. The power LED on the module should light.<\/li>\n<li>Tip the module slowly and watch the second LED change. If it does, the sensor works and anything that goes wrong next is in your code.<\/li>\n<\/ol>\n<h2>Software and libraries<\/h2>\n<p>No library is required.<\/p>\n<h2>Minimal working example<\/h2>\n<p>Run this first, and tip the sensor while watching the Serial Monitor. The point is to learn which reading means tilted on <em>your<\/em> board.<\/p>\n<pre><code>\/\/ Siratek: reading a tilt sensor, and finding out which state means tilted.\n\nconst int tiltPin = 2;\n\nvoid setup() {\n  pinMode(tiltPin, INPUT);\n  Serial.begin(9600);\n  Serial.println(\"Siratek: tip the sensor and watch the reading.\");\n}\n\nvoid loop() {\n  int state = digitalRead(tiltPin);\n\n  Serial.print(\"reading = \");\n  Serial.println(state);\n\n  delay(200);\n}<\/code><\/pre>\n<h3>Acting on a change<\/h3>\n<p>Once you know which value means tilted, react to the moment it changes rather than to the state, and ignore the chatter as the ball rolls. This is the debounce pattern from <em>Making a Button Behave<\/em>, applied to a different switch.<\/p>\n<pre><code>\/\/ Siratek: reporting when the sensor is disturbed.\n\nconst int tiltPin = 2;\nconst int ledPin = 9;\n\nconst int tiltedReading = LOW;          \/\/ Change to HIGH if yours reads the other way.\nconst unsigned long settleTime = 80;    \/\/ The ball chatters as it rolls.\n\nint steadyState = HIGH;\nint lastReading = HIGH;\nunsigned long lastChange = 0;\nint disturbances = 0;\n\nvoid setup() {\n  pinMode(tiltPin, INPUT);\n  pinMode(ledPin, OUTPUT);\n  Serial.begin(9600);\n  Serial.println(\"Siratek: watching for movement.\");\n}\n\nvoid loop() {\n  unsigned long now = millis();\n  int reading = digitalRead(tiltPin);\n\n  if (reading != lastReading) {\n    lastChange = now;\n  }\n  lastReading = reading;\n\n  if (now - lastChange &gt;= settleTime &amp;&amp; reading != steadyState) {\n    steadyState = reading;\n\n    if (steadyState == tiltedReading) {\n      disturbances++;\n      digitalWrite(ledPin, HIGH);\n      Serial.print(\"disturbed \");\n      Serial.println(disturbances);\n    } else {\n      digitalWrite(ledPin, LOW);\n      Serial.println(\"back to rest\");\n    }\n  }\n}<\/code><\/pre>\n<p>The one line to edit is <code>tiltedReading<\/code>. Set it to whatever the first sketch showed when you tipped the sensor.<\/p>\n<h2>Expected result<\/h2>\n<p>The first sketch prints a steady stream of <code>0<\/code> or <code>1<\/code>, changing as you tip the module past its switching angle. The change is abrupt rather than gradual, because the ball either bridges the contacts or does not.<\/p>\n<p>The second prints a numbered line each time the sensor is disturbed, and lights the LED while it stays tilted. Shaking it should give one count per genuine movement rather than a burst.<\/p>\n<p>The switching angle is not adjustable and is set by the sensor\u2019s construction. Mounting the module at a slight angle is how you change the point at which it triggers.<\/p>\n<h2>Troubleshooting<\/h2>\n<table>\n<thead>\n<tr>\n<th>Symptom<\/th>\n<th>Likely cause<\/th>\n<th>Check or fix<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>Power LED dark<\/td>\n<td><code>VCC<\/code> or <code>GND<\/code> not connected<\/td>\n<td>Check both before anything else<\/td>\n<\/tr>\n<tr>\n<td>Output never changes<\/td>\n<td>Threshold screw turned too far<\/td>\n<td>Turn it back slowly while tipping the module<\/td>\n<\/tr>\n<tr>\n<td>Output never changes, LED does<\/td>\n<td>Wrong pin, or wrong pin number in the sketch<\/td>\n<td><code>DO<\/code> to <code>D2<\/code>, and match the sketch<\/td>\n<\/tr>\n<tr>\n<td>Reads inverted<\/td>\n<td>Your board rests the other way<\/td>\n<td>Change <code>tiltedReading<\/code> to the other value<\/td>\n<\/tr>\n<tr>\n<td>One movement, many counts<\/td>\n<td>The ball chattering<\/td>\n<td>Raise <code>settleTime<\/code> to 120 or 150<\/td>\n<\/tr>\n<tr>\n<td>Triggers at the slightest knock<\/td>\n<td>Mounted near its switching angle<\/td>\n<td>Mount it further from the trigger point<\/td>\n<\/tr>\n<tr>\n<td>Never triggers however far you tip<\/td>\n<td>Sensor mounted the wrong way up<\/td>\n<td>Turn the module over<\/td>\n<\/tr>\n<tr>\n<td>Random triggering when still<\/td>\n<td>Vibration from something nearby<\/td>\n<td>Expected; this part cannot tell the difference<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h2>Next steps<\/h2>\n<ul>\n<li><strong>Easier first:<\/strong> <em>Read a Push Button<\/em>, which is the same idea with a finger instead of gravity.<\/li>\n<li><strong>Related:<\/strong> <em>Making a Button Behave<\/em>, for the settling pattern used above.<\/li>\n<li><strong>Next project:<\/strong> add the active buzzer for a parcel alarm that sounds when a box is moved.<\/li>\n<\/ul>\n<h2>Sources and attribution<\/h2>\n<p>All text, wiring guidance and code in this tutorial are original Siratek work.<\/p>\n<ul>\n<li>Arduino core documentation for <code>digitalRead()<\/code> and <code>millis()<\/code>.<\/li>\n<li>The board\u2019s pin labels, indicator LEDs and comparator are described from the photographs on the Siratek product page, and should be confirmed against the part in hand.<\/li>\n<\/ul>\n<p>No third-party tutorial text, diagrams, photographs or code has been reproduced here.<\/p>\n<p><\/p>\n<\/body>","protected":false},"excerpt":{"rendered":"<p>A tilt sensor is a switch that is operated by gravity instead of a finger. Inside the small metal cylinder is a rolling ball; tip the sensor past a certain angle and the ball rolls onto two contacts and closes&#8230;<\/p>\n","protected":false},"featured_media":0,"template":"","siratek_tutorial_level":[],"class_list":["post-52372","siratek_tutorial","type-siratek_tutorial","status-publish","hentry"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v28.8 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Detecting Movement with a Tilt Sensor - siratek<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"http:\/\/siratek.net\/index.php\/tutorials\/tilt-switch\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Detecting Movement with a Tilt Sensor - siratek\" \/>\n<meta property=\"og:description\" content=\"A tilt sensor is a switch that is operated by gravity instead of a finger. Inside the small metal cylinder is a rolling ball; tip the sensor past a certain angle and the ball rolls onto two contacts and closes...\" \/>\n<meta property=\"og:url\" content=\"http:\/\/siratek.net\/index.php\/tutorials\/tilt-switch\/\" \/>\n<meta property=\"og:site_name\" content=\"siratek\" \/>\n<meta property=\"og:image\" content=\"http:\/\/siratek.net\/wp-content\/uploads\/2026\/11\/tilt-switch-wiring.svg\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data1\" content=\"5 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"http:\\\/\\\/siratek.net\\\/index.php\\\/tutorials\\\/tilt-switch\\\/\",\"url\":\"http:\\\/\\\/siratek.net\\\/index.php\\\/tutorials\\\/tilt-switch\\\/\",\"name\":\"Detecting Movement with a Tilt Sensor - siratek\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/siratek.net\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"http:\\\/\\\/siratek.net\\\/index.php\\\/tutorials\\\/tilt-switch\\\/#primaryimage\"},\"image\":{\"@id\":\"http:\\\/\\\/siratek.net\\\/index.php\\\/tutorials\\\/tilt-switch\\\/#primaryimage\"},\"thumbnailUrl\":\"http:\\\/\\\/siratek.net\\\/wp-content\\\/uploads\\\/2026\\\/11\\\/tilt-switch-wiring.svg\",\"datePublished\":\"2026-11-01T10:51:03+00:00\",\"breadcrumb\":{\"@id\":\"http:\\\/\\\/siratek.net\\\/index.php\\\/tutorials\\\/tilt-switch\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"http:\\\/\\\/siratek.net\\\/index.php\\\/tutorials\\\/tilt-switch\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"http:\\\/\\\/siratek.net\\\/index.php\\\/tutorials\\\/tilt-switch\\\/#primaryimage\",\"url\":\"https:\\\/\\\/siratek.net\\\/wp-content\\\/uploads\\\/2026\\\/11\\\/tilt-switch-wiring.svg\",\"contentUrl\":\"https:\\\/\\\/siratek.net\\\/wp-content\\\/uploads\\\/2026\\\/11\\\/tilt-switch-wiring.svg\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"http:\\\/\\\/siratek.net\\\/index.php\\\/tutorials\\\/tilt-switch\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/siratek.net\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Tutorials\",\"item\":\"https:\\\/\\\/siratek.net\\\/index.php\\\/tutorials\\\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"Detecting Movement with a Tilt Sensor\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/siratek.net\\\/#website\",\"url\":\"https:\\\/\\\/siratek.net\\\/\",\"name\":\"siratek\",\"description\":\"Your Best Electronic Retail Shop\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/siratek.net\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Detecting Movement with a Tilt Sensor - siratek","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"http:\/\/siratek.net\/index.php\/tutorials\/tilt-switch\/","og_locale":"en_US","og_type":"article","og_title":"Detecting Movement with a Tilt Sensor - siratek","og_description":"A tilt sensor is a switch that is operated by gravity instead of a finger. Inside the small metal cylinder is a rolling ball; tip the sensor past a certain angle and the ball rolls onto two contacts and closes...","og_url":"http:\/\/siratek.net\/index.php\/tutorials\/tilt-switch\/","og_site_name":"siratek","og_image":[{"url":"http:\/\/siratek.net\/wp-content\/uploads\/2026\/11\/tilt-switch-wiring.svg","type":"","width":"","height":""}],"twitter_card":"summary_large_image","twitter_misc":{"Est. reading time":"5 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"http:\/\/siratek.net\/index.php\/tutorials\/tilt-switch\/","url":"http:\/\/siratek.net\/index.php\/tutorials\/tilt-switch\/","name":"Detecting Movement with a Tilt Sensor - siratek","isPartOf":{"@id":"https:\/\/siratek.net\/#website"},"primaryImageOfPage":{"@id":"http:\/\/siratek.net\/index.php\/tutorials\/tilt-switch\/#primaryimage"},"image":{"@id":"http:\/\/siratek.net\/index.php\/tutorials\/tilt-switch\/#primaryimage"},"thumbnailUrl":"http:\/\/siratek.net\/wp-content\/uploads\/2026\/11\/tilt-switch-wiring.svg","datePublished":"2026-11-01T10:51:03+00:00","breadcrumb":{"@id":"http:\/\/siratek.net\/index.php\/tutorials\/tilt-switch\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["http:\/\/siratek.net\/index.php\/tutorials\/tilt-switch\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"http:\/\/siratek.net\/index.php\/tutorials\/tilt-switch\/#primaryimage","url":"https:\/\/siratek.net\/wp-content\/uploads\/2026\/11\/tilt-switch-wiring.svg","contentUrl":"https:\/\/siratek.net\/wp-content\/uploads\/2026\/11\/tilt-switch-wiring.svg"},{"@type":"BreadcrumbList","@id":"http:\/\/siratek.net\/index.php\/tutorials\/tilt-switch\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/siratek.net\/"},{"@type":"ListItem","position":2,"name":"Tutorials","item":"https:\/\/siratek.net\/index.php\/tutorials\/"},{"@type":"ListItem","position":3,"name":"Detecting Movement with a Tilt Sensor"}]},{"@type":"WebSite","@id":"https:\/\/siratek.net\/#website","url":"https:\/\/siratek.net\/","name":"siratek","description":"Your Best Electronic Retail Shop","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/siratek.net\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"}]}},"jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/siratek.net\/index.php\/wp-json\/wp\/v2\/siratek_tutorial\/52372","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/siratek.net\/index.php\/wp-json\/wp\/v2\/siratek_tutorial"}],"about":[{"href":"https:\/\/siratek.net\/index.php\/wp-json\/wp\/v2\/types\/siratek_tutorial"}],"version-history":[{"count":0,"href":"https:\/\/siratek.net\/index.php\/wp-json\/wp\/v2\/siratek_tutorial\/52372\/revisions"}],"wp:attachment":[{"href":"https:\/\/siratek.net\/index.php\/wp-json\/wp\/v2\/media?parent=52372"}],"wp:term":[{"taxonomy":"siratek_tutorial_level","embeddable":true,"href":"https:\/\/siratek.net\/index.php\/wp-json\/wp\/v2\/siratek_tutorial_level?post=52372"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}