Introduction To Arduino
Introduction To Arduino
The Arduino environment has been designed to be easy to use for beginners who have no
software or electronics experience. With Arduino, you can build objects that can respond to
and/or control light, sound, touch, and movement. Arduino has been used to create an amazing
variety of things, including musical instruments, robots, light sculptures, games, interactive
furniture, and even interactive clothing.
Arduino is used in many educational programs around the world, particularly by
designers and artists who want to easily create prototypes but do not need a deep understanding
of the technical details behind their creations. Because it is designed to be used by nontechnical
people, the software includes plenty of example code to demonstrate how to use the Arduino
board’s various facilities. Though it is easy to use, Arduino’s underlying hardware works at the
same level of sophistication that engineers employ to build embedded devices. People already
working with microcontrollers are also attracted to Arduino because of its agile development
capabilities and its facility for quick implementation of ideas. Arduino is best known for its
hardware, but you also need software to program that hardware. Both the hardware and the
software are called “Arduino.” The combination enables you to create projects that sense and
control the physical world. The software is free, open source, and cross-platform. The boards
are inexpensive to buy, or you can build your own (the hardware designs are also open source).
In addition, there is an active and supportive Arduino community that is accessible worldwide
through the Arduino forums and the wiki (known as the Arduino Playground). The forums and
the wiki offer project development examples and solutions to problems that can provide
inspiration and assistance as you pursue your own projects.
Arduino Software
Software programs, called sketches, are created on a computer using the Arduino integrated
development environment (IDE). The IDE enables you to write and edit code and convert this
code into instructions that Arduino hardware understands. The IDE also transfers those
instructions to the Arduino board (a process called uploading).
Arduino Hardware
The Arduino board is where the code you write is executed. The board can only control and
respond to electricity, so specific components are attached to it to enable it to interact with the
real world. These components can be sensors, which convert some aspect of the physical world
to electricity so that the board can sense it, or actuators, which get electricity from the board
and convert it into something that changes the world. Examples of sensors include switches,
accelerometers, and ultrasound distance sensors. Actuators are things like lights and LEDs,
speakers, motors, and displays.
Arduino Types
1- Arduino UNO
Microcontroller ATmega328P
Operating Voltage 5V
Input Voltage (recommended) 7-12V
Input Voltage (limit) 6-20V
Digital I/O Pins 14 (of which 6 provide PWM output)
PWM Digital I/O Pins 6
Analog Input Pins 6
DC Current per I/O Pin 20 mA
DC Current for 3.3V Pin 50 mA
Flash Memory 32 KB (ATmega328P)
of which 0.5 KB used by bootloader
SRAM 2 KB (ATmega328P)
EEPROM 1 KB (ATmega328P)
Clock Speed 16 MHz
LED_BUILTIN 13
Length 68.6 mm
Width 53.4 mm
Weight 25 g
2- Arduino Nano
4- Cheapduino
Although the int (short for integer, a 16-bit value in Arduino) data type is the most common
choice for the numeric values encountered in Arduino applications, you can use Arduino data
type to determine the data type that fits the range of values your application expects.
Discussion
Except in situations where maximum performance or memory efficiency is required, variables
declared using int will be suitable for numeric values if the values do not exceed the range
(shown in the first row in Arduino data type) and if you don’t need to work with fractional
values. Most of the official Arduino example code declares numeric variables as int. But
sometimes you do need to choose a type that specifically suits your application.
Sometimes you need negative numbers and sometimes you don’t, so numeric types
come in two varieties: signed and unsigned. unsigned values are always positive. Variables
without the keyword unsigned in front are signed so that they can represent negative and
positive values. One reason to use unsigned values is when the range of signed values will not
fit the range of the variable (an unsigned variable has twice the capacity of a signed variable).
Another reason programmers choose to use unsigned types is to clearly indicate to people
reading the code that the value expected will never be a negative number.
boolean types have two possible values: true or false. They are commonly used for
things like checking the state of a switch (if it’s pressed or not). You can also use HIGH and
LOW as equivalents to true and false where this makes more sense; digitalWrite(pin, HIGH)
is a more expressive way to turn on an LED than digitalWrite(pin, true) or
digitalWrite(pin,1), although all of these are treated identically when the sketch actually runs,
and you are likely to come across all of these forms in code posted on the Web.
Logical operators
Bit operators
Here is a sketch that demonstrates the example values shown in Table of Bit operators
//*
* bits sketch
* demonstrates bitwise operators
*//
void setup() {
Serial.begin(9600);
}
void loop(){
Serial.print("3 & 1 equals "); // bitwise And 3 and 1
Serial.print(3 & 1); // print the result
Serial.print(" decimal, or in binary: ");
Serial.println(3 & 1 , BIN); // print the binary representation of the result
Serial.print("3 | 1 equals "); // bitwise Or 3 and 1
Serial.print(3 | 1 );
Serial.print(" decimal, or in binary: ");
Serial.println(3 | 1 , BIN); // print the binary representation of the result
Serial.print("3 ^ 1 equals "); // bitwise exclusive or 3 and 1
Serial.print(3 ^ 1);
Serial.print(" decimal, or in binary: ");
Serial.println(3 ^ 1 , BIN); // print the binary representation of the result
byte byteVal = 1;
int intVal = 1;
byteVal = ~byteVal; // do the bitwise negate
intVal = ~intVal;
Serial.print("~byteVal (1) equals "); // bitwise negate an 8 bit value
Serial.println(byteVal, BIN); // print the binary representation of the result
Serial.print("~intVal (1) equals "); // bitwise negate a 16 bit value
Serial.println(intVal, BIN); // print the binary representation of the result
delay(10000);
}
Discussion
Bitwise operators are used to set or test bits. When you “And” or “Or” two values, the operator
works on each individual bit. It is easier to see how this works by looking at the binary
representation of the values. Decimal 3 is binary 00000011, and decimal 1 is 00000001. Bitwise
And operates on each bit. The rightmost bits are both 1, so the result of And-ing these is 1.
Moving to the left, the next bits are 1 and 0; And-ing these results in 0. All the remaining bits
are 0, so the bitwise result of these will be 0. In other words, for each bit position where there
is a 1 in both places, the result will have a 1; otherwise, it will have a 0. So, 11 & 01 equals 1.
Compound operators
Compound bitwise AND (&=)
x &= y; // equivalent to x = x & y;
Parameters
x: a char, int or long variable
y: an integer constant or char, int, or long
Example1:
First, a review of the Bitwise AND (&) operator
0 0 1 1 operand1
0 1 0 1 operand2
----------
0 0 0 1 (operand1 & operand2) - returned result
Example2:
1 0 1 0 1 0 1 0 variable
1 1 1 1 1 1 0 0 mask
----------------------
1 0 1 0 1 0 0 0
Here is the same representation with the variable's bits replaced with the symbol x
x x x x x x x x variable
1 1 1 1 1 1 0 0 mask
----------------------
x x x x x x 0 0
So if:
myByte = B10101010;
myByte &= B11111100 ==
B10101000;
Parameters
x: a char, int or long variable
y: an integer constant or char, int, or long
Example1:
First, a review of the Bitwise OR (|) operator
0 0 1 1 operand1
0 1 0 1 operand2
----------
0 1 1 1 (operand1 | operand2) - returned result
Example2:
1 0 1 0 1 0 1 0 variable
0 0 0 0 0 0 1 1 mask
----------------------
1 0 1 0 1 0 1 1
Here is the same representation with the variables bits replaced with the symbol x
x x x x x x x x variable
0 0 0 0 0 0 1 1 mask
----------------------
x x x x x x 1 1
So if:
myByte = B10101010;
myByte |= B00000011 ==
B10101011;