Arduino Notes
Arduino Notes
Arduino was created in the year 2005 by two Italian engineers David Cuartielles and Massimo Banzi with
the goal of encouraging students to program the Arduino uno microcontroller and improve their skills
about electronics and use it in the real world.
The Arduino Uno is a microcontroller board based on the ATmega328. It has 14 digital input/output pins,
6 analog inputs, a 16 MHz crystal oscillator, a USB connection, a power jack, memory and a reset
button. It contains everything needed to support the microcontroller; simply connect it to a computer
with a USB cable or power it with a AC-to-DC adapter or battery to get started.
Arduino uno microcontroller can sense the environment by receiving input from a variety of sensors and
can affect its surroundings by controlling lights, motors, and other actuators. The microcontroller is
programmed using the Arduino programming language and the Arduino development environment
."Uno" means one in Italian and is named to mark the version, Arduino 1.0. The Arduino IDE is derived
from Wiring and Processing.
1
● This has an ICSP (In-Circuit Serial Programming) connector for bypassing the USB port and
interfacing the Arduino directly as a serial device. This port is necessary to re-bootload your chip
if it corrupts and can no longer be connected to your computer.
Power USB
Arduino board can be powered by using the USB cable from your computer. All you
need to do is connect the USB cable to the USB connection (1).
Voltage Regulator
The function of the voltage regulator is to control the voltage given to the Arduino
board and stabilize the DC voltages used by the processor and other elements.
Crystal Oscillator
The crystal oscillator helps Arduino in dealing with time issues. How does Arduino
calculate time? The answer is, by using the crystal oscillator. The number printed
2
on top of the Arduino crystal is 16.000H9H. It tells us that the frequency is
16,000,000 Hertz or 16 MHz.
Arduino Reset
You can reset your Arduino board, i.e., start your program from the beginning. You
can reset the UNO board in two ways. First, by using the reset button (17) on the
board. Second, you can connect an external reset button to the Arduino pin
labelled RESET (5).
Analog pins
The Arduino UNO board has five analog input pins A0 through A5. These pins can
read the signal from an analog sensor like the humidity sensor or temperature
sensor and convert it into a digital value that can be read by the microprocessor.
Main microcontroller
Each Arduino board has its own microcontroller (11). You can assume it as the brain
of your board.
ICSP pin
ICSP (In-Circuit Serial Programming) pins are for bypassing the USB port and
interfacing the Arduino directly as a serial device. This port is necessary to
re-bootload your chip if it corrupts and can no longer be connected to your
computer.
3
TX and RX LEDs
On your board, you will find two labels: TX (transmit) and RX (receive). They appear
in two places on the Arduino UNO board. First, at the digital pins 0 and 1, to
indicate the pins responsible for serial communication. Second, the TX and RX led
(13). The TX led flashes with different speed while sending the serial data. The
speed of flashing depends on the baud rate used by the board. RX flashes during
the receiving process.
Digital I/O
The Arduino UNO board has 14 digital I/O pins (15) (of which 6 provide PWM (Pulse
Width Modulation) output. These pins can be configured to work as input digital
pins to read logic values (0 or 1) or as digital output pins to drive different modules
like LEDs, relays, etc. The pins labeled “~” can be used to generate PWM.
AREF
AREF stands for Analog Reference. It is sometimes used to set an external
reference voltage (between 0 and 5 Volts) as the upper limit for the analog input
pins.
ARDUINO Terminology :-
4
To open an existing project example, select File → Example → Basics → Blink.
Here, we are selecting just one of the examples with the name Blink. It turns the LED on and off with
some time delay. You can select any other example from the list.
To avoid any error while uploading your program to the board, you must select the correct Arduino
board name, which matches with the board connected to your computer.
5
Here, we have selected the Arduino Uno board according to our tutorial, but you must select the name
matching the board that you are using.
6
● Upload the program to your board.
Before explaining how we can upload our program to the board, we must demonstrate the function of
each symbol appearing in the Arduino IDE toolbar.
7
B − Used to upload a program to the Arduino board.
F − Serial monitor used to receive serial data from the board and send the serial data to the board.
Now, simply click the "Upload" button in the environment. Wait a few seconds; you will see the RX and
TX LEDs on the board, flashing. If the upload is successful, the message "Done uploading" will appear
in the status bar.
Arduino programs can be divided into three main parts: Structure, Values (variables and constants),
and Functions.
Let us start with the Structure. Software structure consist of two main functions −
● Setup function- The setup() function is called when a sketch starts. Use it to initialize the
variables (not declaration), pin modes, start using libraries, etc. The setup function will only run
● Loop function - After creating a setup() function, which initializes and sets the initial values, the
loop() function does precisely what its name suggests, and loops consecutively, allowing your
program to change and respond. Use it to actively control the Arduino board.
Data Types -
The following table provides all the data types that you will use during Arduino programming.
Data void bool unsi int unsi word long unsi float doub strin struc array enum pointer
type ean gned gned gned le g t
char int long
8
RAM N/A 1byte 1 2 2 2 4 4 4 4 1 N/A 1 N/A N/A
usag byte byte byte byte byte byte byte byte byte byte
e +x +x
Control Statements are elements in Source Code that control the flow of program execution. They
are −
If statement
It takes an expression in parenthesis and
a statement or block of statements. If the if (expression)
expression is true then the statement or
{
block of statements gets executed
otherwise these statements are skipped. Block of statements;
1 }
If …else statement
An if statement can be followed by an
optional else statement, which executes if (expression) {
when the expression is false.
Block of statements;
2 }
else {
Block of statements;
9
If…else if …else statement
The if statement can be followed by an
optional else if...else statement, which is if (expression_1) {
very useful to test various conditions
Block of statements;
using single if...else if statement.
}
else if(expression_2) {
Block of statements;
3 }
else {
Block of statements;
10
Switch Case statement
Similar to the if statements, switch...case
controls the flow of programs by allowing switch (variable) {
the programmers to specify different
case label:
codes that should be executed in various
conditions. // statements
break;
case label: {
// statements
4
break;
default: {
// statements
break;
Conditional Operator ? :
The conditional operator ? : is the only
ternary operator in C. expression1 ? expression2 : expression3
5
Programming languages provide various control structures that allow for more complicated execution
paths.
11
while loop
while loops will loop continuously, and infinitely, while(expression) {
1 until the expression inside the parenthesis, () Block of statements;
becomes false. Something must change the }
tested variable, or the while loop will never exit.
do…while loop do {
The do…while loop is similar to the while loop. In Block of statements;
the while loop, the loop-continuation condition is }
2 tested at the beginning of the loop before while (expression);
performed the body of the loop.
12
Infinite loop for (; ;) {
It is the loop having no terminating condition, so // statement block
the loop becomes infinite. }
Or
Or
do {
Block of statements;
}
while(1);
2. digitalWrite() - The digitalWrite() function is used to write a HIGH or a LOW value to a digital pin.
3. digitalRead() -Reads the value from a specified digital pin, either HIGH or LOW.
13
● pin - the number of the
digitalRead() Function Syntax digital pin you want to read
digitalRead(pin); (int).
Example -
void loop() Return -
{ ● val − HIGH, or LOW.
val = digitalRead(4); // read the input pin
digitalWrite(7, val); // sets the LED to the button's
value
}
4. analogRead( ) function - we can read the voltage applied to one of the pins. This function
returns a number between 0 and 1023, which represents voltages between 0 and 5 volts.
Example -
void loop()
{
14
In telecommunication and data transmission, serial communication is the process of sending data one
bit at a time, sequentially, over a communication channel or computer bus. The Serial Monitor is a
separate pop-up window that acts as a separate terminal for communication.
The serial monitor is the 'tether' between the computer and your Arduino , it lets you send and receive
text messages, handy for debugging and also controlling the Arduino from a keyboard. For example,
you will be able to send commands from your computer to turn on LEDs.
The baud rate is the rate at which information is transferred in a communication channel. For
communicating with the computer, use baud rate: 9600 (transferring a maximum of 9600 bits per
second).
print()- Example -
Prints data to the serial port as human-readable ASCII
text. 1.Serial.print(78) gives "78"
println()- 2.Serial.print(1.23456) gives "1.23"
Prints data to the serial port as human-readable ASCII
text followed by a carriage return character ( '\r') and 3.Serial.print('N') gives "N"
a newline character ('\n').
4.Serial.print("Hello world.") gives
Syntax - "Hello world."
Serial.print(val)
Serial.print(val, format)
Serial.println(val)
Serial.println(val, format)
Parameters -
val: the value to print - any data type.
begin() - Sets the data rate in bits per second (baud) Example -
for serial data transmission.
void setup() {
Syntax - Serial.begin(9600); // opens serial port, sets
data rate to 9600 bps
Serial.begin(speed) }
Serial.begin(speed, config)
Parameters -
speed: in bits per second (baud) - long
15
Serial.end()
ARRAYS IN ARDUINO - An array is a collection of variables that are accessed with an index number .
Creating Array -
int myInts[6];
int myPins[] = {2, 4, 8, 3, 6};
int mySensVals[6] = {2, 4, -8, 3, 2};
char message[6] = "hello";
Example -
16
/*
Arrays
void setup() {
// the array elements are numbered from 0 to (pinCount
- 1).
// use a for loop to initialize each pin as an output:
for (int thisPin = 0; thisPin < pinCount; thisPin++) {
pinMode(ledPins[thisPin], OUTPUT);
}
}
void loop() {
// loop from the lowest pin to the highest:
for (int thisPin = 0; thisPin < pinCount; thisPin++) {
// turn the pin on:
digitalWrite(ledPins[thisPin], HIGH);
delay(timer);
// turn the pin off:
digitalWrite(ledPins[thisPin], LOW);
PROGRAMMING Examples :-
1. Example Blink
17
Schematic- Sketch -
// Turns on an LED on for one second, then off for
one second, repeatedly.
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); // turn the LED on
(HIGH is the voltage level)
delay(1000); // wait for a second
digitalWrite(LED_BUILTIN, LOW); // turn the LED off
by making the voltage LOW
delay(1000); // wait for a second
Hardware Requirements- }
Arduino Board
LED
390 ohm resistor
2. Example Fade-
Schematic- Sketch -
/* This example shows how to fade an LED on pin
9 using the analogWrite() function.
The analogWrite() function uses PWM, so if you want
to change the pin you're using, be sure to use
another PWM capable pin. On most Arduino, the
PWM pins are identified with a "~" sign, like ~3, ~5,
~6, ~9, ~10 and ~11.
18
*/
Schematic- Sketch -
// these constants won't change. They are the
// lowest and highest readings you get from your sensor:
const int sensorMin = 0; // sensor minimum, discovered
through experiment
19
const int sensorMax = 600; // sensor maximum,
discovered through experiment
void setup() {
// initialize serial communication:
Serial.begin(9600);
}
void loop() {
// read the sensor:
int sensorReading = analogRead(A0);
// map the sensor range to a range of four options:
int range = map(sensorReading, sensorMin, sensorMax,
0, 3);
Schematic- Sketch -
// constants won't change. They're used here to
// set pin numbers:
const int buttonPin = 2; // the number of the
pushbutton pin
const int ledPin = 13; // the number of the
LED pin
20
// variables will change:
int buttonState = 0; // variable for reading the
pushbutton status
void setup() {
// initialize the LED pin as an output:
pinMode(ledPin, OUTPUT);
// initialize the pushbutton pin as an input:
pinMode(buttonPin, INPUT);
}
void loop() {
// read the state of the pushbutton value:
buttonState = digitalRead(buttonPin);
Schematic- Sketch -
// these constants won't change:
const int ledPin = 13; // led connected to
digital pin 13
const int knockSensor = A0; // the piezo is
connected to analog pin 0
const int threshold = 100; // threshold value to
decide when the detected sound is a knock or
not
21
int ledState = LOW; // variable used to store
the last LED status, to toggle the light
void setup() {
pinMode(ledPin, OUTPUT); // declare the ledPin
as as OUTPUT
Serial.begin(9600); // use the serial port
}
void loop() {
// read the sensor and store it in the variable
sensorReading:
sensorReading = analogRead(knockSensor);
Schematic- Sketch -
/* function:use a magnet to approach the reed switch, and
the LED light up. Move the magnet farther and the LED
will go out.
*/
const int digitalInPin = 7;// reed switch attach to pin7
const int ledPin = 13;// pin13 built-in led
void setup()
{
pinMode(digitalInPin,INPUT);// set digitalInPin as INPUT
pinMode(ledPin,OUTPUT); // set ledPin as OUTPUT
}
22
void loop()
{
boolean stat = digitalRead(digitalInPin);//read the value of
pin7 to stat
if(stat == HIGH)// if it it HIGH
{
digitalWrite(ledPin,LOW);// then turn off the led
}
else //else
{
digitalWrite(ledPin,HIGH); // turn on the led
}
}
● Small in size
● Easy to interface
● Inexpensive
● Low-power
● Easy to use
2. Humidity Sensor -
23
Following are the advantages of Humidity
Sensors −
3. Photoresistor -
A photoresistor (or
light-dependent resistor, LDR, or
photoconductivecell) is a
light-controlled variable resistor.
The resistance of a photoresistor
decreases with increasing incident
light intensity. A photoresistor can
be applied in light-sensitive
detector circuits, and light- and
dark-activated switching circuits.
4. Accelerometer -
5. SERVO Control -
24
Arduino board to control RC (hobby) servo
motors. Servos have integrated gears and a shaft
that can be precisely controlled. Standard servos
allow the shaft to be positioned at various angles,
usually between 0 and 180 degrees. Continuous
rotation servos allow the rotation of the shaft to
be set to various speeds.
Functions-
attach()
write()
writeMicroseconds()
read() FOR IMPLEMENTATION REFER BELOW GIVEN
LINK :
attached()
https://playground.arduino.cc/Learning/SingleSer
detach() voExample
Examples -
A proximity sensor is Soil moisture The Reed switch The Piezo buzzer is a
a sensor able to detect sensors measure (sometimes also known tiny speaker that
25
the presence of nearby the volumetric as Herkon) is an electrical works on the principles
objects without any water content in switch operated by an of Piezoelectricity.
physical contact. soil applied magnetic field.It Certain crystals within
consists of a pair of the device change
contacts on shape when we apply
ferromagnetic metal electricity. By applying
reeds in a hermetically the electrical signal at
sealed glass envelope. the right frequency, a
The contacts may be sound can be
normally open, closing generated from the
when a magnetic field is crystal.
present, or normally
closed and opening when
a magnetic field is
applied.
Exercise Time
SHORT QUESTIONS
1. How many times does setup() run?
2. How many times does loop() run?
3. Expand PWM ?
4. PWM output varies from _ to _ ?
5. The digital pin (6,4,3,2) which does not have PWM capability is ?
6. How many analog ports does an Arduino have?
26
7. Which language is the Arduino IDE written in?
8. In delay(t); t is given in :_____ (sec, millisec,nanosec,microsec)
9. If Serial is begun with Serial.begin(9600); How much time will it take to send 2 ASCII characters?
( 1.667 millisecond,1.9220 millisecond,4.800 millisecond,1.9200 millisecond)
10. Which of these is not an analog input device (pressure sensor: servo,button,potentiometer)?
11. Which of these is an analog input device (pressure sensor,servo,button,LED)?
12. Which of these is an output device(pressure sensor,servo,button,Potentiometer)?
13. The Arduino IDE is derived from which programming languages?
LONG QUESTIONS
1. In your own words, what is a microcontroller?
2. Give three examples of things you can do with a microcontroller.
3. Where is the microcontroller on the Arduino board?
4. What are two good signs that a device has a microcontroller in it?
5. What do you use the Arduino pins for?
6. Where would you go on the Internet to get help with an Arduino problem?
7. What is the difference between the Arduino IDE and Board?
8. How will you know that your sketch is okay?
9. Name three things that contain microcontrollers.
10. What is the Arduino Playground ?
11. What does the IDE Verify button do?
12. How do you mark comments in your sketch?
27