You are on page 1of 65

Short course

on
Introduction to Microcontroller

Abdul Rashid Husain


abrashid@utm.my

Hasan Alqaraghuli
pc_hssn@icloud.com

30th Jan 2018


Sekolah Tun Fatimah (STF), Johor Bahru

1
Outlines:
i. What is microcontroller (μC) and its application?
ii. Sensor and Actuator – interfaced to μC
iii. Installation of Arduino IDE and ArduBlock
iv. Introduction to Arduino – Example 1
v. Programming with ArduBlock
vi. Example 2: Two LEDs Blinking
vii. Example 3: Arrays of LEDs Blinking in Pattern
viii. Example 4: Push button and limit switch as inputs
ix. Example 5: Switches and activation for LEDs running
pattern
x. Example 6: Potentiometer as input – analog input
xi. Example 7: Relay control
xii. Example 8: Light sensor and relay to control 12V LEDs
xiii. Demo on Simulator – offline and Online solution
2
What is microcontrollers ?

Microcontroller is the brain of devices and it operates on its own to


control the designed system
3
Manufacturers

4
List down 10 devices around you
that contain microcontrollers?

5
Applications

LED CUBE – LEDs arranged in matrix form to


display various creative patterns
6
Applications

Electronically Scanning Radar (ESR) in


automotive
Applications

Top 10 Arduino–based Projects


Applications

Automotive Energy

Home appliances Medical devices


9
How to get started…

Simplest development
tools

Online community for


support and sharing

Cost-effective

10
Sensor and Actuator – Interfaced to μC

Sensor A Actuator X

Physical Actuator Y
Sensor B Microcontroller
signals
. .
. .

Sensor Ni Actuator Zi

- Physical signals such as temperature, humidity, light, speed, etc. are


analog signals (continuous)
- Sensors are used to convert the physical signals into digital form
that can be used by μC (Input)
- μC will perform the operation according to the program written by
users
- Appropriate output signal from μC is used to drive actuators of the
system (output) 11
Sensor and Actuator – Interfaced to μC
Solar tracking system

Solar panel will be rotated in such a way that the panel will get
‘maximum’ concentrated sunlight for optimum power generation.

- Sensor : light detector

- μC : based on the intensity of light, it will calculate the optimum


angle for the panel to receive the sunlight
- Actuator : DC or Stepper motor will be rotated according to the
calculated angle
12
Sensor and Actuator – Interfaced to μC

Components Sensor/Actuator? (Physical signal)?


LED
LM 35 Temperature sensor
Buzzer
Ultrasonic sensor
Bluetooth module
Push button
Relay
DC motor
Compressor
Stepper Motor
Variable resistor

13
Steps in using μC

Writing the program in IDE

Compile the program

Download the compiled


program (HEX file) into μC

μC runs as single chip


controller to control the
system

14
Introduction to Arduino

15
Family of Arduino

16
17
Development tool
Integrated Development Environment (IDE) – coding,
compiling, debugging.

18
Development tool
Editor – hard coding to be done

19
Development tool
• We choose a lot simpler approach – graphical
programming
• The are many tools available online and most of them are
free
• ArduBlock

20
Development tool

• By using ArduBlock the sequence will be as follows:

Write the program by Program is converted to Program is uploaded to


using graphical standard Arduino the Arduino board and
approach in ArduBlock language run as single-chip
system

21
Development tool

Program converted My own code


from ArduBlock 22
Installation of Arduino™ IDE
• Download the Arduino™ IDE from website:
https://www.arduino.cc/download_handler.php?f=/arduino-1.8.5-windows.exe
• After downloading, Run the installation file.

Step 1 : License agreement Step 2 : Select components

23
Step 3 : Installation directory Step 4 : Installation running

Step 5 : Installation completed

Step 6
After finishing the installation
procedure, Run the program.
Introduction of Arduino™ IDE

25
Setting-up configuration

i. Selecting board type.


Tools/Board/select board
(Arduino Uno)
(The Arduino Uno board
should be connected)

ii. Selecting USB Port


number.
Tools/Serial Port/”Select
port”
(Arduino Uno)

26
ArduBlock Installation
• Download the ArduBlock from website:
https://sourceforge.net/projects/ardublock/files/ardublock-beta-
20140702.jar/download
• Create folder on desktop named as “tools”
Inside this folder, create another folder “ArduBlockTool”
Inside this folder, create another folder “tool”.
Finally copy the downloaded file into this directory.
“tools\ArduBlockTool\tool\”ArduBlock filename”.
• Copy the whole “tools” folder to this directory
“C:\Users\xxxxx\Documents\Arduino”
• Now open Arduino IDE and go to “Tools/ArduBlock”.
27
Now open Arduino IDE and go to “Tools/ArduBlock”.

28
Example 0: Turn ON one LED

- Connect GND and 5V pin from Arduino


to the LED and resistor
- Measure the voltage with multi meter
- LED turns ON without any control
‘mechanism’
29
Example 1: LED Blinking
- The LED ON/OFF time is controlled
such that it produces blinking LED
Select pin number and set as
- Connect LED to pin 8 as shown in the
Digital Output
figure.
- In order to turn ON and OFF LEDs,
Set the pin to HIGH (5V)
with Arduino, we need to follow the
‘ON’
sequence:
Set DELAY time
(to maintain the 5V)

Set the pin to LOW (0V)


‘OFF’

Set DELAY time


(to maintain the 0V)
- Before continuing with LED Blinking example, let’s start
writing code in ArduBlock, by selecting Tools/ArduBlock
- Once it is selected, ArduBlock IDE programming space as shown below will
appear.
- It contains the function buttons on the left, and empty programing space
with one ‘loop-do’ in yellow block in the space.
To upload file to Arduino

Function
buttons Programming space
A few important functions in ArduBlock are:
i. Control button
Control

Loop - do

Delay function

If-then conditional loop

If-then-else conditional loop


A few important functions in ArduBlock are:
ii. Pins
Pins
Digital pin
Analog pin
Set digital pin

Set analog pin


A few important functions in ArduBlock are:
iii. Test

Test
Comparing two digital values

Comparing two analog values


A few important functions in ArduBlock are:
iv. Variable/Constants

Set integer variable


Variable/Constants

LOW
HIGH
A few important functions in ArduBlock are:
v. Math Operators

Math Operators

map
Example 1: LED Blinking
- Writing code in ArduBlock

Select pin number and set as


Digital Output

Set the pin to HIGH (5V)


‘ON’

i. Select ‘Pins’ and choose ‘set digital pin’ block


ii. Place the ‘set digital pin’ block inside the ‘loop-do’ block
iii. Set the pin number to be ‘D8’
iv. Set the pin to ‘HIGH’ to indicate 5V
Example 1: LED Blinking
- Writing code in ArduBlock

Select pin number and set as


Digital Output

Set the pin to HIGH (5V)


‘ON’

Set DELAY time


(to maintain the 5V)

v. Select ‘Control’ and choose ‘delay MILLIS’ block


vi. Place the ‘delay MILLIS’ block inside the ‘loop-do’ block
under the ‘set digital pin’ block
vii. Set the ‘delay MILLIS’ to 500 which indicate 0.5 sec timing delay
Example 1: LED Blinking
- Writing code in ArduBlock

Select pin number and set as


Digital Output

Set the pin to HIGH (5V)


‘ON’

Set DELAY time


(to maintain the 5V)
viii. Select ‘Pins’ and choose ‘set digital pin’ block
Set the pin to LOW (0V) ix. Set the pin number to be ‘D8’ and ‘LOW’
‘OFF’ x. Select ‘Delay MILLIS’ and set the ‘delay
MILLIS’ to 500
xi. The program is complete!
Set DELAY time
(to maintain the 0V)
Example 1: LED Blinking
- Save the ArduBlock program and convert/upload to Arduino

i. Select the ‘Save As’ button at the tool bar


under one name i.e. Example1.abp
ii. Connect the USB port to the Arduino board
iii. Upload the file to Arduino IDE by selecting
‘Upload to Arduino’
iv. Give a name to the saved Arduino file. i.e.
exemp1
v. The LED at pin 8 will be blinking!
Example 1: LED Blinking
- Description on the Arduino program

Pin number 8 is selected and


set as ‘output’
Pin number 8 is set to HIGH
so that it gives 5V - ON
Time delay of 0.5 sec is
generated so that pin 8 is ON
for 0.5 sec
Pin number 8 is set to LOW
so that it gives 0V - OFF

Time delay of 0.5 sec is


generated so that pin 8 is OFF
for 0.5 sec
Example 1: LED Blinking
In summary, for this example, we have the
circuit connected to Arduino, ArdoBlock
program and Arduino program to produce
blinking LED.

43
Example 2: Two LED Blinking

- Two LEDs will be blinking alternately.


- Connect two LEDs to pin 7 and pin 8
as shown in the figure.
- Write down the sequence to perform
this operation.
- By modifying the Arduino program in
Example 1, write the code to perform
the sequence.

44
Example 2: Two LED Blinking

45
Example 3: Arrays of LEDs Blinking
with patterns.
- Four LEDs will be blinking in pattern.
- Connect four LEDs to pin 3 to pin 6 as
shown in the figure.
- Choose a blinking pattern and write
down the sequence to perform this
operation.
- Write the code in ArduBlock and
upload to Arduino block to check the
blinking pattern.

46
Example 3: Arrays of LEDs Blinking
with patterns.

47
Example 3: Arrays of LEDs Blinking
with patterns.

48
Example 4: Push button and limit switch
as input
- Push button and limit switch acts
as input to the Arduino when it is
connected to HIGH or LOW
- Connect one LED at pin 13, and
push button to pin 8.
- Pin 13 (LED) is the ‘output’ pin
while pin 8 (push button) is the
input.
- In this example, the LED will only
be blinking IF the button is
pressed, which becomes a
condition in the program.
- Write the program flow chart.

49
Example 4: Push button and limit switch
as input

- In order to ‘sense’ the voltage at push


button, the if-then-else loop is needed.
- The standard if-then-else is:
Check the condition
if (check Condition) {
// do some tasks if condition is true Do tasks if
} else {
condition is true
// do other task if the condition is
false }
Do tasks if
condition is
- For the Example 4 the condition is: false
if (button is pressed) {
// turn ON LED
} else {
// turn OFF LED}

- Push button can be replaced with limit


switch
50
Example 4: Push button and limit
switch as input

51
Example 5: Switch to activate LEDs
pattern
- Signal from Push button will be used to
activate array of LEDs blinking pattern.
- Connect four LEDs to pin 2 to pin 5 as
and push button at pin 1
- This is the combination of Example 3
and Example 4.
- In this example, if button is pressed,
then LEDs will blink according to certain
pattern.
- If button is not pressed, then, LEDs will
be OFF
- Write the program flow chart.

52
Example 5: Switch to activate LEDs
pattern

53
Example 5: Switch to activate LEDs
pattern

54
Example 6: Potentiometer as analog
input
- Potentiometer is a variable resistor
where the voltage read varies according
to the position of the knob
- The varying resistor will vary the voltage
input.
- This varying voltage is an analog signal
and will be scaled to digital values.
- Connect the circuit as shown with the
middle pin of potentiometer is
connected to pin A0 (analog input 0)
- By varying the knob slowly, the LED will
turn ON one by one until all LEDs are
ON.

55
Example 6: Potentiometer as analog
input
- Since potentiometer will vary the voltage
(analog input), the voltage level will be
partitioned to 5 level which are:

ANALOG DIGITAL
0
0V 1024 <1
0 = 256
4 256
5V
1024 <2
512
- map block from math operator is used to
<3
do this operation 768
<4
1024
=4

56
Example 6: Potentiometer as analog
input

57
Example 6: Potentiometer as analog
input

58
Example 6: Potentiometer as analog
input

59
Example 6: Potentiometer as analog
input

60
Example 7: Relay control

- Relay is used to separate 5V circuit to


high voltage circuit.
- The high voltage circuit can be
connected to 12V or up to 240V circuit
depending on the specification of the
relay.
- Connect the circuit as shown where pin
2 is connected to push button (input)
and pin 7 as output (to IN in relay)
- Once the button is pressed, relay will be
triggered to turn ON LED.
61
Example 8: Light sensor and Relay control

62
Demo on Simulator - Tinkercad

63
Simulator

64
Create WA group and
we are Arduino family!

-Terima kasih-

65

You might also like