Adding external sensor to the CPX and makecode

Hello! I am trying to connect an external color sensor ( to the CPX using makecode, I know there is one built-in to the CPX but I need to it to be external for my project !

in this video:
you guys mention adding external sensors to the CPX but only add a servo so it never gets to the part about adding external sensors…

so do I need to somehow use the i2C blocks under Advanced -> pins in makecode but can someone point me to a working example project or offer any advice on how to use these, I know I need the block that is “ambientcolor” once the color has been detected but can I use this i2C block to get these values from an external sensor?

Alternatively I found I found this Makecode package for the TCS34725 Color Sensor. … CS34725.ts

do you think I could use this? Although I have no idea how I get this package into my makecode lol

I am so confused! Thanks so much!

Hi Eliza,

Here is a handy guide on how to add sensors to your CPX, this one is for a light sensor but you could easily adapt it for a colour sensor, hope it helps!

Thanks for sending this but I am specifically looking for how to do this in MakeCode, the info you sent is for circuitpython, but I appreciate you taking the time to send this!

Hi Eliza, My apologies, maybe this link on i2c connections to the micro bit in makecode is more what you need?

Hi @Eliza143145, your link is broken so I’m having trouble finding the library you’re pointing to:

However, if that doesn’t pan out there are other ways…
To expand on @Mitchell’s reply, you may be up for a little legwork but it’s not so bad. Without a library you can still work with the device. Things will just be a little more manual, but you can still pre-bake some code for students; to conceal the inner-workings and keep the experience very understandable.

You can create your own functions in makecode to

  • Initialise the device
  • Perform a reading

You work with i2c devices by writing data to- and reading data from registers. You will need to dig into the color sensor manual to work out the commands that you will need to send with i2c write and i2c read.

Following the rabbit hole for the device you linked, we can find the manual/datasheet

And digging in, we can find the register table. A good hello world script is to attempt to read the device ID, since you know exactly what data it’s going to return

If you have access to a logic analyser at your university, that could be an invaluable debugging tool - you will be able to see the data flowing back and forth to make sure the system is working.

After your hello-world, you will need to write a function that initialises the device (sets it up to perform the measurements you want) and another to read the colour data. Your students will need to include your functions in their code, but that’s no big deal. Instruct them to copy+paste the functions in and you’re ready to roll.

Here’s some pseudocode to give you inspiration

function initialiseSensor() {
    // Write to appropriate registers to set up the device

    pins.i2cWriteRegister(0, 0, 0) // your setup code here
    pins.i2cWriteRegister(0, 0, 0) //
    pins.i2cWriteRegister(0, 0, 0) //

function readColor() {
    // read data from appropriate registers
    let grnHi = pins.i2cReadRegister(0, 0)
    let grnLo = pins.i2cReadRegister(0, 0)
    let redHi = pins.i2cReadRegister(0, 0)
    let redLo = pins.i2cReadRegister(0, 0)
    let bluHi = pins.i2cReadRegister(0, 0)
    let bluLo = pins.i2cReadRegister(0, 0)
    // etc

    let data = // something to concatenate / process the data

    return data
1 Like