microbitsensoren
Deutsch   English   

10. MORE MICRO:BIT SENSORS

 

 

YOU LEAR ...

 

how to use the following micro:bit sensors:

  • Light sensor
  • Temperature sensor
  • Touch sensor
  • Microphone sensor

 

 

LIGHT SENSOR

 

To measure the ambient light level, the micro:bit uses the LEDs on the front of the board. Normally, LEDs convert electrical current into visible light. However, the reverse is also possible: visible light can be converted into electrical current.

The micro:bit can measure this current and return values between 0 and 255 for the ambient light level, where 0 means completely dark and 255 means very bright.

 

Note that the micro:bit uses only the top row of LEDs to measure the light level. If you cover this row, the displayed value should be 0.

 

 

EXAMPLE

  The ambient light level is measured using the command display.read_light_level()and displayed in the output window using print(). The measurement interval is 1000 milliseconds.

Program:

from microbit import *

while True:
    lightValue = display.read_light_level()
    print(lightValue)
    sleep(1000)
► Copy to clipboard

 

 

TEMPERATURE SENSOR

 

The temperature sensor is integrated into the processor. It returns the ambient temperature. However, the measurements are not very accurate because the processor heats up, especially when it has been running for a long time.

For accurate temperature measurements, you should use an external sensor, such as the SHT85 temperature and humidity sensor.

 

 

 

EXAMPLE

  In this example, the temperature is measured in °C using the command temperature() and displayed in the output window using print().

The measurement interval is 300 milliseconds. If you touch the micro:bit near the processor with your warm fingers, a higher temperature will be displayed.

Program:

from microbit import *

while True:
    temp = temperature()
    print(temp)
    sleep(300)
► Copy to clipboard

 

 

TOUCH SENSOR

 

The touch sensor is located in the micro:bit logo. When you touch the logo, the sensor returns True; otherwise, it returns False.

 

 

 

 

EXAMPLE

  The command pin_logo.is_touched() returns True when you touch the logo and False otherwise. You can display these values in the output window using print(). In this example, the image YES is displayed on the LED display when the logo is touched. Otherwise, the image NO is displayed.

Program:

from microbit import *

while True:
    if pin_logo.is_touched():
        display.show(Image.YES)  
    else:
        display.show(Image.NO)  
    sleep(100)
► Copy to clipboard

Note: When checking a Boolean value, you do not need to write:
if pin_logo.is_touched() == True:
Instead, you can write more elegantly:
if pin_logo.is_touched():

 

 

MICROPHONE SENSOR

 

The micro:bit V2 is equipped with a microphone, which is visible as a small gold-coloured chip on the back of the board. The microphone sensor measures the sound level and returns values between 0 and 255. A value of 0 corresponds to complete silence, while 255 indicates a very loud sound.

 

 

 

 

EXAMPLE

  In this example, the ambient sound level is measured using the command microfone.sound_level() and displayed using print(). If the sound level is greater than 40, , all LEDs on the display light up for 500 milliseconds.

Program:

from microbit import *

def on():
    for y in range(5):
        for x in range(5):
            display.set_pixel(x, y, 9)
    sleep(500)
            
def off():
    display.clear()
                
while True:
    level = microphone.sound_level()
    print(level)
    if level > 40:
        on()
    else:
        off()            
    sleep(100)
► Copy to clipboard

 

 

REMEMBER ...

 

The light sensor uses the command display.read_light_level() to return the ambient light level as a value between 0 and 255.

The temperature sensor uses the command temperature() to measure the temperature in °C.

The command pin_logo.is_touched() returns True if you touch the micro:bit logo.

The command microfone.sound_level()returns the ambient sound level as a value between 0 and 255.

 

 

 

EXERCISES

 

 

1.

Light sensor
Write a program that turns on all LEDs on the display when the light sensor returns a value less than 100. When the light level is greater than 100, the LEDs should turn off.


2.

When you touch the micro:bit logo, the melody JUMP_UP should be played.

   

3.

Temperature sensor
Store the initial temperature in a variable called temp0. Then read the temperature sensor every 1000 milliseconds.

  • If the temperature increases, display the image ARROW_N.
  • If the temperature decreases, display the image ARROW_S.
  • If the temperature remains unchanged, display the image ARROW_E.

 

4.
Start a program by clapping.
Write a function go() that starts with the LED in the top-left corner and then moves across the entire display. Each LED should light up for 400 milliseconds and then turn off. Next, the LED immediately to its right should light up and turn off. When the first row is complete, the LEDs in the second row should light up one after another, and so on until all LEDs have been activated.
 
The function go() should be called when sound_level > 40, for example, when you clap your hands.