Programming Arduino
Getting Started with your first Sketch
Mr Roboshop Electronics
www.roboshopstore.com
[email protected]
Table of Contents
Chapter 1: Introduction to Electronics ............................................ 1
What is Ohm’s Law? .................................................................... 1
How to make an Electrical Circuit? ............................................... 3
Chapter 2: Introduction to Arduino ................................................ 4
Chapter 3: Understanding Arduino Boards ..................................... 6
Chapter 4: Installing Arduino .......................................................... 7
Chapter 5: Using Arduino IDE ......................................................... 8
Chapter 6: Arduino Programming ................................................. 12
6.1 Functions ............................................................................. 12
6.2 Control Structure ................................................................. 15
6.2.1 Conditional and Comparison operators ........................... 15
6.3 Variables ............................................................................. 17
6.3.1 Constant......................................................................... 17
Chapter 7: Analog & Digital Application ........................................ 18
7.1 Digital Application ............................................................... 18
7.2 Analog Application............................................................... 21
Chapter 8: How to make a Traffic Light ......................................... 23
Chapter 9: How to Make Street Light ............................................ 26
Chapter 10: How to make an Automatic Fan ................................. 29
-
Voltage is the different of potential that would drive one
ampere of current against one ohm of resistance.
1
A unit of electric current flow.
Types of Current:
Ohm’s is the SI unit of
electrical resistance,
expressing the resistance
in a circuit transmitting a current per ampere when
subjected to a potential difference of one volt.
2
Closed Circuit Schematic Diagram
Current Resistance
P
o
w
e
r
Closed Circuit is a complete circuit which current can
flow from positive terminal to negative terminal.
Open Circuit
Open Circuit is a circuit is not
complete which current can’t
flow. When there is no current
flow that’s mean, the output
such as DC Motors or Lamp
cannot function.
3
Arduino is a microcontroller that has been used in
controlling devices or systems. Basically, called as the
“Brain” of the devices. It uses to control the and process the
input such as sensors and giving the output, such as display
unit.
4
5
Features of Arduino Uno:
Microcontroller: ATmega328
Crystal Frequency: 16 MHz (external crystal)
Flash Memory: 32 KB from which 0.5 KB used by bootloader (ATmega328)
RAM Memory: 2 KB (ATmega328)
EEPROM Memory: 1 KB (ATmega328)
Operating Voltage: 5V DC
Input Voltage: 7-12V (Powered by either USB or DC Adapter)
Analog Inputs: 6
Digital I/O: 16 (out of them 6 PWM Pins)
6
Open your browser and go to www.arduino.cc
Click on Software and downloads for Arduino IDE
7
Open up your Arduino IDE.
After open your Arduino IDE, a window will pop
up like this.
8
Step 1: Write your Code into Arduino IDE.
This function used to run
your code once.
This function used to run
your code repeatedly.
Step 2: Connect your Arduino to your computer.
9
Step 3: Go to Tools -> Boards -> Arduino Uno
Step 4: Go to Tools -> Port -> Select Available
Port
10
Step 5: Verify your code.
Click this Button to Verify
Step 6: Upload your code to Arduino.
Click this Button to Upload
11
A function is used in order to group your code and make your
code become more efficient. If we want to combine of
multiple input or output, it is recommended to use function.
This diagram is an illustration of function basic
understanding.
Motor
Main Function
Functions Sensor
Function
From this illustration, we can see that there are two
functions separated which are Motor Function and Sensor
Function. But his two function need to include in the main
function in order to execute it.
12
Include Motor function and
Sensor function in main program
function.
Motor Function
Sensor Function
13
Additional Tasks
**Try to print out Hello world! by using
void yourname() function and upload to your Arduino.
14
If, which is used in conjunction with a comparison operator,
tests whether a certain condition has been reached, such as
an input being above a certain number.
The format for if test is:
Example: if (someVariables > 50)
{
//do something here
}
The program tests to see someVariable is greater than 50. If
it is, the program takes a particular action. Put another way,
if the statement in parentheses is true, the statements inside
the brackets are run. If not, the program skips over the code.
15
This is an example of comparison or control structures:
Comparison Operators:
== Equal to
!= Not equal to
< Less than
> Greater than
<= Less than or equal to
>= Greater than or equal to
16
Constant are predefined expressions in the Arduino language. They
are used to make programs easier to read. We classify constant in
groups:
Defining Pin levels: HIGH and LOW
When reading or writing to a digital pin there are only two possible
values a pin can take: HIGH and LOW.
HIGH
The meaning of HIGH (in reference to a pin) is somewhat different
depending on whether a pin is set to an INPUT or OUTPUT.
LOW
The meaning of LOW (in reference to a pin) is somewhat different
depending on whether a pin is set to an INPUT or OUTPUT.
Defining Digital Pins modes: INPUT and OUTPUT
Digital pin can be used as INPUT or OUTPUT. Changing a pin with
pinMode() will change the electrical behaviour of the pin.
INPUT
Usually be used for switch, sensors or keypads as input to the
Arduino.
OUTPUT
Usually be used in Display unit or LED as output from Arduino.
17
Hardware Required
• Arduino Board
• Red LED
• 220-ohm resistor
• Jumper Wires
• Breadboard
Circuit
18
Flowchart
START
LED HIGH FOR 1S
LED LOW FOR 1S
END
1. A Flowchart is used in order
to design the program or the
task.
2. Above picture shows the
breadboard connection.
19
Code
Additional Task
**Modify your code by blinking LED every 5
seconds.
20
Hardware Required
• Arduino Board
• Red LED
• Potentiometer
• Jumper Wires
• Breadboard
Circuit
21
Flowchart
START
ANALOG INPUT
LED BRIGHTNESS
END
Code
22
In this section, we will learn on how to use Digital
sensor.
Hardware Required
• Arduino Board Make sure the LED pin is
• LED connected to Arduino
• Resistors correctly. If not, it will blink
wrongly.
• IR Sensor
• Jumper Wires
• Breadboard
Circuit
• IR Sensor OUT to D7
• LED Red to D13
• LED Yellow to D12
• LED Green to D11
23
Flowchart
START
READ IR SENSOR
YELLOW LED BLINKING
NO
YES
IF
SENSOR=LOW
LED RED ON FOR 3S
LED YELLOW ON FOR 0.1S
LED GREEN ON FOR 3S
END
24
Code
In Electronics there is Active HIGH and Active Low Device. In
our case, IR Sensor is Active LOW Sensor. It means that, this
sensor is triggered when it received 0V as input.
25
In this section, we will learn on how to use Analog
sensor.
Hardware Required Some components doesn’t
• Arduino Board have polarity. Can u guess,
which component doesn’t
• LED
have polarity?
• 220-ohm Resistor
• 10k ohm Resistor
• LDR Sensor
• Jumper Wires
• Breadboard
Circuit
• LDR to A0
• LED Anode to D7
Note: Check the
polarity of LED and
Value of Resistor
26
Flowchart
START
READ LDR VALUE
DISPLAY IN SERIAL
MONITOR
LED OFF
LDR >500
NO
YES
LED ON
END
27
Code
ADDITIONAL TASK
**Make your LED only light up between range
500 to 700 only
28
In this section, we will learn on how to use sensor
libraries.
MAKE SURE DIODE AND
TRANSISTOR POLARITY IS
Hardware Required CORRECT BEFORE CONNECT
• DHT11 Temperature Sensor TO COMPUTER.
• DC Motor
• Arduino Board
• Jumper Wires
• Diode
• Transistor
• Breadboard
Circuit
• DHT11 OUT to A0
• Base Transistor to D12
BASE
COLLECTOR
EMITTER
29
Flowchart
START
Read DHT11 Sensor
Value
Display in Serial Monitor
MOTOR OFF
YES
NO
IF Temp >
30
MOTOR ON
END
30
Code
Don’t Forget to add DHT
libraries to Arduino.
ADDITIONAL TASK
**Add OR comparator as condition for Humidity
31