Arduino Programming Lab
Arduino Programming Lab
LAB OBSERVATION
Name:________________________________________
Pin:__________________________________________
Class:_________________ Section:_________________
(R-20)
III-B.Tech : II-Sem
CONTENTS
Exp. Page.
No Date EXPERIMENT NAME
No
INTRODUCTION TO ARDUINO
1. 1
1. INTRODUCTION TO ARDUINO
DESCRIPTION:
ARDUINO UNO
Arduino provides a standard form factor that breaks the functions of the micro-controller into a
more accessible package.
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.
Board Types
The differences are based on the number of inputs and outputs (the number of sensors, LEDs,
and buttons you can use on a single board), speed, operating voltage, form factor etc. Some
boards are designed to be embedded and have no programming interface (hardware), which you
would need to buy separately. Some can run directly from a 3.7V battery, others need at least
5V.
USB via
Arduino Uno R3 5V 16MHz 14 6 6 1
ATMega16U2
USB via
Arduino Uno R3 SMD 5V 16MHz 14 6 6 1
ATMega16U2
FTDI-Compatible
Arduino Ethernet 5V 16MHz 14 6 6 1
Header
FTDI-Compatible
Arduino Fio 3.3V 8MHz 14 8 6 1
Header
Pro micro
5V 16MHz 14 6 6 1 Native USB
5V/16MHz
Pro micro
5V 16MHz 14 6 6 1 Native USB
3.3V/8MHz
LilyPad Arduino
3.3V 8MHz 14 6 6 1 Native USB
USB
FTDI-Compatible
Mega Pro 3.3V 3.3V 8MHz 54 16 14 4
Header
FTDI-Compatible
Mega Pro 5V 5V 16MHz 54 16 14 4
Header
Arduino Mega
3.3V 84MHz 54 12 12 4 USB native
2560 R3
Arduino UNO
We will study the Arduino UNO board because it is the most popular board in the Arduino board
family. In addition, it is the best board to get started with electronics and coding. Some boards
look a bit different from the one given below, but most Arduinos have majority of these
components in common.
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 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 six 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. The main IC (integrated circuit) on the Arduino is slightly different from board to board.
The microcontrollers are usually of the ATMEL Company. You must know what IC your board
has before loading up a new program from the Arduino IDE. This information is available on the
top of the IC. For more details about the IC construction and functions, you can refer to the data
sheet.
ICSP pin
Mostly, ICSP (12) is an AVR, a tiny programming header for the Arduino consisting of MOSI,
MISO, SCK, RESET, VCC, and GND. It is often referred to as an SPI (Serial Peripheral
Interface), which could be considered as an "expansion" of the output. Actually, you are slaving
the output device to the master of the SPI bus.
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.
After learning about the main parts of the Arduino UNO board, we are ready to learn how to set
up the Arduino IDE. Once we learn this, we will be ready to upload our program on the Arduino
board.
In this section, we will learn in easy steps, how to setup the Arduino IDE on our computer and
prepare the board to receive the program via USB cable.
Step 1 − First you must have your Arduino board (you can choose your favorite board) and a
USB cable. In case you use Arduino UNO, Arduino Duemilanove, Nano, Arduino Mega 2560,
or Diecimila, you will need a standard USB cable (A plug to B plug), the kind you would
connect to a USB printer as shown in the following image.
In case you use Arduino Nano, you will need an A to Mini-B cable instead as shown in the
following image.
You can get different versions of Arduino IDE from the Download page on the Arduino Official
website. You must select your software, which is compatible with your operating system
(Windows, IOS, or Linux). After your file download is complete, unzip the file.
The Arduino Uno, Mega, Duemilanove and Arduino Nano automatically draw power from
either, the USB connection to the computer or an external power supply. If you are using an
Arduino Diecimila, you have to make sure that the board is configured to draw power from the
USB connection. The power source is selected with a jumper, a small piece of plastic that fits
onto two of the three pins between the USB and power jacks. Check that it is on the two pins
closest to the USB port.
Connect the Arduino board to your computer using the USB cable. The green power LED
(labeled PWR) should glow.
After your Arduino IDE software is downloaded, you need to unzip the folder. Inside the folder,
you can find the application icon with an infinity label (application.exe). Double-click the icon to
start the IDE.
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.
Here, we have selected Arduino Uno board according to our tutorial, but you must select the
name matching the board that you are using.
Select the serial device of the Arduino board. Go to Tools → Serial Port menu. This is likely to
be COM3 or higher (COM1 and COM2 are usually reserved for hardware serial ports). To find
out, you can disconnect your Arduino board and re-open the menu, the entry that disappears
should be of the Arduino board. Reconnect the board and select that serial port.
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.
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.
Note − If you have an Arduino Mini, NG, or other board, you need to press the reset button
physically on the board, immediately before clicking the upload button on the Arduino Software.
Sketch − The first new terminology is the Arduino program called “sketch”.
Structure
Arduino programs can be divided in three main parts: Structure, Values (variables and
constants), and Functions. In this tutorial, we will learn about the Arduino software program,
step by step, and how we can write the program without any syntax or compilation error.
Let us start with the Structure. Software structure consist of two main functions −
Setup( ) function
Loop( ) function
Void setup ( ) {
PURPOSE − The setup() function is called when a sketch starts. Use it to initialize the
variables, pin modes, start using libraries, etc. The setup function will only run once, after each
power up or reset of the Arduino board.
INPUT − -
OUTPUT − -
RETURN − -
Void Loop ( ) {
}
PURPOSE − 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.
INPUT − -
OUTPUT − -
RETURN − -
Print Page
CIRCUIT DIAGRAM:
APPARATUS:
1. Arduino uno board
2. Temperature sensor LM35
3. 5V Adaptor
4. Bread board
5. USB Cable
6. Jumper Wires
THEORY:
The LM35 Analog Temperature Sensor is a popular option for temperature sensing over the
range of -55 to 150°C.
If you have a microcontroller like an Arduino and you want to measure temperature, the
LM35 is one of the more popular temperature measurement ICs to use. The LM35 is a 3-pin
analog thermometer packaged in a TO-92 package. The output is a linear voltage that
corresponds to 10.0mV/°C, so the output is directly readable in degrees Celsius using an
analog port on a microcontroller. Accuracy is typically within 0.5°C. Since the voltage output
range of the sensor is limited to 1.5V, if a greater resolution is desired, the analog reference
voltage on the uC can be changed from the default 5V to something closer to 1.5V which will
decrease the voltage step size on the analog readings.
PROGRAM:
// Define the analog pin, the LM35's Vout pin is connected to
#define sensorPin A0
void setup() {
// Begin serial communication at 9600 baud rate
Serial.begin(9600);
}
void loop() {
// Get the voltage reading from the LM35
int reading = analogRead(sensorPin);
// Convert that reading into voltage
float voltage = reading * (5.0 / 1024.0);
// Convert the voltage into the temperature in Celsius
float temperatureC = voltage * 100;
// Print the temperature in Celsius
Serial.print("Temperature: ");
Serial.print(temperatureC);
Serial.print("\xC2\xB0"); // shows degree symbol
Serial.print("C | ");
// Print the temperature in Fahrenheit
float temperatureF = (temperatureC * 9.0 / 5.0) + 32.0;
Serial.print(temperatureF);
Serial.print("\xC2\xB0"); // shows degree symbol
Serial.println("F");
OUTPUT:
PROCEDURE:
2. Click on file
3. Click on New
5. Click on Save
6. Click on Verify
7. Click on Upload the code into Arduino Uno by using USB cable.
RESULT:
CIRCUIT DIAGRAM
APPARATUS:
THEORY:
Pulse Width Modulation, or PWM, is a technique for getting analog results with digital means.
Digital control is used to create a square wave, a signal switched between on and off. This on-
off pattern can simulate voltages in between the full Vcc of the board (e.g., 5 V on UNO, 3.3 V
on a MKR board) and off (0 Volts) by changing the portion of the time the signal spends on
versus the time that the signal spends off. The duration of "on time" is called the pulse width.
To get varying analog values, you change, or modulate, that pulse width. If you repeat this on-
off pattern fast enough with an LED for example, the result is as if the signal is a steady
voltage between 0 and Vcc controlling the brightness of the LED.
OUTPUT:
PROGRAM:
void setup() {
// initialize digital pin 13 as an output.
pinMode(9, OUTPUT);
}
PROCEDURE:
2. Click on file
3.Click on New
5. Click on Save
6.Click on Verify
7.Click on Upload the code into Arduino Uno by using USB cable.
RESULT:
APPARATUS:
1. Arduino uno board-1
2. 5V Adaptor
3. Bread board
4. USB Cable
5. Jumper Wires
6. RS232 to TTL connector
7. RS232 to USB cable
PROGRAM:
Serial.begin(9600);
}
void loop() {
Serial.println(my_str);
OUTPUT:
PROCEDURE:
1. Click on Arduino IDE software.
2. Click on file
3. Click on New
5. Click on Save
6. Click on Verify
7. Click on Upload the code into Arduino Uno by using USB cable.
9. Connect arduino board to USB through RS232 cable and select comport on hyper terminal
RESULT:
APPARATUS:
1. Arduino uno board-1
2. RS232 to TTL connector
3. RS232 to USB cable
4. 5V Adaptor
5. Bread board
6. USB Cable
7. Jumper Wires
THEORY:
Strings are used to store text. They can be used to display text on an LCD or in the Arduino IDE
Serial Monitor window. A string is a special array that has one extra element at the end of the
string which always has the value of 0 (zero). This is known as a "null terminated string".The
string can be printed out to the Arduino IDE Serial Monitor window by
using Serial.println() and passing it the name of the string.
PROGRAM-1
char my_str[6]; // an array big enough for a 5 character string
void setup() {
Serial.begin(9600);
my_str[0] = 'H'; // the string consists of 5 characters
my_str[1] = 'e';
my_str[2] = 'l';
my_str[3] = 'l';
my_str[4] = 'o';
my_str[5] = 0; // 6th array element is a null terminator
}
void loop() {
Serial.println(my_str);}
PROGRAM-2
void setup() {
Serial.begin(9600);
void loop() {
Serial.println(my_str);
}
PROCEDURE:
2. Click on file
3. Click on New
5. Click on Save
6. Click on Verify
7. Click on Upload the code into Arduino Uno by using USB cable.
9. Connect arduino board to USB through RS232 cable and select comport on hyper terminal
RESULT:
APPARATUS:
1. Arduino uno board-1
2. 5V Adaptor
3. Bread board
4. USB Cable
5. Jumper Wires
THEORY:
It contains the character, followed by its ASCII code in decimal, hexadecimal, and
sometimes, even octal and binary representations. In this program, we will print out all these
representations for printable ASCII characters. Remember that the first printable ASCII
character starts from number 33, and the printable characters go on up to number 126. Since we
will print out the ASCII table on the Serial Monitor, we will concern ourselves with only
printable characters. Let us begin the walkthrough of this code. As you can see, we begin with
the initialization of Serial in the setup and then wait for the Serial port to connect. After that, we
just print the title of the sketch. Within the loop, we print the value of thisByte as a character
(using Serial.write()), then we print its decimal value (using Serial.print()), and then we print its
hexadecimal, octal and binary representations, using format specifiers in Serial.print().
We later increment the value of this Byte, until we reach the value of 126, after which we enter
an infinite loop, and essentially do nothing.
PROGRAM:
void setup() {
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}
Serial.println("ASCII Table ~ Character Map");
}
// Serial.print(thisByte, DEC);
OUTPUT:
while (true) {
continue;
}
}
// go on to the next character
thisByte++;
delay(500);
}
PROCEDURE:
2. Click on file
3. Click on New
5. Click on Save
6. Click on Verify
7. Click on Upload the code into Arduino Uno by using USB cable.
RESULT:
APPARATUS:
1. Arduino uno board-1
2. RS232 to TTL connector
3. RS232 to USB cable
4. 5V Adaptor
5. Bread board
6. USB Cable
7. Jumper Wires
THEORY:
Communication is of two types, simplex, where the role of the sender and receiver are
fixed (one way) and duplex, where either side is capable of sending and receiving (both ways).
Duplex communication is further divided into half-duplex, where only one side can send data at a
given moment (the other side recieves and waits for their turn) and full-duplex, where both sides
can simultaneously transmit and receive data. The communication between your PC and Arduino
is full-duplex.
A PC has a USB port, which the operating system (like Windows) presents as a virtual
COM (communication) port for the Arduino IDE to use. The Arduino board, on the other end,
has a built-in Serial port along with a USB-UART converter. The converter on the Arduino,
allows outgoing data to be converted to USB, and incoming data to be converted to UART.
The hardware is then abstracted upon via software on both ends. On the Arduino’s side,
the running program uses the Serial library (a collection of functions) to read/write data. The
program you write will use the Serial library’s functions and methods to be able to use the port.
On the PC, the user can see the data received from Arduino or send back data using
the Serial monitor built in to the Arduino IDE. The Serial monitor is a program that ships with
the Arduino IDE and allows you to view the data (and also send some back) which is being sent
on the virtual Serial port which your Arduino connects to. It can be opened by going to
Tools>Serial monitor
OUTPUT
PROGRAM:
#include <LiquidCrystal.h>
const int rs = 2, en = 3, d4 = 4, d5 = 5, d6 = 6, d7 = 7;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
char rx_byte = 0;
String rx_str = "";
void setup() {
Serial.begin(9600);
lcd.begin(16,2);
//Serial.println("Enter your Data");
lcd.setCursor(0,0);
lcd.print("Enter Your Data");
}
void loop() {
if (Serial.available() > 0) {
rx_byte = Serial.read();
if (rx_byte != '\n') {
rx_str += rx_byte;
}
else {
// end of string
Serial.println(rx_str);
lcd.setCursor(0,1);
lcd.print(rx_str);
rx_str = "";
Serial.println("");
Serial.println("Enter Your Data");
}
}
PROCEDURE:
2. Click on file
3. Click on New
5. Click on Save
6. Click on Verify
7. Click on Upload the code into Arduino Uno by using USB cable.
Connect arduino board to USB through RS232 cable and select comport on HyperTerminal
RESULT:
CIRCUIT DIAGRAM:
APPARATUS:
1. Arduino uno board-1
2. 5V Adaptor
3. Bread board
4. Resistors 10k-8 NO
5. Resistors 22k-8 NO
6. USB Cable
7. Jumper Wires
THEORY:
Digital to Analog Converter (DAC) is a system that converts digital signal to its corresponding
analog signal. The R-2R Ladder is one of the most popular DAC converters because of its
simplicity. It utilizes only two values of resistors and it can be extended to any number of
bits. The number of levels are equal to two power the number of bits. In this project, 8 bits means
there will be 256 levels. We can calculate the maximum voltage output by this equation.
and the voltage output equation for R-2R DAC itself is this :
As you can see in the circuit, there are 8 digital input from b0 to b7 that represent each bit. It’s in
the format of LSB. I used R = 1k ohm resistors because It’s the one that I already have so many
and I can get 2R by series 1k ohm resistor to be 2k ohm easily.
From the simulation, we know that the highest logic 11111111 (or 255 in decimal) is giving
4.96V at the output voltage and the 00000000 (or 0 in decimal) logic gives 0V. And 10000000
(128 in decimal) gives exactly 2.5V. This result proves that the Voltage output of the R-2R
Ladder DAC Equation is right.
OUTPUT:
SINE WAVE:
RAMP SIGNAL:
SINE WAVE:
PROGRAM:
int valSin;
void setup() {
Serial.begin(9600);
DDRD = B11111111; // Port D at Arduino Uno (pin 0-7)
}
void loop() {
for (int i = 0; i < 360; i++) { //looping the value of i from 0 to 360 degree
valSin = ((sin(i * DEG_TO_RAD) + 1) * 255) / 2; // creating the sinwave from i, 255 is the highest
value in 8 bit (0-255)
PORTD = valSin; // assign the sinwave to portD
delay(10); // set the delay of the analog value
Serial.println(analogRead(A0));
}
}
RAMP SIGNAL
PROGRAM:
void setup()
{
Serial.begin(9600);
DDRD = B11111111; // Port D at Arduino Uno (pin 0-7)
}
void loop()
{
for ( int i = 0; i < 256; i++) { //looping the value of i from 0 to 255, creating a ramp wave from 0 to 255,
it's more like stairs wave
PORTD = i; // the integer value of i is automatically translated to binary and assign to port D, ex 3 is
automatically transformed into 00000011 which means only pin 0 and pin 1 is high (1) and the other pin
is low (0)
delay(10); // set the delay of the analog value
Serial.println(analogRead(A0));
}
PROCEDURE:
2. Click on file
3. Click on New
5. Click on Save
6. Click on Verify
7. Click on Upload the code into Arduino Uno by using USB cable.
RESULT:
CIRCUIT DIAGRAM:
APPARATUS:
1. Arduino uno board-1
2. Stepper motor(28BYJ-48)-1
3. Motor driver(DRV8825)-1
4. 5V Adaptor
5. Bread board
6. USB Cable
7. Jumper Wires
THEORY:
DRV8825
The DRV8825 stepper motor driver carrier is a breakout board for TI’s DRV8825 micro-stepping
bipolar stepper motor driver. The DRV8825 features adjustable current limiting, over-current and over
temperature protection, and six microstep resolutions (down to 1/32-step). It operates from 8.2 – 45 V and
can deliver up to approximately 1.5 A per phase without a heat sink or forced air flow (rated for up to 2.2
A per coil with sufficient additional cooling).
The DRV8825 driver has three step size (resolution) selector inputs: M0, M1 and M2 . By setting
the appropriate logic levels for these pins, we can set the motor to one of six step resolutions
M0 M1 M2 Microstep Resolution
STEP input controls the microsteps of the motor. Each HIGH pulse sent to this pin drives the
motor according to the number of microsteps determined by the microstep selection pins. The
higher the pulse frequency, the faster the motor will spin.
DIR input controls the spinning direction of the motor. Pulling it HIGH turns the motor
clockwise, while pulling it LOW turns it counterclockwise.
If you want the motor to only turn in one direction, you can connect the DIR directly to VCC or
GND.
EN is an active low input pin. When this pin is pulled LOW, the DRV8825 driver is enabled. By
default, this pin is pulled low, so unless you pull it high, the driver is always enabled. This pin is
particularly useful when implementing an emergency stop or shutdown system.
SLP is an active low input pin. Pulling this pin LOW puts the driver into sleep mode, reducing
power consumption to a minimum. You can use this to save power, especially when the motor is
not in use.
RST is an active low input as well. When this pin is pulled LOW, all STEP inputs are ignored. It
also resets the driver by setting the internal translator to a predefined “home” state. Home state is
basically the initial position from which the motor starts, and it varies based on microstep
resolution.
28BYJ-48 stepper
The 28BYJ-48 stepper motor is a commonly used stepper motor. which converts electrical pulses
into discrete mechanical rotation. When electrical signals are applied, the stepper motor rotates
in precise and fixed angle increments known as steps. The motor consists of 4 coils that make a
ring around the rotor. These coils are known as the stator, as they are stationary and static. Each
coil is rated at +5V, making it easy to control with any microcontroller, such as an Arduino.
PROGRAM:
//------------------------------
void setup() {
//------------------------------
void loop() {
delay(delay2);
digitalWrite(DirPin, LOW); // set direction
// Rotate stepper rotatations revolutions, using full step method
digitalWrite(M0Pin, LOW); // define step size
digitalWrite(M1Pin, LOW);
digitalWrite(M2Pin, LOW);
PROCEDURE:
2. Click on file
3. Click on New
5. Click on Save
6. Click on Verify
7. Click on Upload the code into Arduino Uno by using USB cable.
9. Observe output
CIRCUIT DIAGRAM:
APPARATUS:
THEORY:
Accelerometers are widely used in low-power, low-cost motion and tilt sensing
applications such as mobile devices, gaming systems, disk drive protection, image stabilization,
and sports and health devices.
At the core of the module is a small, low-power, low-noise triple axis MEMS
accelerometer from Analog Devices – ADXL335. It can measure not only static acceleration
caused by gravity, but also dynamic acceleration caused by motion, shock, or vibration. This
breadboard-friendly module breaks out every pin of the ADXL335 to a 6-pin, 0.1′′ pitch header,
including 3 analog outputs for X, Y, and Z axis measurements, 2 supply pins, and a self-test pin.
The ADXL335 operates on 1.8V to 3.6VDC (typically 3.3V). However, the on-board
3.3V regulator makes it ideal for interfacing with 5V microcontrollers like the Arduino.
The ADXL335 has a full sensing range of ±3g. Meaning the maximum amount of
acceleration that the ADXL335 can accurately measure and represent as an output is ±3g. If it is
accelerated at 4g, for example, the accelerometer will not break, but the output may rail.
OUTPUT:
PROGRAM:
void setup() {
Serial.begin(9600);
}
void loop() {
Serial.print("X = : ");
Serial.print(analogRead(xpin));
Serial.print("\t");
Serial.print("\t");
Serial.print("Y = : ");
Serial.print(analogRead(ypin));
Serial.print("\t");
Serial.print("\t");
Serial.print("Z = : ");
Serial.print(analogRead(zpin));
Serial.println();
delay(500);
}
PROCEDURE:
2. Click on file
3.Click on New
5. Click on Save
6. Click on Verify
7. Click on Upload the code into Arduino Uno by using USB cable.
9. Connect arduino board to USB through RS232 cable and select comport on hyper terminal
10. And move the accelerometer in different directions and observe values on hyper terminal.
RESULT: