Things used in this project

Hardware components:
Microbitheart
BBC micro:bit board
×1
DFRobot BOSON kit for micro:bit
×1

Code

night-light-measure.pyPython
calculate lower/upper bound of demo 1
from microbit import *

light_sensor = pin1.read_analog()
counter = 0
timer = running_time()

while (running_time() - timer) <= 3 * 1000:
   light_sensor += pin1.read_analog()
   counter += 1

light_sensor /= counter
print("mean light sensor value: ", light_sensor)
night-light.pyPython
demo 1
from microbit import *

light_sensor = pin1.read_analog()
counter = 0
timer = running_time()

light = 966.4033
dark = 14.81614

while True:
   light_sensor = pin1.read_analog()
   LED = int((light - light_sensor)/(light - dark)*1023)
   if LED > 1023:
      LED = 1023
   elif LED < 0:
      LED = 0
   print("LED lightness: ", LED)
   pin2.set_analog_period(1)
   pin2.write_analog(LED)
   sleep(0.5)
ceiling-fan.pyPython
demo 2
from microbit import *

switch = False

while True:
   if pin12.read_digital() is 1:
      while pin12.read_digital() is 1:
         sleep(0.5)
      switch = not switch
      if switch:
         pin16.write_digital(1)
         print("Turn ON")
      else:
         pin16.write_digital(0)
         print("Turn OFF")
alarm.pyPython
demo 3
from microbit import *
import music
status = pin8.read_digital()
def detect_shake():
   old_tilt_status = pin8.read_digital()
   sleep(0.1)
   new_tilt_status = pin8.read_digital()
   return abs(new_tilt_status - old_tilt_status)

while True:
   counter = 0
   timer = running_time()
   shake = detect_shake()
   while (running_time() - timer) <= 500:
      shake += detect_shake()
      counter += 1
   status = shake/counter
   print(status)
   if status is not 0.0:
      print("Alarm!!!")
      music.play(music.DADADADUM)

Credits

Ecddc9bf12bbd2c848d8b2e7757445e3
CAVEDU Education
10 projects • 9 followers
We provide tutorials of robotics, Arduino, Raspberry Pi and IoT topics. http://www.cavedu.com
Contact
Icon square orangewhite 120x120
DFRobot
7 projects • 31 followers
Contact
Photo
DFRobot Robotics
3 projects • 1 follower
Contact

Replications

Did you replicate this project? Share it!

I made one

Love this project? Think it could be improved? Tell us what you think!

Give feedback

Comments

Add projectSign up / LoginAbout BBC micro:bit