Arduino Learning Guide For Beginners Using Maker UNO X R15 Feb23
Arduino Learning Guide For Beginners Using Maker UNO X R15 Feb23
Arduino Learning Guide For Beginners Using Maker UNO X R15 Feb23
I'm Eng Tong from Cytron Technologies. I wrote this guide with you in mind - a
student (or teacher), learning Arduino for the first time.
When I first started learning Arduino, I discovered that there were lots of
documents, learning guides and tutorials available on the Internet - and ironically,
that was my biggest problem! There were simply too many that I had no idea
where to start and which one to refer to.
And many of the learning guides that claim to be crafted for beginners are just
too superficial - most of the time, they only show you the schematics, give you the
sample code and tell you what to expect but they do not explain how it works.
It's very frustrating for people like me who do not have C-programming
background. Although I can achieve the expected outcomes by following the
schematic and copying the code given, I'm pretty sure I will not be able to create
my own projects even after I've completed all the lessons.
There's nothing special with the projects in this learning guide. Basically, I just
added explanation on some aspects that I initially struggled with and I also
simplified the technical part for better understanding. I sincerely hope that
beginners can benefit from this learning guide and enjoy learning Arduino.
Cheers,
Written by ET Tan
Edited by Cheryl Ng
Illustrated by Suhana Oazmi
Revised by Hairil & `Awatif (v1.5)
LESSON 6 DC MOTOR
Project 14: Control A DC Motor........................................................ 76
Project 15: Controlling A Motor Speed Using A Pushbutton............. 82
1. Peel off the labelling stickers and attach them to the pin headers as
shown below.
2. Take out the breadboard, remove the adhesive backing and then
attach it to the Maker UNO X board.
1. Go to https://www.arduino.cc/en/main/software.
3.Click “INSTALL”.
4.Click “OK”.
6. Click to expand “Ports (COM & LPT)”. Check which port the CH340
driver has been assigned to. Take note of the COM number (for
example, the driver is linked to my COM3).
2. Double click the zip file, open the unzipped folder and then double
click the pkg file.
2. Launch Arduino IDE and you will see the following new sketch.
void setup() {
// put your setup code here, to run once:
pinMode(7, OUTPUT);
}
void loop() {
// put your main code here, to run repeatedly:
digitalWrite(7, HIGH);
}
Output
If error occurs, check and correct your code line by line, and then
compile again.
ü Check to make sure you have not missed out any signs -
; , ( ) and { } .
5. Slide the switch on the board to turn on the “Debug” mode. Check
your result.
ARDUINO FUNCTION
void setup() {
// put your setup code here, to run once:
pinMode(7, OUTPUT);
}
void loop() {
// put your main code here, to run repeatedly:
digitalWrite(7, HIGH);
delay (100);
digitalWrite(7, LOW);
delay (100);
}
void setup() {
// put your setup code here, to run once:
pinMode(7, OUTPUT);
}
void loop() {
// put your main code here, to run repeatedly:
digitalWrite(7, HIGH);
delay (1000);
digitalWrite(7, LOW);
delay (1000);
}
5. Observe the result and compare with the previous program. Can you
tell the difference?
void loop() {
// put your main code here, to run repeatedly:
ARDUINO FUNCTION
In this project, we want to blink LED 2, LED 3, LED 4, LED 5, LED 6 and
LED 7 in sequence.
void setup() {
pinMode(2, OUTPUT);
pinMode(3, OUTPUT);
pinMode(4, OUTPUT);
pinMode(5, OUTPUT);
pinMode(6, OUTPUT);
pinMode(7, OUTPUT);
}
void loop() {
digitalWrite(2, HIGH);
digitalWrite(3, HIGH);
digitalWrite(4, HIGH);
digitalWrite(5, HIGH);
digitalWrite(6, HIGH);
digitalWrite(7, HIGH);
delay (1000);
digitalWrite(2, LOW);
digitalWrite(3, LOW);
digitalWrite(4, LOW);
digitalWrite(5, LOW);
digitalWrite(6, LOW);
digitalWrite(7, LOW);
delay (1000);
}
void setup() {
pinMode(2, OUTPUT);
pinMode(3, OUTPUT);
pinMode(4, OUTPUT); Set Pin 2 to Pin 7 as output.
pinMode(5, OUTPUT);
pinMode(6, OUTPUT);
pinMode(7, OUTPUT);
}
void loop() {
digitalWrite(2, HIGH); Turn on LED 2 to LED 7
digitalWrite(3, HIGH);
digitalWrite(4, HIGH);
digitalWrite(5, HIGH);
digitalWrite(6, HIGH); Hold for 1000ms.
digitalWrite(7, HIGH);
delay (1000);
digitalWrite(2, LOW);
digitalWrite(3, LOW); Turn off LED 2 to LED 7
digitalWrite(4, LOW);
digitalWrite(5, LOW);
digitalWrite(6, LOW); Hold for 1000ms.
digitalWrite(7, LOW);
delay (1000);
}
void setup() {
pinMode(2, OUTPUT);
pinMode(3, OUTPUT);
pinMode(4, OUTPUT);
pinMode(5, OUTPUT);
pinMode(6, OUTPUT);
pinMode(7, OUTPUT);
}
void loop() {
digitalWrite(2, HIGH);
delay (1000);
digitalWrite(3, HIGH);
delay (1000);
digitalWrite(4, HIGH);
delay (1000);
digitalWrite(5, HIGH);
delay (1000);
digitalWrite(6, HIGH);
delay (1000);
digitalWrite(7, HIGH);
delay (1000);
digitalWrite(2, LOW);
delay (1000);
digitalWrite(3, LOW);
delay (1000);
digitalWrite(4, LOW);
delay (1000);
digitalWrite(5, LOW);
delay (1000);
digitalWrite(6, LOW);
delay (1000);
digitalWrite(7, LOW);
delay (1000);
}
Although both versions of the code will turn on the LEDs, the results
are different. In Version 1, you will see as though all the LEDs light up
at the same time.This is because the lines are executed line by
line in quick succession (in less than 1 micro second).
void setup() {
pinMode(4, OUTPUT);
pinMode(2, INPUT_PULLUP);
}
void loop() {
if (digitalRead(2) == LOW)
{digitalWrite(4, HIGH);
}
else {
digitalWrite(4, LOW);
}
}
void loop() {
if (digitalRead(2) == LOW) { Read P i n 2. If it is LOW (switch is
digitalWrite(4, HIGH); pre sse d),
} set Pin 4 to HIGH (turn on LED 4)
else { .
digitalWrite(4, LOW); Else (switch is not pressed),
} set Pin 4 to LOW (turn off LED 4).
}
ARDUINO FUNCTION
if (condition 1) {
// do thing A
}
else if (condition 2) {
// do thing B
}
else {
// do thing C
}
4. Modify your Project 4 code to the following, and then upload to your
Maker UNO X.
void setup() {
pinMode(4, OUTPUT);
pinMode(3, INPUT);
}
void loop() {
if (digitalRead(3) == LOW)
{digitalWrite(4, HIGH);
}
else {
digitalWrite(4, LOW);
}
}
1. Switch is an input device; you need to set that pin as input before
you can use it.
pinMode(pin, INPUT);
BASIC ELECTRONICS
VS
In this project, we are going to construct a simple circuit with one LED.
void setup() {
pinMode(2, INPUT_PULLUP);
pinMode(5, OUTPUT);
}
void loop() {
if (digitalRead(2) == LOW)
while (1) {
digitalWrite(5, HIGH);
delay(200);
digitalWrite(5, LOW);
delay(200);
}
else digitalWrite(5, LOW);
}
BASIC ELECTRONICS
CODING SYNTAX
A while loop will loop continuously, and infinitely, until the expression
inside the parenthesis () becomes false.
while (condition)
{
// do something
}
Example:
1. Do something for 200 times.
var = 0;
while (var < 200)
{
// do something
var++;
}
2. Do something endlessly.
while (1)
{
// do something
}
1. Using the same circuit as Project 6, write and upload the following
code to your board.
void setup() {
pinMode(5, OUTPUT);
}
void loop() {
analogWrite(5, 60);
delay (200);
analogWrite(5, 50);
delay (200);
analogWrite(5, 40);
delay (200);
analogWrite(5, 30);
delay (200);
analogWrite(5, 20);
delay (200);
analogWrite(5, 10);
delay (200);
analogWrite(5, 0);
delay (1000);
}
void setup() {
pinMode(5, OUTPUT); Set Pin 5 (connected to external
} LED) as output.
void loop() {
analogWrite(5, 60); Light up LED at brightness 60 for
delay (200); 200ms.
void loop() {
analogWrite(5, brightness);
delay (200);
if (brightness == 0) {
delay (1000);
brightness = 60;
}
else {
brightness = brightness-10;
}
}
void loop() {
Light up LED at the current value
analogWrite(5, brightness); assigned to the variable '
delay (200);
brightness'. Hold for 200ms.
ARDUINO FUNCTION
CODING SYNTAX
x == y (x is equal to y)
x != y (x is not equal to y)
x < y (x is less than y)
x > y ( x is greater than y)
x <= y (x is less than or equal to y)
x >= y (x is greater than or equal to y)
1. Open a new sketch, write the following code and then upload to your
board.
void setup() {
pinMode(8, OUTPUT);
}
void loop() {
tone (8, 262, 250);
delay (325);
tone (8, 294, 250);
delay (325);
tone (8, 330, 250);
delay (325);
tone (8, 349, 250);
delay (325);
tone (8, 392, 250);
delay (325);
tone (8, 440, 250);
delay (325);
tone (8, 494, 250);
delay (325);
tone (8, 523, 250);
delay (1000);
}
void setup() {
pinMode(8, OUTPUT); Set Pin 8 (connected to on-board piezo
} buzzer) as output.
void loop() {
tone (8, 262, 250); Play tone “Do” 262Hz for 250ms
delay (325);
.
tone (8, 294, 250); Play tone “Re” 294Hz for 250ms.
delay (325);
tone (8, 330, 250);
delay (325); Play tone “Me” 330Hz for 250ms.
tone (8, 349, 250); Play tone “Fa” 349Hz for 250ms.
delay (325);
tone (8, 392, 250); Play tone “So” 392Hz for 250ms.
delay (325);
tone (8, 440, 250); Play tone “La” 440Hz for 250ms.
delay (325);
tone (8, 494, 250);
delay (325); Play tone “Ti” 494Hz for 250ms.
tone (8, 523, 250); Play tone “High Do” 523Hz for 250ms.
delay (1000);
}
1. The position of a music note on the staff (i.e. the five horizontal
lines) determines its tone. The higher the note sits on the staff,
the higher the frequency of the sound and vice versa. You can
refer to the following link to get the frequencies for all 88 keys on
the piano : https://www.arduino.cc/en/Tutorial/ToneMelody
Notes
Rests
1. Modify your previous code into the following and then upload to your
board.
void setup() {
pinMode(8, OUTPUT);}
void loop() {
tone (8, 392, 125);
delay (163);
tone (8, 392, 125);
delay (163);
tone (8, 440, 250);
delay (325);
tone (8, 392, 250);
delay (325);
tone (8, 523, 250);
delay (325);
tone (8, 494, 500);
delay (650);
}
void setup() {
pinMode(8, OUTPUT); Set Pin 8 (connected to on-board
} piezo buzzer) as output.
void loop() {
tone (8, 392, 125); Play tone 'G4' 392Hz for 125ms.
delay (163);
tone (8, 392, 125);
delay (163); Play tone 'G4' 392Hz for 125ms.
1. Did you notice that Project_9 melody only plays once and it
doesn't keep repeating like in the previous project?
void setup() {
// put your setup code here, Start
to run once:
} Do this once (upon powering up or
when reset button is pressed)
void loop() {
// put your main code here,
to run repeatedly:
} Do this repeatedly (until powered
off or reset button is pressed )
void setup() {
// iterate over the notes of the melody:
for (int thisNote = 0; thisNote < 8; thisNote++) {
#include "pitches.h"
void setup() {
// iterate over the notes of the melody:
for (int thisNote = 0; thisNote < 6 ; thisNote++) {
56
Simplifying Digital Making
#include "pitches.h"
Import all the pitch values for typical notes
as defined in pitches.h
int melody[] = {
NOTE_G4, NOTE_G4, NOTE_A4, NOTE_G4, NOTE_C5,
NOTE_B4
}; Define melody[ ] array with the notes to be
played in sequence.
int noteDurations[] =
{8, 8, 4, 4, 4, 4, 2
}; Define noteDurations [ ] array with the
corresponding note durations.
void setup() {
for (int thisNote = 0; thisNote < 6; thisNote++) {
For loop to get the program to play each
note in sequence. 6 here refers to the
number of notes to be played.
Example:
void loop() {
digitalWrite(2,HIGH);
delay (1000); These lines of code from
Project_3 can be shortened into
digitalWrite(3,HIGH);
just three lines using for
delay (1000); statement, as shown below:
digitalWrite(4,HIGH);
delay (1000); for (int i=2; i<8; i++)
digitalWrite(5,HIGH); {
delay (1000); digitalWrite (i , HIGH);
digitalWrite(6,HIGH); delay (1000);
delay (1000); }
digitalWrite(7,HIGH);
delay (1000);
}
Example:
int melody[ ] = { NOTE_C4, NOTE_D4, NOTE_E4 }
void setup() {
Serial.begin(9600);
}
void loop() {
Serial.println(analogRead(A0));
delay(1000);
}
void setup() {
Serial.begin(9600);
}
void loop() {
Serial.println(analogRead(A0));
delay(1000);
}
ARDUINO FUNCTION
1. Only pins A0, A1, A2, A3, A4 and A5 have analog input function.
Hence, we need to connect analog sensors to these pins if we
want to get analog input values.
void setup() {
Serial.begin(9600);
}
void loop() {
Serial.println(analogRead(A0));
delay(1000);
}
4. Once uploaded, click on the serial monitor button. Place your hand in
front of the sensor. Observe the value displayed on the serial monitor
window as you move your palm towards the IR sensor, and then
away from it.
1. Get ready a piece of white cardboard and a black marker pen. Use
the black marker pen to draw a thick black line (about 2cm in width)
across the centre of the cardboard as shown below.
8 cm 2cm
10 cm
Condition IR Reading
White Surface
Black Line
4. Write the following code into a new sketch and then upload to your
board.
int irValue;
void setup() {
Serial.begin(9600);
pinMode (3, OUTPUT);
}
void loop() {
Serial.println(analogRead(A0));
irValue = analogRead(A0);
if (irValue > 500) {
digitalWrite (3, HIGH);
delay(200);
}
else {
digitalWrite (3, LOW);
delay(200);
}
}
“irValue > 500” is the threshold for my case. You may need to change
the threshold value according to your readings in Step 4. You can
pick any value between the two conditions but it is good practice to
set the threshold value at midpoint.
Threshold Value =
void setup() {
Serial.begin(9600); Setup serial communication.
pinMode (3, OUTPUT); Set Pin 3 as output.
}
Check irValue:
if (irValue > 500) {
IF irValue > 500 (i.e. black line is
digitalWrite (3, HIGH); detected), turn on LED 3.
delay(200);
Hold for 200ms.
}
else {
digitalWrite (3, LOW); Else, turn off LED 3.
delay(200); Hold for 200ms.
}
}
IR Transmitter IR Transmitter
IR Receiver IR Receiver
BLACK Surface; WHITE Surface;
IR Reading - HIGH IR Reading - LOW
In this project, the analog input value from the IR sensor keeps
changing depending on the colour of the object it is currently facing.
We need to store the value to a certain place and only summon it
when needed. To do that, we can assign a variable.
Diode
The terminal with a white
stripe should connect to 220 ohm resistor
the positive wire (red)
2N2222 Transistor
void setup() {
pinMode(2, INPUT_PULLUP);
pinMode(5, OUTPUT);
}
void loop() {
if (digitalRead(2)==LOW) {
while(1) {
analogWrite(5, 255);
delay(3000);
analogWrite(5, 80);
delay(3000);
}
}
else digitalWrite(3, LOW);
}
void loop() {
if (digitalRead(2)==LOW){ Check Pin 2 value. IF Pin 2 value =
while(1) { LOW (i.e. switch is pressed),always
analogWrite(5, 255); set Pin 5 to 255 (motor spins at max
delay(3000); speed) for 3 seconds,
analogWrite(5, 80); then set Pin 5 to 80 (motor spins at
delay(3000); medium speed) for 3 seconds.
}
}
else digitalWrite(5, LOW); Else set Pin 5 to LOW (motor is
} turned off; not spinning).
1. Make sure the DC motor is supported. The motor may not have
sufficient torque to spin if the blades are resting on a surface.
2. Turn on the Debug mode and observe the built-in LED at Pin 5.
After the switch is pressed, the LED should alternately light up at
full brightness for 3 seconds and then dim for another 3 seconds.
Nevertheless, motors can still work even when you apply lesser or
greater voltage than recommended. For the DC motor that we are
using in this project, the recommended voltage is 3-6V. Let's try to
apply 3.3V and 5V and see what happens.
We can observe that the speed of the DC motor changes with the
input voltage. The higher the input voltage, the faster the motor spins.
Thus in Project 14, when we set pin 5 value to 255, the motor spins
at full speed and then it slows down when pin 5 value is set to 80.
int mode = 0;
void setup() {
pinMode(2, INPUT_PULLUP);
pinMode(5, OUTPUT);
}
void loop() {
switch(mode) {
case 0:
analogWrite (5, 0);
break;
case 1:
analogWrite (5, 100);
break;
case 2:
analogWrite (5, 255);
break;
default:
mode = 0;
break;
}
if (digitalRead(2)==LOW) {
while(digitalRead(2)==LOW);
mode++;
if (mode==3){
mode=0;
}
}
switch (var) {
case 1:
//do something when var equals 1
break;
case 2:
//do something when var equals 2
break;
default:
// if nothing else matches, do the default
// default is optional
break;
}
2. To stop the motor from spinning, you can write either of these:
or
Without “ ; “
while (condition) {
//if the condition is true, program will run all lines of code
inside { }
}
With “ ; “
while (condition) ;
//if the condition is true, it will only run this line until the
condition becomes false.
Example:
while(digitalRead(2)==LOW);
// If switch 2 is pressed (condition is true), the program will
stay at this line of code until switch 2 is released (condition
becomes false). Then, and only then will it move on to execute
the next line of code.
Ultrasonic Sensor
Jumper wire
Ultrasonic
VCC TRIG ECHO GND
Sensor
Maker UNO X 5V PIN 11 PIN 12 GND
long duration;
int distance;
void setup() {
pinMode(11, OUTPUT);
pinMode(12, INPUT);
Serial.begin (9600);
}
void loop() {
digitalWrite(11, LOW);
delayMicroseconds(2);
digitalWrite(11, HIGH);
delayMicroseconds(10);
digitalWrite(11, LOW);
duration = pulseIn (12, HIGH);
distance = duration*0.034/2;
delay(50);
Serial.print (“Distance = “);
Serial.print (distance);
Serial.println (“cm “);
}
void setup() {
pinMode(11, OUTPUT);
Set Pin 11 (trigger pin) as
pinMode(12, INPUT);
output.Set Pin 12 (echo
Serial.begin (9600); pin) as input.
} Set up serial monitor
(9600 baud)
void loop() {
digitalWrite(11, LOW); Set Pin 11 (trigger pin) to low
delayMicroseconds(2); .Delay for 2 microseconds.
Set Pin 11 to high (emit
digitalWrite(11, HIGH);
signal).Delay for 10
delayMicroseconds(10); microseconds.
digitalWrite(11, LOW); Set Pin 11 to low again.
long duration;
int distance;
void setup() {
pinMode(11, OUTPUT);
pinMode(12, INPUT);
Serial.begin (9600);
}
void loop() {
ultrasonic ();
Serial.print ("Distance = ");
Serial.print (distance);
Serial.println ("cm ");
}
void ultrasonic() {
digitalWrite(11, LOW);
delayMicroseconds(2);
digitalWrite(11, HIGH);
delayMicroseconds(10);
digitalWrite(11, LOW);
duration = pulseIn (12, HIGH);
distance = duration*0.034/2;
delay(50);
}
void loop() {
ultrasonic (); Call the “ultrasonic”
Serial.print ("Distance = "); function (i.e.
Serial.print (distance); execute all the lines of
Serial.println ("cm "); code in thefunction “
} ultrasonic”.)
void ultrasonic() {
digitalWrite(11, LOW);
delayMicroseconds(2); These lines of code
digitalWrite(11, HIGH); have been grouped
delayMicroseconds(10); into a function named
digitalWrite(11, LOW); “ultrasonic”.
duration = pulseIn (12, HIGH);
distance = duration*0.034/2;
delay(50);
}
1. You can use this function to read the time cycle of a pulse.
If the value is set to HIGH, pulseIn() waits for the pin to go from
LOW to HIGH, starts timing, then waits for the pin to go back to
LOW and stops timing. It returns the length of the pulse in
microseconds.
2. Data type
You may ask why most of the time we use “int” (integer) to define
a variable but in this project we are using “long” instead. This is
because we need to inform Arduino what kind of number we
want to store in the variable - whether it is a small number, a
large number, or a number with decimal points, etc. In this
project, the number we need to store for 'duration' is large.
long duration;
int distance;
void setup() {
pinMode(8, OUTPUT);
pinMode(11, OUTPUT);
pinMode(12, INPUT);
}
void loop() {
ultrasonic ();
if (distance < 2) {
tone(8, 349);
}
else {
tone (8, 349);
delay(50);
noTone(8);
delay(distance*10);
}
}
void ultrasonic() {
digitalWrite(11, LOW);
delayMicroseconds(2);
digitalWrite(11, HIGH);
delayMicroseconds(10);
digitalWrite(11, LOW);
duration = pulseIn (12, HIGH);
distance = duration*0.034/2;
delay(50);
}
long duration;
int distance;
void setup() {
pinMode(8, OUTPUT); Set pin 8 (piezo buzzer) as output.
pinMode(11, OUTPUT);
pinMode(12, INPUT);
}
void loop() {
ultrasonic (); Call the “ultrasonic” function.
Source: https://en.wikipedia.org/wiki/Theremin
Task: Build a Theremin using your Maker UNO X and the ultrasonic
sensor. Divide the ultrasonic sensor's sensing range into a few zones to
play different tones.
However, this isn't the end of your journey with Arduino. With the knowledge and
skills you've acquired, you're well on your way to building your own projects!
For more project ideas, you can visit our tutorial page or follow us on Facebook. If
you need more components, you can explore and get them from Cytron webstore -
*free shipping for teachers and students!
Remember to share your creations with us if you'd like to be featured or just drop us
a message anytime. We'd love to hear from you!
Cheers~
R1240223
PM-MUNOX-LK