Nothing Special   »   [go: up one dir, main page]

Lecture Notes 1

Download as pdf or txt
Download as pdf or txt
You are on page 1of 59

Advanced Microcontrollers &

Applications (EC-506)

Class: ECE- 5th Semester


Academic session: Aug-Dec,
2022
(SECTION-B)
Open Source Embedded
Development Board
(Arduino)
Introduction to Arduino
• Arduino is a microcontroller based kit
• Purchased from the vendor or can be
made at home using the components.
• It is basically used in communications and
in controlling or operating many devices.
• It was founded by Massimo Banzi and
David Cuartielles in 2005.
Introduction to Arduino
• Arduino is an open source platform used for building
electronics projects.

• Arduino consists of both a physical programmable circuit


board or microcontroller and a software, IDE (Integrated
Development Environment) that runs on the computer.

• It is used to write and upload computer code to the physical


board.

• Arduino boards are equipped with sets of digital and analog


input/output pins, USB connection which is used for loading
programs from computers, power jack, reset button etc.
Arduino IDE Features
• It is an open source software.
• Easy to write code and upload it to the
physical board.
• Easy to learn programming language with its
inbuilt functions.
• Runs on windows, Mac OS and Linux.
• This software can be used with any Arduino
board.
Features of Arduino
• 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.

• 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).

• Unlike most previous programmable circuit boards, Arduino does not need
an extra piece of hardware (called a programmer) in order to load a new
code onto the board. You can simply use a USB cable.

• Additionally, the Arduino IDE uses a simplified version of C++, making it


easier to learn to program.

• Finally, Arduino provides a standard form factor that breaks the functions of
the micro-controller into a more accessible package.
Reasons why Arduino is being preferred
1. It is inexpensive

2. It comes with an open source hardware feature which enables users to develop their own
kit using already available one as a reference source.

3. The Arduino software is compatible with all types of operating systems like Windows,
Linux, and Macintosh etc.

4. It also comes with open source software feature which enables experienced software
developers to use the Arduino code to merge with the existing programming language
libraries and can be extended and modified.

5. It is easy to use for beginners.

6. We can develop an Arduino based project which can be completely stand alone or projects
which involve direct communication with the software loaded in the computer.

7. It comes with an easy provision of connecting with the CPU of the computer using serial
communication over USB as it contains built in power and reset circuitry.
The Many Flavors of Arduino

• Arduino Uno
• Arduino Leonardo
• Arduino LilyPad
• Arduino Mega
• Arduino Nano
• Arduino Mini
• Arduino Mini Pro
• Arduino BT
Arduino-like Systems

• Cortino (ARM)
• Xduino (ARM)
• LeafLabs Maple
(ARM)
• BeagleBoard (Linux)
• Wiring Board
(Arduino
predecessor)
Arduino Add-ons (Shields)
• TFT Touch Screen
• Data logger
• Motor/Servo shield
• Ethernet shield
• Audio wave shield
• Cellular/GSM shield
• WiFi shield
• ...many more
Where to Get an Arduino Board

• Purchase from online vendor (available


worldwide)
– Sparkfun
– Adafruit
– DFRobot
• ... or build your own
– PC board
– Solderless breadboard http://itp.nyu.edu/physcomp/Tutorials/ArduinoBreadboard
Arduino Uno
Original Arduino with RS-232
Other Hardware Choices-Sheilds
Xbee Shield
The Xbee shield allows an Arduino board to communicate
wirelessly using Zigbee. The module can communicate up to 100 feet
indoors or 300 feet outdoors (with line-of-sight). It can be used as
a serial/usb replacement or you can put it into
a command mode and configure it for a variety of broadcast
and mesh networking options.
The Xbee shield was created in collaboration with Libelium, who
developed it for use in their SquidBee motes (used for creating sensor
networks).
Adafruit Servo/Stepper/DC Motor shield
A shield that can control 2 hobby servos and up to 2
unipolar/bipolar stepper motors or 4 bi-directional DC motors.
Battery Shield
A shield from Liquidware that connects to the back of the
Arduino, with a USB- rechargable lithium ion battery that can power an
Arduino for 14-28 hours depending on the circuit
Continue
Adafruit Wave shield
Plays any size audio files from an SD memory
card for music, effects and interactive sound art
Adafruit GPS & Datalogging shield
Connects up a GPS module and can log
location, time/date as well as sensor data to an
SD memory flash card.
Adafruit XPort/Ethernet shield
Allows use of an XPort module for connecting to
the Internet as a client or server.
Continue…
Liquidware TouchShield
OLED touch screen shield.
Adafruit Servo/Stepper/DC Motor shield
A shield that can control 2 hobby servos and up
to 2 unipolar/bipolar stepper motors or 4 bi-
directional DC motors.
Arduino on breadboard
Basic Arduino UNO board
(Hardware)
Basic Arduino UNO board (Hardware)

1 Power USB connector.


2 Power port (Barrel Jack).
3 Voltage Regulator.
4 Crystal oscillator.
5 Reset switch.
6,7,8,9 Pins (3.3,5 GND, Vin)
10 Analog input pins.
11 Main Microcontroller.
12 ICSP Pin.
13 Power LED Indicator
14 Tx and Rx LEDs
15 Digital I/O pins.
16 AREF.
Arduino UNO board with Atmega328
Microcontroller
Digital Pins
In addition to the specific functions listed below, the
digital pins on an Arduino board can be used for
general purpose input and output via the pinMode(),
digitalRead(), and digitalWrite() commands. Each pin
has an internal pull-up resistor which can be turned on
and off using digitalWrite() (w/ a value of HIGH or
LOW, respectively) when the pin is configured as an
input. The maximum current per pin is 40 mA.
Serial: 0 (RX) and 1 (TX). Used to receive (RX) and transmit (TX) TTL
serial data. On the Arduino Diecimila, these pins are connected to the
corresponding pins of the FTDI USB-to-TTL Serial chip.

External Interrupts: 2 and 3. These pins can be configured to trigger an


interrupt on a low value, a rising or falling edge, or a change in value. See
the attachInterrupt() function fordetails.

PWM: 3, 5, 6, 9, 10, and 11. Provide 8-bit PWM output with the
analogWrite() function. On boards with an ATmega8, PWM output is
available only on pins 9, 10,and 11.

Reset: 7. (Arduino BT-only) Connected to the reset line of the bluetooth


module.

SPI: 10 (SS), 11 (MOSI), 12 (MISO), 13 (SCK). These pins support SPI


communication, which, although provided by the underlying hardware. It
allow serial communication with another interface by connecting it serially.

LED: 13. There is a built-in LED connected to digital pin 13. When the pin is
HIGH value, the LED is on, when the pin is LOW, it's off.
Analog Pins
In addition to the specific functions listed below,
the analog input pins support 10-bit analog-to-digital
conversion (ADC) using the analogRead() function.
Most of the analog inputs can also be used as digital
pins: analog input 0 as digital pin 14 through analog
input 5 as digital pin 19. Analog inputs 6 and 7
(present on the Mini and BT) cannot be used as digital
pins.
I2C: 4 (SDA) and 5 (SCL). Support I2C (TWI)
communication using the Wire library
Power Pins
VIN (sometimes labelled "9V"). The input voltage to the Arduino board
when it's using an external power source (as opposed to 5 volts from the USB
connection or other regulated power source). You can supply voltage
through this pin, or, if supplying voltage via the power jack, access it through
this pin. Note that different boards accept different input voltages ranges,
please see the documentation for your board. Also note that the LilyPad has
no VIN pin and accepts only a regulated input.

5V. The regulated power supply used to power the microcontroller and
other components on the board. This can come either from VIN via an on-
board regulator, or besupplied by USB or another regulated 5V supply.

3V3. A 3.3 volt supply generated by the on- board FTDI chip.

GND. Ground pins.


Other Pins
AREF. Reference voltage for the analog inputs. It
provide the analog reference voltage for analog to
digital conversion

Reset. Bring this line LOW to reset the


microcontroller.
Block Diagram of
arduino
Function of Each block
USB Plug: By using this plug we can download hex file of
our program by connecting it with PC. It also provide
supply to arduino board. This USB plug is connected with
FTDI Chip internally

FTDI Chip: Future Technology Device International Chip:


This chip is nothing but voltage level converter chip. This chip
is used to convert USB voltage level to TTL level and vice
versa.

Digital I/O and PWM section: This section is used as for


general purpose input output usage. By using this section we
can perform digital input and output functions. In this
section some pins are also used as PWM output pins. It is
used for PWM based application like servo motors.
Continue…
Serial Tx and Rx Section: These two pins are used for serial communication
in arduino. It is internally connected with USB plug through FTDI chip.

Power Supply: Power supply section provide different voltage level to different
part of arduino board. This part contain bridge rectifier and voltage regulator.

AVR Atmega CPU: This block is the heart of the controller. Generally Atmega
8, Atmega 16, Atmega 32, Atmega 328 controller are used in arduino board.
This block execute all the fuctions that written in arduinoprogram.

SPI Interface section: This section is used for serial peripheral interfacing.

Power Pins section: This section include various voltage level


pins including ground pins.

Analog input section: We know that Atmega 8 CPU has inbuilt 6 channel 10
– bit ADC. This ADC can convert analog data from analog pin into digital. So,
for taking analog value for digital conversion we have to use this section.
Circuit Diagram of Arduino
Atmega328 Microcontroller and its pin
mapping with Arduino board pins
Arduino Architecture
• Based on Harvard architecture where the program code and
program data have separate memory.

• It consists of two memories- Program memory and the data


memory.

• The code is stored in the flash program memory, whereas the


data is stored in the data memory.

• The Atmega328 has 32 KB of flash memory for storing code (of


which 0.5 KB is used for the bootloader), 2 KB of SRAM and 1
KB of EEPROM

• Operates with a clock speed of 16MHz.


Arduino Architecture
Features of the Arduino UNO:

• Microcontroller: ATmega328
• Operating Voltage: 5V
• Input Voltage (recommended): 7-12V
• Input Voltage (limits): 6-20V
• Digital I/O Pins: 14 (of which 6 provide PWM output)
• Analog Input Pins: 6
• DC Current per I/O Pin: 40 mA
• DC Current for 3.3V Pin: 50 mA
• Flash Memory: 32 KB of which 0.5 KB used by bootloader
• SRAM: 2 KB (ATmega328)
• EEPROM: 1 KB (ATmega328)
• Clock Speed: 16 MHz
Arduino IDE ( Software)

• A program written with the IDE for Arduino is


called a sketch.

• The IDE enables 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
as uploading).
Arduino IDE (Software)
• The Arduino IDE supports the languages C and C++
using special rules of code structuring.

• It consists of only two functions, setup and loop.

• The setup function is used to initialize variables, input


and output pin modes and other libraries needed in the
sketch.

• After setup has been called, function loop is executed


repeatedly in the main program. It controls the board until
the board is powered off or reset.
How to program an Arduino?

• Programs can be directly loaded to the device without requiring any


hardware programmer to burn the program.

• 0.5KB of Bootloader- allows the program to be burned into the circuit.

• It is the first program which executes when a device is connected to a power


supply.

• All we have to do is to download the Arduino software and writing the


code.
Arduino IDE (Software)
• Series of menus like the
File, Edit, Tools menu
• Buttons like verify,
upload, new, open, save,
serial monitor.
• Text editor to write the
code
• Text console and
Message area which
displays the output and
the feedback like
showing the errors
Arduino IDE (Software)
Steps to program an Arduino
•Programs written in Arduino are known as sketches. A basic sketch
consists of 3 parts
1. Declaration of Variables
2. Initialization: It is written in the setup () function.
3. Control code: It is written in the loop () function

The sketch is saved with .ino extension. Because INO files are plain text files,
you can also open them in any text editor, including:
Notepad++ (Windows) Apple TextEdit (Mac) GitHub Atom (cross-platform)

•Any operations like verifying, opening a sketch, saving a sketch can be


done using the buttons on the toolbar or using the tool menu.
•The sketch should be stored in the sketchbook directory.
•Choose the proper board from the tools menu and the serial port
numbers.
•Click on the upload button or choose upload from the tools menu. Thus
the code is uploaded by the bootloader onto the microcontroller.
Board Type
Serial Port / COM Port
Few of basic Adruino functions are:

• digitalRead(pin): Reads the digital value at the given


pin.
• digitalWrite(pin, value): Writes the digital value to the
given pin.
• pinMode(pin, mode): Sets the pin to input or output
mode.
• analogRead(pin): Reads and returns the value.
• analogWrite(pin, value): Writes the value to that pin.
• serial.begin(baud rate): Sets the beginning of serial
communication by setting the bit rate.
Serial Communication - Writing
IMPORTANT: • Serial.begin(baud)
USB serial
communication is Initialize serial port for communication (and sets baud
shared with rate) Note: Serial.end() command
Arduino pins 0 Example: is usually unnecessary, unless you
and 1 (RX/TX) need to use pins 0 & 1
– Serial .beg in (9600); // 9600 baud

Format can be:• S e r i a l . p r i n t ( v a l ) , Se r i a l . p r i n t ( v a l , f m t )


BIN, HEX, OCT, Prints data to the serial port
or an integer
specifying the
Examples:
number of digits – Serial.print(“Hi”); // p r i n t a s t r i n g
to display – Serial.print(78); // works with numbers, too
– S e r i a l.p r i n t (va r i abl e ) ; // works with v a r i a b l e s
– Serial.print(78,BIN); // w i l l p r i n t 1001110

• Serial.println(val)
Same as S e r i a l . p r i n t ( ) , but with line-feed
Serial Communication -Reading
• Serial.available()
Returns the number of bytes available to be read, if any
Example:
i f ( S e r i a l . a v a i l a b l e ( ) > 0) {
data = S e ri a l . re a d ( ) ;
}

To read data from serial port:


• l e t t e r = Serial .read ()
• l e t t e r s = Serial.readBytesUntil (character, buffer, length)
• number = Se r i a l . p a r s e I n t ( )
• number = Serial .parseFloat ()
Activity 1: LED Blink
• Load the “Blink” example
(File→Examples→Basics→Blink)
void setup() {
Use pin 13 as digital output // i n i t i a l i z e the d i g i t a l pin as an output.
// Pin 13 has an LED connected on most Arduino boards:
pinMode(13, OUTPUT);
Set output high (+5V) }

void loop() {
di g i t a lW r i t e ( 1 3 , HIGH); // s et the LED on
Wait 1000 milliseconds delay(1000); // wait f o r a second
di g i t a lW r i t e ( 1 3 , LOW); // set the LED o f f
delay(1000); // wait f o r a second
Set output low (0V) }

• Compile, then upload the program


• Congratulations! you are now blinkers!
Now connect your own LED
LED:

Notes:
• Resistor is needed to limit current
• Resistor and LED may be
interchanged
(but polarity of LED is important)
http://www.wikipedia.org/ • Pin 13 is special: has built-in
resistor and LED
• Change program and upload
Example: Using a Solderless
Breadboard
Comments
• Comments can be anywhere
• Comments created with // or /* and */
• // is used for single linecomment.
• /* */ is used for multiline
comments
• Comments do not affect code
• You may not need comments, but
think about the community!
Programming Symbols
/ / - Single line comment
/* */ - Multiline comment
{ } –used to define a block of code that starts andends.
; - used to define the end of a line of code.
Numeric Data Type in Arduino Programming
Types of operators
• Arithmetic Operators
• Comparison Operators
• Boolean Operators
• Bitwise Operators
• Compound Operators
Arithmetic Operators
Comparison Operators
Boolean Operators
Bitwise Operators
Compound Operators
Arduino programming Language

You might also like