fbpx
Micropython Lesson 8: Tilt Sensor
Micropython Lesson 8

Tilt Sensor

Use the tilt sensor as a switch

Components Required

Magicbit
Buy
tilt sensor
Buy

Introduction

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.

Learning outcomes

  • Digital reads and print them on the serial monitor
  • Working principle of the tilt sensor

Theory

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.

Tilt sensor working principal

Methodology

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.

Code

 

from machine import Pin
import time

Tilt = Pin(32, Pin.IN)

while True:
   print(Tilt.value())
   time.sleep_ms(1000)

Explanation

Tilt = Pin(32, Pin.IN): Defining input pin Tilt.value() Read the data input of configured data pin.

Related Posts
Leave a Reply