# LED blink when light level under 100

**URL:** <https://forum.makecode.com/t/led-blink-when-light-level-under-100/6485>\
**Category:** micro:bit\
**Created:** [March 2, 2021, 6:28pm UTC](https://forum.makecode.com/t/led-blink-when-light-level-under-100/6485 "2021-03-02T18:28:33Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Theoforus](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/theoforus/32/3474_2.png) [@Theoforus](https://forum.makecode.com/u/Theoforus)\
**Post date:** [March 2, 2021, 6:28pm UTC](https://forum.makecode.com/t/led-blink-when-light-level-under-100/6485/1 "2021-03-02T18:28:33Z")

</div>

I would like to run a function that turns on and off a LED when the light level is under 100.  
I gave it a try, but it did not work. Can anyone help?  
Here´s the code:

lightlevel = 0  
def turn\_off():  
pins.digital\_write\_pin(DigitalPin.P1, 0)  
def intervall():  
pins.digital\_write\_pin(DigitalPin.P1, 1)  
basic.pause(1000)  
pins.digital\_write\_pin(DigitalPin.P1, 0)  
basic.pause(randint(1000, 5000))

def on\_forever():  
global lightlevel  
lightlevel = input.light\_level()  
if input.light\_level() \>= 100:  
intervall()  
else:  
turn\_off()  
basic.forever(on\_forever)

---

<div class="post-metadata">

**Author:** ![xavvvvvv](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/xavvvvvv/32/15324_2.png) [@xavvvvvv](https://forum.makecode.com/u/xavvvvvv)\
**Post date:** [March 2, 2021, 6:58pm UTC](https://forum.makecode.com/t/led-blink-when-light-level-under-100/6485/2 "2021-03-02T18:58:59Z")

</div>

@Theoforus Does it need any extensions to run.

---

<div class="post-metadata">

**Author:** ![Theoforus](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/theoforus/32/3474_2.png) [@Theoforus](https://forum.makecode.com/u/Theoforus)\
**Post date:** [March 2, 2021, 9:51pm UTC](https://forum.makecode.com/t/led-blink-when-light-level-under-100/6485/3 "2021-03-02T21:51:23Z")

</div>

Thank you for your reply. I don´t think so. This is just a test to see if I can get a device turn on and off depending on the amount of light in the room. By now I´m just connecting a LED to P1 at the microbit.

---

<div class="post-metadata">

**Author:** ![Theoforus](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/theoforus/32/3474_2.png) [@Theoforus](https://forum.makecode.com/u/Theoforus)\
**Post date:** [March 3, 2021, 1:24am UTC](https://forum.makecode.com/t/led-blink-when-light-level-under-100/6485/4 "2021-03-03T01:24:01Z")

</div>

Here are the link to the project in makecode: [https://makecode.microbit.org/\_Pb5RzC5L1hp4](https://makecode.microbit.org/_Pb5RzC5L1hp4)

---

<div class="post-metadata">

**Author:** ![Theoforus](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/theoforus/32/3474_2.png) [@Theoforus](https://forum.makecode.com/u/Theoforus)\
**Post date:** [March 3, 2021, 4:47pm UTC](https://forum.makecode.com/t/led-blink-when-light-level-under-100/6485/5 "2021-03-03T16:47:26Z")

</div>

I found out. The light level was set to =0. When I removed it all was fine.

> **[Lys trigger LED](https://makecode.microbit.org/37206-30558-50633-65619)**
>
> Made with ❤️ in Microsoft MakeCode for micro:bit.
