HomeTurtlegraficsGPanelRobotics WebTigerPython
 Python - Online
sensor
Deutsch   English   

3. ACCELERATION SENSOR

 

 

YOU LEARN HERE...

 

how you can use the acceleration sensor to detect changes in position and movements of the Calliope.

 

 

SENSOR VALUES

 

The position sensor (acceleration sensor) measures both the constant gravitational acceleration of around 10 m/s2, which points vertically downwards, and the accelerations caused by movements.

The position sensor is clearly visible on the board. Similar sensors are also built into most smartphones.

The sensor measures the x, y and z components of the acceleration, from which the forward tilt (pitch) and sideways tilt (roll) can be calculated.

 

It is similar to the attitude display of aeroplanes, which are displayed with an artificial horizon. If you tilt the board forwards or backwards, the pitch angle changes; if you tilt it sideways, the roll angle changes.

In the following programme you use the functions accelerometer.get_x(), accelerometer.get_y() or accelerometer.get_z(), which return values in the range from approximately -2000 to 2000 (corresponds to the accelerations -20 m/s2 tos 20 m/s2). With acceloremeter.get_values() you get back all three values in one tuple.

If the z-axis is pointing vertically downwards, the value of the acceleration due to gravity, i.e. 9.81 m/s2must be measured. The sensor returns approximately 981.

Program:

from calliope_mini import *

while True:
    ax = accelerometer.get_x()
    ay = accelerometer.get_y()
    az = accelerometer.get_z()
    print(ax, ay, az)
    sleep(1000)
► Copy to clipboard

 

 

EXAMPLES


 

Example 1: Query sensor values

Start the programme below, tilt the Calliope left and right around the USB port and observe the sensor values that are displayed in the output window with print(acc).

The display shows the arrow ARROW_E for a positive value and the arrow ARROW_W for a negative value.


 

Program:

from calliope_mini import *

while True:
    acc = accelerometer.get_x()
    print(acc)
    if acc > 0:
        display.show(Image.ARROW_E)
    else:
        display.show(Image.ARROW_W)
    sleep(100)  
► Copy to clipboard

 

Example 2: Spirit level vial

 

The Calliope should work like a spirit level. You use the fact that the x and y components of the accelerations are approximately 0 when the board is in a horizontal position.

 


Only one pixel is displayed, which shifts to the right, left, up or down depending on the tilt. The aim is to align the level so that the centre LED lights up.

In the programme, you use four conditions to determine the x and y values of the pixel and then delete the pixel before lighting the new one.

Program:

from calliope_mini import *

x = 1
y = 1

while True:
    accX = accelerometer.get_x()
    accY = accelerometer.get_y() 
    if accX > 100 and y < 4:
        y += 1
    elif accX < -100 and y > 0:
        y -= 1
    elif accY > 100 and x < 4:
        x += 1
    elif accY < -100 and x > 0:
        x -= 1
    display.clear()
    display.set_pixel(x, y, 9)
    sleep(100)
► Copy to clipboard

 

 

 

REMEMBER ...

 

You can use the acceleration sensor to record the movements and position of the Calliope. The sensor values are regularly queried every 100 milliseconds in an ‘endless’ while true loop. This is also known as polling the sensor values.

 

 

TO SOLVE BY YOURSELF

 

 

1.

Add to the first example so that the rotation of the Calliope around the x-axis is also measured and displayed with the appropriate arrows Image.ARROW_N or Image.ARROW_S. Note that the values are not clearly < 0 or < 0. One possibility is to display the matching arrows if the sensor value is < 100 or < -100. Then you can tilt the Calliope unambiguously in one direction.


2.

By tilting the Calliope, all pixels at the edge should be switched on individually.