0 ratings0% found this document useful (0 votes) 27 views7 pagesAssignment-5es & Iot
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content,
claim it here.
Available Formats
Download as PDF or read online on Scribd
Gil Arduino Programming Structure
ected to a computer via USB, where it connects with
the Arduino Development Environment (IDE). The user writes the Arduino code
in the IDE, then uploads it to the microcontroller which executes the code,
interacting with inputs and outputs such as sensors, motors and lights.
© The Arduino board is conn
® fuses aceEmbedded Systems and loT 5-14 JOT and Arduing Program,
nr
AA ‘ ~~
en in C++ with an addition of special methods and fy,
“ is writt
Arduino code the Arduino IDE, it should be uploadeg “*
on
‘After the sketch is written in
Arduino board for execution.
Libraries : Arduino provides built-in libraries, which provide basic functional
is also possible to import other libraries and expand the Arduing an
capabilities and features. These libraries are roughly divided into libraries a
interact with a specific component or those that implement new functions, t
© The Arduino programming language is based on a very simple hardware
programming language called processing, which is similar to the C language, aj,
the sketch is written in the Arduino IDE, it should be uploaded on the Arduing
board for execution.
The first step in programming the Arduino board is downloading and installing
the Arduino IDE. The open source Arduino IDE runs on Windows, Mac OS X ang
Linux. Downloaded the Arduino software (depending on your OS) from the
official website and follow the instructions to install.
‘The Arduino board is connected to a computer via USB, where it connects with
the Arduino development environment (IDE). The user writes the Arduino code in
the IDE, then uploads it to the microcontroller which executes the code, interacting
with inputs and outputs such as sensors, motors and lights.
To get it started with Arduino Uno board and blink the built-in LED, load the
example code by selecting Files>Examples>Basics>Blink. Once the example code is
loaded into IDE, click on the ‘upload’ button given on the top bar. Once the
upload is finished, we should see the Arduino built-in LED blinking. Below is the
example code for blinking :
// the setup function nuns once when we press reset or power the board
void setup() {
// initialize digital pin LED_BUILTIN as an output.
pinMode(LED_BUILTIN, OUTPUT);
}
// the loop function runs over and over again forever
void loop() {
digitalWrite(LED_BUILTIN, HIGH); // tum the LED on (HIGH is the
// voltage level)
// wait for a second
// tam the LED off by making the
ae // voltage LOW
} // wait for a second
delay(1000);
digitalWrite(LED_BUILTIN, Low);
TECHNICAL PUBLICATIONS® . an thrust for knowledge
ICATIOL sp-thrus
o. ledgejoa ane [ol 5-16 Jo and Arduino Programming
sya ms OTE AVOID. SPOT EOINTUL
yal
te!
‘ty to connect to a sensor, display,
+A eae, eles Hor example, the bulltinLiquidCryotal library makes it easy to talk to
"
rele LCD dlaplays. there are hundreds of additional libraries available on the
; jorniet fOr download,
le
vw ate are acollyetion of code that makes ite
wil
skotchos
«A programy written In the Arduino Programming Language is called sketch, A
gheteh is normally saved with the “ino” extension (from Arduino).
yhe Arduino program can be divided in three main parts : Structure, Values
(variables and constants) and Functions,
| Structure setup( ), loop( )
2, Control structure +
if, if....else, for, switeh case, while, do....while, break,
continue.
3. Variables ; Constants —» HIGHILOW, INPUTIOUTPUT, truel false, integer
constants, floating point constants, Data types —> void, boolean, char, byte,
unsigned char
4, Functions : Digital 1/O — pinMode( ), digitalWrite( ), digitalRead( ), Analog 1/O
— analogReference( ), analogRead( ), analogWrite( ).
Strucutra | | Functions Values |
[ Set-up wan | [ vention | [ Variables | E conan |
Fig. 5.5.1
iti duino +
* Remarks for writing a program for Ar E
1. To complete the statement a semicolon "7" is used at the end of the statement.
2. To enclose the block parenthesis "()" are used. aaa in a program contains some
statements, declaration of the variables, functions, or loops,
° 1 knows
TECHNIGAL PUBLICATIONS® - an upIMust for knowledge5-16 foT and Ary
Embedded Systems and loT =
; tatement in the code to better und,
written for each u erst
3. Soames ae pai It can be done by using double forward stag, =)" a,
Seer ec ymont if there is only 2 single line comment. However ip “t,
start of the ees in a row, a forward slash asterisk "/*" at the start and ee
pa line eo my" at the end of the comment, Comments can also py wert
forware fo
Jude any statement.
° Fig 5.5.2 shows basic Arduino Sketch Structure.
Fig. 5.5.2 Arduino sketch structure
* Variable declaration section : In the variable declaration section, usually contains
variables that we must declare. To declare is to indicate that something will be
used or specified for a particular use
a + Here we setup the “actions” of what the program is going to do ©
make the final product function. This could inchade describing which pins will be
TECHNICAL PuBUCA Tions® 87 up-thrust for kmowteageoT and Ardui
Ege which mathematical elegant rig
a ve . ations wi
. ation of the i ‘ will
6 ais i tlie a libraries, Variables useq f pean Setup() function
on’, of the Arduino are also declared in this fun es e code, Similarly, pin
ction. :
\e
000 ati ‘
a ynunication between the Arduino board and the com, Tt also initializes the
section : The Loop section tells puter. It only runs once,
writing the sketch :
yoid setup() {
gerial-begin(9600);
serial.printin("Hello, worldl");
}
yoid loop() {
}
+ Running the sketch : Plug Arduino into PC using a USB cable. Click the Upload
putton to load the program to the Arduino.
«+ Now open the Arduino IDE Serial Monitor Window to see the sketch run and
print the text message.
Opens serial
monitor window
no
. le in jal monitor window.
apie in the serial monitor win«
id be visibl
ts shoul
tpt
«The text that the program outP!
Tions® - a” tp-thrust for knowledge
TECHNICAL PUBLICA’
‘ci ileEmbedded Systems and loT
a
5-10 oT and Arduino Progr
min
BEF Pins
=
.
To use the Arduino pins, you need to define which pin is being used ,
functionality. A convenient way to define the used pins is by using ;
‘#dofino pinNamo pinNumber’
The functionality is either input or output and is defined by using the pinMo, ey
method in the setup section. o
‘The pins on the Arduino can be configured as either inputs or outputs. Arduing
(Atmega) pins default to inputs, so they don't need to be explicitly declared a5
inputs with pinMode() when we are using them as inputs.
Pins configured this way are said to be in a high-impedance state. Input ping
make extremely small demands on the circuit that they are sampling, equivalent to
a series resistor of 100 MQ in front of the pin.
This means that it takes very little current to move the input pin from one state to
another and can make the pins useful for such tasks as implementing a capacitive
touch sensor, reading an LED as a photodiode, or reading an analog sensor with a
scheme such as RCTime.
Each pin operates at 5 V at HIGH and 0 V at LOW. It can provide or receive a
maximum current of 40 mA, but only 20 mA continuous, which is merely
sufficient to drive a single - color LED @ 20 mA for full brightness continuously.
Total current for the chipset shall not exceed 200 mA, ie., driving 10 single - color
LEDs @ 20 mA.
Pin 13 : There is a built-in LED connected to Pin 13, under Pin 13. It is useful for
debugging.
PWM Output : 6 of the pins (pins 3, 5, 6, 9, 10 and 11, marked with ~) can
produce PWM (Pulse Width Modulated) output
Digital input pins can be configured as pinMode(pin, INPUT), where the pin is
the digital pin number we want to initialize. Often it is useful to steer an input
pin to a known state if no input is present. This can be done by adding a pull-up
resistor (to +5 V), or a pulldown resistor (resistor to ground) on the input. A 10K
resistor is a good value for a pull - up or pulldown resistor.
If the pin is configured as INPUT_PULLUP during initialization, it inverts the
behavior of the INPUT mode, where HIGH means the sensor is OFF and LOW
means the sensor is ON. It is because there are 20 K pull-up resistors built into the
Atmega chip that can be accessed from software.
Pins can be configured as OUTPUT with pinMode(pin, OUTPUT), where the P#
is the digital pin number we want to initialize as output. These pins are also in ®
low - impedance state. This means that they can provide a substantial amount of
current to other circuits.
nd ity
TECHNICAL PUBLICATIONS® - an up-thrust for knowledge5-19 oT werd brduina Progremnenitig
eM) gsi function in Arduino IDE,
» URS
GH o LOW value. If the pin has bem
paved its voltage will be set to the correspo
digital pin, to a
OUTPUT with
Y for HIGH, OV
of
(LOW) the internal pull-up on he input pin It is recom
-Mode() to INPUT_PULLUP to enable the internal pull-up re
, The analog input pins can also be used as digital pins, referred to 25 AD, Al, ete
duino program for LED blink :
‘2 LED =13; // The digital pin to
‘yoid savup ()
which the LED is comected
pinMMode (LED, OUTPUT); //Declaring pin 12 az output pin
}
eid loop) // The loop function runs again and again
{
digitalWrite (LED, HIGH); //Tum ON the LED
delay(1000); //Wait for 1 see
digitalRead (LED, LOW); // Tum off the LED
delay(1000); // Wait for 1 sec
i
Here, LED is declared globally and is set to pin number 13. This will reduce the
number of iterations required to update the pin number in the program when we
connect the LED to the other digital pin.
* A pin on Arduino can be set as input or output by using pinMode function.
PinMede(12, OUTPUT); —_// sets pin 13 as output pin
pinMode(13, INPUT); // sets pin 13 as input pin
ReadingMWriting digital values
digitalWrrite(13, LOW); // Makes the output voltage on pin 13,0 V
digitalWrite(13, HIGH); // Makes the output voltage on pin 13,5 V
itbuttonState=digitalRead(2); // reads the value of pin 2 in buttonState
—_