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
You will need:
- a micro:bit v2.x
- a Kitronik edge connector breakout board for the micro:bit
- an Adafruit RGB (TCS34735) colour sensor or Flora Colour Sensor, if you’re building an e-textiles project
- an understanding of how to create basic scripts in makecode and load them onto the micro:bit
Optional hardware and tools:
- jumper leads and breadboard or even just a small breadboard will do with some jumper wires
- soldering iron and solder if you would like to attach the header to your sensor permanently with a more solid connection
Step 1: Load the enviro:bit extension in makecode
- Go to
extensions
and typeenvirobit
into the search
- Click on the
envirobit
extension to add it to your makecode editor. You should now see this menu in your makecode editor:
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:
Step 3: Write our code to tell us when colours are detected
- From the
enviro:bit
menu, drag this block to theon start
block:
- Add the following blocks to the
forever
block
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
- 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.