5. Case 04: Farmlands Protection Device
5.1. Introduction
In order to protect the farmlands from people to get in, we can set a farmland protection device.
5.2. Function
Detect if there is any people approaching with the PIR sensor and Sonar:bit, if yes, program to remind people of not coming by lighting on the rainbow LED.
5.3. Products Link
5.4. Picture
5.5. Hardware Connection
Connect the Sonar:bit to P1, the PIR sensor to P2 and the rainbow LED to P9 on IoT:bit.
5.6. Software Programming
Click “Advanced” in the MakeCode to see more choices.
For programming, we need to add a package: click “Extensions” at the bottom of the MakeCode drawer and search with “iot-environment-kit” in the dialogue box to download it.
Notice: If you met a tip indicating that some codebases would be deleted due to incompatibility, you may continue as the tips say or create a new project in the menu.
5.7. Program
Drag the on start block from the drawer, initialize the strip connecting to P9 with 1 led.
Set to connect the sonar:bit to P1 with unit “cm” and the PIR sensor to P2, save the returned values as the variable “ultrasonic_distance” and the variable “IR” accordingly.
Judge if the variable “ultrasonic_distance” is in the threshold, if yes, judge if the variable “IR” is 1, if yes, set the strip to show red and the buzzer to alarm; or turn the lights off.
If the variable “ultrasonic_distance” is not in the threshold, program to turn off it light.
5.8. Result
If the device detects any people coming to the farmlands, the red light turns on and the buzzer alarms.