JavaScript: Tilt Sensor with Johnny-Five on Node.js

The Johnny-Five Tutorial Series is geared towards Arduino programming on Node.js, using the Johnny-Five framework. Get caught up here.

A tilt sensor is a component that senses its own change in inclination. Similar to a button, tilt sensors are activated through a physical interaction mechanism; in the tilt sensor case, this is the act of changing the sensor’s inclination.

There are two primary types of tilt sensors:

  • Metal Ball
  • Mercury

Both types contain a conductive material, either a metal ball or blob of mercury. When this conductive material is moved to a specific inclinition (varying by make and model, but often 30°) it will short the two pins. When the circuit is shorted, ie. a “tilt” is detected, the digital sensor signal is brought HIGH (ie. the value is 1).


Connect the following:

  • Signal to D7
  • Power to 5V
  • Ground to GND

Save the following as tilt-sensor.js:

var five = require("johnny-five");
var board = new five.Board();

board.on("ready", function() {
  var tilt = new five.Sensor.Digital(7);

  tilt.on("change", function() {
    if (this.value) {

Run node tilt-sensor.js to see the program in action!


The program above is very simple, how might it be extended to visually or audibly indicate the “tilt”? Post links to your own solutions in the comments!

* The device used in this tutorial is included in the Sun Founder 13 Modules Sensor Kit or Sun Founder 37-in-1 Kit

** Fritzing Components


We moved off of Disqus for data privacy and consent concerns, and are currently searching for a new commenting tool.

Contact Us

We'd love to hear from you. Get in touch!


P.O. Box 961436
Boston, MA 02196