Micropython Lesson 8
Use the tilt sensor as a switch
In this example, you are learning how to use Tilt sensor. Tilt sensor produces digital outputs such as high and low. Therefore, tilt sensor works as a switch that gives on and off states.
- Digital reads and print them on serial monitor
- Working principle of the tilt sensor
When the device gets power and is in its upright position, then the rolling ball settle at the bottom of the sensor to form an electrical connection between the two end terminals of the sensor.
Figure 1: Working principle of tilt sensor 
As the first step, you should connect a Magicbit tilt sensor to Magicbit core board. For this, you can use one side connector from four side connectors of the Magicbit core board or if you want to extend the length the connection, you can use jumper wires. For this example, the tilt sensor was connected to the upper left connector of the Magicbit core board. Then connect the Magicbit to your pc and upload the code. You can get outputs using serial monitor.
from machine import Pin import time Tilt = Pin(32, Pin.IN) while True: print(Tilt.value()) time.sleep_ms(1000)
Tilt = Pin(32, Pin.IN): Defining input pin Tilt.value() Read the data input of configured data pin.