Introduction to Arduino UNO
Tarikh:29 September 2018
Tempat: Axiata Arena, Bukit Jalil
Mohd Nizam Bin Abd Samad
SK Parit 6
What is Arduino Brain Microcontroller
Eyes
Arduino is an open-source
electronics platform based on easy-
to-use hardware and software. It's
intended for anyone making
interactive projects.
VS LDR Servo
motor
Hands
Arduino UNO
Human
Arduino UNO Arduino spec ATmega328P
1 Microcontroller 5V
Arduino UNO 2 Operating Voltage 7-12V
3 Input Voltage 14 (6 provide PWM)
4 Digital I/O Pins 6
5 Analog Input Pins 13
6 LED_BUILTIN 68.6 x 53.4 mm
7 Size LxW
Standalone
ATmega328p
Arduino Setup
How to set up
1. Hardware and software
2. Arduino IDE Installation or mblock
3. Test
Arduino Setup - ① Hardware and software
PC
Arduino IDE Arduino UNO
USB Cable B
Arduino Setup - Arduino IDE Installation
A. Download https://www.arduino.cc/en/Main/Software
①
③ Install
②
Arduino Setup - ③ Test
① ②
Test: Click
1. Tools>Board:>Arduino/
Genuino Uno
2. Tools>Port:>Com?(Ardu
ino/Genuino Uno)
3. File>Examples>01.Basi
cs>Blink
4. Upload
③ ④ LED pin 13 Blink How to Check?
Go to Device
⑤ Manager
Arduino Programming
Basic programming
1. Programming structure
2. Pin - input output
3. READ/WRITE
4. Data type
5. Arithmetic and compound
6. Control, comparison and boolean
7. Communication-serial
Programming - ① Structure
3 Main Parts
1. General (data type,
library, pin’s name)
1. Void Setup () { Run one }
1. Void Loop () { Run }
Programming - ① Structure
Example programing
① int led = 9; 1. “;” → Used to end a
② statement
void Setup () {
pinMode (led, output); 1. “//” → single line comment
//pinMode (8, input);
} 1. “delay” → Pauses the
program for the amount of
③ void Loop () { time (in milliseconds)
digitalWrite (led, HIGH);
delay (1000);
digitalWrite (led, LOW);
delay (500);
}
Programming - ② Input and output
Digital Pin → Select INPUT or OUTPUT
Example:
void Setup () {
pinMode (8, OUTPUT);
pinMode (9, INPUT);
}
Analog Pin → INPUT
Programming - ③ READ/WRITE
pinMode Analog/digital Read/write
Digital Pin Input digital digitalRead (pin)
Analog analogRead (pin)
(PWM)
Output digital digitalWrite (pin, HIGH)
digitalWrite
(pin, LOW)
Analog analogWrite (pin, 0-
Analog Pin 255) Analog (PWMan)alogRead (pin)
Input
Programming - ③ READ/WRITE (PWM)
PWM Digital Pin
Pulse Width Modulation,
or PWM, is a technique
for getting analog results
with digital means.
Project preparations
PIN
=
GND
Close loop
Output: Projects
Project 1: Running LED
Project 2: LED with tone
Project 3: LED brightness
Project 4: Servo Motor
Output: Project 1- Running LED
Challenge: Coding
Run 6 LEDs go and back
void setup() {
pinMode(5,OUTPUT);
pinMode(6,OUTPUT);
pinMode(7,OUTPUT);
}
void loop() {
digitalWrite(5,HIGH);
delay(500);
digitalWrite(5,LOW);
digitalWrite(6,HIGH);
delay(500);
digitalWrite(6,LOW);
digitalWrite(7,HIGH);
delay(500);
digitalWrite(7,LOW);
}
Output: Project 2- Coding tone (7,349,250);
LED with tone delay (250);
void setup() tone (7,349,250);
Challenge: { delay (250);
Set one LED for one tone code. On and off the LEDs following pinMode(13,OUTPUT); digitalWrite (13, HIGH);
the tone code. }
tone (7,330,250);
void loop() delay (250);
{ tone (7,330,250);
delay (250);
tone (7,262,250); digitalWrite (13, LOW);
delay (250);
tone (7,262,250); tone (7,294,250);
delay (250); delay (250);
digitalWrite (13, HIGH); tone (7,294,250);
delay (250);
tone (7,392,250); digitalWrite (13, HIGH);
delay (250);
tone (7,392,250); tone (7,262,500);
delay (250); delay (500);
digitalWrite (13, LOW); digitalWrite (13, LOW);
}
tone (7,440,250);
delay (250);
tone (7,440,250);
delay (250);
digitalWrite (13, HIGH);
tone (7,392,500);
delay (500);
digitalWrite (13, LOW);
Output: Project 3-LED brightness
Challenge: Coding
Add different tones for different brightness
void setup() {
pinMode(9,OUTPUT);
}
void loop() {
analogWrite(9,50);
delay(100);
analogWrite(9,100);
delay(100);
analogWrite(9,150);
delay(100);
analogWrite(9,200);
delay(100);
analogWrite(9,250);
delay(100);
analogWrite(9,200);
delay(100);
analogWrite(9,150);
delay(100);
analogWrite(9,100);
delay(100);
}
Output: Project 4- Servo Motor
Challenge: Coding
Add different tones and LEDs for different angles
#include <Servo.h>
Servo myservo;
void setup() {
myservo.attach(6);
}
void loop() {
myservo.write(0);
delay(500);
myservo.write(90);
delay(500);
myservo.write(180);
delay(500);
myservo.write(90);
delay(500);
}
Programming - ④ Data type
Display information about data types used in Arduino
code Data type Example
int x=2;
int Integer (2-byte) -32,768 to 32,767 float y=4.2;
float floating-point (4 bytes) 3.4028235E+38 and as char x=”a”
numbers low as -3.4028235E+38
String y=”led”
char character Character literals are written in
value single quotes
String Text Used to store text
Programming - ⑤ Arithmetic and compound
Arithmetic Compound Example1
+ addition ++ increment int x = 10;
int y = 5;
- subtraction -- decrement Int z;
* multiplication += compound addition z=x+y ➡ z=10+5=15
z=x-y ➡ z=10-5=5
/ division -= compound subtraction z=x*y ➡ z=10*5=50
z=x/y ➡ z=10/5=2
= assignment operator
x++ ➡ x = 10+1 = 11
x-- ➡ x = 10-1 = 9
x += y ➡ x = x+y = 10+5 = 15
x -= y ➡ x = x-y = 10-5 = 5
Programming - ⑥ Control, comparison and
boolean
Control Comparison Boolean Example1 Example2
if && and
== equal to || or if (x == 0 && y > 100) { for (int i = 2; i <= 8; i++) {
else digitalWrite (3, HIGH); digitalWrite (i, HIGH);
else if != not equal to ! not } }
for < less than else {
digitalWrite (3,LOW);
> greater than }
<= less than or
equal to
>= greater than
or equal to
Programming - ⑦ Communication-serial
Used for communication between the Arduino Coding
board and a computer or other devices
void setup() {
Functions Serial.begin(9600);
if }
available()
begin() void loop() {
print() Serial.print("Hello world!!!”);
println() delay(2000);
read()
write() }
Challenge:
Display your name and school name in two different lines
Input: Projects
Project 5: Push Button
Project 6: Turn on LED using keyboard
Project 7: Light sensor
Project 8: Distance sensor
Input: Project 5- Push Button
Challenge: Coding
Push button 1 to on LED and push button 2 to off LED
int val=0;
void setup() {
pinMode(9, OUTPUT);
pinMode(4, INPUT);
}
void loop(){
val = digitalRead(4);
if (val == HIGH) {
digitalWrite(9, HIGH);
}
else {
digitalWrite(9, LOW);
}
}
Input: Project 6-Turn on LED using keyboard
Challenge: Coding
Change the angle of servo motor using keyboard. A → 0°, B →
45°, C → 90°, D → 120°,E → 180°, void setup() {
Serial.begin(9600);
pinMode(13,OUTPUT);
}
void loop() {
if (Serial.available()){
int x=Serial.read();
if (x=='1'){
digitalWrite(13,HIGH);
Serial.println("led on");
}
if (x=='2') {
digitalWrite(13,LOW);
Serial.println("led off");
}
}
}
Input: Project 7- Light sensor
Coding
int sensorValue=0;
void setup() {
Serial.begin(9600);
pinMode(9,OUTPUT);
}
void loop() {
sensorValue = analogRead(A0);
Serial.println(sensorValue);
delay(100);
if (sensorValue > 500) {
digitalWrite(13,HIGH);
}
if (sensorValue < 500) {
digitalWrite(13,LOW);
}
}
Challenge:
Set LED1(<300), LED2(300-500), LED3(500-700) and LED4(>700)
Input: Project 8- Distance sensor
Coding
long duration, distance;
const int echoPin = 7;
const int trigPin = 8;
void setup()
{
Serial.begin (9600);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
}
void loop()
{
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
Challenge: duration = pulseIn(echoPin, HIGH);
Set the different distance with different tone distance = duration/57.8; // (cm)
Serial.println(distance);
delay(50);
}
Input and output project: Distance meter
10 Paper
5 LED3 15
LED2 LED4
0 Servo LED5 20
LED1 motor
Arduino project application
Where to learn arduino
The following sites are always a good source of Arduino knowledge, and in no particular order:
● Arduino - Learn the basics (Arduino.cc)
● Learn electronics and microcontrollers using Arduino! (Adafruit)
● Arduino Tutorials (Open Source Hardware Group)
● Arduino Basics Projects Page (ArduinoBasics)
● Educ8s.com (Educ8s YouTube Channel)
● Arduino Your Home & Environment (Arduinotronics)
● Arduino Tutorials | How To Mechatronics
● Arduino Tutorials - tronixstuff
● a place for the electronic hobbyist (Henry's Bench)
● Stonez56: Family, Life, Kids, Work, Arduino (Stonez56)
● Arduino Tutorials | JeremyBlum.com
● Arduino Projects: Arduino Pro Mini Based Projects (Engineers Garage)
● Arduino Tutorials (HumanHardDrive)
Where to buy
1. Kelab IRC FKE https://ircshop.com/
2. NADI Eleczone solution http://nadieleczone.com.my/categories/
3. ZD Electronics https://www.facebook.com/zdelectronic/
4. Autobotic https://www.autobotic.com.my/
5. MyDuino http://myduino.com/
6. Cytron https://www.cytron.com.my/
7. Robotedu http://www.lelong.com.my/merchant/robotedu.htm
Thank you
Notes