Coding Ireland STEM Report 2024 Have Your Say
Microbit Robotics Sensors & Circuits Artificial Intelligence
Expert
60 mins
235 points
What you need:
  • Computer/laptop
  • Microbit
  • Move Motor Car
  • USB Cable

Car Distance Sensors

In this project we program the distance sensors on the Move Motor Car to detect and navigate around objects.

1 - Introduction

The Move Motor car has and ultrasonic sensor at the front of it.

What is an ultrasonic sensor?

An ultrasonic sensor is an instrument that measures the distance to an object using ultrasonic sound waves. An ultrasonic sensor uses a transducer to send and receive ultrasonic pulses that relay back information about an object’s proximity. High-frequency sound waves reflect from boundaries to produce distinct echo patterns.

How Ultrasonic Sensors Work

Ultrasonic sensors work by sending out a sound wave at a frequency above the range of human hearing. The transducer of the sensor acts as a microphone to receive and send the ultrasonic sound. The sensor determines the distance to a target by measuring time lapses between the sending and receiving of the ultrasonic pulse.

Source: MaxBotix




2 - Create a new project

Go to the https://makecode.microbit.org website, create a new project and add the kitronik-move-motor extension to your project.


3 - Measure distance

Let's program the sensor to measure the distance to an object.

Create a new variable called distance and add the following code to your project. This code will store the distance (in cms) that the sensor is reading into the 'distance' variable and then it will shot the number on the Microbit that's attached to the Move Motor car.

Download your code onto the Microbit, turn on your Move Motor car and test that the code works by putting an object at different distances from the sensor and seeing the result on the Microbit.


4 - Follow an object

Now that we know how far away the object is, let's add some code to move the car so that it is always 10cm away.

Change your code to the following, download it onto your Microbit and then test that the car moves when you move the object away and then stops when it is 10cm away.


5 - Reverse

So far the car will stay any distance under 10cm away from the object. Let's add some code to reverse the car until it is exactly 10cm away.

Update your code to the following, download it onto your Microbit and test that it works by moving the object back and forth. Your car should move to stay exactly 10cm away from the object.



Join our club 😃

To view the remaining 2 steps and access hundreds of other coding projects please login or create an account.

Copyright Notice
This lesson is copyright of Coding Ireland. Unauthorised use, copying or distribution is not allowed.
🍪 Our website uses cookies to make your browsing experience better. By using our website you agree to our use of cookies. Learn more