Introduction To Arduino
Introduction To Arduino
Introduction
Introduction to Arduino
• Arduino is a prototype platform (open-source) based
on an easy-to-use hardware and software. It consists of
a circuit board, which can be programed (referred to as
a microcontroller) and a ready-made software called
Arduino IDE (Integrated Development Environment),
which is used to write and upload the computer code
to the physical board.
• Arduino boards are able to read analog or digital input
signals from different sensors and turn it into an output
such as activating a motor, turning LED on/off, connect
to the cloud and many other actions.
Arduino IDE
• The open-source Arduino Software
(IDE) makes it easy to write code and
upload it to the board. It runs on
Windows, Mac OSX, and Linux. You
can control your board functions by
sending a set of instructions to the
microcontroller on the board via
Arduino IDE (referred to as uploading
software). The Arduino IDE uses a
simplified version of C++, making it easier
to learn to program.
Data Types in Arduino
Program Structure in Arduino
Arduino - Board
• Various kinds of Arduino boards are available
depending on different microcontrollers used.
However, all Arduino boards have one thing in
common: they are programed through the Arduino
IDE.
• The differences are based on the number of inputs and
outputs (the number of sensors, LEDs, and buttons you
can use on a single board), speed, operating voltage,
form factor etc. Some boards are designed to be
embedded and have no programming interface
(hardware), which you would need to buy separately.
Some can run directly from a 3.7V battery, others need
at least 5V.
Arduino - Board
Voltage Levels in Arduino
Voltage Levels in Arduino
Arduino - I/O Functions
• Digital Mode in Arduino
• You can set the mode of any Pin by using the
following command:
Syntax:
• pinMode(pin, mode)
Parameters:
• pin: the Pin number whose mode you wish to set
• mode: INPUT or OUTPUT
Arduino - I/O Functions
• Arduino Digital Input
• You can read the status of any Pin by using the following
command:
Syntax:
• Val=digitalRead(pin)
Parameters:
• Val: variable to save the return of the function
• pin: the Pin number you wish to read its status
• Note: the pin should be previously set as INPUT port.
Arduino - I/O Functions
• Arduino Digital Output
• You can set the value (sending logic value) of
any Pin by using the following command:
Syntax:
• digitalWrite(pin, value)
Parameters:
• pin: the Pin number
• value: HIGH or LOW
First Example (Blinking an LED)
Turn the LED (ON) for one second and then turn it (OFF) for tow second connected to
digital pin 2
Operating an LED
Blinking an LED
Program Code
The circuit Assumptions:
- LED attached to pin 2
-1K resistor attached to LED (Negative pin) and then to ground
-Turns a light emitting diode(LED) connected to digital pin 2 (ON) for one
second and then turn it (OFF) for tow second
Arduino - I/O Functions
Arduino Analog Input:
• The Arduino Uno board contains a 6 channel, 10-bit analog to
digital converter. This means that it will map input voltages
between 0 and 5 volts into integer values between 0 and
1023. This yields a resolution between readings of:
5 volts / 1024 units or, .0049 volts (4.9 mV) per unit.
Syntax:
• Var=analogRead(pin)
Parameters:
• Var: variable to save the return of the function (int)
• pin: the number of the analog input pin to read from (0 to 5)
• Not: This function returns a number between 0 and 1023,
which represents voltages between 0 and 5 volts.
Arduino - I/O Functions
Producing PWM in Arduino:
• Writing an analog value to a pin can be used to generate
PWM signal. The frequency of the PWM signal on most pins
is approximately 490 Hz. On the Uno and similar boards,
pins 5 and 6 have a frequency of approximately 980 Hz.
Syntax:
• analogWrite(Pin, Value)
Parameters:
• Pin: the pin to write to,
• Value: the duty cycle: between 0 (always off) and 255
(always on).
Arduino - I/O Functions
Mapping Variables:
• Mapsa number from one range to another.
Syntax:
• X= map(value, fromLow, fromHigh, toLow, toHigh)
Parameters:
• X= new remapped value.
• value: the number or variable to map.
• fromLow: the lower bound of the value's current range.
• fromHigh: the upper bound of the value's current range.
• toLow: the lower bound of the value's target range.
• toHigh: the upper bound of the value's target range.
Arduino Mega 2560