Micropython Lesson 8
Use the tilt sensor as a switch
In this example, you are learning how to use a tilt sensor. Tilt sensor produces digital outputs such as high and low. Therefore, the tilt sensor works as a switch that gives on and off states.
- Digital reads and print them on the serial monitor
- Working principle of the tilt sensor
When the device gets power and is in its upright position, then the rolling ball settles at the bottom of the sensor to form an electrical connection between the two end terminals of the sensor.
As the first step, you should connect a Magicbit tilt sensor to the 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 of 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 a 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.