RK-micro:bit robot – line-following programming tutorial(MakeCode)

Introduction

This step-by-step tutorial will show you how to control the RK-micro:bit robot by MakeCode. For the assembly tutorial, please follow this link.

In this tutorial, the robot will follow the black track by its IR light sensor which is located in its front end. Check the video:

author 楊國立 (intern)
Time 1~ 2 hours BOM
Difficulty 2 / 5 

Content

How to control the motor and robot behavior

In RK-micro:bit Car, the two motors are placed back-to-back, we call this a differential drive platform, which means the robot behaviors are decided by the direction/speed of these two motors.

Since we are using a L298n motor driver module to control the two motors, we acutually control four pins, two pins for one motor:

  • P1, P2 – the left motor
  • P14, P15 – the right motor

First we take a look at how to control one motor, if the motor rotate in the opposite direction, just switch the red/ black wire:

P1 – H P1 – L
P2 – H Brake (can NOT rotate the shaft) Rotate CW
P2 – L Rotate CCW Nothing (can rotate the shaft)

By combining two motors, we can make the robot move around, you can further adjust the motors’ speed to have more detailed control over the robot:

Right motor – Forward Right motor – Backward
Left motor – Forward Robot moves forward Robot turns right
Left motor – Backward Robot turns left Robot moves backward

Makecode

We will use MakeCode graphical IDE to program RK-micro:bit Car. The code should be quite straight-forward and fun, enjoy!

STEP1: In the on start event, we declare a variable for the motor speed and use it to P13 and P0.

  

STEP2: The code in forever will run continuously, which means that it will keep looping until you unplug the microbit. 

Since the IR light sensor is connected to P12, and it will return 1 and 0 when it detects the black track and white ground. We can take advantage of this to make a zigzag line-following robot.

In the video, you can see we put the robot (IR light sensor) at the left-hand side of the black track, this means the robot will move toward the front right to seek for black track. If yes, then it will move toward the front left a little bit -> these two-conditional pattern will repeat forever.

So when the light sensor detects the black track (digital read pin P12 = 1), the robot will move toward the front right, otherwise it will move toward the front left.

Note: please modify the motor parameters to make your robot move even better!

Resource

The complete code can be accessed at the following webpage or download the HEX file.

 

 

 

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *