# Micro:bit Light and UV Sensing

**URL:** <https://forum.core-electronics.com.au/t/micro-bit-light-and-uv-sensing/4362>\
**Category:** Guides\
**Created:** [17 January 2019 03:54 UTC](https://forum.core-electronics.com.au/t/micro-bit-light-and-uv-sensing/4362 "2019-01-17T03:54:42Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Stephen](https://yyz2.discourse-cdn.com/flex028/user_avatar/forum.core-electronics.com.au/stephen/32/1599_2.png) [@Stephen](https://forum.core-electronics.com.au/u/Stephen)\
**Post date:** [17 January 2019 03:54 UTC](https://forum.core-electronics.com.au/t/micro-bit-light-and-uv-sensing/4362/1 "2019-01-17T03:54:42Z")

</div>

Stephen just shared a new tutorial: [**"Micro:bit Light and UV Sensing"**](http://core-electronics.com.au/tutorials/Micro_bit_Light_and_UV_Sensing.html)  
  
 ![](https://canada1.discourse-cdn.com/flex028/uploads/core_electronics/original/2X/a/a58dde4e2793e0846616a4cb251210f70d94ad2c.png)  
  
Every Micro:bit&nbsp;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**](http://core-electronics.com.au/tutorials/Micro_bit_Light_and_UV_Sensing.html)

---

<div class="post-metadata">

**Author:** ![Christopher46689](https://avatars.discourse-cdn.com/v4/letter/c/e36b37/32.png) [@Christopher46689](https://forum.core-electronics.com.au/u/Christopher46689)\
**Post date:** [29 April 2019 02:07 UTC](https://forum.core-electronics.com.au/t/micro-bit-light-and-uv-sensing/4362/2 "2019-04-29T02:07:39Z")

</div>

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.

shaby

---

<div class="post-metadata">

**Author:** ![Stephen](https://yyz2.discourse-cdn.com/flex028/user_avatar/forum.core-electronics.com.au/stephen/32/1599_2.png) [@Stephen](https://forum.core-electronics.com.au/u/Stephen)\
**Post date:** [1 May 2019 01:53 UTC](https://forum.core-electronics.com.au/t/micro-bit-light-and-uv-sensing/4362/3 "2019-05-01T01:53:37Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Scot51597](https://avatars.discourse-cdn.com/v4/letter/s/e9a140/32.png) [@Scot51597](https://forum.core-electronics.com.au/u/Scot51597)\
**Post date:** [18 July 2021 23:52 UTC](https://forum.core-electronics.com.au/t/micro-bit-light-and-uv-sensing/4362/4 "2021-07-18T23:52:34Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![James](https://yyz2.discourse-cdn.com/flex028/user_avatar/forum.core-electronics.com.au/james/32/4400_2.png) [@James](https://forum.core-electronics.com.au/u/James)\
**Post date:** [19 July 2021 05:53 UTC](https://forum.core-electronics.com.au/t/micro-bit-light-and-uv-sensing/4362/5 "2021-07-19T05:53:18Z")

</div>

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!  
-James

---

<div class="post-metadata">

**Author:** ![Scot51597](https://avatars.discourse-cdn.com/v4/letter/s/e9a140/32.png) [@Scot51597](https://forum.core-electronics.com.au/u/Scot51597)\
**Post date:** [20 July 2021 00:07 UTC](https://forum.core-electronics.com.au/t/micro-bit-light-and-uv-sensing/4362/6 "2021-07-20T00:07:09Z")

</div>

​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.

---

<div class="post-metadata">

**Author:** ![Owen](https://yyz2.discourse-cdn.com/flex028/user_avatar/forum.core-electronics.com.au/owen/32/2948_2.png) [@Owen](https://forum.core-electronics.com.au/u/Owen)\
**Post date:** [20 July 2021 00:21 UTC](https://forum.core-electronics.com.au/t/micro-bit-light-and-uv-sensing/4362/7 "2021-07-20T00:21:13Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Scot51597](https://avatars.discourse-cdn.com/v4/letter/s/e9a140/32.png) [@Scot51597](https://forum.core-electronics.com.au/u/Scot51597)\
**Post date:** [20 July 2021 02:10 UTC](https://forum.core-electronics.com.au/t/micro-bit-light-and-uv-sensing/4362/8 "2021-07-20T02:10:21Z")

</div>

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.​

 ![IMG_9790.JPG](https://canada1.discourse-cdn.com/flex028/uploads/core_electronics/original/2X/4/46f95b30b437e7a78bab781374753c15f8549fd2.jpeg)

 ![IMG_9789.JPG](https://canada1.discourse-cdn.com/flex028/uploads/core_electronics/original/2X/6/6308634ea6eaeceedcc6ac1ad91176edf9f07af4.jpeg)

 ![IMG_9788.JPG](https://canada1.discourse-cdn.com/flex028/uploads/core_electronics/original/2X/3/3a14bb23331b03b91c39a35b38c63fd29271d5de.jpeg)
