HomeTurtlegraficsGPanelRobotics WebTigerPython
 Python - Online
kompass
Deutsch   English   

8. MAGNETIC FIELD AND GYRO SENSOR

 

 

YOU LEARN HERE...

 

how you can determine the magnetic field with the Calliope and how a gyro sensor works.

 

 

DISPLAY SENSOR VALUES

 

The magnetic field and gyro sensors are integrated in the hardware of the position sensor chip. The magnetic field sensor returns the current x, y and z components of the magnetic field at the location of the sensor. With magnatometer.get_x() you can query the x- components. With the Calliope mini3 you must use the compass.get_x() function.

Calliope mini 1/2 Calliope mini 3

Programm:

from calliope_mini import *

while True:
    x = magnetometer.get_x()
    y = magnetometer.get_y()
    z = magnetometer.get_z()
    print(x, y, z)
    sleep(500)
► Copy to clipboard

Programm:

from calliope_mini import *

while True:
    x = compass.get_x()
    y = compass.get_y()
    z = compass.get_z()
    print(x, y, z)
    sleep(500)
► Copy to clipboard

 

 

EXAMPLES

  Search for mines

Place a small magnet, such as those found on memo boards, under a cardboard lid. You can think of the magnet as a mine that you have to find with the Calliope as a mine detector. In your program, the closer the board gets to the mine, the brighter the LEDs light up.

 

You use the function magnetometer.get_z(), which returns the strength of the magnetic field in the vertical plane. Then scale it so that you get a brightness value between 0 and 9. In the function light(v) you switch on all LEDs together.

Program:

from calliope_mini import *

def light(n):
    for i in range(5):
        for k in range(5):
            display.set_pixel(i, k, n)

while True:
    z = magnetometer.get_z()
    #z = compass.get_z()
    v = abs(z // 3300)
    n = min(9, v)
    light(n)
    sleep(20) 
    
► Copy to clipboard

 

Measuring the water level

Your test system consists of a flower vase and a magnet attached to a floating cork cone. You can use three nails to stabilize the cone so that the magnet always moves in the same vertical line as the water level changes.

By measuring the z-component of the magnetic field, you obtain information about the height of the water column. You divide it into 3 water level ranges “too high”, “ok” and “too low”.

 

 

It is best to imagine that the vessel is in three “states”, which you record with the variable state, which can take the values “high”, “ok” and “low”. You use the scaled value of the magnetic field to decide whether the state changes.

You can also use sound to create an acoustic alarm.

 

 



Program:

from calliope_mini import *
import music

state = "ok"

while True:
    z = magnetometer.get_z()
    #z = compass.get_z()
    #print(z)   
    if z >= 700 and state != "high":
        state = "high"
        print("->high")
    elif z >= 500 and z < 700 and state != "ok":
        state = "ok"
        print("->ok")
    elif z < 500 and state != "low":
        state = "low"
        print("->low")
    if state == "high":
        music.pitch(2000, 100)
    elif state == "low":
        music.pitch(500, 100)
    sleep(100)
    
► Copy to clipboard

Note that the z-component of the magnetic field depends on the magnet you are using in this experiment. Activate the line print(z) at the beginning and change the limit values if necessary.


Using the gyrometer

The gyro sensor measures the rotation speed (angle per time unit, also called angular velocity), in contrast to the position sensor, the values remain close to zero when the Calliope stops moving.

With gyrometer.get_x(), gyrometer.get_y() and gyrometer.get_z() you get values in the range -32768 and 32767. Analogous to the mine search, all LEDs are highlighted.

 

Program:

from calliope_mini import *

def light(n):
    for i in range(5):
        for k in range(5):
            display.set_pixel(i, k, n)

while True:
    bz = gyrometer.get_z()
    v = abs(bz // 3300)
    v = min(9, v)
    light(v)
    sleep(20)
► Copy to clipboard


 

REMEMBER ...

 

The magnetic field sensor can be used for contactless detection of the position of a magnet or a magnetic coil. The gyro sensor is used to determine the speed of rotation.

 

 

TO SOLVE BY YOURSELF

 

 

 
1.

Non-contact revolution counters (e.g. for wheels) often use a magnet that passes in front of a magnetic field sensor once for each revolution. Attach a small magnet (e.g. from a memoboard or a magnetic snapper) to a bicycle wheel and write a program that writes out the number of revolutions in the terminal window. How can this be used to build a kilometer counter?