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

1-10

This lab manual outlines the curriculum for the Microprocessor and Microcontroller course for the B.E. IV Semester in Electronics & Communication at Government Engineering College Bharuch for the academic year 2024-2025. It includes a list of experiments focusing on Arduino UNO and ATmega32 microcontroller programming, along with evaluation criteria for student performance. The document also presents the institute's vision and mission, emphasizing the importance of quality education and ethical behavior in engineering.

Uploaded by

h50522039
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)
7 views

1-10

This lab manual outlines the curriculum for the Microprocessor and Microcontroller course for the B.E. IV Semester in Electronics & Communication at Government Engineering College Bharuch for the academic year 2024-2025. It includes a list of experiments focusing on Arduino UNO and ATmega32 microcontroller programming, along with evaluation criteria for student performance. The document also presents the institute's vision and mission, emphasizing the importance of quality education and ethical behavior in engineering.

Uploaded by

h50522039
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/ 77

Microprocessor and Microcontroller

Lab Manual

Year : 2024 - 2025


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)
Punctuality (2)

Implementatio
Ability & Logic
Programming

of Program
& Efficiency
Readability
Sr. Start End

Output of
Name of Experiments Marks Sign

Active
No Date Date

n (2)
Understand arduino UNO
open
source hardware and
1
programming environment.
Write program to blink LED
2 using Arduino UNO.

Interface switch and LED to


arduino UNO and write a
program to turn on LED by
3 pressing switch
Interface LCD with
Arduino UNO and write
program to display some
4 string on LCD display.

Generate PWM waveform and


change intensity of LED
5
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
9 a 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
d isplay 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.

11 1. Write a C program to d isplay


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.
13 Interfacing of 4x4 keypad with
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
and transmit character
through serial
15 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.

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
l oader
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

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

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

Arduino Programming

Click on the Arduino executable which has the Arduino logo


The following screen comes up:

10 Electronics & Communication Engineering Department, GEC Bharuch


Microprocessor & Microcontroller (3141008) Date:

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( )
{

14 Electronics & Communication Engineering Department, GEC Bharuch


Microprocessor & Microcontroller (3141008) Date:

digitalWrite(pin, HIGH); // turns 'pin' on

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( )

17 Electronics & Communication Engineering Department, GEC Bharuch


Microprocessor & Microcontroller (3141008) Date:

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

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( )

18 Electronics & Communication Engineering Department, GEC Bharuch


Microprocessor & Microcontroller (3141008) Date:

{
Serial.println(analogRead(A0)); // sends analog value

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:

Review Questions:

1. What microcontroller is used on the Arduino Uno?


2. What is the operating voltage of the Arduino Uno?
3. How many digital I/O pins does the Arduino Uno have?
4. How many analog input pins does the Arduino Uno have?
5. What is the clock speed of the Arduino Uno?
6. What are the power input options for the Arduino Uno?
7. What is the function of the Vin pin?
8. How is power regulated on the Arduino Uno?
9. What happens if the input voltage exceeds 12V?
10. What is the purpose of the 3.3V pin on the Arduino Uno?
11. What type of architecture does the ATmega328P microcontroller use?
12. How much Flash memory does the ATmega328P have?
13. What is the size of SRAM in the ATmega328P?
14. How much EEPROM does the ATmega328P provide?
15. What is the role of the bootloader in Arduino Uno?
16. What are the communication protocols supported by the Arduino Uno?
17. Which pins are used for UART communication?
18. Which pins are used for I²C communication?
19. Which pins are used for SPI communication?
20. What is the baud rate of the serial communication by default?
21. How many pins on the Arduino Uno can generate PWM signals?
22. What is the resolution of the PWM signal on Arduino Uno?
23. What is the resolution of the analog-to-digital converter (ADC)?
24. Which pins are used for external interrupts?
25. What is the default reference voltage for analog inputs?.
26. What USB chip is used on the Arduino Uno?
27. What is the purpose of the USB connection on the Arduino Uno?
28. How can the Arduino Uno be programmed without USB?
29. What does the TX LED on the Arduino Uno indicate?
30. What does the RX LED on the Arduino Uno indicate?
31. What type of crystal oscillator does the Arduino Uno use?

20 Electronics & Communication Engineering Department, GEC Bharuch


Microprocessor & Microcontroller (3141008) Date:

32. Why is a stable clock source important in microcontrollers?


33. Can the Arduino Uno operate without an external clock source?
34. What is the function of the ceramic resonator in Arduino Uno?
35. What is the purpose of the reset button on the Arduino Uno?
36. What is the ICSP header used for?
37. How is auto-reset triggered during USB programming?
38. How many pins does the ICSP header have?
39. What is the purpose of the onboard LED on Pin 13?
40. What does the power LED indicate?
41. How much current can each I/O pin source or sink?.
42. What is the total current limit for all I/O pins?
43. What is the EEPROM write/erase cycle limit?
44. What is the maximum size of a sketch that can be uploaded to the Arduino Uno?
45. What is the maximum length of a string sent over serial communication?
46. Can the Arduino Uno output analog voltages?
47. What is the purpose of the AREF pin?
48. How can you extend the I/O pins of the Arduino Uno?
49. What is the maximum sampling rate of the ADC on Arduino Uno?
50. What type of USB port is used on the Arduino Uno?

21 Electronics & Communication Engineering Department, GEC Bharuch


Microprocessor & Microcontroller (3141008) Date:

EXPERIMENT - 2

Aim: Write program to blink LED using Arduino UNO.

Hardware reuired: Arduino Uno development board, LED, Resistor

Software reuired: 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:

Review Questions:

1. How can I connect an LED to an Arduino Uno?


2. Why do I need a resistor when connecting an LED to the Arduino?
3. What is a simple Arduino code to blink an LED?
4. Can I use any digital pin for controlling the LED?
5. How can I control the brightness of an LED?
6. What happens if I connect the LED directly to the Arduino without a resistor?
7. How many LEDs can I control using an Arduino Uno?
8. How can I control multiple LEDs at once?
9. Can I control an RGB LED with Arduino?
10. What is the maximum current an Arduino pin can supply to an LED?

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 reuired: Arduino Uno development board, LED, Resistor, jumper wire
and Switch

Software reuired: 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:

Review Questions:

1. What components are required to interface an LED and a switch with an Arduino
Uno?
2. What is the circuit setup for interfacing an LED and a switch?
3. What code is used to control the LED using the switch?
4. How does the pull-down resistor in the switch circuit work?
5. How can I test if the circuit is working?
6. How can I modify the code to make the LED toggle with each button press?
7. Why do we use a resistor with the LED?
8. What is the role of the pinMode() function in the Arduino code?
9. What happens if I connect the switch directly to the pin without a pull-up or pull-
down resistor?
10. How can I add debounce to the switch in the code?
11. Can I control multiple LEDs with one switch?
12. How can I read the state of the switch using the Serial Monitor?
13. Can I use the internal pull-up resistor of the Arduino for the switch?
14. How can I connect multiple switches to control different LEDs?
15. What are common troubleshooting tips for this circuit?

25 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 reuired: Arduino Uno development board, 2x16 LCD Display,
Resistor, one potentiometer and jumper wire

Software reuired: 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 LiuidCrystal 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 reuires seven I/O pins from the Arduino, while the 8-bit mode

reuires 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
26 Electronics & Communication Engineering Department, GEC Bharuch
Microprocessor & Microcontroller (3141008) Date:

LiuidCrystal( )
Description
Creates a variable of type LiuidCrystal. 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
LiuidCrystal(rs, enable, d4, d5, d6, d7) LiuidCrystal(rs, rw, enable, d4,
d5, d6, d7) LiuidCrystal(rs, enable, d0, d1, d2, d3, d4, d5, d6, d7)
LiuidCrystal(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 LiuidCrystal

cols: the number of columns that the display has


rows: the number of rows that the display has
setCursor( )
Description

27 Electronics & Communication Engineering Department, GEC Bharuch


Microprocessor & Microcontroller (3141008) Date:

Position the LCD cursor; that is, set the location at which subseuent text written to
the LCD will
be displayed.

Syntax
lcd.setCursor(col, row)
Parameters
lcd: a variable of type LiuidCrystal
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:

28 Electronics & Communication Engineering Department, GEC Bharuch


Microprocessor & Microcontroller (3141008) Date:

Schematic Diagram:

Arduino program:

#include <LiuidCrystal.h>

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


LiuidCrystal 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");
}

29 Electronics & Communication Engineering Department, GEC Bharuch


Microprocessor & Microcontroller (3141008) Date:

Observation:

Conclusion:

Review Questions:

1. What is a 16x2 LCD?


2. What are the main pins on a 16x2 LCD?
3. How do you interface a 16x2 LCD with Arduino Uno?
4. What library is used to control the 16x2 LCD in Arduino?
5. What is a sample code for interfacing a 16x2 LCD with Arduino Uno?
6. How do you adjust the contrast of the LCD?
7. What is 4-bit mode, and why is it used?
8. How do you troubleshoot if the LCD doesn't display anything?
9. Can a 16x2 LCD display graphics?
10. What are some applications of a 16x2 LCD with Arduino?
11. What are the operating voltage and current requirements of a 16x2 LCD?
12. What is the difference between Command Mode and Data Mode in a 16x2 LCD?
30 Electronics & Communication Engineering Department, GEC Bharuch
Microprocessor & Microcontroller (3141008) Date:

13. How do you clear the LCD screen in Arduino code?


14. How can you display custom characters on a 16x2 LCD?
15. What is the function of the Enable (E) pin?
16. Can a 16x2 LCD be powered directly from Arduino Uno?
17. What are some common errors while interfacing 16x2 LCD with Arduino?
18. What are the two most common modes of interfacing an LCD?
19. How do you move the cursor to a specific position on the LCD?
20. How do you make text scroll on a 16x2 LCD?
21. How do you turn the backlight of the LCD on or off?
22. Can you interface a 16x2 LCD using I2C communication?
23. How can you initialize a 16x2 LCD in I2C mode?
24. Can you use a 16x2 LCD with an Arduino Mega or Nano?
25. What are alternatives to the 16x2 LCD for better displays?

31 Electronics & Communication Engineering Department, GEC Bharuch


Microprocessor & Microcontroller (3141008) Date:

EXPERIMENT - 5

Aim: Implement serial communication with Arduino board.

Hardware reuired: Arduino Uno development board Software

reuired: 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)

32 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:

33 Electronics & Communication Engineering Department, GEC Bharuch


Microprocessor & Microcontroller (3141008) Date:

Review Questions:

1. What is serial communication in the context of Arduino Uno?


2. Which pins are used for serial communication on Arduino Uno?
3. How do you initialize serial communication in Arduino?
4. What is the default baud rate, and why is it important?
5. How do you send data from Arduino to a computer?
6. How do you receive data from a computer on Arduino?
7. What are some common baud rates used in serial communication?
8. What is the purpose of Serial.available()?
9. How can you interface Arduino with a Bluetooth module using serial
communication?
10. How do you use software serial communication on Arduino Uno?
11. How do you send and receive strings in serial communication?
12. How do you clear the serial buffer?
13. Can Arduino Uno interface with sensors using serial communication?
14. How do you send binary data over serial communication?
15. What happens if you use Serial.print() in the loop without a delay?
16. How do you debug Arduino code using serial communication?
17. What is the maximum length of a string that can be received in Arduino?
18. Can Arduino Uno communicate with other Arduinos using serial communication?
19. What are the common issues in serial communication interfacing?
20. What are some applications of serial communication in Arduino projects?

34 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 reuired: Arduino Uno development board, 2x16 LCD display,


Resistor, Jumper wires and LM35 temperature sensor

Software reuired: 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)

Interfacing diagram:

35 Electronics & Communication Engineering Department, GEC Bharuch


Microprocessor & Microcontroller (3141008) Date:

Arduino program:

#include <LiuidCrystal.h> LiuidCrystal


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);
}

36 Electronics & Communication Engineering Department, GEC Bharuch


Microprocessor & Microcontroller (3141008) Date:

Observation:

Conclusion:

Review Questions:

1. What is an LM35 temperature sensor?


2. What is the purpose of interfacing an LM35 and a 16x2 LCD with Arduino?
3. How do you connect the LM35 to the Arduino Uno?
4. How do you connect a 16x2 LCD to the Arduino Uno?
5. What library is required for the LCD?
6. What is the formula to convert LM35 sensor output voltage to temperature?
7. What is a sample code for interfacing LM35 and LCD with Arduino Uno?
8. How do you calibrate the LM35 for accuracy?
9. What is the expected output of the system?
10. How can you improve the accuracy of the LM35?
11. How do you troubleshoot if the LCD doesn’t display anything?
12. Can this system measure negative temperatures?
13. Can you display temperature in Fahrenheit?
14. What are some applications of LM35 and LCD interfacing?
15. How do you make the system more interactive?

37 Electronics & Communication Engineering Department, GEC Bharuch


Microprocessor & Microcontroller (3141008) Date:

EXPERIMENT - 7

Aim: Introduction to AVR studio software.

Software reuired: 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

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

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

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

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

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

43 Electronics & Communication Engineering Department, GEC Bharuch


Microprocessor & Microcontroller (3141008) Date:

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.

44 Electronics & Communication Engineering Department, GEC Bharuch


Microprocessor & Microcontroller (3141008) Date:

Observation:

45 Electronics & Communication Engineering Department, GEC Bharuch


Microprocessor & Microcontroller (3141008) Date:

Conclusion:

Review Questions:

1. What is AVR Studio?


2. What are the key features of AVR Studio?
3. What programming languages can be used in AVR Studio?
4. What are the steps to create a new project in AVR Studio?
5. What is the function of the Makefile in AVR Studio?
6. What tools are used for debugging in AVR Studio?
7. How do you compile a program in AVR Studio?
8. What is the difference between “Build” and “Rebuild” in AVR Studio?
9. What file formats are generated after compilation?
10. How do you load a hex file to an AVR microcontroller using AVR Studio?
11. What are “fuses” in AVR microcontrollers, and how are they set in AVR Studio?
12. How do you simulate code in AVR Studio without actual hardware?
13. What is the purpose of the AVR-GCC compiler in AVR Studio?
14. What is the use of the “Watch Window” in AVR Studio?
15. What are some common errors in AVR Studio and how can they be fixed?
16. How can you optimize code in AVR Studio for better performance?
17. How do you use interrupts in AVR Studio?
18. What is the “I/O View” in AVR Studio?
19. What are the advantages of using AVR Studio?
20. What are some alternatives to AVR Studio?

46 Electronics & Communication Engineering Department, GEC Bharuch


Microprocessor & Microcontroller (3141008) Date:

EXPERIMENT – 8

Aim: How to Program AVR Microcontroller.

Hardware reuired: USBasp, ATmega32 microcontroller, connecting wires,


bread board.

Software reuired: 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.

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

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


49 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

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

51 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 :

52 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

53 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 uickly : 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.

Electronics & Communication Engineering Department, GEC Bharuch


Microprocessor & Microcontroller (3141008) Date:

Observation:

Conclusion:

Electronics & Communication Engineering Department, GEC Bharuch


Microprocessor & Microcontroller (3141008) Date:

Review Questions:

1. What is USBasp, and what is its primary use in AVR microcontroller programming?
2. Why is USBasp preferred over traditional parallel programmers for AVR
microcontrollers?
3. What are the main components of the USBasp hardware?
4. Describe the role of the onboard microcontroller in USBasp.
5. What types of connectors are commonly used in USBasp for connecting to the AVR
microcontroller?
6. How does the USBasp provide power to the target device? Can it power the target
microcontroller directly?
7. Which software tools are commonly used with USBasp to program AVR
microcontrollers (e.g., AVRDude)?
8. Why is a driver often required for USBasp on certain operating systems?
9. How do you install and configure USBasp drivers on Windows, Linux, and macOS?
10. What are the typical steps to program an AVR microcontroller using USBasp and
AVRDude?
11. What are fuse bits in AVR microcontrollers, and how can you configure them using
USBasp?
12. Explain the role of the -U option in AVRDude when using USBasp.
13. What is the purpose of the jumper settings on the USBasp board (e.g., slow SCK
jumper)?
14. How can you verify a successful upload or programming of firmware using USBasp?
15. What could cause a USBasp to not be recognized by a computer, and how would you
troubleshoot this issue?
16. If programming fails due to a "target not detected" error, what steps would you take
to resolve the issue?
17. How do you handle situations where the USBasp programmer operates too quickly
for a new AVR microcontroller?
18. Can USBasp be used to flash bootloaders onto AVR microcontrollers? If so, how?
19. How does USBasp support ISP (In-System Programming)?
20. What are the limitations of USBasp when compared to other AVR programmers like
Atmel ICE?

Electronics & Communication Engineering Department, GEC Bharuch


Microprocessor & Microcontroller (3141008) Date:

EXPERIMENT – 9

Aim: Interfacing of LED with ATmega32 microcontroller.

Hardware reuiered: USBasp loader, ATmega32 microcontroller, Resistors,


LEDs, Connecting wires.

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

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

Interfacing diagram:

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

Electronics & Communication Engineering Department, GEC Bharuch


Microprocessor & Microcontroller (3141008) Date:

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

Interfacing diagram:

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

Electronics & Communication Engineering Department, GEC Bharuch


Microprocessor & Microcontroller (3141008) Date:

Observation:

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


Interfacing diagram:

Program:
#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);

Electronics & Communication Engineering Department, GEC Bharuch


Microprocessor & Microcontroller (3141008) Date:

PORTB = 0x08;
_delay_ms(1000);
PORTB = 0x10;
_delay_ms(1000);
PORTB = 0x20;
_delay_ms(1000);
PORTB = 0x40;
_delay_ms(1000);
PORTB = 0x80;
_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:

Electronics & Communication Engineering Department, GEC Bharuch


Microprocessor & Microcontroller (3141008) Date:

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

Interfacing diagram:

Program:
#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;
}

Electronics & Communication Engineering Department, GEC Bharuch


Microprocessor & Microcontroller (3141008) Date:

Observation:

Conclusion:

Review Questions:

1. What is the purpose of interfacing an LED with an ATmega32?


2. Which ATmega32 pins can be used to interface with LEDs?
3. Why is a current-limiting resistor required when interfacing an LED with ATmega32?
4. How do you calculate the value of the current-limiting resistor for an LED?
5. What is the typical forward voltage and current rating of a standard LED?
6. How do you configure a pin as an output in ATmega32 for interfacing an LED?
7. How do you turn an LED ON or OFF using ATmega32?
8. What is the difference between sourcing and sinking current in the context of LED
interfacing?
9. Write a simple C code snippet to blink an LED connected to pin PB0 of ATmega32.
10. How can you control the brightness of an LED using PWM in ATmega32?
11. Provide a code snippet to generate a PWM signal on OC0 (PB3) for LED dimming
using Timer0.

Electronics & Communication Engineering Department, GEC Bharuch


Microprocessor & Microcontroller (3141008) Date:

12. What are common issues when interfacing LEDs with ATmega32, and how do you
resolve them?
13. How can multiple LEDs be controlled simultaneously using ATmega32?
14. How would you interface an RGB LED with ATmega32?
15. What are some applications of LED interfacing with microcontrollers?

Electronics & Communication Engineering Department, GEC Bharuch


Microprocessor & Microcontroller (3141008) Date:

EXPERIMENT – 10

Aim: Interfacing of switch and LED with ATmega32 microcontroller.

Hardware reuiered: USBasp loader, ATmega32 microcontroller, Resistors,


LEDs, Connecting wires.

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

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

Interfacing diagram:

Program:

#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;

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:

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

#define LED 0x01


#define SW 0x02

int main (void)


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:

Electronics & Communication Engineering Department, GEC Bharuch


Microprocessor & Microcontroller (3141008) Date:

Program:

#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
{

Electronics & Communication Engineering Department, GEC Bharuch


Microprocessor & Microcontroller (3141008) Date:

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

Observation:

Conclusion:

Review Questions:
1. What is the purpose of interfacing an LED and a switch with the ATmega32
microcontroller?
2. What are the key pins on the ATmega32 used for LED and switch interfacing?
3. How do you connect an LED to a GPIO pin of ATmega32?
4. Why is a resistor used when interfacing an LED with ATmega32?
5. How do you configure a GPIO pin to control an LED in ATmega32?
6. How can you turn the LED ON or OFF programmatically?

60 Electronics & Communication Engineering Department, GEC Bharuch


Microprocessor & Microcontroller (3141008) Date:

7. How do you connect a switch to a GPIO pin of ATmega32?


8. What is the purpose of a pull-up or pull-down resistor in switch interfacing?
9. How do you configure a GPIO pin to read a switch state in ATmega32?
10. How do you read the state of a switch programmatically?
11. How can you toggle an LED using a switch in ATmega32?
12. What debounce issue might occur when using a switch, and how can it be handled?
13. Write a simple program to turn on an LED when a switch is pressed and turn it off
when released.
14. How can you interface multiple LEDs and switches with ATmega32?
15. What precautions should be taken when connecting multiple LEDs and switches to
ATmega32?
16. What could cause an LED not to turn on during interfacing?
17. Why might a switch not respond as expected in your program?

61 Electronics & Communication Engineering Department, GEC Bharuch


Microprocessor & Microcontroller (3141008) Date:

EXPERIMENT – 11

Aim: Interfacing of seven segment display with ATmega32 microcontroller.

Hardware requiered: USBasp loader, ATmega32 microcontroller, Resistors,


LEDs, Connecting wires, Seven Segment, BCD to seven segment converter.

Software required: AVR Studio, avrdude, USBasp driver.

Program11A:Write a C program to display 0 to 9 on single seven segment.

Interfacing diagram:

Program:
#include <avr/io.h>
#include <util/delay.h>
int main(void)
{
unsigned char seg_code[ ]={0xfc, 0x60, 0xda, 0xf2, 0x66, 0xb6, 0xbe, 0xe0, 0xfe,
0xf6, 0xee, 0xfe, 0x9c, 0xfc, 0x9e, 0x8e};
unsigned char cnt=0;
DDRD = 0xFF;
While(1)
{
for(cnt =0x00; cnt<=0x09; cnt++)

62 Electronics & Communication Engineering Department, GEC Bharuch


Microprocessor & Microcontroller (3141008) Date:

{
PORTD = seg_code[cnt];
_delay_ms(1000);
}
}
return 0;
}

Observation:

Program11B:Write a C program to display 00 to 99 on two seven segment.

Interfacing diagram:

Program:
#include <avr/io.h>
63 Electronics & Communication Engineering Department, GEC Bharuch
Microprocessor & Microcontroller (3141008) Date:

#include <util/delay.h>
#define SegOne 0x01
#define SegTwo 0x02
int main(void)
{
unsigned char seg_code[ ]={0xfc, 0x60, 0xda, 0xf2, 0x66, 0xb6, 0xbe, 0xe0, 0xfe,
0xf6, 0xee, 0xfe, 0x9c, 0xfc, 0x9e, 0x8e};
int cnt, num, temp=0, i;
DDRD = 0xFF;
DDRB = 0xFF;
While(1)
{
for (cnt = 0x00; cnt <= 99; cnt++)
{
for (i = 0; i <50; i++)
{
num = cnt;
temp = num % 10;
num = num / 10;
PORTB = SegTwo;
PORTD = seg_code[temp];
_delay_ms(10);
temp = num % 10;
num = num / 10;
PORTB = SegOne;
PORTD = seg_code[temp];
_delay_ms(10);
}
}
}
return 0;
}

64 Electronics & Communication Engineering Department, GEC Bharuch


Microprocessor & Microcontroller (3141008) Date:

Observation:

Conclusion:

Review Questions:
1. What is a seven-segment display, and how does it work?
2. What are the types of seven-segment displays?
3. How is a seven-segment display interfaced with an ATmega32 microcontroller?
4. Which ATmega32 ports are commonly used for interfacing a seven-segment
display?
5. What is the role of a current-limiting resistor in a seven-segment interface?
6. What is the difference between a common cathode and a common anode 7-
segment display? How do the segment control requirements differ for each type
when interfacing with an ATmega32?
7. How would you connect a 7-segment display to the ATmega32 microcontroller?
What are the pin assignments for a 7-segment display with 7 individual
segments (A to G)?
8. What is multiplexing in the context of 7-segment displays? How can it be
implemented to drive multiple 7-segment displays using the ATmega32?
9. Write the segment pattern for displaying the digit "5" on a common cathode 7-
segment display and explain how it works.

65 Electronics & Communication Engineering Department, GEC Bharuch


Microprocessor & Microcontroller (3141008) Date:

10. Why is it necessary to use current-limiting resistors when interfacing a 7-


segment display with the ATmega32, and what would happen if they were
omitted?
11. Describe the steps needed to multiplex a 4-digit 7-segment display using the

ATmega32.
12. What considerations must be made to ensure the display updates correctly
without flickering?
13. If a 7-segment display is flickering when displaying digits, what potential issues
could cause this problem, and how would you resolve them?
14. Explain the role of the DDR (Data Direction Register) and PORT registers in
controlling a 7-segment display using the ATmega32. How would you configure
the ports for segment control?
15. What are the control methods for multiple seven-segment displays?
16. How is multiplexing implemented in a multi-digit seven-segment display?
17. Write a simple C program to display digits 000-999 sequentially on a multiple
seven-segment display using ATmega32 also draw the interfacing diagram.

66 Electronics & Communication Engineering Department, GEC Bharuch

You might also like