Micro:bit Picosat Series - Sensor Module #1 - Light levels sensor

Introduction

The Lux sensor is in fact based on a photodiode. The Photodiode’s resistance changes as the amount of light shone onto the photodiode changes.

What is an amount of light? A possible analogy is like the water exiting the shower head. If your hand is right next to the shower head, it becomes overwhelmed with water very quickly. But when the hand is further away, then less water will touch your hand in a period of time.

The closer the photodiode is to the light source, the more lumens it will measure. The further away, the less lumens.

Method

  1. Attach the Micro:bit and the sensor board to the Carrier board as shown above.

  2. Using the USB cable, connect the Micro:bit to your computer.

  3. Startup Google Chrome and go to the website https://makecode.microbit.org/

  4. Start a New Project with the New Project button.

  5. Select Javascript in top center menu.

  6. Delete all the javascript code visible on the screen.

  7. Copy and paste the following javascript into the text area.

    let lux = 0;

    let R = 0;

    let rho = 0;

    let R1 = 39000; // the circuits inbuilt resistor


    basic.forever(function () {
    basic.pause(500); // pause the function to reduce number of readouts. rho = pins.analogReadPin(AnalogPin.P2) / 1023; //find the sensor voltage proportional to input voltage R = R1 * rho / (1 - rho); // calculate the resistance of the humidity sensor
    lux = Math.exp(-0.7925 * Math.log(R) + 12.43); // formula to calculate value of lumens based on the resistance measured. serial.writeValue("Lux (Lumens)", lux) //print the value of lumens after calculation})

  8. Changethe file name from “Untitled” to “LuxLevels”.

  9. Press the download button to automatically transfer the file to the connected Micro:bit.

  10. When the download is complete, the button for “Show Console Device” may appear. If so, click on it to display the light levels as lumens on screen.

  11. To see the effects of lumens rapidly change, try point the sensor closer and further away from a light source like a light bulb or screen monitor.

Explanation

What does code do?

The code has to perform several calculations in order to return a human understandable value.

The sensor acts like a resister. When 3Volts is passed through the photodiode, the voltage will begin to drop. We measure this drop in voltage to help tell us what the resistance was in the photodiode. The resistance of the photodiode correlates or relates to lumens.

A mathematical equation has been derived to link the resistance measure in the photodiode to the expected lumens.

Firstly, when Micro:bit reads the voltage of a pin, it always converts this value to a range between 0 and 1023.

Secondly, we have another equation to help find the resistance of the photodiode. This is based on a ratio of voltage and resitance before and after the photodiode.

Finally, a mathematical equation was designed to provide the value of lumen for a given resistance. This was done using data provided by the suppliers of this photodiode.

Patrick Wang