Anda di halaman 1dari 75

Microcontroller 4 Teachers

Latihan pengaturcaraan mikropengawal berdasarkan kurikulum RBT


Tingkatan 2

1
House Rules 2
1
BE ON TIME

2
ASK QUESTIONS &
PARTICIPATE
6
BE RESPECTFUL

4
REDUCE USAGE OF PHONES

5
CLASS-YES
Objektif Pembelajaran 3
Objektif Pembelajaran 4
Kriteria Kejayaan 5

• Novice
• Complete 1 or 2 challenges
• Amateur
• Complete almost all challenges
• Master
• Complete all challenges
Pembahagian Masa 6

• Sesi 1: (3 jam)
• Apakah itu mikropengawal
• Litar Output / Asas Pengaturcaraan
• Sesi 2: (1.5 jam)
• Litar Input
• Sesi 3: (1.5 jam)
• Struktur Kawalan Pilihan
• Penghasilan Projek Mini
What is Arduino? 7
https://www.youtube.com/watch?v=9vQY2oTrLkY
What words would you associate Arduino with? 8

It’s everywhere!
Microcontroller is a
mini computer that can
be programmed to
control electronic
components
Microcontroller communicates in binary 9

1 Got current VS No current


0
We program the pins to send out the electric signals
Arduino as Microcontroller 10

Start

setup program • Arduino only runs two program.


• Setup program at the beginning
loop program and a loop program that runs
over and over again
Open Your Arduino Program 11

Start

setup program

loop program
Open Your Arduino Program 12

Let’s connect our


microcontroller
Connect your microcontroller to the computer 13
Connect your microcontroller to the computer 14

Install Driver
Connect your microcontroller to the computer 15
Your First Program! 16
Start

Turn on LED at Pin 13

Wait 1 second

Turn off LED at Pin13

Wait 1 second

End
Setting Up 17

OUTPUT tells Arduino that this pin is


an output pin

Don’t forget to end the line with a ;


16 is the target of this function

pinMode is a function that tells the

{} marks the beginning or the end of a function


Arduino the mode of a pin (either
INPUT or OUTPUT
Loop 18
Pin 16 is the target of the digitalWrite function
The signal sent is HIGH
digitalWrite is a function that sends
signal to a pin, turning it on or off.

How to turn it off?

Time is measured in milliseconds. 1 second has 1000 milliseconds

{} Delay is a function that tells Arduino to wait for x amount of milliseconds


Putting Them Together 19

Type this program in


Uploading the program 20

Click upload

Save your file with appropriate file name

Select the right port for the Arduino


21

Does it blink?
NO!
What’s the problem? 22

Start

Turn on LED at Pin 13

Wait 1 second

Turn off LED at Pin13

Wait 1 second

End
23

How to fix it?


Type this correction in
25

Does it blink?
YES!
26

Pin 13
HIGH
27

Pin 13
LOW
Blink 2 LEDs together 28
Blink 2 LEDs together 29

State the pinMode for this new led pin

Turn on the new led pin

Turn off the new led pin


Challenge A 30

Bil Tugasan

A1 Kelipkan LED pada
pin 5 setiap 1 saat
A2 Kelipkan 2 LED
bersama-sama setiap
0.5 saat
A3 6 LED Countdown
A4 6 LED Corak chase-
light
Let’s Make a Circuit! 31

Breadboard
Resistors 32
Light Emitting Diodes (LED) 33
What is a circuit? 34

Basic elements of a circuit:


 Power source
 Pathway for the current to flow through
 Something that uses the current
Circuit 1 – Controlled by Arduino 35

220 
Circuit 1 – Controlled by Arduino 36
Circuit 1 – Controlled by Arduino 37
Is this connection okay?
Schematic for connecting 1 LED at Pin 13 38
Connecting 2 LEDs 39
Take turns to turn on
Schematic for connecting 6 LEDs ? 40
Schematic for connecting a buzzer 41
Suis untuk mengaktifkan
pembaz Breadboard

Pembaz
Tone function 42

tone(pin, frequency , duration )


Frequency to be played (62 –
65565). However, human can only The duration of the frequency.
hear frequency between 20 – Measured in milliseconds
20000

Some of the frequencies are Common duration used in music


named. These are call notes are ½ of a second, ¼ of a second,
and are used in music 1/8 of a second
composition
43
tone(pin, frequency , duration )

NOTE_E6 165 NOTE_C4 262


NOTE_F6 175 NOTE_D4 294
NOTE_G6 196 NOTE_E4 660
NOTE_A6 220 NOTE_F4 649
NOTE_B6 247
Tips and tricks 44

Add a delay between notes


so that you can hear
individual notes more
clearly
Challenge B 45

Bil Tugasan

B1 Lampu Isyarat
B2 Persembahan muzik dan
lampu
B3 Ulangi A3 atau A4 dengan
LED yang sambung di
breadboard
Potentiometer 46

Positive (5V)

Ground (GND)

To analog pin
(A0 – A5)
Code to read input 47

Begins communication with


the computer

Prints the value to the


computer
48

What do you see


when you turn the
knob?
BONUS – Using the value in analogWrite 49

Where do we keep our


potentiometer value?

How to use this value in


our analogWrite?

analogWrite(Pin, value)
BONUS – Using the value in analogWrite 50
BONUS – Using the value in analogWrite 51
Light Dependent Resistor 52
Connecting LDR 53
Connecting LDR 54
Code for LDR? analogRead as well! 55
Challenge C 56
Bil Tugasan

C1 Buat penyelidikan atas talian untuk
carikan kegunaan analogWrite.
Nyalakan 1 LED dengan analogWrite

C2 Gunakan nilai yang dibaca oleh sama


ada LDR ataupun perintang boleh
laras untuk tetapkan keterangan LED
Making Decisions in Arduino 57

Input Output

Process and
Decide
It’s all about deciding! 58
Decision Making Flowchart 59

TRUE FALSE
CONDITION

A B

OTHER STEPS IN THE


PROGRAM
Comparison Operators 60
‘If’ example 61

Start

Read input
from A1
TRUE FALSE

Is A1 less than
500?

Turn on LED Turn off LED

End
Challenge D 62
Bil Tugasan 
D1 Hidupkan LED jika gelap
D2 Hidupkan 3 LED jika nilai
potentiometer adalah lebih daripada
800. Kalau tidak, hidupkan 1 LED
63

Advanced Topic
More than 1 situation 64
Logic Operator 65
Advance Ranging 66

1 LED 2 LEDs 6 LEDs

< 250 > 700

0 250 700 1024

> 250 && < 700


Advance Ranging 67

1 LED
1 LED

< 250 > 700

0 250 700 1024

< 250 || > 700


Advance Ranging 68

1 LED

0 250 700 1024

If (x > 250)
If ( !(x<250) )
Advance Ranging 69

0 250 700 1024

If ( !(x > 250 && x < 700) )


Challenge E 70

Bil Tugasan

E1 Semakin gelap, semakin
banyak LED dinyalakan
E2 Bilangan LED yang
dinyala boleh dikawal
dengan memutar perintang
boleh laras
Resources 71

arduino.cc

instructables.com
makeruno.com.my
https://goo.gl/8AZwj4

cytron.io
Sekian Terima Kasih
Breadboard
Suis untuk
74
mengaktifkan pempaz

Pembaz
75

Anda mungkin juga menyukai