Tilt Sensor with micro:bit


A tilt sensor is a type of sensor that allows the detection of orientation, and are tiny, inexpensive and low-power components. They can be used as a simple way to detect orientation.

How it works: Tilt sensors are typically cylindrically shaped tubes with a conductive object such as a rolling ball contained within. When the sensor is tilted downwards, the conductive ball no longer completes the circuit. But when returned to the normal upward position, the balls make contact and the circuit is completed.

In this guide, we will learn to connect the tilt sensor to a micro:bit, and make it turn an LED on and off. Completing this guide will enable you to use the tilt sensor in your own projects.

Parts Used in This Guide


Step 1  The Module

Before we begin, let's take a closer look at the tilt sensor module!

It has three pins:

  • DO: Digital Output
  • GND: In electronics, we define a point in a circuit to be a kind of zero volts or 0V reference point, on which to base all other voltage measurements. This point is called  ground or GND.
    • Note: Voltage is the difference in potential between two points. As it is difficult to talk about voltage without a reference point, we need another point to compare it to. 
  • 3.3V  :  'VCC' stands for Voltage Common Collector, we'll connect the VCC pin to 3.3V on the micro:bit

Step 2  Connect module to breadboard

Step 3  Connect P0 to DO

Step 4  Connect GND to GND

Step 5  Connect 3.3V to VCC

Step 6  Connect GND to LED (Negative lead)

Step 7  Add a resistor

Step 8  Connect P1 to resistor

Step 9  The MakeCode for LED!

  • First, head over to MakeCode editor and create a new project.
  • In the 'on start' block, add a 'set pull pin P0 to up' block. This can be found by clicking 'Pins' tab and then 'more ...'
  • Let's create a variable and name it 'sensorVal'. 
    • As DO pin of tilt sensor is connected to Pin 0 of the micro:bit, we set sensorVal to digital read pin P0.
  • Now in the forever block, drag and drop a 'if true... then' block. Change its values to sensorVal = 0.
  • Use a 'digital write pin' block and change its values to 'P1' and '1'.
  • Click on the white plus (with a circular border) icon twice, to add an 'else if' block
  • Delete the extra 'else' block by clicking on the white minus (with a circular border) icon next to it.
  • Duplicate the 'sensorVal = 0' block and change it value to '1'. Place this block in 'else if' 
  • Duplicate the 'digital write pin P1 to 1' block
  • Change the value to 'P1' and '0'

Step 10  Add some arrows!

Finally let's add two 'show leds' blocks. Make the first one show a downwards pointing arrow, and the second one show an upwards arrow.

Step 11  Upload the hex file

Alright, it's time to upload an test the MakeCode!

  1. Connect a microUSB cable from the micro:bit to your computer
  2. Click on the 'Download' button on the bottom left corner of MakeCode editor.
  3. The hex file will be downloaded, and you can find it in your Downloads folder.
  4. Open up Finder if you are using Mac OSX, or Explorer if using Windows. Locate the downloaded hex file.
  5. Drag and drop it to the MICRO:BIT drive and the micro:bit will blink as the code uploads!

Step 12  Test it out!

Try tilting it downwards or horizontally and see what happens.

When the sensor is tilted to horizontal orientation, the circuit opens which turns the LED OFF. When the tilt sensor is vertical or upright, the circuit is closed, setting the LED ON.

Step 13  The code

              int tilt_Pin=0;
int LED_Pin=1;
void setup() {

void loop() {
  int sensorVal;
   else {

Another way you can program the tilt sensor with the micro:bit is by using the Arduino IDE! 

Follow our previous guide on how to install and set up the Arduino IDE for micro:bit, if you haven't already done so. Copy and paste the following code and you will see that it works similar to the MakeCode version.

  • We create two variables, tilt_pin and LED_Pin and assign them to Pin 0 and 1 of the micro:bit.
  • In void setup(), we need to specify that tilt_pin is an input whereas LED_Pin is an output.
  •  In void loop(), we assign the variable, sensorVal to read P0. If this value is 1 or HIGH, the LED will light up.
  • Else, the LED will be turned off.

Step 14  Upload the code

It's time to upload the code. After you have connected the micro:bit to the computer using a microUSB, follow these steps:

  1. Click on the verify button on the top left hand corner of the Arduino IDE
  2. Click on the upload button next to it, after it has been verified.
  3. Wait for the code to be uploaded as the micro:bit blinks. Once it has finished uploading, it will state 'Done uploading' at the bottom left hand corner of the Arduino IDE.