Lab 1

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 4

Lab 1

Introduction to Arduino Mega 2560

1 Aim
The aim of this lab is to:

• familiarize yourselves with the Arduino Mega 2560

• learn to write and compile a simple program using the Arduino software

• learn to upload a program into the ATmega2560 microcontroller

• learn to construct Arduino based circuits

2 Materials
• Arduino Mega 2560 Board

• PC with USB port and Arduino software

• USB cable

• Breadboard

• LED

• 120Ω resistor

• Wires

3 Introduction
The Arduino Mega 2560 is a microcontroller board based on the ATmega2560. It has 54
digital input/output pins (of which 14 can be used as PWM outputs), 16 analog inputs, 4
UARTs (hardware serial ports), a 16 MHz crystal oscillator, a USB connection, a power
jack, an ICSP header, and a reset button. A diagram of the Arduino Mega 2560 is shown
in Fig. 1.

1
3//
 ;;9#

<  * =, ! >=,*  
+' >=,
44#'8 =,
/ .+ " ()

Figure 1: Arduino Mega 2560 Board

The Arduino Mega 2560 can be programmed with the Arduino software. The AT-
mega2560 on the Arduino Mega comes preburned with a bootloader that allows you to
upload new code to it without the use of an external hardware programmer. Each of
the 54 digital pins on the Mega 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) of 20-50 kΩ. The Arduino Mega2560 can be powered via the USB connection
or with an external power supply. The power source is selected automatically. Some of
the technical specifications of the Arduino Mega 2560 is given in the table below.

Table 1: Specifications of the Arduino Mega 2560


Microcontroller ATmega2560
Operating Voltage 5V
Input Voltage (recommended) 7-12V
Input Voltage (limits) 6-20V
Digital I/O Pins 54 (of which 14 provide PWM output)
Analog Input Pins 16
DC Current per I/O Pin 40 mA
DC Current for 3.3V Pin 50 mA
Flash Memory 256 KB of which 8 KB used by bootloader
SRAM 8 KB
EEPROM 4 KB
Clock Speed 16 MHz

2
4 Schematic
Create a circuit as shown in Fig. 2 on the breadboard. To build the circuit, attach a
120-ohm resistor to pin 13. Then attach the long leg of an LED (the positive leg, called
the anode) to the resistor. Attach the short leg (the negative leg, called the cathode) to
ground. Then plug your Arduino board into your computer via USB.

Figure 2: Breadboard circuit (image from fritzing)

5 Source Code
Start the Arduino software and enter the code as follows. After creating the sketch, click
on Compile button. If there is any error, debug the error in your sketch. After the
status display at the bottom of the Arduino software say “Done Compiling”, then click
on the Upload button. The TX and RX led’s should start flashing to indicate that the
program is being uploaded to the flash memory of the ATmega2560 microcontroller.

1 /*
2 Blink
3 Turns on an LED on for one second , then off for one second , repeatedly .
4 */
5
6 // Pin 13 has an LED connected on most Arduino boards .
7 // give it a name :
8 int led = 13;
9
10 // the setup routine runs once when you press reset :
11 void setup () {
12 // initialize the digital pin as an output .
13 pinMode ( led , OUTPUT );
14 }

3
15
16 // the loop routine runs over and over again forever :
17 void loop () {
18 digitalWrite ( led , HIGH ); // turn the LED on ( HIGH is the voltage level )
19 delay (1000); // wait for a second
20 digitalWrite ( led , LOW ); // turn the LED off by making the voltage LOW
21 delay (1000); // wait for a second
22 }

6 Notes
• In the program above, the first thing you do is to initialize pin 13 as an output pin
with the line
pinMode(13, OUTPUT);

• In the main loop, you turn the LED on with the line:
digitalWrite(13, HIGH);

• This supplies 5 volts to pin 13. That creates a voltage difference across the pins of
the LED, and lights it up. Then you turn it off with the line:
digitalWrite(13, LOW);

• That takes pin 13 back to 0 volts, and turns the LED off. In between the on and the
off, you want enough time for a person to see the change, so the delay() commands
tell the Arduino to do nothing for 1000 milliseconds, or one second. When you use
the delay() command, nothing else happens for that amount of time.

You might also like