Page 1
Using Single-board Computer in Teaching Programming at Junior
Secondary Level
By Chu K F
26 June 2018
1
Page 2
Some Questions to Consider
• Hardware and Software
– Balance between Circuit Building and Program Coding
• Choice of microcontroller
– Arduino or BBC micro:bit
• Development tool:
– Arduino IDE / Python Editor or Visual Programming
• Duration of each lesson (single / double period)
• Task-based or Project-based
• etc.
2
Page 6
Learning Components
• Basic knowledge
• Guided tasks
• Self-exploration project
– Students can apply the skills they learnt in the guided tasks to complete simple project
6
Page 7
Sample Projects
1. A light show with LEDs and Buzzer
2. A simple interactive game using push-buttons, LEDs and buzzer.
3. A light-control music box
4. A smart fan
7
Page 8
Project 1
A Light Show with LEDs and Buzzer
8
Page 9
Project Requirements • Designing a light show by using a single-board
microcontroller, LEDs and buzzer.
9
Page 10
Knowledge and Skills
• Construct simple circuits by using single-board microcontroller, breadboard, LEDs, buzzers, resistors, etc.
• Create simple programs by using visual programming IDE – definite loop
– indefinite loop
– infinite loop
– selection statements
– etc.
10
Page 11
Basic Knowledge
• What is Arduino?
11
Digital I/O Pins D0 to D13
USB Port
External Power Supply
Analog Input Pins A0 to A5
Ground Pin
5V Power Pin
Page 12
Basic Knowledge • Breadboard (different types)
12
Page 13
Basic Knowledge • Breadboard (different types)
13
Page 14
Basic Skills
• Construction of Circuit (Circuit Diagram)
14
To USB port
of PC
Page 15
Basic Skills
• Construction of Circuit (Photos)
15
Page 16
Knowledge and Skills
• Program Coding
16
Script Area
Page 17
Knowledge and Skills
• Using mBlock 3 (Basic Settings)
17
Page 18
Knowledge and Skills
• Scratch mode
18
Page 19
Knowledge and Skills
• Arduino mode
19
Page 20
Guided Tasks
• Blinking 1 LED
20
To USB port
of PC
Page 21
Guided Tasks
• Controlling three LEDs
21
To USB port
of PC
Page 22
Guided Tasks
• Controlling three LEDs
– Other choices
22
Page 23
Guided Tasks
• Traffic Light Simulation
23
Page 26
Guided Tasks • Controlling a series of LEDs
26
Page 27
Guided Tasks
• Controlling a series of LEDs
– Other choices
27
Page 28
Guided Tasks
• Turning ON the LEDs One by One
• Programming Skills
– Using definite loops to simplify some repeated patterns.
– Using user-defined blocks (subprograms) to make the program more modular.
– Using variable to adjust the waiting time.
28
Page 30
Guided Tasks
• Be careful when using Arduino mode
– Inspect the program code if necessary
– Are the output pins set properly at the start?
30
Page 32
Guided Tasks
• Blinking the LEDs Continuously
• Blinking the LEDs Randomly
• Turning ON and OFF the LEDs in Different Directions
• Blinking the Red, Green and Yellow LEDs Randomly
• Slowing Down the Blinking Rate of LEDs
32
Page 36
Guided Tasks
• For micro:bit, using on-board LED Screen
36
Page 37
Guided Tasks • Blinking the LEDs on the Middle Row Continuously
37
Page 38
Guided Tasks • micro:bit - Controlling a Series of External LEDs
38
Page 40
Guided Tasks • Composing and Playing Music
40
Page 42
Guided Tasks • Blinking LEDs and Playing Music Concurrently
42
Page 43
Guided Tasks • Blinking LEDs and Playing Music Concurrently
– Scratch mode vs Arduino mode
43
Page 46
Guided Tasks • Blinking LEDs and Playing Music Concurrently
• Reference:
– https://learn.adafruit.com/multi-tasking-the-arduino-part-1/overview
46
Page 47
Guided Tasks • Blinking LEDs and Playing Music Concurrently
– No problem with micro:bit
47
Page 49
Self-Exploration Project
• Students are required to develop a Light Show by using Arduino/micro:bit and LEDs.
– with no more than 10 LEDs
– design a program that display at least four lighting patterns
– If possible, add music by using a buzzer
49
Page 50
Self-Exploration Project
• System Development Life Cycle
1. Problem definition
2. Problem analysis
3. Algorithm design and Program coding
4. Program debugging/testing
5. Program documentation
50
Page 51
Project 2
A Simple Interactive Game
51
Page 52
Project Requirements
• Designing a simple interactive game using push-buttons, LEDs and buzzer.
52
Page 53
Knowledge and Skills
• Construct simple circuits by using single-board microcontroller, breadboard, push-buttons, LEDs, buzzers, resistors, etc.
• Create simple programs by using visual programming IDE – definite loop
– indefinite loop
– infinite loop
– selection statements
– etc.
53
Page 54
Basic Knowledge
• Momentary Switch
• Maintained Switch (Toggle Switch)
• Push-button
54
Page 55
Basic Knowledge
• Push-button module
• No need to connect the 10 kΩ pull-down resistor
55
Page 56
Basic Knowledge
• Other choice
– Touch Sensor
56
Page 57
Guided Tasks • 1 Push-button & 1 LED
57
Page 59
Guided Tasks
• Using a Push-button as a Momentary Switch
59
Page 60
Guided Tasks • Using a Push-button as Toggle Switch
60
Page 61
Guided Tasks • 2 Push-buttons & 1 LED
61
Page 62
Guided Tasks • Using Sensor Shield with GVS pins to simplify
connection
62
Page 64
Guided Tasks
• Using two Push-buttons as On and Off Switches
64
Page 65
Guided Tasks
• Controlling several LEDs by a Push-button
65
Page 67
Guided Tasks • Referring to the previous task, when the push
button is pressed, the LEDs will blink. When the button is released, the LEDs will stop blinking and all the LEDs will be turned off.
• Question:
Modify the program so that when the button is released, the LEDs will stop blinking and only one LED will remain on.
67
Page 70
Guided Tasks
• Designing a Fast Answer Game System (搶答遊戲)
70
Page 71
Self-Exploration Project
71
Page 72
Guided Tasks
• Step-by-Step
1. Turn an LED on when pressing the corresponding push-button.
2. Ensure that only one LED is turned on at a time.
3. Produce suitable sound effect when pressing each button.
4. Make the program more modular
72
Page 79
Guided Tasks
79
• For micro:bit
Page 81
Self-Exploration Project
• Designing a simple interactive game using push-buttons, LEDs and buzzer.
81
Page 82
Self-Exploration Project
82
Page 83
Self-Exploration Project
83
Page 84
Self-Exploration Project
84
Page 87
micro:bit version
87
Page 88
Project 3
A Light-Control Music Box
88
Page 89
Project Requirements
• Design a light-control music box.
89
Page 90
Knowledge and Skills
• Construct simple circuits with single-board microcontroller, potentiometer, RGB LED, light sensor (LDR) and DC motor, etc.
• Know how to handle analog input in microcontroller
• Know how to handle analog output (PWM) in microcontroller
• Create simple light sensor control systems
90
Page 91
Basic Knowledge
• Analog Input
• Analog (PWM) Output
91
Page 92
Basic Knowledge
• Analog Input in Arduino
92
Analog Input pins A0, A1, A2, A3, A4, A5
Digital Input / Output pins D0, D1, D2, D3, D4, D5, D6, D7, D8, D9, D10, D11, D12, D13
Page 93
Basic Knowledge
• Analog (PWM) Output in Arduino
93
PWM Output pins D3, D5, D6, D9, D10, D11
Page 94
Basic Knowledge
• Analog Input and Output in micro:bit
94
Page 95
Guided Tasks • Controlling the Colour of an RGB LED
95
Common cathode
Page 96
Guided Tasks • Controlling the Colour of an RGB LED
96
Page 97
Guided Tasks • Controlling the Colour of an RGB LED
97
Page 98
Guided Tasks • Automatic LED Control by Light Sensor (LDR)
98
Page 99
Guided Tasks • Automatic LED Control by Light Sensor (LDR)
99
Page 100
Guided Tasks • Control of Music Playing by Light Sensor (LDR)
100
Page 101
Guided Tasks • Control of Music Playing by Light Sensor (LDR)
101
Page 104
Guided Tasks • Controlling DC Motor
104
Page 105
Guided Tasks • Controlling DC Motor
105
Page 106
Guided Tasks • Controlling Direction of Rotation
106
Page 107
Guided Tasks • Controlling Speed of Rotation
107
Page 108
Self-Exploration Project
• Design a light-control music box.
108
Page 109
Self-Exploration Project
• Design a light-control music box.
109
Page 110
Self-Exploration Project
• Design a light-control music box.
110
Page 111
Self-Exploration Project
• Design a light-control music box.
111
Page 112
Self-Exploration Project
• Design a light-control music box.
112
Page 115
Project 4
A Smart Fan
115
Page 116
Project Requirements
• Design a smart fan
116
Page 117
Guided Tasks
• Measuring Temperature by LM35
117
Page 118
Guided Tasks
• Measuring Temperature by LM35
118
A0 value 5000 125 A0 value
1024 10 256T
Page 119
Guided Tasks
• Measuring Temperature and Humidity by DHT11
119
Page 120
Guided Tasks
• Measuring Temperature and Humidity by DHT11
120
Page 121
Guided Tasks
• Measuring Temperature and Humidity by DHT11
121
Page 122
Guided Tasks
• Controlling a Fan by using Temperature Sensor
122
Page 123
Guided Tasks
• Controlling a Fan by using Temperature Sensor
– Other choices
123
Page 124
Guided Tasks
• Controlling the On / Off of a Fan
124
Page 125
Guided Tasks
• Controlling the Speed of a Fan
125
Temperature (C) Speed (%) PWM (D6)
< 25 0 0
25 - 26 40% 255 × 0.4 = 102
27 - 28 60% 255 × 0.6 = 153
29 - 30 80% 255 × 0.8 = 204
> 30 100% 255
Page 127
Guided Tasks
• Controlling a Fan by using Temperature Sensor
127
Page 128
Guided Tasks
• Controlling a Fan by using Temperature Sensor
128
Page 129
Guided Tasks
• Measuring Distance by using Ultrasonic Sensor
129
Page 130
Guided Tasks
• Measuring Distance by using Ultrasonic Sensor
130
Page 131
Guided Tasks
• Measuring Distance by using Ultrasonic Sensor
131
Page 132
Guided Tasks
• Measuring Distance by using Ultrasonic Sensor
132
Page 133
Guided Tasks
• Controlling Servo Motor
133
Page 134
Guided Tasks
• Controlling Servo Motor
134
to PWM pin
to 5V pin
to GND pin
Page 135
Guided Tasks
• Servo Motor with Ultrasonic Sensor
135
Page 136
Guided Tasks
• Detecting the Nearest Obstacle to the Sensor
136
Page 137
Guided Tasks
• Detecting the Nearest Obstacle to the Sensor
137
Page 139
Self-Exploration Project
• Design a smart fan
139
Page 140
Self-Exploration Project
• Design a smart fan
140
Page 141
Self-Exploration Project
• Design a smart fan
141
Page 142
Self-Exploration Project
• Design a smart fan
142
Page 143
Self-Exploration Project
• Design a smart fan
143
Page 144
Self-Exploration Project
• Design a smart fan
144
Page 145
Self-Exploration Project
• Further example
– https://www.youtube.com/watch?time_continue=79&v=nhLk_kOy6w8
145
Page 146
Reference
• Arduino輕鬆入門 : 範例分析與實作設計 / 葉難,博碩文化股份有限公司
• Beginning Arduino by Michael McRoberts
• 用mBlock玩Arduino - Starting from Scratch:林信良
• etc.
146
Page 147
~ END ~
Thank You!
147