CAVEDU EducationDFRobotDFRobot Robotics
Published © GPL3+

Micro:bit Smart Home - light, fan and alarm system.

We will show you how to use micro:bit and DFrobot BOSON kit to build a smart house.

IntermediateFull instructions provided3 hours7,563
Micro:bit Smart Home - light, fan and alarm system.

Things used in this project

Story

Read more

Code

night-light-measure.py

Python
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.py

Python
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.py

Python
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.py

Python
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

CAVEDU Education

CAVEDU Education

11 projects • 28 followers
We provide tutorials of robotics, Arduino, Raspberry Pi and IoT topics. http://www.cavedu.com
DFRobot

DFRobot

62 projects • 144 followers
Empowering Creation for Future Innovators
DFRobot Robotics

DFRobot Robotics

7 projects • 13 followers

Comments