0% found this document useful (0 votes)
7 views

Topic 3 - External Input Devices Python

Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

Topic 3 - External Input Devices Python

Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 30

Topic 3: EXTERNAL INPUT

DEVICES – PIR & LIGHT SENSOR


Topic 3 Objective
● Introduction to external inputs
● Selection Statements (If…Else)
● Conditional Loops
● Activities & Reflection
Activity: Sensors
See

Pay attention to some of the sensors being used in


this Robotic Dog.

Think

What sensors were being used?

What was the output for every action?

Write your comments after watching the video.

Act

How can we use this for the next activity later on?
Activity: Sensors
Sensor Video Reflection Answers

What sensors were being used?


●Camera Sensor
●Touch Sensor
●Gesture Sensor
●Light Sensor
Sensor Video Reflection Answers

What was the output for every action?


●Camera sensor: Interact with other objects
●Touch sensor: Pulls the "bone" touch sensor away
(reacts)
●Gesture sensor: "Reacts" to any hand gestures,
e.g. turns face away when hand comes in front.
●Light sensor: Goes to "sleep" when it it dark.
Passive Infra-Red (PIR) Sensor

A PIR sensor is an electronic sensor


that measures infrared light (IR)
radiating from objects in its field of
view.
They are most often used in motion
detectors.
PIR sensors are commonly used in
security alarms and automatic lighting
applications.
Selection Statements (If . . . else)

If…else statement allows the program to


run a different set of statements depending Start

on whether the condition is true or false.

An example would be NAPFA test. _____ you No


Is it Leave umbrella at
finish running 1.6 km(or 2.4 km), _____ you raining? home

can stop, _____ you have to continue


running. Another example would be _____ it Yes

is raining, _____ you do not need to water


Bring umbrella End
the plants, _____ water the plants.
Selection Statements (If . . . else)

If…else statement allows the program to


run a different set of statements depending Start

on whether the condition is true or false.

An example would be NAPFA test. If you No


Is it Leave umbrella at
finish running 1.6 km(or 2.4 km), then you raining? home

can stop, else you have to continue


running. Another example would be if it is Yes

raining, then you do not need to water the


Bring umbrella End
plants, else water the plants.
Conditional Loops
Start

A conditional loop is a
set of programming Is plate Yes
instructions that is empty?
End

performed at only 1
No
point of the program
repeatedly until a Use spoon to scoop
up rice on plate
condition is satisfied.
Put spoon in
mouth
Download and Save Library File:

Go to:
bit.ly/ZENCFF2023_MB_TEXT_SUPP​
Download sw_gpio.py
Download and Save Library File:
Go to: python.microbit.org
Click on Project > Open > Choose
your sw_gpio file.py > Change to
add file
Download and Save Library File:

You should see the sw_gpio.py


library file.

We can now use the codes in


this library for our activities!
Activity 3.1:

Write a program to play a melody and turn ON light (micro:bit


onboard LED) for 5 seconds when PIR sensor detects a motion
and turn OFF light when there is no motion detected.
Activity 3.1:

Write a program to play a melody and turn ON light (micro:bit onboard LED) for
5 seconds when PIR sensor detects a motion and turn OFF light when there is no
motion detected.
Activity 3.1:

STEP ②
Write the program.

Import library files and set the


sensor = PIR(pin1)

We will read and store the


values of the PIR sensor in this
variable.
Activity 3.1:
STEP ②
Write the program. Following this pseudocode:
In our while True loop:
if the sensor detects motion:
show a sad icon
play a melody after showing the icon
else:
display a happy face
sleep 1 second (1000ms)
Activity 3.1:
STEP ②
Write the program
Activity 3.1:
STEP ③
Run the Program

Challenge:
How can we make this
into an intruder alarm?
Activity 3.1:
Challenge:
How can we make this into an intruder alarm?
Modify the codes to have text and icons, with a different sound!
Light Sensor
A light sensor can detect
the light value of the
current environment.
Light sensors measure
illuminance.

Where do you think light Illuminance 💡


sensors are useful? The amount of light illuminating
a given surface area.
Activity 3.2:

Write a program to turn ON light (micro:bit onboard LEDs) for 5


seconds when light sensor detects low intensity of light (dim or
darkness) and turn OFF light when the room is bright enough.
Let’s decompose the problem

Activity 3.2:
Start
Write a program to turn ON light
(micro:bit onboard LEDs) for 5
seconds when light sensor detects low Is light sensor No
intensity of light (dim or darkness) value less
than _____ ?
and turn OFF light when the room is
bright enough. Yes

End

Is the room dark or


bright in this case?
Activity 3.2
Wire Light Board
Sensor P1
STEP ① Yellow S S (Yellow)
Red V V (Red)
Insert the micro:bit into the Black G G (Black)

expansion board (IOT:bit) and


connect the Light Sensor to pin 1
on the board, as shown in the
picture below.
Activity 3.2

STEP ②

Write the program.


An analog read pin reads a range of
value from 0 to 1023. Let us first
determine the intensity of light
detected in the current
environment and we would use a
variable to hold this value.
Activity 3.2 Micro:bit
Reading
STEP ③
Covered light sensor
Download the program to find
Light sensor on table
the intensity of light detected
Light sensor near a light
in the current environment.
source
Try covering the light sensor, What is your light threshold value?
leaving it on the table or lifting it Value: _________________
near a light source.
(When the surrounding light intensity is
below the threshold, you will trigger the LED
to turn ON)
Activity 3.2
STEP ③

Modify the codes:

If the light level is below the


threshold value, show a sad
icon and play baddy music.

Else show a happy face.


OLED display​

OLED (Organic light-emitting


Diode) is a display that is made of
128x64 individual white OLED
pixels. It is a useful output to
display more information for
detailed user feedback. ​
Activity 3.3
Program OLED to display ​light level readings. Add your OLED Display and
the related codes​:
End of Topic 3

You might also like