Professional Documents
Culture Documents
CH2 G12G WB
CH2 G12G WB
CH2 G12G WB
Microcontrollers II
Section 1 : Algorithm description techniques
Aim
This section will introduce you to computational thinking and problem-solving principles. Writing
algorithms is one of the important steps of computational thinking. In grade 11, you learned
how to use flowcharts to represent an algorithm. This section introduces another algorithm
description technique called pseudocode. You will learn how to construct pseudocode to
represent an algorithm and plan the solutions to your problem to avoid any mistakes when
writing code.
Learning outcomes
Prior knowledge
• Arduino code structure
• Using flowcharts
My STREAM focus
Section 1
Chapter 2
25
Copyright © Ministry of Education – United Arab Emirates
Key vocabulary
computational
thought process for problem solving
thinking
26
Copyright © Ministry of Education – United Arab Emirates
Activity 2.1.1
a. Match the steps of the “Snake and Ladder” game algorithm in Column A with the
symbols of a flowchart in Column B.
Column A Column B
b. Draw the flowchart for the “Snake and Ladder” game algorithm given above.
Section 1
Chapter 2
27
Copyright © Ministry of Education – United Arab Emirates
Activity 2.1.2
Study the algorithm given below, then answer the following questions.
Step 1: Start
Step 2: Accept the length of the two line segments as L1 and L2
Step 3: If L1 and L2 are equal, then display ‘Line Segments are equal’
Step 4: If L1 and L2 are not equal, then display ‘Line Segments are not equal’
Step 5: Stop
Draw the flowchart for the given Write the Arduino code for the given
algorithm. algorithm.
Section 1
Chapter 2
28
Copyright © Ministry of Education – United Arab Emirates
Activity 2.1.3
Construct pseudocode to represent the following algorithm.
Step 1: Create a variable called sum and set it equal to zero
Step 2: Look at each of the elements in the list
Step 3: Add the element to the sum
Step 4: Continue until you reach the end of the list
Step 5: return the sum
Pseudocode
Section 1
Chapter 2
29
Copyright © Ministry of Education – United Arab Emirates
Activity 2.1.4
The following flowchart represents an algorithm used to control temperature. First,
construct pseudocode for the algorithm, then write the Arduino code to solve the
problem.
30
Copyright © Ministry of Education – United Arab Emirates
Activity 2.1.5
Write the Arduino code for the following pseudocode.
BEGIN
READ a,b
IF (a>b) THEN
DISPLAY a is greater
ELSE
DISPLAY b is greater
END IF
END
Arduino code
Section 1
Chapter 2
31
Copyright © Ministry of Education – United Arab Emirates
Key skills
I don’t I’m an
Learning outcome (Please tick the box to show your understanding I understand.
understand. expert.
of the skills below).
Teacher’s comment:
Section 1
Chapter 2
32
Copyright © Ministry of Education – United Arab Emirates
Section 2 : Control structures
Aim
In grade 11, you were introduced to the sequence and selection (if/if-else) control structures.
This section will introduce you to the third type of control structures, the iteration (loop) control
structures, namely for and while repetition statements. This section explains when to use the
iteration control structures, how they work, and the Arduino syntax to implement them.
Learning outcomes
Prior knowledge
• Arduino code structure
• Algorithm description techniques
My STREAM focus
Key vocabulary
33
Copyright © Ministry of Education – United Arab Emirates
Activity 2.2.1
Draw the flowchart of an algorithm that prints the factorial of an integer number selected
by the user. Then, write the Arduino code to solve the problem using while-loop.
Hint: The factorial of a given integer is the multiplication of all positive integers less than
or equal to the given integer. For example, the factorial of 4 is 4 × 3 × 2 × 1 .
34
Copyright © Ministry of Education – United Arab Emirates
Activity 2.2.2
Construct the psuedocode for an algorithm that prints the sum of all odd numbers
between 1 to 50. Then, write the Arduino code to solve the problem using while-loop.
Hint:
• Integer that are divisible by 2, are even numbers.
• Integer that are not divisible by 2, are odd numbers.
Section 2
Chapter 2
35
Copyright © Ministry of Education – United Arab Emirates
Activity 2.2.3
Draw the flowchart of an algorithm that prints the multiplication table of a number. Then,
write the Arduino code to solve the problem using for-loop. The multiplication table
should be printed in the following format.
2X1=2
2X2=4
2X3=6
2X4=8
2 X 5 = 10
2 X 6 = 12
2 X 7 = 14
2 X 8 = 16
2 X 9 = 18
2 X 10 = 20
36
Copyright © Ministry of Education – United Arab Emirates
Activity 2.2.4
A leap year occurs once every four years. A year is a leap year if the following conditions
are satisfied:
• year is multiple of 400.
• year is multiple of 4 and not multiple of 100.
Construct the pesudocode for an algorithm that prints all leap years between two given
years selected by the user. Then, write the Arduino code to solve the problem.
Hint: You have to use relational and boolean operators to check whether a year is leap
year or not.
Section 2
Chapter 2
37
Copyright © Ministry of Education – United Arab Emirates
Student reflection
List three things you have learned and two things you have enjoyed.
Three things I have learned:
1-
2-
3-
Key skills
I don’t I’m an
Learning outcome (Please tick the box to show your understanding I understand.
understand. expert.
of the skills below).
Teacher’s comment:
Section 2
Chapter 2
38
Copyright © Ministry of Education – United Arab Emirates
Section 3: Analogue signals
Aim
In grade 11, you learned how to read and generate digital signals. In this section, you will learn
the difference between analogue and digital signals. You will learn how to read the analogue
signals on Arduino using analogue input statements. You will also learn how to use pulse width
modulation (PWM) to generate analogue signals for simple electrical devices.
Learning outcomes
Prior knowledge
• Identify the fundamentals of Arduino programming.
• Use breadboards for building electronic circuits.
• Identify basic electronic components.
• Read and generate digital signals in Arduino.
• Create simple functions in Arduino.
My STREAM focus
Section 3
Chapter 2
39
Copyright © Ministry of Education – United Arab Emirates
Key vocabulary
PWM
(Pulse Width a technique for generating an analogue signal
Modulation) using digital means
40
Copyright © Ministry of Education – United Arab Emirates
Activity 2.3.1
Write a code to control the brightness of an LED. The brightness of the LED should start
from zero, then gradually increase by 60% after every second until it reaches the maximum
brightness.
Section 3
Chapter 2
41
Copyright © Ministry of Education – United Arab Emirates
Activity 2.3.2
Run the given Arduino code, the modify it to gradually dim the brightness of the LED,
from maximum brightness to zero. Each iteration, the brightness should decrease by a
decrement of 10.
Arduino code:
int LED = 9 ;
void setup() {
// put your setup code here to run once:
}
void loop() {
// put your main code here to run repeatedly:
for (int i = 0; i <= 255; i+=5) {
analogWrite(LED, i);
delay(1000);
}
}
In this activity you want the brightness of the LED to gradually decrease.
So, you are not dealing with it as a digital on/off device but as an analogue
Note output device. Therefore, you are using the analogWrite function. For
the analogWrite function, you don’t need to call the pinMode function.
Section 3
Chapter 2
42
Copyright © Ministry of Education – United Arab Emirates
Activity 2.3.3
Consider the following code:
int potReading;
int potentiometer = 12;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
// put your main code here, to run repeatedly:
potReading = analogread(potentiometer);
Serial.println(“The potentiometer value is:”);
Serial.println(potReading);
delay(1000);
}
void loop() {
}
Error Correction
2. The code for reading the potentiometer’s value is written inside the setup function.
What do you expect the result would be after executing that code?
Section 3
Chapter 2
43
Copyright © Ministry of Education – United Arab Emirates
Activity 2.3.4
We want to make an LED blink by sending a signal from digital pin 13 on the Arduino board.
Which of the following connections is correct? Circle the correct answer.
(a) (b)
Section 3
(c) (d)
Chapter 2
44
Copyright © Ministry of Education – United Arab Emirates
Activity 2.3.5
Which of the connections below meets the design given in the circuit below? Circle the
correct answer.
(a) (b)
Section 3
Chapter 2
(c) (d)
45
Copyright © Ministry of Education – United Arab Emirates
Activity 2.3.6
List the pins that can be used with the function ‘analogWrite’ to provide variable voltage
on Arduino Leonardo.
Section 3
Chapter 2
46
Copyright © Ministry of Education – United Arab Emirates
Activity 2.3.7
Write a code that controls the brightness of an LED. The LED should display three different
brightness intensities (0, 40%, 100%) with one second for each stage. Build the following
circuit to test and evaluate the system.
Note: We take percentages from the maximum limit for the ‘analogWrite’ function =
255.
0% = 0 * 255 = 0
40% = 0.40 * 255 = 102
100% = 1.00 * 255 = 255
Section 3
Chapter 2
void loop() {
for(int brightness = 0 ; brightness < 255 ; brightness +=5){
analogWrite(LED, brightness);
delay(50);
}
48
Copyright © Ministry of Education – United Arab Emirates
Activity 2.3.9
Write the Arduino code for a system that consists of one potentiometer and four LEDs. As
the potentiometer’s knob position changes, the number of LEDs that are on changes (see
the table below). Use the following breadboard diagram to build and test the system.
Section 3
Chapter 2
49
Copyright © Ministry of Education – United Arab Emirates
Extra space
Section 3
Chapter 2
50
Copyright © Ministry of Education – United Arab Emirates
Student reflection
List three things you have learned and two things you have enjoyed.
Three things I have learned:
1-
2-
3-
Key skills
I don’t I’m an
Learning outcome (Please tick the box to show your understanding I understand.
understand. expert.
of the skills below).
Teacher’s comment:
Section 3
Chapter 2
51
Copyright © Ministry of Education – United Arab Emirates
Section 4 : Sensors and actuators
Aim
The aim of this section is to explore the different types of sensors and actuators that can be
programmed using Arduino. Sensors are used to detect changes in the surrounding environment,
while actuators are used to add mobility to the system and to perform certain actions. In this
section, you will study IR sensors, ultrasonic sensors and servo and DC motors. The content
explains how these components work, their equivalent circuitry, and how to control them using
Arduino.
Learning outcomes
Prior knowledge
My STREAM focus
Section 4
Chapter 2
52
Copyright © Ministry of Education – United Arab Emirates
Key vocabulary
Section 4
Chapter 2
53
Copyright © Ministry of Education – United Arab Emirates
Activity 2.4.1
Consider the following code:
int PWM_value = 150;
int motor = 3;
void setup() {
// put your setup code here, to run once:
pinMode(motor, OUTPUT);
}
void loop() {
// put your main code here, to run repeatedly:
analogWrite(motor,PWM_value);
delay(100);
}
Modify the code to make the motor run faster.
Section 4
Chapter 2
54
Copyright © Ministry of Education – United Arab Emirates
Activity 2.4.2
1. Explain the parameters of the map function.
No Explanation
Section 4
Chapter 2
55
Copyright © Ministry of Education – United Arab Emirates
Activity 2.4.3
The following tables show the connections between the motor driver module and the
Arduino microcontroller for both motors A and B.
1. Complete the circuit below to show how two DC motors can be controlled by Arduino
using an L298N motor driver.
Section 4
Chapter 2
56
Copyright © Ministry of Education – United Arab Emirates
2. Analyse the following code, then modify it to rotate the motors in the opposite direction.
Modified code:
Section 4
Chapter 2
57
Copyright © Ministry of Education – United Arab Emirates
Activity 2.4.4
Modify the code shown to control the DC motors to turn left.
//speed of motors between 0 and 255, if you, like you can change it
int pwm_speedA = 255;
int pwm_speedB = 240;
void setup()
{
Serial1.begin(9600);
//pins for motor controller
pinMode(9, OUTPUT);
pinMode(6, OUTPUT);
pinMode(5, OUTPUT);
pinMode(11, OUTPUT);
pinMode(10, OUTPUT);
pinMode(3, OUTPUT);
}
void loop()
{
//turning right
//DC Motor A Turned on
digitalWrite(10, HIGH);
digitalWrite(11, LOW);
analogWrite(3, pwm_speedA);
digitalWrite(6, LOW);
analogWrite(5, 0);
}
Chapter 2
58
Copyright © Ministry of Education – United Arab Emirates
Activity 2.4.5
Edit the given code to control the shaft rotation of a servo motor using a potentiometer.
The hardware connection is shown below:
void setup() {
// attaches the servo on pin 9 to the servo object
________________________________________
}
void loop() {
// read the value of the potentiometer
val = _____________________________________
// scale it to use it with the servo (value between 0 and 180)
motor_position = map(val, 0, 1023, 0, 180);
Section 4
delay(15);
}
59
Copyright © Ministry of Education – United Arab Emirates
Activity 2.4.6
Edit the Arduino code given in the previous activity to restrict the movement of the motor
between 10o and 120o.
Hint: You need to edit the map function.
// scale it to use it with the servo (value between 10 and 120)
Edit the Arduino code given in the previous activity to reverse the direction of rotation of
the motor. Instead of going from 10o to 120o, it should go from 120o to 10o.
Hint: You need to edit the map function.
// scale it to use it with the servo (value between 120 and 10)
Section 4
Chapter 2
60
Copyright © Ministry of Education – United Arab Emirates
Activity 2.4.7
Consider the following code:
int sensor1;
int sensor2;
void setup()
{
pinMode(IR_sensor1_pin, INPUT);
pinMode(IR_sensor2_pin, INPUT);
pinMode(motor_in1, OUTPUT);
pinMode(motor_in2, OUTPUT);
pinMode(en, OUTPUT);
}
void loop(){
sensor1 = digitalRead(IR_sensor1_pin);
sensor2 = digitalRead(IR_sensor2_pin); Section 4
analogWrite(en, 255);
delay(20);
}
61
Copyright © Ministry of Education – United Arab Emirates
else{
digitalWrite(motor_in1, LOW);
digitalWrite(motor_in2, LOW);
analogWrite(en, 0);
delay(20);
}
}
62
Copyright © Ministry of Education – United Arab Emirates
Activity 2.4.8
Create a cardboard holder to mount three IR sensors. As each sensor passes over a black
line, an LED should turn ON. What would happen if the sensors were placed too close to
one another?
Use the diagram below as a reference.
Section 4
Chapter 2
63
Copyright © Ministry of Education – United Arab Emirates
Activity 2.4.9
The code below was written to program a regular digital IR sensor. The code does the
following:
• When an obstacle is detected: an LED turns ON and a message is displayed on the
serial monitor.
• When no obstacle is detected: an LED turns OFF and a message is displayed on the
serial monitor.
If you use the TCRT5000 sensor instead, how would the system respond when you use the
same code?
int LED = 13;
int isObstaclePin = 7;
int isObstacle = HIGH;
void setup() {
Serial.begin(9600);
pinMode(LED, OUTPUT);
pinMode(isObstaclePin, INPUT);
}
void loop() {
isObstacle = digitalRead(isObstaclePin);
if (isObstacle == LOW) {
Serial.println(“OBSTACLE!!, OBSTACLE!!”);
digitalWrite(LED, HIGH);
}
else {
Serial.println(“clear”);
digitalWrite(LED, LOW);
}
delay(200);
}
Section 4
Chapter 2
64
Copyright © Ministry of Education – United Arab Emirates
Activity 2.4.10
Create a simple collision detection system. If the distance is less than 50cm, then an LED
should start blinking ON and OFF to alert the driver.
Section 4
Chapter 2
65
Copyright © Ministry of Education – United Arab Emirates
Activity 2.4.11
Write Arduino code for a system that measures the distance using an ultrasonic sensor.
Then, prints a message on the serial monitor describing the distance to the object as per
the table given below.
Use the following breadboard diagram to build and test the system.
Section 4
Chapter 2
66
Copyright © Ministry of Education – United Arab Emirates
Extra space
Section 4
Chapter 2
67
Copyright © Ministry of Education – United Arab Emirates
Student reflection
List three things you have learned and two things you have enjoyed.
Three things I have learned:
1-
2-
3-
Key skills
I don’t I’m an
Learning outcome (Please tick the box to show your understanding I understand.
understand. expert.
of the skills below).
using Arduino.
Teacher’s comment:
Chapter 2
68
Copyright © Ministry of Education – United Arab Emirates
Section 5: Arduino display tools
Aim
This section aims to introduce Arduino’s display tools, namely the LCD and the LED matrix.
Both serve as useful tools for visualising and monitoring the values sent and received from your
Arduino board.
Learning outcomes
Prior knowledge
• Use breadboards for building electronic circuits.
• Identify the basic electronic components.
My STREAM focus
Key vocabulary
grid of wires
69
Copyright © Ministry of Education – United Arab Emirates
Activity 2.5.1
Modify the Arduino example to print your name. Take a picture of the LCD screen and
paste it into the box below.
Arduino code:
#include <LiquidCrystal.h>
void setup() {
// set up the LCD’s number of columns and rows:
lcd.begin(______,______);
void loop() {
What will you see on your LCD screen after you enter the program in Arduino?
Section 5
Chapter 2
70
Copyright © Ministry of Education – United Arab Emirates
Activity 2.5.2
Study the following code, then write the expected outcome on the LCD shown below.
void setup() {
lcd.begin(16, 2);
lcd.setCursor(3, 0);
lcd.print(“Section 5”);
lcd.setCursor(6, 1);
lcd.print(“Done”);
}
Activity 2.5.3
Use the 8x8 LED matrix to display two different images. The images can be anything you
want, an emoji, a letter, a number etc. The time between displaying the first image and the
second one should not be more than 5 seconds.
Section 5
Chapter 2
71
Copyright © Ministry of Education – United Arab Emirates
Activity 2.5.4
In the circuit below, the servo angles are controlled via a potentiometer and are displayed
on the LCD. Construct the circuit, then complete the code below to:
- control the servo motor using a potentiometer
- display the current position of the servo on the LCD
//LCD settings
#include <LiquidCrystal.h>
LiquidCrystal lcd(8, 9, 4, 5, 6, 7); // initialise the library with the numbers of the interface
pins
void setup()
{
Section 5
72
Copyright © Ministry of Education – United Arab Emirates
}
void loop()
{
//reads the value of the potentiometer
//Print the message “Pot. Value” on the screen by setting the cursor to (0,0)
//Print the potentiometer value on the screen by setting the cursor to (11,0)
//Repeat the steps above to print the message “Angle” and the servo position in line 1
Section 5
Chapter 2
}
73
Copyright © Ministry of Education – United Arab Emirates
Student reflection
List three things you have learned and two things you have enjoyed.
Three things I have learned:
1-
2-
3-
Key skills
I don’t I’m an
Learning outcome (Please tick the box to show your understanding I understand.
understand. expert.
of the skills below).
Teacher’s comment:
Section 5
Chapter 2
74
Copyright © Ministry of Education – United Arab Emirates