0% found this document useful (0 votes)
30 views

Microprocessor and Microcontroller Lab Manual: Government Engineering College Bharuch

This document introduces the Arduino Uno open source microcontroller board. It describes the hardware features of the Arduino Uno board such as the ATmega328 microcontroller, 14 digital input/output pins, 6 analog inputs, 16MHz clock speed, and power supply options. The purpose of the first experiment is to understand the Arduino Uno board and its programming environment.

Uploaded by

wazir172408
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
30 views

Microprocessor and Microcontroller Lab Manual: Government Engineering College Bharuch

This document introduces the Arduino Uno open source microcontroller board. It describes the hardware features of the Arduino Uno board such as the ATmega328 microcontroller, 14 digital input/output pins, 6 analog inputs, 16MHz clock speed, and power supply options. The purpose of the first experiment is to understand the Arduino Uno board and its programming environment.

Uploaded by

wazir172408
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 62

Microprocessor and Microcontroller

Lab Manual

Year : 2022 - 2023


Subject Code : 3141008
Class : B. E. IV Semester
Branch : Electronics & Communication

ELECTRONICS & COMMUNICATION ENGINEERING

GOVERNMENT ENGINEERING COLLEGE


BHARUCH
GOVERNMENT ENGINEERING COLLEGE
BHARUCH

CERTIFICATE

Mr./Miss __________________________________________ of _________________________


Department, _________Class, ________________________Enrollment No. has
satisfactorily completed his/her term work in
_____________________________________ subject for the term ending in
______________ month of _____________________academic year.

Date:

Signature of Faculty Head of the Department


List of Experiments

Student Performance Evaluation

Participation (2)

Experiments (2)
Implementation
Punctuality (2)

Ability & Logic


Programming

Readability &

Program (2)
Efficiency of
Sr. Start End

Output of
Name of Experiments Marks Sign

Active
No Date Date

(2)
Understand arduino UNO open

1 source hardware and


programming environment.
Write program to blink LED
2
using Arduino UNO.
Interface switch and LED to
arduino UNO and write a
3
program to turn on LED by
pressing switch.
Interface LCD with arduino
UNO and write program to
4
display some string on LCD
display.
Generate PWM waveform and

5 change intensity of LED


connected with Arduino board.
Write and execute Arduino
program to read analog value.

6 Sense temperature using LM35


sensor and display temperature
value on LCD.
Introduction to AVR Studio
7
Simulator.
Program the AVR ATmega32
8
Microcontroller.
Interfacing of LED with
ATmega32 microcontroller.
1. Write a C program to blink a

9 single LED.
2. Write a C program to blink 8
LED.
3. Write a C program for
scrolling of 8 LED.
4. Write a C program to display
8-bit binary counter value on
8 LED.
Interfacing of switch and LED
with ATmega32
microcontroller.
1. Write a C program to blink
LED by pressing a switch.

10 2. Write a C program to toggle


LED by pressing the same
switch.
3. Write a C program to blink
alternate LED by pressing
the same switch.
Interfacing of seven segment
display with ATmega32
microcontroller.
1. Write a C program to display

11 0 to 9 on single seven
segment.
2. Write a C program to display
00 to 99 on two seven
segment.
Interfacing of LCD with
ATmega32 microcontroller.
A. Program the LCD in 4-bit
mode.
1. Write a C program for display
some message on LCD.
2. Write a C program for
scrolling the message on
12
LCD.
3. Write a C program for LCD
counter.
B. Program the LCD in 8-bit
mode.
1. Write a C program for display
some message on LCD.
2. Write a C program for
scrolling the message on
LCD.
3. Write a C program for LCD
counter.
Interfacing of 4x4 keypad with
13
ATmega32 microcontroller.
Interfacing of ADC with
ATmega32 microcontroller.
1. Write a C program to use
single channel ADC in
14
ATmega32.
2. Write a C program to
interface Temperature
sensor LM35.
Establish Serial Communication
in ATmega32.
1. Write a C program to receive
and transmit character
through serial
communication.
2. Write a C program to receive
15
and transmit character
through serial
communication and also
display the same on LCD.
3. Write a C program to receive
and transmit string through
serial communication.
Program Timer/Counter in
ATmega32.
1. Write a C program to toggle
LED every 100msec by
generating delay using
Timer0.
16
2. Write a C program to turn on
LED connected to PB3/OC0
pin after 4 external clock
pulse.
3. Write a C program to toggle
LED connected to PB3/OC0
pin after 4 external clock
pulse.
4. Write a C Program to
generate square wave by
using Timer0.
Interfacing of motor with
ATmega32.
1. Write a C program to monitor
the status of switch and

17 perform the following:


(a) if SW = 0, the DC motor
moves clockwise.
(b) if SW = 1, the DC motor
moves anti clockwise.
Institute Vision

Imparting value based integrative Education to empower youth by fostering


innovation and leadership ethos for socio-professional needs.

Institute Mission
1. Providing State-of-the-art infrastructure by strengthening industry-institute-
interaction to achieve excellence in engineering education.

2. Fostering talent by developing global mindset and sustained leadership in


professional outlook to meet ever-rising industry expectations.

3. Nurturing students by inculcating in them ethical and professional behavior


for their holistic development.
Department Vision

To be recognized by the society to produce innovative, creative, analytically


skilled and confident engineers in electronics and communication field by quality
higher education.

Department Mission

1. To impart knowledge through R & D activity and to provide unique learning


environment to enable students to face challenges in order to serve the needs
of society, industry, government and scientific community.

2. To provide quality technical education in EC undergraduate students leading


to placements preferably in core companies and for higher education and
research.
Microprocessor & Microcontroller (3141008) Date:

EXPERIMENT - 1

Aim: Understand arduino UNO open source hardware and programming


environment.

Hardware required: Arduino Uno

Software required: Arduino open source software.

Introduction to Arduino
The lab will be based on the Arduino Uno. The Arduino Uno is a microcontroller
board based on the ATmega328. It has 14 digital input/output pins (of which 6
can be used as PWM outputs), 6 analog inputs, a 16 MHz ceramic resonator, a
USB connection, a power jack, an ICSP header, 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.

7 Electronics & Communication Engineering Department, GEC Bharuch


Microprocessor & Microcontroller (3141008) Date:

Features of Uno board


Microcontroller ATmega328
Operating Voltage 5V
Input Voltage 7-12V
(recommended)
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 (ATmega328) of which 0.5 KB used by
boot loader
SRAM 2 KB (ATmega328)
EEPROM 1 KB (ATmega328)
Clock Speed 16 MHz

Power

The Arduino Uno can be powered via the USB connection or with an external
power supply. The power source is selected automatically. External (non-USB)
power can come either from an AC-to-DC adapter (wall-wart) or battery. The
adapter can be connected by plugging a 2.1mm center-positive plug into the
board's power jack. Leads from a battery can be inserted in the Gnd and Vin pin
headers of the POWER connector.

The board can operate on an external supply of 6 to 20 volts. If supplied with less
than 7V, however, the 5V pin may supply less than five volts and the board may
be unstable. If using more than 12V, the voltage regulator may overheat and
damage the board. The recommended range is 7 to 12 volts.

The power pins are as follows:

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

8 Electronics & Communication Engineering Department, GEC Bharuch


Microprocessor & Microcontroller (3141008) Date:

source). You can supply voltage through this pin, or, if supplying voltage via the
power jack, access it through this pin.

• 5V. This pin outputs a regulated 5V from the regulator on the board. The board
can be supplied with power either from the DC power jack (7 - 12V), the USB
connector (5V), or the VIN pin of the board (7-12V).

• 3V3. A 3.3 volt supply generated by the on-board regulator. Maximum current
draw is 50 mA.

• GND. Ground pins.

Each of the 14 digital pins (pins 0 to 13) on the Uno can be used as an input or
output, using pinMode(), digitalWrite(), and digitalRead() functions. They
operate at 5 volts. Each pin can provide or receive a maximum of 40 mA and has
an internal pull-up resistor (disconnected by default) . In addition, some pins
have specialized functions:

Serial: 0 (RX) and 1 (TX). Used to receive (RX) and transmit (TX) TTL serial
data.

These pins are connected to the corresponding pins of the ATmega8U2 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.

• PWM: 3, 5, 6, 9, 10, and 11. Provide 8-bit PWM output with the analogWrite()
function.

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

communication using the SPI library.

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

The Uno has 6 analog inputs, labeled A0 through A5, each of which provide 10
bits of resolution (i.e. 1024 different values). By default they measure from

9 Electronics & Communication Engineering Department, GEC Bharuch


Microprocessor & Microcontroller (3141008) Date:

ground to 5 volts, though it is possible to change the upper end of their range
using the AREF pin and the analogReference( ) function.

Additionally, some pins have specialized functionality:

• TWI: A4 or SDA pin and A5 or SCL pin. Support TWI communication using
the Wire library.

ATmega168/328-Arduino Pin Mapping

10 Electronics & Communication Engineering Department, GEC Bharuch


Microprocessor & Microcontroller (3141008) Date:

Arduino Programming

Click on the Arduino executable which has the Arduino logo


The following screen comes up:

The programs written for Arduino are called sketches. For the sketch to work on
the Arduino Uno, there are two hardware related settings need to be done in the
Arduino IDE –
• Board
• Serial Port
For selecting the board, go to the Tools tab and select Board. From the menu
select Uno.

11 Electronics & Communication Engineering Department, GEC Bharuch


Microprocessor & Microcontroller (3141008) Date:

When you connect your Arduino Uno to the USB port of your laptop, it will be
mapped as a serial port.
To know the serial port to which your Arduino is mapped, follow the following
procedure:
 Right click on My Computer
 Select the Manage option
 In the pop up screen for Computer Management, select the Device Manager
 Expand the Ports item; the Arduino Uno will appear as one of the drop down
items

12 Electronics & Communication Engineering Department, GEC Bharuch


Microprocessor & Microcontroller (3141008) Date:

In the Arduino IDE, select the Serial Port as the port to which the Arduino is
mapped.

13 Electronics & Communication Engineering Department, GEC Bharuch


Microprocessor & Microcontroller (3141008) Date:

The basic structure of the Arduino sketch is fairly simple and has two required
functions:
void setup( )
{
statements;
}
void loop( )
{
statements;
}
Where setup( ) is the preparation, loop() is the execution. Both functions are
required for the program to work. The setup function should follow the
declaration of any variables at the very beginning of the program. It is the first
function to run in the program, is run only once, and is used to set pin Mode or
initialize serial communication.
The loop function follows next and includes the code to be executed continuously
reading inputs, triggering outputs, etc. This function is the core of all Arduino
programs and does the bulk of the work.
setup( )
The setup() function is called once when your program starts. Use it to initialize
pin modes, or begin serial. It must be included in a program even if there are no
statements to run.
void setup( )
{
pinMode(pin, OUTPUT); // sets the 'pin' as output
}
loop( )
After calling the setup() function, the loop() function does precisely what its
name suggests, and loops consecutively, allowing the program to change,
respond, and control the Arduino board.
void loop( )
{
digitalWrite(pin, HIGH); // turns 'pin' on

14 Electronics & Communication Engineering Department, GEC Bharuch


Microprocessor & Microcontroller (3141008) Date:

delay(1000); // pauses for one second


digitalWrite(pin, LOW); // turns 'pin' off
delay(1000); // pauses for one second
}

pinMode(pin, mode)
Used in void setup() to configure a specified pin to behave either as an INPUT or
an OUTPUT.
pinMode(pin, OUTPUT); // sets ‘pin’ to output
There are also convenient pullup resistors built into the Atmega chip that can be
accessed from software. These built-in pullup resistors are accessed in the
following manner:
pinMode(pin, INPUT); // set ‘pin’ to input
digitalWrite(pin, HIGH); // turn on pullup resistors
Pullup resistors would normally be used for connecting inputs like
switches. Notice in the above example it does not convert pin to an output, it is
merely a method for activating the internal pull-ups.
Pins configured as OUTPUT can provide 40 mA (milliamps) of current to other
devices/circuits. This is enough current to brightly light up an LED (don't forget
the series resistor), but not enough current to run most relays, solenoids, or
motors.
Short circuits on Arduino pins and excessive current can damage or destroy the
output pin, or damage the entire Atmega chip. It is often a good idea to connect
an OUTPUT pin to an external device in series with a 470Ω or 1KΩ resistor.

digitalRead(pin)
Reads the value from a specified digital pin with the result either HIGH or LOW.
The pin can be specified as either a variable or constant (0-13).
value = digitalRead(Pin); // sets 'value' equal to the input pin

digitalWrite(pin, value)
Outputs either logic level HIGH or LOW at (turns on or off) a specified digital pin.
The pin can be specified as either a variable or constant (0-13).

15 Electronics & Communication Engineering Department, GEC Bharuch


Microprocessor & Microcontroller (3141008) Date:

digitalWrite(pin, HIGH); // sets 'pin' to high


The following example reads a pushbutton connected to a digital input and turns
on an LED connected to a digital output when the button has been pressed:
int led = 13; // connect LED to pin 13
int pin = 7; // connect pushbutton to pin 7
int value = 0; // variable to store the read value
void setup( )
{
pinMode(led, OUTPUT); // sets pin 13 as output
pinMode(pin, INPUT); // sets pin 7 as input
}
void loop( )
{
value = digitalRead(pin); // sets 'value' equal to the input pin
digitalWrite(led, value); // sets 'led' to the button's value
}

analogRead(pin)
Reads the value from a specified analog pin with a 10-bit resolution. This
function only works on the analog in pins (0-5). The resulting integer values
range from 0 to 1023.
value = analogRead(pin); // sets 'value' equal to 'pin'
Note: Analog pins unlike digital ones, do not need to be first declared as INPUT
or OUTPUT.

analogWrite(pin, value)
Writes a pseudo-analog value using hardware enabled pulse width modulation
(PWM) to an output pin marked PWM. On Uno, this function works on pins 3, 5,
6, 9, 10, and 11. The value can be specified as a variable or constant with a value
from 0-255.
analogWrite(pin, value); // writes 'value' to analog 'pin'
A value of 0 generates a steady 0 volts output at the specified pin; a value of 255
generates a steady 5 volts output at the specified pin. For values in between 0

16 Electronics & Communication Engineering Department, GEC Bharuch


Microprocessor & Microcontroller (3141008) Date:

and 255, the pin rapidly alternates between 0 and 5 volts - the higher the value,
the more often the pin is HIGH (5 volts). For example, a value of 64 will be 0 volts
three-quarters of the time, and 5 volts one quarter of the time; a value of 128 will
be at 0 half the time and 255 half the time; and a value of 192 will be 0 volts one
quarter of the time and 5 volts three-quarters of the time. Because this is a
hardware function, the pin will generate a steady wave after a call to analogWrite
in the background until the next call to analogWrite (or a call to digitalRead or
digitalWrite on the same pin).
Note: Analog pins unlike digital ones do not need to be first declared as INPUT or
OUTPUT.
The following example reads an analog value from an analog input pin, converts
the value by dividing by 4, and outputs a PWM signal on a PWM pin:
int led = 10; // LED with 220 resistor on pin 10
int pin = A0; // potentiometer on analog pin 0
int value; // value for reading
void setup( )
{
} // no setup needed
void loop( )
{
value = analogRead(pin); // sets 'value' equal to 'pin'
value /= 4; // converts 0-1023 to 0-255
analogWrite(led, value); // outputs PWM signal to led
}

delay(ms)
Pauses a program for the amount of time as specified in milliseconds, where
1000 equals 1 second.
delay(1000); // waits for one second

millis( )
Returns the number of milliseconds since the Arduino board began running the
current program as an unsigned long value.

17 Electronics & Communication Engineering Department, GEC Bharuch


Microprocessor & Microcontroller (3141008) Date:

value = millis( ); // sets ‘value’ equal to millis()


Note: This number will overflow (reset back to zero), after approximately 9
hours.

Serial.begin(rate)
Opens serial port and sets the baud rate for serial data transmission. The typical
baud rate for communicating with the computer is 9600 although other speeds
are supported.
void setup( )
{
Serial.begin(9600); // opens serial port
} // sets data rate to 9600 bps
Note: When using serial communication, digital pins 0 (RX) and 1 (TX) cannot be
used at the same time.

Serial.println(data)
Prints data to the serial port, followed by an automatic carriage return and line
feed. This command takes the same form as Serial.print(), but is easier for
reading data on the Serial Monitor.
Serial.println(analogValue); // sends the value of // 'analogValue'
Note: For more information on the various permutations of the Serial.println()
and Serial.print( ) functions please refer to the Arduino website.

The following simple example takes a reading from analog pin0 and sends this
data to the
computer every 1 second.
void setup( )
{
Serial.begin(9600); // sets serial to 9600bps
}
void loop( )
{
Serial.println(analogRead(A0)); // sends analog value

18 Electronics & Communication Engineering Department, GEC Bharuch


Microprocessor & Microcontroller (3141008) Date:

delay(1000); // pauses for 1 second


}

After entering your program, click on the Verify button for compilation. If there
are errors, the line numbers of the errors are shown in the bottom window.
Correct the errors. After successful verification, upload your program to the
Arduino using the Upload button. A common cause for failure in uploading is that
your Arduino is not connected to a different COM port than the one shown in
the Arduino IDE.

Conclusion:

19 Electronics & Communication Engineering Department, GEC Bharuch


Microprocessor & Microcontroller (3141008) Date:

EXPERIMENT - 2

Aim: Write program to blink LED using Arduino UNO.

Hardware required: Arduino Uno development board, LED, Resistor

Software required: Arduino IDE open source software.

Interfacing diagram:

Schematic Diagram:

20 Electronics & Communication Engineering Department, GEC Bharuch


Microprocessor & Microcontroller (3141008) Date:

Arduino program:

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);
delay(1000); // wait for a second
digitalWrite(LED_BUILTIN, LOW);
delay(1000); // wait for a second
}

OR
int LED = 13;

void setup( )
{
pinMode(LED, OUTPUT);
}

void loop( )
{
digitalWrite(LED, HIGH);
delay(1000); // wait for a second
digitalWrite(LED, LOW);
delay(1000); // wait for a second
}

21 Electronics & Communication Engineering Department, GEC Bharuch


Microprocessor & Microcontroller (3141008) Date:

Observation:

Conclusion:

22 Electronics & Communication Engineering Department, GEC Bharuch


Microprocessor & Microcontroller (3141008) Date:

EXPERIMENT - 3

Aim: Interface switch and LED to arduino UNO and write a program to turn on
LED by pressing switch.
Hardware required: Arduino Uno development board, LED, Resistor, jumper
wire and Switch

Software required: Arduino IDE open source software.

Interfacing diagram:

Schematic Diagram:

23 Electronics & Communication Engineering Department, GEC Bharuch


Microprocessor & Microcontroller (3141008) Date:

Arduino program:

int pushbutton = 2;

void setup( )
{
pinMode(LED_BUILTIN, OUTPUT);
pinMode(pushbutton, INPUT);
}
void loop( )
{
int switch_status = digitalRead(pushbutton);
if(switch_status == HIGH)
digitalWrite(LED_BUILTIN, HIGH);
else
digitalWrite(LED_BUILTIN, LOW);
}

Observation:

Conclusion:

24 Electronics & Communication Engineering Department, GEC Bharuch


Microprocessor & Microcontroller (3141008) Date:

EXPERIMENT - 4

Aim: Interface LCD with arduino UNO and write program to display some string
on LCD display.
Hardware required: Arduino Uno development board, 2x16 LCD Display,
Resistor, one potentiometer and jumper wire

Software required: Arduino IDE open source software.

Introduction:

The LCDs have a parallel interface, meaning that the microcontroller has to
manipulate several interface pins at once to control the display. The interface
consists of the following pins:
A register select (RS) pin that controls where in the LCD's memory you're
writing data to. You can select either the data register, which holds what goes on
the screen, or an instruction register, which is where the LCD's controller looks
for instructions on what to do next.
A Read/Write (R/W) pin that selects reading mode or writing mode
An Enable pin that enables writing to the registers
8 data pins (D0 -D7). The states of these pins (high or low) are the bits that
you're writing to a register when you write, or the values you're reading when
you read.
There's also a display constrast pin (Vo), power supply pins (+5V and
Gnd) and LED Backlight (Bklt+ and BKlt-) pins that you can use to power the
LCD, control the display contrast, and turn on and off the LED backlight,
respectively.
The process of controlling the display involves putting the data that form the
image of what you want to display into the data registers, then putting
instructions in the instruction register. The LiquidCrystal Library simplifies this
for you so you don't need to know the low-level instructions.

The Hitachi-compatible LCDs can be controlled in two modes: 4-bit or 8-bit. The
4-bit mode requires seven I/O pins from the Arduino, while the 8-bit mode

25 Electronics & Communication Engineering Department, GEC Bharuch


Microprocessor & Microcontroller (3141008) Date:

requires 11 pins. For displaying text on the screen, you can do most everything in
4-bit mode, so example shows how to control a 16x2 LCD in 4-bit mode.
LCD library functions
LiquidCrystal( )
Description
Creates a variable of type LiquidCrystal. The display can be controlled using 4 or
8 data lines. If the former, omit the pin numbers for d0 to d3 and leave those
lines unconnected. The RW pin can be tied to ground instead of connected to a
pin on the Arduino; if so, omit it from this function's parameters.

Syntax
LiquidCrystal(rs, enable, d4, d5, d6, d7)
LiquidCrystal(rs, rw, enable, d4, d5, d6, d7)
LiquidCrystal(rs, enable, d0, d1, d2, d3, d4, d5, d6, d7)
LiquidCrystal(rs, rw, enable, d0, d1, d2, d3, d4, d5, d6, d7)

Parameters
rs: the number of the Arduino pin that is connected to the RS pin on the LCD
rw: the number of the Arduino pin that is connected to the RW pin on the LCD
(optional)
enable: the number of the Arduino pin that is connected to the enable pin on the
LCD
d0, d1, d2, d3, d4, d5, d6, d7: the numbers of the Arduino pins that are connected
to the corresponding data pins on the LCD. d0, d1, d2, and d3 are optional; if
omitted, the LCD will be controlled using only the four data lines (d4, d5, d6, d7).
begin( )
Description
Specifies the dimensions (width and height) of the display.

Syntax
lcd.begin(cols, rows)
Parameters
lcd: a variable of type LiquidCrystal

26 Electronics & Communication Engineering Department, GEC Bharuch


Microprocessor & Microcontroller (3141008) Date:

cols: the number of columns that the display has


rows: the number of rows that the display has
setCursor( )
Description
Position the LCD cursor; that is, set the location at which subsequent text written
to the LCD will
be displayed.

Syntax
lcd.setCursor(col, row)
Parameters
lcd: a variable of type LiquidCrystal
col: the column at which to position the cursor (with 0 being the first column)
row: the row at which to position the cursor (with 0 being the first row)

Interfacing diagram:

27 Electronics & Communication Engineering Department, GEC Bharuch


Microprocessor & Microcontroller (3141008) Date:

Schematic Diagram:

Arduino program:

#include <LiquidCrystal.h>

// initialize the library with the numbers of the interface pins


LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

void setup( )
{
lcd.begin(16, 2);
}

void loop( )
{
lcd.setCursor(0, 0);
lcd.print("GEC Bharuch");
lcd.setCursor(0, 1);
lcd.print("EC Department");
}

28 Electronics & Communication Engineering Department, GEC Bharuch


Microprocessor & Microcontroller (3141008) Date:

Observation:

Conclusion:

29 Electronics & Communication Engineering Department, GEC Bharuch


Microprocessor & Microcontroller (3141008) Date:

EXPERIMENT - 5

Aim: Implement serial communication with Arduino board.

Hardware required: Arduino Uno development board

Software required: Arduino IDE open source software.

Introduction
The Arduino Uno board is capable of serial communication used for
communication between the Arduino board and a computer or other devices.
The Uno has a single serial port (also known as a UART or USART): Serial. It
communicates on digital pins 0 (RX) and 1 (TX) as well as with the computer via
USB. Thus, if you use these functions, you cannot also use pins 0 and 1 for digital
input or output.

You can use the Arduino environment's built-in serial monitor to communicate
with an Arduino board. Click the serial monitor button in the toolbar and select
the same baud rate used in the call to begin( ).
Functions
begin( )
Description
Sets the data rate in bits per second (baud) for serial data transmission. For
communicating with the computer, use one of these rates: 300, 1200, 2400,
4800, 9600, 14400, 19200, 28800, 38400, 57600, or 115200.
Syntax
Serial.begin(speed)
println( )
Description
Prints data to the serial port as human-readable ASCII text followed by a carriage
return character (ASCII 13, or '\r') and a newline character (ASCII 10, or '\n').
This command takes the same forms as Serial.print( ).
Syntax
Serial.println(val)
Serial.println(val, format)

30 Electronics & Communication Engineering Department, GEC Bharuch


Microprocessor & Microcontroller (3141008) Date:

Parameters
val: the value to print - any data type
format: specifies the number base (for integral data types) or number of decimal
places (for
floating point types)
Returns
byte
println( ) will return the number of bytes written, though reading that number is
optional.

Arduino program:

void setup( )
{
Serial.begin( );
}

void loop( )
{
lcd.setCursor(0, 1);
lcd.print("GEC Bharuch");
lcd.setCursor(1, 1);
lcd.print("EC Department");
}

Observation:

Conclusion:

31 Electronics & Communication Engineering Department, GEC Bharuch


Microprocessor & Microcontroller (3141008) Date:

EXPERIMENT - 6

Aim: Write and execute Arduino program to read analog value. Sense
temperature using LM35 sensor and display temperature value on LCD.

Hardware required: Arduino Uno development board, 2x16 LCD display,


Resistor, Jumper wires and LM35 temperature sensor

Software required: Arduino IDE open source software.

Introduction
analogRead( )
Description
Reads the value from the specified analog pin. The Arduino Uno board contains a
6 channel, 10-bit analog to digital converter. This means that it will map input
voltages between 0 and 5 volts into integer values between 0 and 1023. This
yields a resolution between readings of: 5 volts / 1024 units or, .0049 volts (4.9
mV) per unit.

It takes about 100 microseconds (0.0001 s) to read an analog input, so the


maximum reading rate is about 10,000 times a second.

Syntax
analogRead(pin)
Parameters
pin: the number of the analog input pin to read from (A0 to A5 on Uno)
Returns
int (0 to 1023)

32 Electronics & Communication Engineering Department, GEC Bharuch


Microprocessor & Microcontroller (3141008) Date:

Interfacing diagram:

Arduino program:

#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
int sensorPin = A0;
int tempC, tempF;
void setup( )
{
lcd.begin(16, 2);
}
void loop( )
{
int temperature = analogRead(sensorPin);
float voltage = temperature * 5.0;
voltage = voltage / 1024.0;
tempC=(voltage - 0.5) * 100;
lcd.setCursor(0,0);
lcd.print(“Temperature in ‘C”);
lcd.print(tempC);
delay(200);
}

33 Electronics & Communication Engineering Department, GEC Bharuch


Microprocessor & Microcontroller (3141008) Date:

Observation:

Conclusion:

34 Electronics & Communication Engineering Department, GEC Bharuch


Microprocessor & Microcontroller (3141008) Date:

EXPERIMENT - 7

Aim: Introduction to AVR studio software.

Software required: AVR Studio 5.1

Introduction:

AVR Studio 5 is a software development environment produced by Atmel for its


AVR 8-bits and 32-bits family of AVR microcontroller.

Some Features of AVR Studio 5

It is a full software development environment with an editor, simulator,


programmer, etc.

It comes with its own integrated C compiler the AVR GNU C Compiler (GCC). As
such you do not need a third party C compiler.

It provides a single environment to develop programs for both the 8-bits and 32-
bits AVR series of microcontrollers.

Provides support for several programmers including the STK500, AVR Dragon,
etc.

Creating a new AVR C Project in AVR Studio 5

35 Electronics & Communication Engineering Department, GEC Bharuch


Microprocessor & Microcontroller (3141008) Date:

This AVR tutorial will go through the steps to create a AVR C project in AVR
Studio 5. This tutorial assume that you have already install AVR Studio 5 on your
computer

Step 1: To create a C project first start AVR Studio 5 by going to the start menu
on your PC select Atmel AVR Tools then AVR Studio 5.0. See the figure below.

After AVR Studio 5 starts the following window will appear. From which get help,
open an existing project, create a new project, open an example project, etc.

36 Electronics & Communication Engineering Department, GEC Bharuch


Microprocessor & Microcontroller (3141008) Date:

Step 2: Click on New Project pointed to by the red arrow in the diagram above
to start a new project. The following window will appear.

Step 3: To start a new C project select C from the panel to the left pointed to by
the red arrow. Also type the file Name and Location pointed to by the red
arrows at the bottom of the window. The following window will then appear.

37 Electronics & Communication Engineering Department, GEC Bharuch


Microprocessor & Microcontroller (3141008) Date:

Step 4: The window above is the device selection screen for AVR Studio 5. Scroll
down and select the microcontroller you will be using. The following window
will then appear.

38 Electronics & Communication Engineering Department, GEC Bharuch


Microprocessor & Microcontroller (3141008) Date:

This is the AVR studio 5 editor where you type your C program. The editor starts
your C program for you by providing you with the structure shown in the editor
of the figure above.

Generating / Creating the AVR HEX File in AVR Studio 5

The AVR Hex File

This AVR tutorial discusses how to generate or create an hex file in AVR Studio 5.
We will be generating/creating the hex file for the AVR C code shown in the AVR
Studio 5 editor below.

After typing your AVR program may it be C or assembly to generate the hex file
goes to Build menu and click Build Solution. See the figure below

39 Electronics & Communication Engineering Department, GEC Bharuch


Microprocessor & Microcontroller (3141008) Date:

If the AVR C or Assembly program was builds successfully a message will display
at the bottom of the AVR Studio 5 Editor indicating that Build succeeded. See the
bottom of the figure below.

You can view the content of the generated hex file by double clicking on the file
with the .hex extension in the Solution Explore to the left of the AVR Studio 5
editor. The hex file that we just generated is displayed in the AVR Studio 5 Editor
shown in the figure below.

40 Electronics & Communication Engineering Department, GEC Bharuch


Microprocessor & Microcontroller (3141008) Date:

Observation:

Conclusion:

41 Electronics & Communication Engineering Department, GEC Bharuch


Microprocessor & Microcontroller (3141008) Date:

EXPERIMENT – 8

Aim: How to Program AVR Microcontroller.

Hardware required: USBasp, ATmega32 microcontroller, connecting wires,


bread board.

Software required: AVR Studio 5.0

Introduction:

Setting up Atmel Studio for USBasp and AVR Programming

First of all, let’s take a look at your current system configuration

Software
1. Windows 10/ Windows 8/ Windows 7
2. Atmel Studio 7 / Atmel Studio 6/ Atmel Studio 5
3. WinAVR

Hardware
 USBasp
Or
 USBasp based AVR Development board

USBasp

Make sure you have Atmel Studio, WinAVR and a USBasp to follow this tutorial.

42 Electronics & Communication Engineering Department, GEC Bharuch


Microprocessor & Microcontroller (3141008) Date:

First Step

Install both Atmel Studio and WinAVR. You can follow the guided instruction
embedded in the installation wizard.

Next step

Configuring Toolchain for Compilation


Since I am going to upload my codes to AVR using avrdude. I need to compile the
codes first using the same toolchain.
To do that, I need to make a custom toolchain configuration.

Step 1
Go to Tools > Options > Toolchain
Select Atmel AVR 8-bit option from the dropdown menu.

Step 2
Click Add Flavour, then set the package name “AVRdudeDev” or anything you may
like.
For the package base path go to WinAVR installation directory and find
the bin folder. Now copy the path and paste it. [here it is C:\WinAVR-
20100110\bin].

Click Add.

43 Electronics & Communication Engineering Department, GEC Bharuch


Microprocessor & Microcontroller (3141008) Date:

You can make the newly created toolchain package default by clicking on to
the Set as Default button.

If you’re done with the toolchain package configuration, click OK to close the
Dialog Window.

Creating External Tool for USBasp

To upload the compiled hex files. You need to configure the external tool for
USBasp. If you don’t create an external tool for USBasp that’s fine too, then you
need to type the avrdude commands to upload or use other software to upload
the hex file for you.
Why bother when Atmel Studio can help you on both code compilation and hex
file uploading process?

Step 1

Go to Tools > External Tools


Click add and fill up the blanks with following strings.

Check: Use Output window


Uncheck: Prompt for arguments, Treat output as Unicode

Title: You can give any title you want.

44 Electronics & Communication Engineering Department, GEC Bharuch


Microprocessor & Microcontroller (3141008) Date:

Command: This will initiate the avrdude program, so you need to put the
avrdude directory path in this blank. For me it was E:\WinAVR-
20100110\bin\avrdude.exe

Arguments: This is the most tricky part. This is the arguments section where
you need to pass specIfic arguments to do specIfic tasks. Since here we have used
external tool as the program code loader. We need to put the command which
we’d use in avrdude for uploading codes.

Here is the argument that I provided

avrdude -c usbasp -p m32 -u -U


flash:w:$(ProjectDir)Debug\$(TargetName).hex:i

AVRDude Tutorial : Burning hex files on Atmel AVR using USBasp and
AVRdude

The AVRdude is excellent program for burning hex code into Atmel
AVR microcontroller. USBasp is awesome USB based Atmel AVR programmer. In
this tutorial we will see how to use AVRdude for burning hex files into AVR
microcontroller using USBasp.

In order to program AVR microcontroller you need the .HEX file. It is nothing but
the machine code for the microcontroller. This file is generated by the AVR
assembler, which converts assembly code into machine code. Assembly code can
be produced by third party C cross compiler software or can be handwritten.
Typically everyone uses Atmel Studio, or Arduino environment to write
programs in C language. After compiling, these tools generate .hex file as their
output.

AVRdude executables for Windows (or tar archive for linux) can be found at:
All releases : http://download.savannah.gnu.org/releases/avrdude/ look for
version 6.3
Windows exe : http://download.savannah.gnu.org/releases/avrdude/avrdude-
6.3-mingw32.zip

Download and extract this executable in a suitable folder.

NOTE 1: Make sure the path for the avrdude’s folder is added to the PATH
environment variable, otherwise “avrdude.exe” cannot be accessed fron any
random directory on the command prompt. Refer to this page on how to add a
path to PATH environment variable.

NOTE 2: When you connect your USBasp hardware to the Windows 10 machine,
you will need to download and install the drivers for the same.

Connections

45 Electronics & Communication Engineering Department, GEC Bharuch


Microprocessor & Microcontroller (3141008) Date:

Connect the USBasp to PC.

Pinout of USBasp

Connection of USBasp and Microcontroller

Connect SPI programming pins of USBasp to the AVR microcontroller. Following


figure shows sample schematic diagram, If you have dIfferent AVR, then connect
MOSI,MISO, SCK, RESET and GND pins of that uC to corresponding pins of
USBasp.

Give +5V supply to the microcontroller.

46 Electronics & Communication Engineering Department, GEC Bharuch


Microprocessor & Microcontroller (3141008) Date:

If you are burning a fresh microcontroller, close the Slow Serial Clock jumper of
USBasp. Since many brand new microcontroller are factory programmed for
internal 1MHz oscillator. USBasp uses very high speed serial clock for faster
programming. Thus you will have to specIfically tell USBasp to use slow serial
clock. This setting is done by above mentioned jumper.

NOTE: If you have microcontroller which has internal oscillator enabled and
after the programming you are not planning to change its fuse bits back to
external clock setting, then you can skip the crystal.

Executing AVRdude:

Fortunately AVRdude is command line tool, so that you can be very sure of what
you are doing with your uC Or Unfortunately AVRdude is command line tool, so
you will
have to spend little time to get familiar with it.
o Open the command prompt. (Press WinKey + R. Run dialogbox will appear.
Type cmd and press enter.)
o Navigate to the directory where .hex file is located. For example:

> cd D:\mega32\sample code\ex01_simple_input_output\Exe


> d:

If you have NOT added the avrdude’s path to the system path, execute following
command with your path. Otherwise skip this step.

> set PATH=%PATH%;c:\avrdude_6_3\;

To burn the hex file enter following command. Consider for example name of my
hex file is io.hex :
> avrdude –c usbasp –p m32 –u –U flash:w:io.hex

You should see something like this :

47 Electronics & Communication Engineering Department, GEC Bharuch


Microprocessor & Microcontroller (3141008) Date:

Explanation for command

avrdude –c usbasp –p m32 –u –U flash:w:io.hex

-c : Indicates the programmer type. Since we are using the USBasp programmer,
argument “usbasp” is mentioned.

-p : Processor. We are using ATmega32, hence “m32”.

-u : Disables the default behavior of reading out the fuses three times before
programming, then verIfying at the end of programming that the fuses have not
changed. Always use this option. Many times it happens that we forget to switch
on the AVR’s +5V power supply, then at the end of programming cycle, avrdude
detects inconsistent fuses and tries to reprogram them. Since there is no power
supply, fuses gets programmed incorrectly and entire microcontroller gets
screwed up(means becomes useless). Thus always use this option.

-U : memtype:op:filename[:format]
Perform a memory operation. Multiple ‘-U’ options can be specified in order to
operate on multiple memories on the same command-line invocation.

Memtype

The memtype field specifies the memory type to operate on.


calibration One or more bytes of RC oscillator calibration data.
eeprom The EEPROM of the device.
efuse The extended fuse byte.
flash The flash ROM of the device.
fuse The fuse byte in devices that have only a single fuse byte.
hfuse The high fuse byte.
lfuse The low fuse byte.
lock The lock byte.

Op

The op field specifies what operation to perform:


r read the specified device memory and write to the specified file
w read the specified file and write it to the specified device memory
v read the specified device memory and the specified file and perform a verIfy
operation.

Filename

SpecIfy the hex file name. If file is not in current directory specIfy file name with
appropriate path.

Format

48 Electronics & Communication Engineering Department, GEC Bharuch


Microprocessor & Microcontroller (3141008) Date:

Format need not be specIfied, for hex files, avrdude will automatically detect the
format.

The trick to do it quickly : The Batch file

It is extremely boring to type such a long command every time you program the
uC. Therefore to avoid this you can create something called as Batch file. It is a
text file which contains series of commands which will be executed by dos
command processor. To create batch file follow these steps :

o Open notepad
o Type our avrdude command. i.e. copy paste following line into notepad.
avrdude –c usbasp –p m32 –u –U flash:w:io.hex
o Save the file with filename “burn.bat” and put it into the directory, which has
the hex file.

Now whenever you recompile your program and want to burn it, simply double
click on burn.bat. This will execute avrdude command that we have typed in it.

Possible errors and problems

1)couldn’t find usbasp:

found 5 busses
found 5 busses
avrdude: error: could not find USB device “USBasp” with vid=0x16c0 pid=0x5dc

This happens when USBasp is not connected or not recognized by the PC. Try to
connect it to dIfferent USBport. Make sure that “Self programming” jumper of
USBasp is open. Always disconnect AVR from USBasp, before plugging USBasp to
the PC.

2)target does not answer

found 5 busses
avrdude: error: programm enable: target doesn’t answer. 1
avrdude: initialization failed, rc=-1
Double check connections and try again, or use -F to override this check.
avrdude done. Thank you.

Check the connections of USBasp with uC. Check the power supply of the uC.
Check whether you have connected the crystal and decoupling capacitors. If
everything is fine and still you are getting this error, then it indicates that either
ur uC is dead or its fuse bits have got screwed up.

Observation:

49 Electronics & Communication Engineering Department, GEC Bharuch


Microprocessor & Microcontroller (3141008) Date:

Conclusion:

50 Electronics & Communication Engineering Department, GEC Bharuch


Microprocessor & Microcontroller (3141008) Date:

EXPERIMENT – 9

Aim: Interfacing of LED with ATmega32 microcontroller.

Hardware requiered: USBasp loader, ATmega32 microcontroller, Resistors,


LEDs, Connecting wires.

Software required: AVR Studio 5, avrdude, USBasp driver.

Program 9A: Write a C program to blink single LED continuosly.

Interfacing diagram:

#include <avr/io.h>
#include <util/delay.h>
int main (void)
{
DDRB |= 0x01;
While(1)
{
PORTB ^= 0x01;
_delay_ms(1000);
}
return 0;
}
Observation:

51 Electronics & Communication Engineering Department, GEC Bharuch


Microprocessor & Microcontroller (3141008) Date:

Program 9B: Write a C program to blink 8 LED continuosly.

Interfacing diagram:

#include <avr/io.h>
#include <util/delay.h>
int main (void)
{
DDRB |= 0xFF;
While(1)
{
PORTB ^= 0xFF;
_delay_ms(1000);
}
return 0;
}

Observation:

52 Electronics & Communication Engineering Department, GEC Bharuch


Microprocessor & Microcontroller (3141008) Date:

Program 9C: Write a C program for scrolling of 8 LED.


Interfacing diagram:

#include <avr/io.h>
#include <util/delay.h>
int main (void)
{
DDRB |= 0xFF;
While(1)
{
PORTB = 0x01;
_delay_ms(1000);
PORTB = 0x02;
_delay_ms(1000);
PORTB = 0x04;
_delay_ms(1000);
PORTB = 0x08;
_delay_ms(1000);
PORTB = 0x10;
_delay_ms(1000);
PORTB = 0x20;
_delay_ms(1000);
PORTB = 0x40;
_delay_ms(1000);
PORTB = 0x80;

53 Electronics & Communication Engineering Department, GEC Bharuch


Microprocessor & Microcontroller (3141008) Date:

_delay_ms(1000);
}
return 0;
}

OR

#include <avr/io.h>
#include <util/delay.h>
int main (void)
{
DDRB |= 0xFF;
PORTB = 0x01;
While(1)
{
If(PORTB == 0x00)
PORTB = 0x01;
_delay_ms(1000);
PORTB = 1<<PORTB;
}
return 0;
}

Observation:

Program 9D: Write a C program to display 8-bit binary counter value on 8 LED.

54 Electronics & Communication Engineering Department, GEC Bharuch


Microprocessor & Microcontroller (3141008) Date:

Interfacing diagram:

#include <avr/io.h>
#include <util/delay.h>
int main (void)
{
DDRB |= 0xFF;
Unsigned char count=0;
While(1)
{
PORTB = count;
_delay_ms(1000);
Count++;
}
return 0;
}

Observation:

Conclusion:

55 Electronics & Communication Engineering Department, GEC Bharuch


Microprocessor & Microcontroller (3141008) Date:

EXPERIMENT – 10

Aim: Interfacing of switch and LED with ATmega32 microcontroller.

Hardware requiered: USBasp loader, ATmega32 microcontroller, Resistors,


LEDs, Connecting wires.

Software required: AVR Studio 5, avrdude, USBasp driver.

Program 10A: Write a C program to blink single LED by pressing switch.

Interfacing diagram:

#include <avr/io.h>
#include <util/delay.h>

#define LED 0x01


#define SW 0x02

int main (void)


{
DDRB |= LED;
DDRB &= ~SW;
While(1)
{
If(PINB & SW)
PORTB &= ~LED;

56 Electronics & Communication Engineering Department, GEC Bharuch


Microprocessor & Microcontroller (3141008) Date:

Else
PORTB |= LED;
}
return 0;
}

Observation:

Program 10B: Write a C program to toggle single LED by pressing the same
switch.

Interfacing diagram:

#include <avr/io.h>
#include <util/delay.h>

#define LED 0x01


#define SW 0x02

int main (void)


{

57 Electronics & Communication Engineering Department, GEC Bharuch


Microprocessor & Microcontroller (3141008) Date:

DDRB |= LED;
DDRB &= ~SW;
While(1)
{
If (!(PINB & SW))
{
_delay_ms(200);
While(!(PINB & SW));
PORTB ^= LED;
}
}
return 0;
}

Observation:

Program 9C: Write a C program to blink alternate LED by pressing the same
switch.

Interfacing diagram:

58 Electronics & Communication Engineering Department, GEC Bharuch


Microprocessor & Microcontroller (3141008) Date:

#include <avr/io.h>
#include <util/delay.h>

#define R_LED 0x01


#define SW 0x02
#define G_LED 0x04

int main(void)
{
DDRB |= G_LED;
DDRB &= ~SW;
DDRB |= R_LED;

volatile unsigned int flag=0;

While(1)
{
If (!(PINB & SW))
{
_delay_ms(20);
If (!(PINB & SW))
{
While(!(PINB & SW));
flag = !flag;
}
}
If (flag)
{
PORTB &= ~R_LED;
PORTB |= G_LED;
}
else
{

59 Electronics & Communication Engineering Department, GEC Bharuch


Microprocessor & Microcontroller (3141008) Date:

PORTB |= R_LED;
PORTB &= ~G_LED;
}
}
return 0;
}

Observation:

Conclusion:

60 Electronics & Communication Engineering Department, GEC Bharuch

You might also like