Light-seeking Robit Smart Car


You might have already played with your new Robit Smart Car and got it to move, avoid obstacles, and even avoid falling off platforms.

In this guide, you will next learn to use the light sensor on the Robit Smart Car. With the light sensor, the Robit will be transformed into a light-seeking robot! It can also be programmed to turn its LEDs on or off depending on light. 

Complete this guide to add more function to your robot car buddy!

Step 1  Connect the motors and sonar module

Before we can create a light-seeking robot, we'll need to:

  • Connect the motors to M1 and M2 on the robit motherboard.
  •  In a later part of this guide, we'll also get the robit smart car to avoid hitting obstacles. So hook up the sonar module to the Pin 14 on the motherboard as well.

We won't need the line tracking module in this guide.

Step 2  Install MakeCode packages

First, open MakeCode Editor. To easily program the Robit Smart Car, we'll use the 'robit' and 'neopixel' MakeCode packages over in the Extensions tab:

  • Click on 'Advanced'
  • Click on 'Extensions'
  • Type 'robit' in the search bar 
  • Click on the robit package and it will be automatically added to the MakeCode editor
  • Repeat the first two steps but this time, search for 'neopixel'
  • Click on the neopixel package and it will also automatically be added into the editor

Step 3  Get LEDs to light up!

The on-board light sensor on the Robit motherboard is connected to Pin 10 port on the micro:bit. 

You might also notice two on-board LEDs, labelled LED1 and LED2, these are connected to the Pin 12 port on the micro:bit. 

Let's start by programming the LEDs to light up when there is no light or in dim light.

  • First, create a variable called 'lightVal'. You can do so by clicking on 'Variables' tab and 'Make a Variable ...'
  • Over in 'Pins' tab, get out a 'analog read pin ...' block. Make sure it is set to P10 as the light sensor is connected to Pin 10.
  • We'll now create the condition that will determine when the LED will light up. Click on 'Logic' tab and get a 'if ... then ... else ...' block.
  • Set the condition to be: if lightVal < 500 then ... Else ... '. 
    • This means if the light value from the light sensor is less than 500, do the following within the 'if' block. 
    • Else, do the following within the 'else' block.
  • To get the LED lighting up. Get a 'Neopixel at pin ... with ... leds as ... ' block from 'Neopixel' tab. Also get a '... show color ...' block. Place the first block within the 'show color' block.
  • Set the colour as 'white'.
  • Use the same blocks for the 'Else' block. But this time, set the colour as 'black'.

Note: If there is no option for 'P12' when changing 'Neopixel at pin ...' to be 'Neopixel at P12 ...' there is another way: 

  • Click  ' { } Javascript ' ,
  • Look at this line: 'neopixel.create(DigitalPin.P0, 2, NeoPixelMode.RGB).showColor(neopixel.colors(NeoPixelColors.White))'
  • Change the 'DigitalPin.P0' part to 'DigitalPin.P12'

Step 4  Move forward until light level reached!

With this MakeCode, the Robit Smart Car will continue to move forward until it finds enough light. 

How much light? We created a 'threshold' value for the light level.

 For the condition: Once this threshold level has been reached and the light sensor detects this, the robot smart car will stop moving and its LED will light up.

  • You can find a 'if ... then ... else ...' block from 'Logic' tab. 
  • Also get a ' ... < ... ' block. 
  • Then get out 'lightVal' and 'threshold' variable blocks in the 'Variables' tab.
  • To get 'Left wheel ... speed ... Right wheel ... speed ... ' and 'Motor Stop All' blocks, get them from the 'Robit' tab.

Arrange them as shown below, or download the full MakeCode by clicking on the Download button below.

Step 5  Avoid obstacles and seek light!

We don't want the robot to hit a wall! So let's program it to avoid obstacles too. If you haven't already, you might want to check out our guide on obstacle avoidance with the robit smart car

  • Like the MakeCode in that guide,  a variable, 'dist' is used to represent the distance from the obstacle. Set it to be 0 in an 'on start' block.
  • Now get out a 'Left wheel ... speed ... Right Wheel ... speed' block.
  • Set the Left wheel to be M1, and Right wheel to be M2. Then set the speed for both to 50.
  • Over in the 'forever' block, place a 'set dist to ...' block
  • Get a 'Ultrasonic pin ... ' block from the Robit tab. Then change its values so it is 'Ultrasonic pin J1 (P13, P14)
  • Doing so, it reads the ultrasonic data from J1 port and then assigns it to the variable, 'dist'.

These are the new conditions used:

  • If the distance from any obstacle is more than 5cm, and the light level is less than the set threshold level, continue moving forward. 
  • Else if the distance from an object is less than 5cm and the level of light is less than the threshold level: reverse and do a turn. Whether it makes a full right turn or left turn depends on a  randomly generated number. 
  • Else, if the light threshold level has been reached (it's bright enough), stop moving and light up the LED.

Download the full MakeCode by clicking on the download button below!

Step 6  Upload the code to micro:bit

To upload the code to the micro:bit, follow these steps:

  1. Connect the micro:bit to the computer via microUSB cable
  2. Click on the 'Download' button and the hex file will be automatically downloaded to your 'Downloads' folder
  3. Drag and drop the downloaded .hex file to the MICROBIT drive
  4. Leave the micro:bit alone for a few seconds as it blinks. The code is uploading!