Automatic Gate with Teachable Machine and Micro:Bit

Automatic Gate with Teachable Machine

Overview

Tools:

  • Ruler
  • Scissors
  • Screwdriver
  • Nuts and bolts
  • Rubber bands

Electronics:

  • BBC Micro:Bit
  • Micro:Bit adapter
  • 180deg micro servo (3,3V compatible)

Building time:

20 min

Difficulty:

Beginner

Project info

This automatic gate use machine learning model trained for image classification andconnects it to Micro:Bit with Teachable Micro:Bit app to detect when it should open, it passes only cars but stays closed to any other objects. Machine learning model used to detect objects in camera image is trained with Teachable Machine App. Of course you can train the AI model to open the gate on whatever conditions you want.

To create this project you have to:

  1. Train the AI model in Teachable Machine – you can learn how to use Teachable machine in my AI In The Classroom free course
  2. Code the Micro:Bit to react as you want
  3. Open the trained model in Teachable Micro:Bit app on your smartphone and connect it with BBC Micro:Bit

Building manual

Design plan

Automatic gate with teachable machine and micro:Bit design plan

Example code

How this code works?

This program controls an automatic gate using a BBC Micro:Bit and a Bluetooth connection. Here’s a simplified explanation of how it works:

  1. Bluetooth Connection:
    The micro:bit connects to a device (like a phone or a computer) via Bluetooth. When the connection is established, it shows a happy face. If the connection is lost, it shows a sad face.
  2. Receiving Commands:
    The micro:bit listens for messages sent over Bluetooth. It expects simple text commands like “CAR” or “NOTHING.”
  3. Opening the Gate:
    When it receives the command “CAR”, the micro:bit moves a servo motor to open the gate (turns it to position 0°) and shows a checkmark.
  4. Closing the Gate:
    When it receives the command “NOTHING”, the micro:bit waits for one second, then moves the servo to 90°, closing the gate and displaying a “no” icon.
  5. Initialization:
    When the micro:bit starts, it activates the Bluetooth communication and shows a square icon to indicate it’s ready.

This setup works with an Teachable Micro:Bit App running on a smartphone and machine learning to recognize cars and send the appropriate commands to the micro:bit.