WorskShop Lab 5
WorskShop Lab 5
WorskShop Lab 5
Lab # 04:
Introduction to Arduino
Instructor: Engr. Irfan Ali
Submission Profile
Comments: ______________________________________________________________________
1
Objectives:
All Arduino boards are completely open-source, empowering users to build them independently
and eventually adapt them to their particular needs. The software, too, is open-source, and it is
growing through the contributions of users worldwide.
2
Figure 1: Arduino Hardware and Software
Thanks to its simple and accessible user experience, Arduino has been used in thousands of
different projects and applications. The Arduino software is easy-to-use for beginners, yet
flexible enough for advanced users. It runs on Mac, Windows, and Linux.
Inexpensive - Arduino boards are relatively inexpensive compared to other development
platforms.
Cross-platform - The Arduino Software (IDE) runs on Windows, Macintosh OSX, and
Linux operating systems. Most microcontroller systems are limited to Windows.
User friendly programming environment - The Arduino Software (IDE) is easy-to-use
for beginners, yet flexible enough for advanced users to take advantage of as well. For
teachers, it's conveniently based on the Processing programming environment, so
students learning to program in that environment will be familiar with how the Arduino
IDE works.
Open source and extensible software - The Arduino software is published as open
source tools, available for extension by experienced programmers. The language can be
expanded through C++ libraries, and people wanting to understand the technical details
can make the leap from Arduino to the AVR C programming language on which it's
based. Similarly, you can add AVR-C code directly into your Arduino programs if you
want to.
Open source and extensible hardware - The plans of the Arduino boards are published
under a Creative Commons license, so experienced circuit designers can make their own
version of the module, extending it and improving it.
Arduino Boards – Arduino UNO:
3
There many different variants of Arduino Boards having different capabilities and are
designed for specific applications. We are going to discuss very commonly used board that is
Arduino UNO. Apart from minor variations all boards have similar architecture / Structure.
Reset Button: Pushing it will temporarily connect the reset pin to ground and restart any code
that is loaded on the Arduino.
Power LED Indicator: Just beneath and to the right of the word “UNO” on your circuit board,
there’s a tiny LED next to the word ‘ON’. This LED should light up whenever you plug your
Arduino into a power source. If this light doesn’t turn on, there’s a good chance something is
wrong. Time to re-check your circuit!
TX RX LEDs: TX is short for transmit; RX is short for receive. These markings appear quite a
bit in electronics to indicate the pins responsible for serial communication. In our case, there are
two places on the Arduino UNO where TX and RX appear – once by digital pins 0 and 1, and a
second time next to the TX and RX indicator LEDs. These LEDs will give us some nice visual
indications whenever our Arduino is receiving or transmitting data (like when we’re loading a
new program onto the board).
4
Voltage Regulator: It controls the amount of voltage that is let into the Arduino board. Think of
it as a kind of gatekeeper; it will turn away an extra voltage that might harm the circuit. Of
course, it has its limits, so don’t hook up your Arduino to anything greater than 20 volts.
Main IC – Microcontroller: The black thing with all the metal legs is an IC, or Integrated
Circuit. Think of it as the brains of our Arduino. The main IC on the Arduino is ATMEGA328P.
Different Arduino boards have different ICs but is usually from the ATMEGA line of IC’s from
the ATMEL Company. This can be important, as you may need to know the IC type (along with
your board type) before loading up a new program from the Arduino software.
Digital pins: Use these pins with digitalRead(), digitalWrite(), and analogWrite(). analogWrite()
works only on the pins with the PWM symbol.
Analog in: Use these pins with analogRead().
GND and 5V pins: Use these pins to provide +5V power and ground to your circuits.
Power connector: This is how you power your Arduino when it’s not plugged into a USB port
for power. Can accept voltages between 7-12V.
USB port: Used for powering your Arduino Uno, uploading your sketches to your Arduino, and
for communicating with your Arduino sketch (via Serial. println() etc.).
5
Arduino Software (IDE):
The Arduino Integrated Development Environment - or Arduino Software (IDE) - contains a text
editor for writing code, a message area, a text console, a toolbar with buttons for common
functions and a series of menus. It connects to the Arduino and hardware to upload programs and
communicate with them.
Writing Sketches:
Programs written using Arduino Software (IDE) are called sketches. These sketches are written
in the text editor and are saved with the file extension. ino. The editor has features for
cutting/pasting and for searching/replacing text. The message area gives feedback while saving
and exporting and displays errors. The console displays text output by the Arduino Software
(IDE), including complete error messages and other information. The bottom right-hand corner
of the window displays the configured board and serial port. The toolbar buttons allow you to
verify and upload programs, create, open, and save sketches, and open the serial monitor.
6
Verify - Checks your code for errors compiling it.
Upload - Compiles your code and uploads it to the configured board. See uploading
below
for details.
Note: If you are using an external programmer with your board, you can hold down the
"shift" key on your computer when using this icon. The text will change to "Upload using
Programmer".
Open - Presents a menu of all the sketches in your sketchbook. Clicking one will open it
within the current window overwriting its content.
The first task is to understand the code and create a circuit needed to blink an LED. You will be
using digital input and output to write your first Arduino program.
To complete this task following components will be required:
Arduino UNO
LED
Resistor
Connecting wires & Breadboard
Below you can find the code for "Blink" of LED with comments. Comments are notes in your
code that explain what the code is doing without changing the program. The Arduino language
uses C/C++, so to make a one-line comment, type "//" before a line. If your comment is more
than one line, you can use "/* ... */", for example:
7
/* This is a comment! */
/* So
is this
*/
Code:
// the setup function runs once when you press reset or power the board
// Code for blinking the LED connected at pin 13
/*
The statements in the "setup()" function are executed once when the
program starts.
*/
void setup() {
// initialize digital pin 13 as an output.
pinMode(13, OUTPUT);
}
// The statements in "loop()" are executed sequentially forever.
void loop() {
digitalWrite(13, HIGH); // turn the LED on
delay(1000); // wait for a second
digitalWrite(13, LOW); // turn the LED off
delay(1000); // wait for a second
}
8
Figure 5: Hardware Results
Pushbuttons or switches connect two points in a circuit when you press them. This example turns
on the LED connected on pin 8 when you press the button.
To complete this task following components will be required:
Arduino UNO
LED
Resistor
Switch/Button
Connecting wires & Breadboard
9
Code:
const int buttonPin = 2; // the number of the pushbutton pin const int
ledPin = 8; // the number of the LED pin
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);
10
Make the hardware connections as shown in figure 6.
In this task, we are going to use a Light Dependent Resistor (LDR) which turns on light
automatically when it gets dark and turns off when it gets light. An LDR's resistance changes
depending upon the amount of light hitting the sensor. Used in conjunction with a 4.7K resistor,
11
this forms a simple voltage divider where the voltage across the LDR changes depending upon
the light.
We can then input this into one of the Analog to Digital inputs in the Arduino Uno to measure
the voltage. Then it’s a simple matter of checking whether the value is above or below a
threshold value and to turn one of the outputs on or off.
The circuit diagram is shown below. As the light increases, the LDR's resistance drops and hence
the voltage across it drops. Thus, the voltage across the resistor increases, and the voltage into
the Arduino ADC increases. The opposite is true as it gets darker.
Code:
Lab Tasks
13
Attachments:
Tasks Code
Results
14
15
16
17
18
19
20
21
22