Use MakeCode to Program the micro:bit with a TCS34725 Colour Sensor

ages 11+ BBC microbit BBC microbit v2 colour sensor makecode microbit microbit accessories microbit tutorials skittles sorting

This tutorial shows how to program the TCS34725 sensor which can be found in the Pimoroni Enviro:bit, Adafruit RGB and Flora sensor boards. These sensor boards all use the I2C ('eye squared see') protocol to communicate - this leaves lots of pins free on your micro:bit for controlling servo motors, LEDs, and even other I2C devices so you can build larger projects e.g., colour sorting machines, or robots.

In this project, you will learn:

  • how to set up the set up the Adafruit TCS34725 sensor with the micro:bit v2.x and makecode
    Adafruit TCS34725 RGB Colour Sensor micro:bit

You will need:

Optional hardware and tools:

Step 1: Load the enviro:bit extension in makecode

  • Go to extensions and type envirobit into the search
    Search and install envirobit extension in makecode for microbit
  • Click on the envirobit extension to add it to your makecode editor. You should now see this menu in your makecode editor:
    MakeCode Enviro:bit extension menu

Step 2: Create variables to hold red, green, and blue values

  • Go to the Variables menu and create variables named red, green, and blue. These will hold the values of each colour detected by our sensor:
    MakeCode variables

Step 3: Write our code to tell us when colours are detected

  • From the enviro:bit menu, drag this block to the on start block:
    MakeCode start sensor
  • Add the following blocks to the forever block
    MakeCode TCS34725 Colour Sensor

Test our code

  • Connect your sensor to the micro:bit as follows:
    • connect pin 19 from the micro:bit to SCL on the sensor
    • connect pin 20 from the micro:bit to SCA on the sensor
    • connect 0V to GND on the sensor
    • connect 3V to Vin on the sensor
      TCS34725 to micro:bit I2C connections
  • Download your code to the micro:bit
  • Hold different coloured objects in front of the sensor and watch the display on the micro:bit change. You can tune the amount of red, green, and blue required to trigger each word. A heart should show when nothing is in front of the sensor.

Next Steps

Now that you can detect colours with your micro:bit, you can attach a board that controls motors e.g., the MonkMakes Servo Kit for the micro:bit and create a mechanism to rotate compartments and push different coloured objects into each.

Older Post Newer Post