In this article,we propose an overview of the Adafruit Circuit Playground Express (CPX) built-in sensors. This aim of this page is to show you how to leverage them in MakeCode. So, if you are not familiar with MakeCode, we advise to have a look on MakeCode page in this wiki.
The CPX built-on sensors available are :
In MakeCode, there are two means to read the code : through events or by reading live data.
OnShake
event runs code when a certain type of shaking is detected by the accelerometer.Rotation
gets a immediate reading from the accelerometer.You will find below the 2 style of programming with sensors : through events (on left of the picture below) and through live data (on right on the picture below).
Below, you will find a small presentation each builtin sensors in the CPX.
On the CPX, there are 2 buttons : Button A
or Button B
. The two buttons could be coded together Button A + B
.
OnEvent
block allows to respond to action (click, long click and other events.IsPressed
that is to say the code check if the button is pressed. You can also used the input WasPressed
in situations in which the program have a risk to busy when the user clicked. In this case, the code check if the button was clicked earlier.Example : The click on button A is used to play a sound. The state of button A is checked in a forever loop to switch between red (if button A is pressed) and green (if nothing happens).
On the CPX, there are 7 capacitive pins (A1, A2, A3, A4, A5, A6 and A7). They can be used as buttons (like buttons A or B).
Button A
or Button B
as the input source, use a pin name like A1
.IsPressed
or WasPressed
.Example : An event on pin A4 is used to show pixels animation and the state of pin A7 is checked in a forever loop to switch between blue or red on the neopixels.
On the CPX, you have 1 light sensor which allows to run code when the brithgness change.
on light
condition, the sensor allows to run code. light level
returns the light intensity : from 0 (no light) to 255 (max light).Example :
Using the on light
event to run a sparkle animation.
The CPX has one accelerometer that runs code when acceleration happens on axes X (left to right), Y (bottom to top), Z (aligned with the gravity). Please see the picture below.
on Gesture
enable to program adafruit to run a code folowing a certain event such shake, freefall or orientation events.acceleration
input can return the acceleration value (in milli-g, e.g 1/1000 of a g) according to direction (on x, y or z axis) or the combined force in all directions.Example : Using a shake to play a sound (event). A forever loop can display the accelerometer reading (live data).
The CPX has one microphone that runs code when a sound happens.
on loud sound
event detect a peak in the sound level, e.g a hand clapping. You can change the lound treshold by using the set sound to sound level (e.g https://learn.adafruit.com/make-it-sense/makecode-3).Example : An sound variation can play a music; a forever loop can continuously display the sound level.
The CPX has one switch button which have 2 positions : left or right. This button can be very useful to create some on/off programs.
on switch moved
input enable to run code when the switch button is moved to the left position or to the right position. switch right
block (in input) indicates if the switch is on the right position.Example : events can be play different animations if the switch button is on the left or on the right (event). A forever loop can play an animation if the switch is on the right position and no animation if the switch is on the left (live data).
The CPX has a thermometer that can track the ambient temperature (in Celcius or Fahrenheit degrees).
on temperature (condition changed)
runs code when temperature changes from hot to cold or from cold to hot. temperature block
return the current temperature by checking how hot its computer chips are.Exmample : If the temperature change over 15°C, a sound is playing. A forever loop can graphs the current temperature using the LEDs over 20°C.
The CPX have an onboard infrared transceiver (i.e transmitter and receiver) which allow to communicate data between CPXs.
On infrared received
receive number.Example : A RVB (i.e number) color can be sent over IR to turn on the neopixel remotely.
Pictures as well as some text was reproduced from the Adafruit circuit playground express documentation. Some pictures are available under a CC BY-NC-SA license. Others are copyright AdaFruit and "all rights reserved" and reproduced with permission. Before you reuse any picture from this website, make sure to look at the license information.
For more information about using Adafruit, see https://learn.adafruit.com.