1-10
1-10
Lab Manual
GOVERNMENT ENGINEERING
COLLEGE BHARUCH
GOVERNMENT ENGINEERING COLLEGE
BHARUCH
CERTIFICATE
Mr./Miss of _
Date:
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.
4. Write a C program to
d isplay 8-bit binary counter
value on 8 LED.
Institute Mission
1. Providing State-of-the-art infrastructure by strengthening industry-
institute- interaction to achieve excellence in engineering education.
Department Mission
EXPERIMENT - 1
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.
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.
• 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
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.
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.
• PWM: 3, 5, 6, 9, 10, and 11. Provide 8-bit PWM output with the analogWrite()
function.
• 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
ground to 5 volts, though it is possible to change the upper end of their range
using the AREF pin and the analogReference( ) function.
• TWI: A4 or SDA pin and A5 or SCL pin. Support TWI communication using the
Wire library.
Arduino Programming
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.
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
In the Arduino IDE, select the Serial Port as the port to which the Arduino is
mapped.
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( )
{
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).
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.
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
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:
Review Questions:
EXPERIMENT - 2
Interfacing diagram:
Schematic Diagram:
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
}
Observation:
Conclusion:
Review Questions:
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
Interfacing diagram:
Schematic Diagram:
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:
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?
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
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
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:
Schematic Diagram:
Arduino program:
#include <LiuidCrystal.h>
void setup( )
{
lcd.begin(16, 2);
}
void loop( )
{
lcd.setCursor(0, 0); lcd.print("GEC
Bharuch"); lcd.setCursor(0, 1);
lcd.print("EC Department");
}
Observation:
Conclusion:
Review Questions:
EXPERIMENT - 5
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)
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:
Review Questions:
EXPERIMENT - 6
Aim: Write and execute Arduino program to read analog value. Sense
temperature using LM35 sensor and display temperature value on LCD.
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.
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:
Arduino program:
Observation:
Conclusion:
Review Questions:
EXPERIMENT - 7
Introduction:
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.
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.
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.
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.
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
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.
Observation:
Conclusion:
Review Questions:
EXPERIMENT – 8
Introduction:
Hardware
USBasp
Or
USBasp based AVR Development board
USBasp
Make sure you have Atmel Studio, WinAVR and a USBasp to follow this tutorial.
First Step
Install both Atmel Studio and WinAVR. You can follow the guided instruction
embedded in the installation wizard.
Next step
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.
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.
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
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.
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
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
Pinout of USBasp
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.
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:
If you have NOT added the avrdude’s path to the system path, execute following
command with your path. Otherwise skip this step.
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
-c : Indicates the programmer type. Since we are using the USBasp programmer,
argument “usbasp” is mentioned.
-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
Op
Filename
SpecIfy the hex file name. If file is not in current directory specIfy file name with
appropriate path.
Format
Format need not be specIfied, for hex files, avrdude will automatically detect the
format.
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.
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.
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:
Conclusion:
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?
EXPERIMENT – 9
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:
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;
}
Observation:
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);
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:
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;
}
Observation:
Conclusion:
Review Questions:
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?
EXPERIMENT – 10
Interfacing diagram:
Program:
#include <avr/io.h>
#include <util/delay.h>
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>
Observation:
Program 9C: Write a C program to blink alternate LED by pressing the same
switch.
Interfacing diagram:
Program:
#include <avr/io.h>
#include <util/delay.h>
int main(void)
{
DDRB |= G_LED; DDRB &=
~SW; DDRB |= R_LED;
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
{
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?
EXPERIMENT – 11
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++)
{
PORTD = seg_code[cnt];
_delay_ms(1000);
}
}
return 0;
}
Observation:
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;
}
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.
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.