Skip to content

Search the documentation

Kit
Firmware

What colour is that? — Smart Car

This one is a two-step example, and that is on purpose. First the robot tells you what it sees. Then you type those numbers into the compare block and it recognises that colour whenever it comes back.

This is one project for both firmware versions: when MoBot opens it, the blocks write the code for the firmware of your Smart Car.

The blocks of this example, from top to bottom.
Click the picture to view it larger.Open full sizeThe same program written as Python is further down this page, in The Python code.
Read the program as text
  • MoBot Start
  • Print to serial "Colour sensor. Hold something coloured right against the sensor."
  • Print to serial "STEP 1: read the four numbers below."
  • Print to serial "STEP 2: type the first three into the compare block and send it again."
  • wait 2 seconds
  • repeat forever
    • Print to serial (create text with "red, green, blue, brightness = " Read color sensor)
    • if (is Read color sensor the color: R 2000 G 900 B 800 ± 600 .)
      • Set LED 2 ON
    • else
      • Set LED 2 OFF
    • wait 0.5 seconds
  • A Smart Car with the colour sensor fitted (underneath, or on the front, depending on your kit).
  • A USB cable.
  • Two or three pieces of coloured card, or coloured bricks. Strong flat colours work far better than shiny or patterned things.
  1. Open 08-colour-detect.mobot, plug in, press Send to Device.

  2. Hold a piece of red card right against the sensor — almost touching.

  3. Read the serial monitor:

    [MOBOT] red, green, blue, brightness = (2143, 874, 792, 3654)
  4. Move the card away and watch all four numbers change. If they never change, the sensor is not connected — stop here.

  1. Write down the first three numbers you saw for your red card.
  2. In MoBot, find the is … the color: R … G … B … ± … block and type your three numbers into R, G and B.
  3. Leave the ± at 600 to begin with.
  4. Press Send to Device again.
  5. Hold the red card against the sensor: LED 2 lights up. Hold a blue or green card: LED 2 goes out.

The test passes when LED 2 lights for your chosen colour and stays dark for a different one.

They are not the 0-255 red/green/blue you may have met in a paint program. They are raw counts straight from the sensor and they can run into the thousands. They also change with the light in the room and with how close the card is. That is why this example makes you read them off your own robot instead of printing numbers here that would be wrong for you.

The ± is how fussy the robot is. Small (say 100) means “almost exactly this colour”. Large (say 1500) means “close enough”. If LED 2 never lights, make ± bigger. If it lights for everything, make it smaller.

block what it does
mobot_readcolor gives four numbers: red, green, blue and overall brightness.
mobot_compcolor true when the reading is within ± of the R, G, B you typed.
mobot_ledon the answer light.

The same blocks write different Python for each firmware, and MoBot picks the one that fits the robot you use. The 2.x tab is for reference. This is a reading copy: editing it changes nothing, so change the blocks instead. The name map lists the names that changed.

08-colour-detect.firmware-3.main.py
# What colour is that? — robotics (firmware 3.0)
#
# This is what MoBot puts on a robot running firmware 3.0 as main.py when you open
# 08-colour-detect.mobot and press Send to Device. It is GENERATED from the blocks;
# editing this file changes nothing. Edit the blocks instead.
# The firmware 2.x version is 08-colour-detect.main.py.
from machine import Pin
import time
print("[MOBOT]", 'Colour sensor. Hold something coloured right against the sensor.')
print("[MOBOT]", 'STEP 1: read the four numbers below.')
print("[MOBOT]", 'STEP 2: type the first three into the compare block and send it again.')
time.sleep(2)
while True:
print("[MOBOT]", ('red, green, blue, brightness = ' + str(robot.color_sensor.read())))
if robot.color_sensor.is_color([2000, 900, 800], robot.color_sensor.read(), tolerance=600):
robot.led2.set(1)
else:
robot.led2.set(0)
time.sleep(0.5)
  • Add a second compare block for a different colour and light LED 1 instead — now the robot can tell two colours apart.
  • Print the ± test as well as the numbers, by dropping the compare block into a Print to serial block. Now you can see True and False scroll past.
  • Shine a torch on the card and watch every number jump. Sensors have opinions about lighting.