Restore BlinkM LED to Factory Settings Using I2C Bus
This example shows how to restore a BlinkM RGB LED to its factory settings by communicating with it over the I2C bus using a Total Phase® Aardvark controller board.
To begin, list all available Total Phase Aardvark controller boards on your machine by using the aardvarklist
command. If you have multiple Aardvark controllers connected to your machine, aardvarklist
returns all of them. You can differentiate controllers by looking at the SerialNumber
column in the table. Find your Aardvark controller's serial number by looking at the serial number printed on the board itself.
controllerList = aardvarklist
controllerList=2×2 table
Model SerialNumber
______________________ ____________
1 "Total Phase Aardvark" "2239143731"
2 "Total Phase Aardvark" "2239284977"
Create a connection to the Aardvark controller using the aardvark
function with the serial number returned by aardvarklist
. In this example, connect to the controller that has serial number 2239143731
.
aardvarkObj = aardvark(controllerList.SerialNumber(1))
aardvarkObj = Aardvark with properties: Model: "Total Phase Aardvark" SerialNumber: "2239143731" AvailableDigitalPins: ["Pin1" "Pin3" "Pin5" "Pin7" "Pin8" "Pin9"] Show all properties, functions
To perform I2C operations with the BlinkM, you must first discover and connect to it.
Before creating a connection to the BlinkM, you must provide it with power to discover it. Set the EnableTargetPower
property of the aardvark
object to true. Enabling this setting provides power to the pins on the controller that are connected to the BlinkM.
aardvarkObj.EnableTargetPower = 1;
Detect I2C peripheral devices connected to the Aardvark using the scanI2CBus
function. This function returns the I2C addresses of peripheral devices. In this example, the BlinkM LED is the only peripheral device connected.
Add a short pause after providing power to the BlinkM to ensure that it is detected by scanI2CBus
.
pause(1) address = scanI2CBus(aardvarkObj)
address = "0x9"
Create a connection to the BlinkM LED peripheral device using the device
function with the address returned by scanI2CBus
.
blinkM = device(aardvarkObj,I2CAddress = address)
blinkM = I2CDevice with properties: Protocol: "I2C" I2CAddress: 9 BitRate: 100000 ByteOrder: "little-endian" Show all functions
The default factory setting for the BlinkM is to play the sequence of colors white, red, green, blue, and off. The white light remains for 100 clock ticks, followed by red, green, blue, and off for 50 clock ticks each. In this example, send this sequence to the BlinkM as a script using the I2C interface.
The BlinkM datasheet specifies that the script length is set by writing 'L'
to the device followed by the following three arguments. You use the write
function to write binary data onto the I2C bus.
First argument — Specify the script ID as 0 to use the default settings.
Second argument — Specify the length of this script as 6.
Third argument — Specify the number of repeats of the script as 0 to play it forever.
Send the command as binary data to the BlinkM using the write
function.
write(blinkM,['L' 0 6 0]);
Since the EEPROM write time is approximately 20 ms for the BlinkM, you might have to pause to ensure that the command is written successfully.
According to the BlinkM datasheet, you can write a script line to the device by writing 'W'
to the device followed by seven arguments.
First argument — Specify the script ID as 0.
Second argument — Specify the line number to write.
Third argument — Specify the duration of clock ticks for the command to last.
Fourth to seventh arguments — Specify the actual BlinkM command and its arguments.
First, set the fade speed of the device by writing 'f'
followed by the speed.
write(blinkM,['W' 0 0 1 'f' 10 0 0]);
For the next five commands, specify the color sequence as white, red, green, blue, and off. According to the datasheet, you can set a color on the LED by writing 'n'
followed by the RGB values. For example, the RGB value for white is 255, 255, 255.
write(blinkM,['W' 0 1 100 'n' 255 255 255]); write(blinkM,['W' 0 2 50 'n' 255 0 0]); write(blinkM,['W' 0 3 50 'n' 0 255 0]); write(blinkM,['W' 0 4 50 'n' 0 0 255]); write(blinkM,['W' 0 5 50 'n' 0 0 0]);
The BlinkM datasheet indicates that the command to start up the device is 'B'
followed by five arguments.
First argument — Specify as 1 to play a script.
Second argument — Specify the script ID as 0.
Third argument — Specify the number of repeats of the script as 0.
Fourth argument — Specify the fade speed.
Fifth argument — Specify the time adjustment.
write(blinkM,['B' 1 0 0 8 0]);
You can play the script written in this example by writing 'p'
followed by three arguments.
First argument — Specify the script ID as 0.
Second argument — Specify the number of repeats of the script as 0.
Third argument — Specify the script line number to start playing from.
write(blinkM,['p' 0 0 0]);
Now that the BlinkM is restored to its default setting, you can update the script to play a different customized sequence on the BlinkM LED.
When you finish working with the BlinkM LED and Aardvark controller, disconnect from both by clearing both objects.
clear blinkM clear aardvarkObj
See Also
aardvarklist
| aardvark
| scanI2CBus
| device
| write