🔬
Wiki.Linhkientot
  • 👋Chào mừng tới Linhkientot's Wiki
  • Hướng dẫn phần mềm
    • Cài đặt Arduino IDE 2.0
    • Cài đặt phần mềm Mixly
    • Cài đặt phần mềm Mind+
    • Cài đặt phần mềm Thonny
  • Arduino Products
    • Hướng dẫn ban đầu Arduino IDE
      • Hướng dẫn nạp chương trình Arduino IDE
      • Thêm thư viện Arduino IDE
      • Cài đặt Driver CH340 trên MAC OS
      • Lập trình Board ESP32 trên Arduino IDE
    • Arduino Starter Kit
      • Lesson 0. Getting Started - install IDE
      • Lesson 2. LEDs
      • Lesson 3. RGB LEDs
      • Lesson 4. Eight LEDs and a Shift Register
      • Lesson 5. The Serial Monitor
      • Lesson 6. Digital Inputs
      • Lesson 7. Make an RGB LED Fader
      • Lesson 8. Analog Inputs
      • Lesson 9. Sensing Light
      • Lesson 10 Making Sounds
      • Lesson 11 LCD Displays Part 1
      • Lesson 12 LCD Displays Part 2
      • Lesson 13 DC Motors
      • Lesson 14 Servo Motors
      • Lesson 15 DC Motor Reversing
      • Lesson 16 Stepper Motors
      • Lesson 17 Email Sending Movement Detector
    • Arm Robot
      • Hướng dẫn lắp ráp Arm-4DoF
      • Hướng dẫn lắp ráp Arm-6DoF
      • Hướng dẫn lắp ráp Arm - 6DoF đế tròn quay
    • Car Robot
      • Hướng dẫn lắp ráp tank TS-100
    • Arm Car Robot
    • Smart home IoT
    • Lập trình cảm biến khí ga/ khói MQ-2 Arduino
  • Micro:bit Products
    • Micro:bit Setup
    • Micro:bit Starter Kit V1
      • Setting up the micro:bit with Makecode
      • Set up Arduino IDE for micro:bit
      • Using the Buttons and LED Matrix on micro:bit
      • Pushbutton with micro:bit
      • Tilt Sensor with micro:bit
      • Temperature Sensor with micro:bit
      • DC Motor with micro:bit
      • Sound Sensor with micro:bit
      • Raindrop Sensor with micro:bit
      • Make an RGB LED Blink with micro:bit
      • Ultrasonic Distance Sensor with micro:bit
      • Analog Inputs and micro:bit
      • Servo with micro:bit
      • Smoke sensor with micro:bit
      • Light-dependent Resistor with micro:bit
      • Infrared Obstacle Avoidance Sensor with micro:bit
      • Using LEDs with micro:bit
    • Micro:bit Starter Kit V2
      • Microbit Introduction
      • Microbit Basic Lessons
      • 1. Lesson: Đèn LED nhấp nháy
      • 2. Lesson: RGB Led
      • 3. Lesson: Đọc giá trị từ triết áp
      • 4. Lesson: Đọc nút nhấn
      • 5. Lesson: Servo Motor
      • 6. Lesson: Passive Buzzer-Còi thụ động
      • 7. Lesson: Active Buzzer-Còi chủ động
      • 8. Lesson: Compass-La bàn
      • 9. Lesson: Accelerometer-Gia tốc kế
      • 10. Lesson: Module phát hiện âm thanh
      • 11. Lesson: Cảm biến ánh sáng (quang trở)
      • 12. Lesson: Cảm biến ngọn lửa
      • 13. Lesson: Cảm biến khói
      • 14. Lesson: Cảm biến siêu âm
      • 15. Lesson: ModuleRelay
      • 16. Lesson: Cảm biến nhiệt độ (DHT11)
      • 17. Lesson: Bộ điều khiển từ xa (IR)
      • 18. Lesson: Hiển thị màn hình LCD i2c 1602
      • 19. Lesson: Hiển thị nhiệt độ, độ ẩm trên màn hình LCD
      • 20. Lesson: Điều khiển quạt bằng Relay
    • Micro:bit Advanced Kit V2
      • Giới thiệu BBC Sensor Shield V2
      • Danh sách linh kiện bộ Kit
    • Micro:bit Car
  • ESP32 PRODUCTS
    • Hướng dẫn lập trình ESP32
  • Raspberry Products
    • Raspberry Pico Kit
    • Raspberry Pico Car
  • Group Robots
    • 🤖Arduino Robots
    • 🐦Micro:bit Robots
    • 🍓Raspberry Robots
Powered by GitBook
On this page
  1. Micro:bit Products
  2. Micro:bit Starter Kit V1

Tilt Sensor with micro:bit

PreviousPushbutton with micro:bitNextTemperature Sensor with micro:bit

Learn to use a tilt sensor with micro:bit

Written By: Cherie Tan

Introduction

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.

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.

    3.3V : 'VCC' stands for Voltage Common Collector, we'll connect the VCC pin to 3.3V on the micro:bit

  • 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.

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 MakeCode for LED

let sensorVal = 0
pins.setPull(DigitalPin.P0, PinPullMode.PullUp)
basic.forever(function () {
    sensorVal = pins.digitalReadPin(DigitalPin.P0)
    if (sensorVal == 0) {
        pins.digitalWritePin(DigitalPin.P1, 1)
    } else if (sensorVal == 1) {
        pins.digitalWritePin(DigitalPin.P1, 0)
    }
})
  • Click on 'New Project'

  • Copy and paste the following into the Javascript interface.

Step 10 Add some arrows

let sensorVal = 0
pins.setPull(DigitalPin.P0, PinPullMode.PullUp)
basic.forever(function () {
    sensorVal = pins.digitalReadPin(DigitalPin.P0)
    if (sensorVal == 0) {
        pins.digitalWritePin(DigitalPin.P1, 1)
        basic.showLeds(`
            . . # . .
            . . # . .
            # . # . #
            . # # # .
            . . # . .
            `)
    } else if (sensorVal == 1) {
        pins.digitalWritePin(DigitalPin.P1, 0)
        basic.showLeds(`
            . . # . .
            . # # # .
            # . # . #
            . . # . .
            . . # . .
            `)
    }
})
  • Copy and paste this code into the Javascript interface.

  • Here, 'basic.showLeds' is used. We've programmed it so that the first one shows a downwards pointing arrow, and the second one shows an upwards arrow.

Step 11 Upload the hex file

  • Alright, it's time to upload an test the MakeCode. Connect a microUSB cable from the micro:bit to your computer

  • Click on the 'Download' button on the bottom left corner of MakeCode editor.

  • The hex file will be downloaded, and you can find it in your Downloads folder.

  • Open up Finder if you are using Mac OSX, or Explorer if using Windows. Locate the downloaded hex file.

  • Drag and drop it to the MICRO:BIT drive and the micro:bit will blink as the code uploads!