Sensors

CodeX has a number of built-in sensors:

  • Ambient Light

  • Infrared

  • Sound

  • Temperature

  • Accelerometer

  • Power supply voltage and current

In addition there are 4 peripheral connectors and 2 expansion ports for more sensor connections!

Start your sensor journey by exploring the Accelerometer.

1. Accelerometer

Sensing orientation and impact.

Just like a game controller or phone that can sense tilting or shaking, this sensor chip lets CodeX detect motion, impacts, and orientation.

An accelerometer is a device that measures proper acceleration. One way you commonly experience acceleration is when you’re moving and your speed or direction of motion changes. Think of speeding up in a car, the force you feel pushing you back in your seat or sideways when going through a turn. The forces you’re feeling are due to acceleration.

So if you’re standing still, acceleration is zero, right?

No! Well, not unless you’re floating in space!

Gravity also causes acceleration. So even if your accelerometer is sitting still on your desk, it will measure an acceleration due to the force of gravity pulling straight down toward the floor.

In a zero-g environment, like outer space, or if an object is in free-fall, the accelerometer will measure zero acceleration in all directions.

CodeX’s accelerometer measures the force of acceleration in 3-directions. Try the following program to print out the accelerometer’s x, y, z axes of acceleration continuously. Rotate your CodeX to different orientations as this program runs:

from codex import *

while True:
    # Read the (x,y,z) tuple from the accelerometer
    xyz = accel.read()

    # Print to the debug console
    print(xyz)

    # Format as a string, and print to LCD
    display.draw_text(f"x = {xyz[0]}", 20, 20, background=BLACK, scale=2)
    display.draw_text(f"y = {xyz[1]}", 20, 40, background=BLACK, scale=2)
    display.draw_text(f"z = {xyz[2]}", 20, 60, background=BLACK, scale=2)

With this program you can see the accelerometer values changing on CodeX’s display AND (while it’s still plugged into your computer) you can also see the print output streaming by on the Debug Console in CodeSpace.

  • Open the Debug Panel by clicking the “hamburger” icon at the lower right of your window in CodeSpace.

2. Graphing Sensor Data

Why not combine your Pixel Graphics capabilities from the previous project with the accelerometer?

The following program graphs the accelerometer’s X-axis value over time:

from codex import *

accel_max = 32768
y_scale = 120

while True:
    display.draw_line(0, 120, 239, 120, RED)

    # Graph the full screen width
    for t in range(240):
        x, y, z = accel.read()

        # Convert the X-axis value to a pixel offset from the baseline
        y_pix = 120 + int(y_scale * x / accel_max)
        display.set_pixel(t, y_pix, WHITE)

    # Clear the screen between plots
    display.clear()

Tilt the CodeX to the left and right as this program runs, and you’ll see a nifty graph of your X-axis orientation.

There are many more ways you can visualize sensor data with CodeX!

Explore and enjoy. We’d love to hear from you at info@firia.com if you have any questions along the way!