Micro:bit Smart Home Projects

Code Download

top1

Code download:

Download and unzip these files. Here all codes are in folder Code.

bottom1

Congratulations! You’ve fully learned the micro:bit features and are ready to use them! This is a remarkable achievement, and I’m sure your enthusiasm for technology and your spirit of exploration must have contributed to it.

z001

Next, we may wish to first understand the modules of smart homes, and you will better understand working principles and application scenarios.

After this learning and accumulation, you will be able to design more innovative and practical smart home solutions. Let’s explore the smart home!

Single Sensor/Module Projects

Project 1: 6812 RGB LED Module

KS6009

Introduction

3top

RGB LED is imaged in the intersection of three primary colours (RGB): red, green and blue. Both white LED and RGB LED are able to emit white light. The former is presented directly in white, while the latter is mixed with red, green and blue.

line3

Trichromatic theory:

z101

Human eyes are sensitive to RGB colours. Most colours can be synthesized by RGB in different proportions. Therefore, the vast majority of monochromatic light can also be decomposed into RGB colours. This is the most basic principle of colorimetry — trichromatic theory.

line3

6812 RGB LED module is common-used which includes four independent and controllable RGB beads. Through digital signals, brightness and colour of them can be adjusted to realize various light effects. In application, it is widely used to decorative lighting, electronic display and entertainment equipment. All in all, this module is easy to control but with powerful functions.

3bottom

Components

Name

Picture

micro:bit board *1

microbitV2

micro USB cable *1

usb

6812 RGB module *1

KS6009

3 pin DuPont wire *1

3pin

Wiring Diagram

z102

Wire up as above and connect the board to your computer via micro USB cable.

Test Code

Complete code:

z103

line1

When using 6812 RGB module, additional Neopixel extension is required.

Add extension:

1. Click Extension.

z104

2. Enter the following link in the search box:

https://github.com/microsoft/pxt-neopixel

and press the “Enter”/z105 key on your keyboard.

z106

3. And click the extension neopixel.

z107

4. Successfully loaded:

z108

Brief description:

Initialization: set the module to pin P14. There are four pixels on the module so we modify the led number to 4.

z109

Set all pixels to light up in red for 1 second.

z110

Similarly, set all pixels to yellow, blue, purple and white with each colour for 1 second.

z111

Test Result

4top

Download the code (for Windows 10 App, click to download code; for browsers, download the “hex” file and sent it to the micro: bit board) and power on via micro USB cable.

The 6812 RGB module lights up in red, yellow, blue, purple and white. Each colour remains 1 second and this sequence repeats.

4bottom

Project 2: Ultrasonic Sensor

KS0504

Introduction

3top

z201

Bats emit ultrasound through the mouth for echolocation or communication. These sound waves will bounce back so that bats orient and determine the obstacle position, size and whether nearby objects are moving. Because of high frequencies of this sound, people are not able to hear that. Scientists developed ultrasonic ranging by studying this method of detection in bats.

Working principle:

  1. The transmitter will generate a high-frequency sound wave signal which will be converted to ultrasonic waves.

  2. This ultrasonic signal will be sent from the transmitter and propagated outward.

  3. When the ultrasonic wave encounters an obstacle, it will be reflected back.

  4. The receiver will pick up these echo signals.

  5. The receiver will also measure the time from transmission to reflection of the wave.

  6. According to this time, the distance of the obstacle can be calculated.

z202

3bottom

Components

Name

Picture

micro:bit board *1

microbitV2

micro USB cable *1

usb

Ultrasonic sensor *1

KS0504

4pin DuPont wire *1

4pin

Wiring Diagram

z203

Test Code

Complete code:

z204

line1

When using ultrasonic module, additional Sonar extension is required.

Add extension:

1. Click Extension.

z104

2. Enter sonar in the search box and search, and you will see the sonar extension.

z205

3. Successfully loaded:

z206

Brief description:

Serial input and output via USB connections.

z207

Initialize ultrasonic module, set pin trig to P12 and pin echo to P8, set the unit to cm.

Store the detected distance value to the variable distance.

z208

Refresh the results every 100ms.

z209

Test Result

4top

Download the code (for Windows 10 App, click to download code; for browsers, download the “hex” file and sent it to the micro: bit board) and power on via micro USB cable. The distance values detected by the ultrasonic sensor will be shown on the serial monitor and they will be refreshed every 100ms.

z210

4bottom

Project 3: 130 Motor

KS6038

Introduction

3top

It is interesting that 130 motor does not require an additional driver board to enable itself. Please mount the fan on the motor first.

130 Motor is a common small DC motor, which is mainly used in small devices such as toys, models and remote controls. 130 refers to its shell diameter of about 13mm. It features small size, low power consumption, flexible speed, cheap price as well as low noise.

3bottom

Components

Name

Picture

micro:bit board *1

microbitV2

micro USB cable *1

usb

Fan *1

fanblade

130 motor *1

electromotor

4pin DuPont wire *1

4pin

Wiring Diagram

z301

Test Code

Complete code:

z302

line1

Brief description:

The digital pins IN + and IN- of the 130 motor are two terminals for the power supply input. Their connection determines the rotation direction of the module.

Forward(IN+): When this signal is high level 1, the motor is turning forward.

Reversal(IN-): When this signal is high level 1, the motor reverses.

When both terminals are at low, the motor stops.

By changing direction of the positive and negative pole, the 130 motor can switch its rotation direction. This is the most basic method.

Connect pin IN- to P16, and pin IN+ to P13.

Forward rotating for 2 seconds:

z303

Stop for 2 seconds:

z304

Reversal rotating for 2 seconds:

z305

Test Result

4top

ATTENTION:

① Before uploading code, please do not put the motor flat on the table, resulting that the motor fails to rotate. It is recommended to pinch the terminal to put the fan up, as follows:

z306

② Before uploading code, please keep a distance from the motor, as its rotation speed is so fast that it may injury you.

③ Before uploading code, please mount the battery holder first. Otherwise, the experiment may fail.

Download the code (for Windows 10 App, click to download code; for browsers, download the “hex” file and sent it to the micro: bit board) and power on via micro USB cable. The 130 motor rotates clockwise and stops, and then rotates counter-clockwise and stops, with two seconds each. These actions repeats.

4bottom

Project 4: PIR Motion Sensor

KS6018

Introduction

3top

The human body temperature is generally constant at about 37°, and will emit infrared signals with a wavelength of about 10μm. Passive infrared probes just work by detecting the specific infrared waves emitted by human body.

These 10μm infrared waves are enhanced by the Fresnel filter and concentrated on the infrared sensing source to control the interference of the environment. The infrared sensing sources are usually pyroelectric elements, which can generate an alarm signal when the infrared radiation temperature of the human body changes. Otherwise, no signal will be output.

3bottom

Components

Name

Picture

micro:bit board *1

microbitV2

micro USB cable *1

usb

PIR motion sensor *1

KS6018

3 pin DuPont wire *1

3pin

Wiring Diagram

z401

Test Code

Complete code:

z402

line1

Brief description:

If the PIR motion sensor detects a human motion nearby, it will output a high level 1; when there is no motion, it outputs low 0.

Pin P15 reads the digital power level output by the PIR motion sensor.

z403

Print the read values on the serial monitor.

z404

Test Result

4top

Download the code (for Windows 10 App, click to download code; for browsers, download the “hex” file and sent it to the micro: bit board) and power on via micro USB cable. Click “Show console Device”.

z405

When the PIR motion sensor detects no any motion, the red LED will light up and the serial monitor prints power level 0. If there is a human motion detected by the sensor, the red LED will turn off and 1 will be printed on the serial monitor, as follows:

z406

If you’re running Windows 7 or 8 instead of Windows 10, Google Chrome won’t be able to match devices. CoolTerm will be required.

Open CoolTerm and click Options to select SerialPort, and set COM port and baud rate to 115200, click OK and Connect.

z407

The serial monitor of CoolTerm shows digital values of power levels.

z408

4bottom

Project 5: DHT11 Temperature and Humidity Sensor

KS6033

Introduction

3top

DHT11 Temperature and Humidity Sensor detects ambient temperature and humidity with the following features:

  1. Small size, suitable for a variety of small electronic projects.

  2. Wide measuring range, temperature range is 0-50°C, humidity range is 20-90%RH.

  3. Easy to use, connect only 3 pins to the single chip microcomputer or computer when using.

  4. Cheap price, a good choice for beginners in their electronic production.

3bottom

Components

Name

Picture

micro:bit board *1

microbitV2

micro USB cable *1

usb

DHT11 Temperature and Humidity Sensor *1

KS6033

3pin DuPont wire *1

3pin

Wiring Diagram

z501

Test Code

Complete code:

z502

line1

When using the temperature and humidity sensor, additional temperature and humidity sensor extension is required.

Add extension:

1. Click Extension.

z104

2. Enter DHT11 in the search box and search, and click DHT11_DHT22 to import the extension.

z503

3. Successfully loaded:

z504

Brief description:

Serial input and output via USB connections.

z505

Set the temperature and humidity sensor pin to P2.

z506

Read the temperature and humidity values and display them on the serial monitor.

z507

Refresh the results every 500ms.

z508

Test Result

4top

Download the code (for Windows 10 App, click to download code; for browsers, download the “hex” file and sent it to the micro: bit board) and power on via micro USB cable.

The sensor detects the ambient temperature and humidity values and reveals them on the serial monitor. The results will be refreshed every 500ms.

z509

4bottom

Project 6: Servo

67820089

Introduction

3top

Servo, in essence, is a kind of position diver. There are lots of specifications of servos. Yet almost boasts three wires. They may be in different colours due to brands. Herein, this servo wires are covered with brown(ground), red(positive) and orange(signal).

z601

3bottom

Components

Name

Picture

micro:bit board *1

microbitV2

micro USB cable *1

usb

Servo *1

67820089

Wiring Diagram

z602

Test Code

Complete code:

z603

line1

Code block:

z604

This block is used to set the rotation angle of the servo, which ranges form 0°~180°.

Brief description:

Set the servo pin to P1 and angle to 0°.

z605

Servo stops rotating for 1 second after reaching the angle of 0°.

z606

Test Result

4top

Download the code (for Windows 10 App, click to download code; for browsers, download the “hex” file and sent it to the micro: bit board) and power on via micro USB cable. The servo will repeat these actions: it rotates to 45°, 90°, 135°, 180°, each position with a pause of 1 second.

4bottom

Project 7: 1602 LCD Display

Introduction

3top

LCD display is a common display device that shows text, images, and video. It is commonly used in electronic products such as TVS, computers and mobile phones. It adopts liquid crystal materials (they change the way light passes in response to changes in voltage) to control the passage of light, so as to present different images and colours.

LCD displays are divided into colourful ones and black-white ones. The former ones are able to display rich colours, while the latter only shows black and white.

The communication mode of this module is I2C. But what is I2C?

I2C is a path that transmits information among electronic devices. For example, you have a toy that want to chat with another one, but they cannot communicate directly. At this point, I2C plays a role of passer allowing them chat with each other.

Imagine there are two persons named Master and Slave. Master orders Slave to do something. He said, “Show me the toy in your hand!” And Slave would give him his toy. Not just that, I2C is able to take more than one Slave. It is the main road with a(many) branch(es).

Wish you have fun with I2C!

z701

The “main road” of I2C is divided into two parts, which is called “pins”. One is clock line (SCL), while the other is data line (SDA).

Like clock, SCL will tick-tock to inform all toys the time. When it emits sound, one toy know it is his time to talk.

SDA, however, passes messages for them. If Master Toy asks “Did you have fun today?” The answer of Slave Toy will be sent back via data line.

With these two lines, their toys can play and chat together.

In this project, we connect the 1602 LCD display SDA interface to pin P20 and SCL to pin P19.

3bottom

Components

Name

Picture

micro:bit board *1

microbitV2

micro USB cable *1

usb

1602 LCD *1

KS0061

4 pin DuPont wire *1

4pin

Wiring Diagram

z702

Test Code

Complete code:

z703

line1

When using LCD display, additional LCD extension is required.

Add extension:

1. Click Extension.

z104

2. Enter the following link in the search box:

https://github.com/keyestudio2019/ks_IoT

and then click search and you will see the IoT_keyestudio. Click to import it.

z704

3. Successfully loaded:

z705

Brief description:

Set the address of the LCD. Set LCD display coordinate of (5,0) to show string “Hello!”, and turn on the backlight.

z706

Create a variable number.

z707

Change the value of the variable number with a magnitude of 1.

z708

Displays the value of the variable number at the LCD (6,1).

z709

Refresh the results every 500ms.

z710

Test Result

4top

Download the code (for Windows 10 App, click to download code; for browsers, download the “hex” file and sent it to the micro: bit board) and power on via micro USB cable. The LCD display shows “Hello!“ on the first line and it counts down on the second line. It refreshes every 500ms.

z711

ATTENTION:

If the display does not react after uploading the code and powering on, rotate the potentiometer on the back of the 1602 LCD display clockwise to adjust the brightness of the backlight.

z712

4bottom

Smart Home Assembly

Congratulations! You have learned and mastered the functions of the modules required for smart homes, and you can use them flexibly! This not only shows your learning ability, but also reflects your enthusiasm and pursuit of intelligent life.

I believe that after this learning and accumulation, you will be able to design more innovative and practical smart home solutions. Let‘s explore the infinite possibilities of smart home with an open mind, and jointly open a new chapter of smarter and more comfortable life!

z001

Next, let’s start the smart home installation together! Through this process, you can not only build the infrastructure of the smart home by hand, but also gain a deep understanding of the function of each module and the principle of collaboration. After the installation is ready, we can officially open the smart home projects.

Installation/Removal of Nylon Rivet

Nylon rivets can be used for fixing or as screws, and it can assemble without tools.

![Nylon Rivet](img/Nylon Rivet.png)

The male parts of rivets plays the role of riveting to insert into the female part for fixing.

If the nylon rivets need to be disassembled after installation, they can be easily removed with tweezers.

First, press the back end of the male part with a tweezers.

![Nylon Rivet](img/Nylon Rivet 1.png)

Or use the tail end of the tweezers to squeeze the male parts.

![Nylon Rivet](img/Nylon Rivet 2.png)

If the male part is loose, clamp it with the tweezers.

![Nylon Rivet](img/Nylon Rivet 3.png)

Finally, gently clip it out.

![Nylon Rivet](img/Nylon Rivet 4.png)

1. Install the expansion board and temperature and humidity sensor

99_00

99_01

99_02

99_03

99_04

Connect the 3pin wire to the modules.

wire colours

module pins

py

ds

pr

dv

pb

dg

99_05

2. Install the display and PIR motion sensor

99_09

99_10

99_11

Connect the 4pin wire to the pin end of the LCD.

99_12

99_13

99_14

Connect the wire to the module and pass the wire through the hole.

99_15

3. Mount the door servo and battery holder board

99_16

Note the position of the servo wire.

99_17

99_18

Attention please! Servo must be calibrated! Or else it may be stuck or burned!

Attention please! Servo must be calibrated! Or else it may be stuck or burned!

Attention please! Servo must be calibrated! Or else it may be stuck or burned!

Connect the servo to pin P1 on the expansion board, insert the micro:bit board and connect it to your computer via micro USB cable.

Download the code.99_85

99_86

For Windows 10 App, click “download”. For browsers, send the hex file to the micro:bit board.

After uploading the code, servo rotates to 180° to fulfil the calibration. Disconnect the micro:bit board to the computer and the servo to the expansion board, and mount the servo according to the following steps.

Install the cross arm gently onto the servo as shown below.

Note that when installing, please keep the cross arm horizontal to the servo. Mount the cross arm gently on the servo and do not rotate the servo.

ss

Gently remove the cross arm after it aligns with the servo after it aligns with the servo, and install the cross arm on the door according to this orientation, as shown in the picture below:

99_19_0

99_19

99_20

Install the door and servo, take out the screws in the servo kit and tighten the them. But please do not screw too tight and do not rotate the servo.

99_21

99_22

Take out the materials as shown below.

99_23

99_24

99_25

99_26

99_27

99_28

99_29

99_30

4. Assemble the smart home

99_31

99_32

99_33

99_34

99_35

99_36

99_37

99_38

99_39

Wires of servo and DHT11 temperature and humidity sensor pass through the hole ① to the outside of the home, and those of PIR motion sensor and 1602 LCD display pass through the hole ②.

99_40

5. Mount the trash can

99_45

99_46

99_47

99_48

99_49

6. Mount the trash can

99_50

Connect the 4-PIN wire to the ultrasonic module as shown below.

99_06

99_51

99_52

99_53

99_54

Put the wires as shown below:

99_91

Attention please! Servo must be calibrated! Or else it may be stuck or burned!

Attention please! Servo must be calibrated! Or else it may be stuck or burned!

Attention please! Servo must be calibrated! Or else it may be stuck or burned!

Connect the servo to pin P9 on the expansion board, insert the micro:bit board and connect it to your computer via micro USB cable.

Download the code.99_85

99_87

For Windows 10 App, click “download”. For browsers, send the hex file to the micro:bit board.

After uploading the code, servo rotates to 0° to fulfil the calibration. Disconnect the micro:bit board to the computer and the servo to the expansion board, and mount the servo according to the following steps.

Install the cross arm gently onto the servo as shown below.

Note that when installing, please keep the cross arm horizontal to the servo. Mount the cross arm gently on the servo and do not rotate the servo.

99_108

7. Mount the trash can lid

99_55

Gently remove the cross arm after it aligns with the servo, and install the cross arm on the trash can lid according to this orientation, as shown in the picture below:

99_56

99_56

99_57

Install the door and servo, take out the screws in the servo kit and tighten the them. But please do not screw too tight and do not rotate the servo.

99_58

99_59

Complete as shown below.

99_60

8. Mount the fan

99_61

99_62

99_63

Connect the 4-PIN wire to the fan.

99_64

99_93

99_94

99_95

Put its 4-PIN wire through the hole ② to the outside of the smart home.

99_92

9. Wire up some modules

99_96

99_97

99_98

99_99

10. Install RGB module

99_41

99_42

99_43

Connect the 3pin wire to the RGB module, and pass the other end of the wire through the hole ② to the outside of the smart home.

99_44

99_104

99_105

99_106

99_107

11. Wire up the rest of modules

99_100

99_101

99_102

99_103

12. Mount the battery holder

Six AA batteries are required.

99_80

When putting the batteries, please pay attention to the + and - .

99_8999_90

After that, insert the battery holder into the house. Note the position of the wire: it is at the right side.

99_81

99_82

99_83

99_84

Smart Home Projects

Everything is ready, let’s go to the journey of smart home, and experience the perfect integration of technology and life!

Project 1: Sound Control Light

Introduction

3top

Herein, we adopt the wisdom of sound to control things. Every time you clap, the 6812 RGB module in the room will bloom into different colours, which is colourful and charming.

3bottom

Test Code

Complete code:

0101

line1

Brief description:

Click extension and search the link in the box: https://github.com/keyestudio2019/ks_IoT

Click IoT_keyestudio to import it.

z704

Initialize 6812 RGB module and create three variables R, G and B.

0102

Set variables R, G, B to random values among 10~255.

0103

Show the sound volume values on the serial monitor and refresh them every 100ms.

0104

If the sound value is greater than 140 (140 is a threshold which can be modified according to needs), the RGB module shows random colors.

0105

Or else, the RGB module goes off.

0106

Test Result

4top

Download the code (for Windows 10 App, click to download code; for browsers, download the “hex” file and sent it to the micro: bit board) and power on via micro USB cable. The micro:bit board detects the sound volume and prints the values on the serial monitor every 100ms. Clap your hands and the sound value may exceed 140. At this time, the RGB module shows random colours.

4bottom

Project 2: Password Door

Introduction

3top

The Smart Password Door is controlled by button A and B of the micro:bit to enter passwords, which will be displayed on the LCD. If they are correct, the door will open, the RGB module will light up, and the LCD will display “Welcome”.

3bottom

Test Code

Complete code:

0201

line1

Brief description:

Click extension and search the link in the box: https://github.com/keyestudio2019/ks_IoT

Click IoT_keyestudio to import it.

z704

Initialize the servo, LCD display, LED matrix, and set the door passwords to AABB.

0202

Press micro:bit button A, and the “A” shows on the middle of the second line on the LCD.

0203

Press micro:bit button B, and the “B” shows on the middle of the second line on the LCD.

0204

Press button A and B to verify the passwords. If they are correct, LED matrix shows icon 0207, and LCD says “Welcome!“. Meanwhile, the servo rotates to open the door.

0205

If you enter wrong passwords, LCD prompts “Error!“ and shows “Enter again” on its second line. A second later, clear the display and say “Enter password:”. Now re-enter the passwords.

0206

Touch the micro:bit logo, the LCD will clear its display and show “Close the door”. Then, the servo rotates to close the door. After that, LCD will be back to prompt “Enter password:”.

0208

Test Result

4top

Download the code (for Windows 10 App, click to download code; for browsers, download the “hex” file and sent it to the micro: bit board) and power on via micro USB cable.

We press buttons A and B to enter passwords which shows on the LCD. After entering, press the two buttons at the same time to verify the passwords. The door will open if the passwords are correct. Touch the logo to close the door.

4bottom

Project 3: Smart Anti-theft Alarm

Introduction

3top

When the PIR motion sensor detects a human motion, the LCD shows “Someone!“ and the micro:bit speaker alarms, and the RGB in the room flashes in red.

3bottom

Test Code

Complete code:

0301

line1

Brief description:

Click extension and search the link in the box: https://github.com/keyestudio2019/ks_IoT

Click IoT_keyestudio to import it.

z704

Initialize the LCD display and the RGB module.

0302

Clear the LCD first.

0303

Determine whether the P15 outputs 1. If yes, the speaker emits sounds and the LCD prompts “Someone!”, and RGB flashes in red.

0304

If no human motion is detected, the speaker remains quiet and LCD shows “All goes well.” with RGB off.

0305

Test Result

4top

Download the code (for Windows 10 App, click to download code; for browsers, download the “hex” file and sent it to the micro: bit board) and power on via micro USB cable.

If the sensor detects a human motion, the speaker alarms and the LCD prompts “Someone!”, and RGB flashes in red. If no human motion is detected, the speaker remains quiet and LCD shows “All goes well.” with RGB off.

4bottom

Project 4: Environment Monitoring

Introduction

3top

This smart environment monitoring system displays the temperature and humidity values detected by the DHT11 sensor and the brightness values detected by the light sensor on the micro:bit mainboard on the LCD. When the brightness value is lower than the set value, the LED matrix will display a smile icon; If it exceeds the set value, the LED matrix will display a crying face icon.

3bottom

Test Code

Complete code:

0401

line1

Brief description:

Click extension and search the link in the box: https://github.com/keyestudio2019/ks_IoT

Click IoT_keyestudio to import it.

z704

Initialize the LCD display.

0402

Connect the temperature and humidity sensor to the pin P0.

0403

Clear the display and show the temperature, humidity and ambient light values on the LCD, and refresh these values per second.

0404

When the brightness value is lower than the set value, the LED matrix will display a smile icon; If it exceeds the set value, the LED matrix will display a crying face icon.

0405

Test Result

4top

Download the code (for Windows 10 App, click to download code; for browsers, download the “hex” file and sent it to the micro: bit board) and power on via micro USB cable. The LCD updates the temperature, humidity and light values every second. When the brightness value is lower than the set value, the LED matrix will display a smile icon; If it exceeds the set value, the LED matrix will display a crying face icon.

4bottom

Project 5: Temperature Sensing Fan

Introduction

3top

When the indoor temperature exceeds the set value, the fan will automatically work to cool down the temperature within a comfortable range. The LCD displays the ambient temperature and fan operating status in real time, providing users with intuitive temperature control feedback.

3bottom

Test Code

Complete code:

0501

line1

Brief description:

Click extension and search the link in the box: https://github.com/keyestudio2019/ks_IoT

Click IoT_keyestudio to import it.

z704

Initialize the LCD display.

0402

Clear the LCD and show temperature values on the LCD.

0502

When the detected temperature reaches 28° (28 can be modified according to your actual needs), the fan turns on and the LCD shows “Fan on”.

0503

Otherwise, the fan stops and the LCD shows “Fan off”.

0504

Test Result

4top

Download the code (for Windows 10 App, click to download code; for browsers, download the “hex” file and sent it to the micro: bit board) and power on via micro USB cable. When the indoor temperature exceeds 28°, the fan will automatically work. Or else, the fan stops working. The LCD displays fan operating status in real time.

4bottom

Project 6: Earthquake Warning

Introduction

3top

When the acceleration sensor of the micro:bit board detects that the house is tilting, the on-board speaker will emit sounds, the LCD will display the value detected by the sensor, and the red LED will turn on.

3bottom

Test Code

Complete code:

0601

line1

Brief description:

Click extension and search the link in the box: https://github.com/keyestudio2019/ks_IoT

Click IoT_keyestudio to import it.

z704

Initialize the LCD display and RGB.

0602

Read the decomposition value of the accelerometer on the Y-axis. If the value is outside the range of 1060~1088, it indicates that the house tilts, so the earthquake is triggered: print the value on the LCD, the speaker alarms, and the RGB lights up in red.

0603

If the decomposition value in the Y-axis is within the range of 1060~1088, it indicates that the house is stable so the earthquake alarm is canceled.

0604

Refresh the results every 200ms.

0605

Test Result

4top

Download the code (for Windows 10 App, click to download code; for browsers, download the “hex” file and sent it to the micro: bit board) and power on via micro USB cable. If the sensor detects that the house tilts, print the Y-axis value on the LCD, the speaker alarms, and the RGB lights up in red.

4bottom

Project 7: Distance Detection

Introduction

3top

Based on an ultrasonic module, this distance detection system detects the distance of obstacles ahead and shows the measurement results in real time on an LCD display.

3bottom

Test Code

Complete code:

0701

line1

Brief description:

Click extension and search the link in the box: https://github.com/keyestudio2019/ks_IoT

Click IoT_keyestudio to import it.

z704

Enter sonar in the search box and search, and you will see the sonar extension.

z205

Initialize the LCD display.

0702

Initialize the ultrasonic sensor.

0703

Clear the LCD and show the distance value of obstacles on the LCD. Refresh the results every 100ms.

0704

Test Result

4top

Download the code (for Windows 10 App, click to download code; for browsers, download the “hex” file and sent it to the micro: bit board) and power on via micro USB cable. The ultrasonic module detects the distance of obstacles ahead and shows the result on the LCD display. The detected results are refreshed every 100ms.

4bottom

Project 8: Smart Trash Can

Introduction

3top

When the user’s hand is near the sensing area of the trash can, the smart trash can will automatically open the lid. Then the lid closes automatically when the hand leaves the sensing range.

3bottom

Test Code

Complete code:

0801

line1

Brief description:

Click extension, enter sonar in the search box and search, and you will see the sonar extension.

z205

Initialize the servo to ensure that the initial state of the lid is in a close state.

0802

Connect the servo to the pin and detect the obstacle distance value. Assign the value to the variable distance.

0803

If the distance value measured by the ultrasonic module is within the range of 2~10cm, the servo rotates to open the lid for 2 seconds to ensure that there is enough time to throw the garbage (the lasting time can be changed according to your needs).

0804

Otherwise, the lid is closed.

0805

The ultrasonic module reads the values every 100ms.

0806

Test Result

4top

Download the code (for Windows 10 App, click to download code; for browsers, download the “hex” file and sent it to the micro: bit board) and power on via micro USB cable. Put you hand in front of the ultrasonic module within the range of 2~10cm, and the servo will rotate to open the lid for 2 seconds. Remove you hand and the servo rotates to close the lid.

4bottom

Project 9: Bluetooth APP Control LED

Introduction

3top

We sent control command on mobile phone/tablet through Bluetooth to control the ON/OFF state on the LED. You can use a Bluetooth control APP or develop your own APP to send control commands.

3bottom

Bluetooth APP

Install the Bluetooth APP we provide.

Android: Directly use the provided installation package APK (in the Android APK folder).

0901

ios: Search for IoT microbit in the App Store. Click to download.

0902

Test Code

Complete code:

0903

line1

Brief description:

Click extension and search the link in the box: https://github.com/keyestudio2019/ks_IoT

Click IoT_keyestudio to import it.

z704

Enter bluetooth in the search box and search, and you will see the bluetooth extension.

0904

0905

Initialize RGB and LED. Serial input and output via USB connections.

0906

Disconnect the Bluetooth.

0907

When the Bluetooth is connected, the icon will be displayed on the LED matrix.

0908

Read the Bluetooth.

0909

If “a” is read, it means that you press the LED command button, so the LED lights up.

0910

If “b” is read, it means that you press the LED command button again, so the LED goes off.

0911

Note: After writing the code, you need to modify the Project Settings to no pairing required and then save, as shown below:

0912

0913

Click the searched Bluetooth name of the micro:bit board.

Test Result

4top

Download the code (for Windows 10 App, click to download code; for browsers, download the “hex” file and sent it to the micro: bit board) and power on via micro USB cable. Open the Bluetooth APP and click CONNECT.

0914

Click the searched Bluetooth name of the micro:bit board.

0915

The Bluetooth is connected.

0916

Click the button shown in the picture below and the LED lights up. Click again to turn off the LED.

0917

4bottom

Project 10: Bluetooth Smart Home

Introduction

3top

Let’s learn how to use Bluetooth APP to control smart home in this project.

3bottom

Bluetooth APP

Install the Bluetooth APP we provide. Please refer to the previous project to install.

The characters sent by the Bluetooth APP button are as follows:

1001

Test Code

Complete code:

line1

Brief description:

Click extension and search the link in the box: https://github.com/keyestudio2019/ks_IoT

Click IoT_keyestudio to import it.

z704

Enter bluetooth in the search box and search, and you will see the bluetooth extension.

0904

0905

Enter sonar in the search box and search, and you will see the sonar extension.

z205

Initialization.

1002

Bluetooth connection is successful, and the LED matrix shows “√”.

1003

Bluetooth is disconnected, and the LED dot matrix display “×” pattern.

1004

Press 1006 once and the code blocks will execute. The RGB turns on.

1007

Press 1006 again and the code blocks will execute. The RGB goes off.

1008

Press 1009 once, and the ultrasonic module in the home activates the ranging function and displays the measured values on the LCD display.

Press it again to turn off this function. Clear the display.

1010

Press 1012 to enable the smart trash can. Note that the ultrasonic sensor in the home should be removed in advance and then installed on the trash can. Put you hand in front of the sensor within the range of 2~10cm, and the servo rotates to open the lid for 2 seconds. Remove your hand and the lid closes.

Press it again to turn off this function. The lid closes.

1011

Press 1014 to enable the temperature control fan to monitor the ambient temperature value in real time. When the temperature exceeds 28°C, the fan is on and the LCD shows “Fan on”. Otherwise, the fan turns off and the LCD shows “Fan off”.

Press it again to turn off this function. The fan stops.

1013

Press 1016 to monitor the ambient environment. The temperature, humidity and light value will be displayed on the LCD. When the light value is lower than 20, the LED matrix shows a smile icon. Or else, the LED shows a crying face.

1015

Press 1018 to activate the anti-theft alarm. If a movement is detected, the speaker alarms and the LCD shows “Someone!” with RGB flashing in red. If not, there is no sounds and LCD displays “All goes well.” with RGB off.

1017

Press 1020 to activate the earthquake warning system. If the tilt of the house is detected, the earthquake alarm is triggered, the value on the Y axis is displayed on the LCD and an alarm sound is emitted with the RGB lighting in red.

1019

Press the following icons to show corresponding icons on the LED matrix. Press again to clear the LED matrix.

1021

1022

1023

Press ①. Each time you tap, the value of neo_count is increased by one. The maximum value is 9 so it does not increase after reaching 9.

1024

Press ②. Each time you tap, the value of neo_count is decreased by one. The minimum value is 1 so it does not decrease after reducing to 1.

1025

Press ③ to zero out the value of neo_count.

1026

Depending on the neo_count value, the RGB module displays different colours.

1027

Press the micro:bit buttons on the mainboard to enter the passwords. Press A displays A and press B displays B on the LCD.

1028

Press A+B to confirm the passwords. If the password is correct, LCD shows “Welcome!” and “Open the door”, and the servo rotates to open the door.

If the password is wrong, LCD shows “Error” and “Enter again” for 1 seconds. And then it displays “Enter password:”.

1029

Touch the logo and LCD shows “Close the door”, and the servo rotates to close the door. A second later, LCD shows “Enter password:“.

1030

Test Result

4top

Download the code (for Windows 10 App, click to download code; for browsers, download the “hex” file and sent it to the micro: bit board) and power on via micro USB cable. Open the Bluetooth APP and tap Connect to connect the Bluetooth. The functions of each icon are shown below:

1031

4bottom