Buying Links: 

The MonkMakes Sensor Board for micro:bit allows you to sense sound level, temperature and light level.

Features

  • 3V and GND connections can be made from either side and allow you to power a second board such as the MonkMakes Relay Board or MonkMakes Speaker.
  • LED ‘power on’ indicator
  • Reverse polarity protection
  • All three sensors are analog and can be connected to pins P0, P1 and P2 using alligator clips.

Getting Started

Connecting to your micro:bit

You only have to wire up the sensors that you are actually using, but you could wire all the sensors up as shown below.

Sound using JavaScript Blocks Editor

Here is an example of using the Sensor Board to display a bargraph to indicate the sound level. Click on the image below to try it out. Making a noise into the microphone will make the LEDs dance.

Thermometer USING JAVASCRIPT BLOCKS EDITOR

This is an example of using the Sensor Board to display the temperature, try putting your finger on the temperature sensor to warm it up. You can run the example below by clicking on it.

 

Light Sensing USING JAVASCRIPT BLOCKS EDITOR

Here is an example of using the Sensor Board to display a bargraph to indicate the light level. Click on the image below to try it out. Put your finger over the light sensor to make it dark or shine a flash-light onto it to make more LEDs light up.

https://makecode.microbit.org/_J8kfgyHDoHRP

 

 

Python

Paste the following code into the Python window and then Download the file and copy it onto your your micro:bit. Open up the REPL to see readings from all three sensors.

from microbit import *

from math import log

class AnalogSensor:

    

    def __init__(self, mb_pin):

        self.mb_pin = mb_pin

        

    def value(self):

        # analog input on mb_pin

        return self.mb_pin.read_analog()

    

    

class SoundSensor(AnalogSensor):

    

    # The top half of the waveform gets clipped above 2V, so only use the bottom half

    # The ADC does not like sampling a rapidly changing audio signal – the average reading

    # drops to about 320 instead of normally biased 511.

    

    def __init__(self, mb_pin, dc_offset, scaled_max):

        super().__init__(mb_pin)

        self.dc_offset, self.scaled_max = dc_offset, scaled_max

        

    def level(self, samples=20, period=5):

        most_neg = 0

        for i in range (0, samples):

            reading = self.dc_offset – self.value()

            if reading > most_neg:

                most_neg = reading

            sleep(period)

        return most_neg * self.scaled_max / self.dc_offset

    

    def sample(self):

        return self.value()

    

    

class LightSensor(AnalogSensor):

        

    def __init__(self, mb_pin, min_value, max_value, scaled_max):

        super().__init__(mb_pin)

        self.min_value, self.max_value, self.scaled_max = min_value, max_value, scaled_max

        self.range = max_value – min_value

        

    def read(self):

        return (self.value() – self.min_value) * self.scaled_max / self.range

    

    

class TemperatureSensor(AnalogSensor):

    

    def __init__(self, mb_pin, r0, r2, b):

        super().__init__(mb_pin)

        self.r0, self.r2, self.b = r0, r2, b

        

    def read_c(self):

        v33 = 1 # actually result is independent of this value so use 1

        V = self.value() * v33 / 1023

        R = self.r2 * (v33 – V) / V

        t0 = 273.15     # 0 deg C in K

        t25 = t0 + 25.0 # 25 deg C in K

        # Steinhart-Hart equation – Google it

        inv_T = 1/t25 + 1/self.b * log(R/self.r0)

        T = (1/inv_T – t0)

        return T

    

    def read_f(self):

        return self.read_c() * 9/5 + 32

sound_sensor = SoundSensor(pin0, 340, 100)

temperature_sensor = TemperatureSensor(pin1, 100000, 100000, 4275)

light_sensor = LightSensor(pin2, 0, 980, 100)

while True:

    print(“Light: ” + str(light_sensor.read()))

    print(“Sound: ” + str(sound_sensor.level()))

    print(“temp: ” + str(temperature_sensor.read_c()))

    print()

    sleep(1000)