micro:bit Light Sensor
This article covers using the onboard light sensor of the micro:bit, as well as Light Dependant Resistors.
On board LEDs
In MakeCode you can use the onboard LEDs to get a rough sense of light intensity.
Try creating this snippet
basic.forever(() => { basic.showNumber(input.lightLevel() / 28) })
Light Dependant Resistor
You can also use a light dependant resistor in much the same way as you would with an Arduino
Code
Blocks
let number = 0 let reading = 0 basic.forever(() => { reading = pins.analogReadPin(AnalogPin.P2) number = reading / 50 basic.showNumber(number) })
Arduino
#include <Adafruit_Microbit.h>
Adafruit_Microbit_Matrix microbitMatrix;
int lightSensorPin = 2;
void setup()
{
microbitMatrix.begin();
}
void loop()
{
int sensorValue = analogRead(lightSensorPin);
microbitMatrix.print(sensorValue);
}