Micro:bit Light and UV Sensing

Stephen just shared a new tutorial: "Micro:bit Light and UV Sensing"

Every Micro:bit has the ability to measure visible light, but what about UV light? Imagine that you are growing a plant on the window sill in a well-lit room. Does your plant have enough light? Plants need UV light to photosynthesize energy, som…

Read more

1 Like

Hi Stephen
A part of your code has me a little baffled. To me it seems that the while loop will only run if the variable UV reads 255 as you have used the equals sign.

To me the logic should be that while ever UV isn’t 255 it should read the analog pin (as you stated that 255 is very hard to get to, it will make the program loop continuously).

The way it is now, the loop will never happen so why have it? If you want to just show the reading each time button b is pressed leave the whole loop out.

If you want it to continuously show the readings after the button is pressed one time, I would also think that the show number and the clear screen should be inside the loop and the while statement should be while button b != 255

Perhaps I am missing some logic here (which I often do).

BTW - I really like what Core is doing in this space - keep up the good work.


1 Like

Hi Christopher,

Its explained in the tutorial, but when creating this tutorial I found that I was getting false readings of 255 from the sensors every fifth read attempt or so. The while loop forces a new reading to be taken until its no longer 255, then shows the result. The eliminates the possibility of a 255 result, which is likely impossible anyway, but also forces the device to take a new reading until its no longer returning an error.

It may not be necessary on all devices, and you could leave the loop out and just ignore the occasional 255 result.

1 Like

When I connect the UV sensor it doesn’t give correct numbers. At one point it just said 15 the whole time, even when covered up. How do I get it to talk to the microbit?

1 Like

Hi Scot,

I’ve had a re-read of the tutorial, it seems pretty simple, do you feel like chucking up some pictures of your wiring, and any code that you’re running?

Keen to see you get your project on its feet!

​Hi James,

I send photos a bit later today. I connected the UV Sensor to the microbit shield or the microbit directly using jumpers and alligator clips because the sensor plug isn’t compatible.

1 Like

Hey Scot,

No worries at all. We’ll take a look into this one later on once there’s more intel. Let us know if you run into any other issues.

Hi James,

Here is my code and the setup. I have used jump leads and alligator clips to connect the UV sensor the the microbit P0, 3V and Ground the same way it says to do it in the tutorial.​