Introduction to Arduino Mega2560 R3

ARDUINO MEGA 2560

Introduction

The ARDUINO MEGA 2560 is designed for projects that require more I/O lines, more sketch memory, and more RAM. With 54 digital I/O pins, 16 analog inputs and a larger space for your sketch it is the recommended board for 3D printers and robotics projects. This gives your projects plenty of room and opportunities maintaining the simplicity and effectiveness of the ARDUINO platform. This document explains how to use your Mega2560 board.

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Components Required:

·        ARDUINO Mega 2560

·        LDR sensor

·        LED

·        Bread Board

·        Jumper Wires

Specification of 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

 

 

 

 

 

 

 

 

 

USB Connector:

USB Connector is used to Upload Program from PC to Arduino Mega. It is also used to Power up the ARDUINO

Power Connector:

It is used to power the ARDUINO using DC adaptor

Using this terminal we can give 7 to 12v supply.

Power Supply Headers:

 ARDUINO Mega has onboard power supply headers through which we can power up Sensors and other I/O peripherals.

Analog Headers:

ARDUINO Mega has 16 Analog pins which are used for reading analog sensors. They are also can be used for reading digital signals.

 Digital Headers:

 ARDUINO Mega has 54 Digital I/O pins which can be used for both input and output purposes digital headers also have pins for SPI communication Communication Headers:

Communications headers consist of serial communication pins ARDUINO Mega have 10 communication pins. These headers also have 4 interrupt pins. 

Pulse Width Modulation:

Pulse Width Modulation, or PWM, is a technique for getting analog results with digital means. Digital control is used to create a square wave, a signal switched between on and off. This on-off pattern can simulate voltages in between full on (5 Volts) and off (0 Volts) by changing the portion of the time the signal spends on versus the time that the signal spends off. The duration of "on time" is called the pulse width. To get varying analog values, you change or modulate, that pulse width.

 Function for reading PWM = analogRead (analog pin no);

Function for writing PWM = analogWrite (PWM pin no, value);

 

ICSP Header:

                     ICSP headers are used for SPI (Serial Peripheral Interface) communication these headers are generally used for ARDUINO to PC communication for boot loading and for ARDUINO to ARDUINO communications.

Reading analog Sensor:

To measure the value of analog signals, the ARDUINO has a built-in analog-to-digital converter (ADC). The ADC turns the analog voltage into a digital value. The function that you use to obtain the value of an analog signal is analogRead(pin). This function converts the value of the voltage on an analog input pin and returns a digital value from 0 to 1023, relative to the reference value. The reference is 5V on most ARDUINOs, 7V on the ARDUINO Mini and Nano, and 15V on ARDUINO Mega. It has one parameter which is the pin number.

ARDUINO programming:

For start doing anything with the ARDUINO, you need to download and install the Arduino IDE (integrated development environment). From this point on we will be referring to the ARDUINO IDE as the ARDUINO Programmer.
The ARDUINO Programmer is based on the Processing IDE and uses a variation of the C and C++ programming languages.

 

After installing open the IDE in your computer. Before starting the program you must select the board type and serial port in IDE. To set the board, go to the following:

Tools --> Boards

 

 

Selecting ARDUINO board in IDE

 

 

Serial Communication:

Used for communication between the ARDUINO board and a computer or other devices. All ARDUINO boards have at least one serial port (also known as a UART or USART): Serial. The ARDUINO Mega has three additional serial ports: Serial1 on pins 19 (RX) and 18 (TX), Serial2 on pins 17 (RX) and 16 (TX), Serial3 on pins 15 (RX) and 14 (TX). To use these pins to communicate with your personal computer, you will need an additional USB-to-serial adaptor, as they are not connected to the Mega’s USB-to-serial adaptor. To use them to communicate with an external TTL serial device, connect the TX pin to your device’s RX pin, the RX to your device’s TX pin, and the ground of your Mega to your device’s ground.

 

Sketch:

The program which we write in ARDUINO IDE is known as a sketch. The ARDUINO sketch is consist of 2 main functions. One is ‘’void setup’’ and another is ‘’void loop’’.

 

 Void Setup:

The setup () function is called when a sketch starts. Use it to initialize variables, pin modes, start using libraries, etc. The setup() function will only run once, after each power-up or reset of the ARDUINO board.

Void loop:

After creating a setup () function, which initializes and sets the initial values, the loop () function does precisely what its name suggests, and loops consecutively, allowing your program to change and respond. Use it to actively control the ARDUINO board.

Obtaining Digital Output:

 

       

Sketch for obtaining digital output

    

 

  For obtaining digital output follow the following instructions                                                                         

·        Initialize the output device such as LED or Buzzer or other etc.

·        Then define the output pin in ‘’Void Setup’’ which you select for the output.

·        Then write your code which defines what you want to do your ARDUINO Mega i:e turn ON the LED or blink the LED etc.

                       

 Reading Digital Input:

 Reading digital Input follow the following Instruction

·           Initialize the input device such as Sensor or another input device.

·           Then initialize a variable to store the value of sensors in it.

·           Then define the input pin in ‘’Void Setup’’ which you select for the input.

·           Then write your code which defines what you want to do your ARDUINO Mega i:e reading sensors value on a serial monitor or activate a load etc.

 

 

Sketch for reading Digital input

Code for Digital Output: 

int LED = 12;                          //initialize LED with pin 13

void setup() {

pinMode (LED,OUTPUT);       // define LED as an OUTPUT

}

 

void loop() {

 

   digitalWrite(LED,HIGH);           // turn on LED

   delay (500);                             // Wait for 500m Seconds

   digitalWrite(LED,LOW);            // turn off LED

   delay (500);                             // Wait for 500m Seconds

   }

   }

 

Digital input

 

Code for Digital Input:

  int Pbutton = 11;               //initialize LDR with pin 4

   int Pbuttonvalue = 0;       //initialize a variable with 0

   int LED = 12;                   //initialize LED with pin 13

void setup() {

   

   pinMode (Pbutton,INPUT);   // define LDR as an INPUT

   Serial.begin(9600);               // initialize serial port

   pinMode (LED,OUTPUT);    // define LED as an OUTPUT

}

 

void loop() {

 

   Pbuttonvalue = digitalRead(LDR);   // store the sensor value in variable

   if (Pbuttonvalue == HIGH)               // if sensor value is high

   {

   Serial.println(Pbuttonvalue);             // print LDR value on serial monitor

   delay (500);                                      // wait for 500m Seconds

   digitalWrite(LED,HIGH);                   // turn on LED

   delay (500);                                      // Wait for 500m Seconds

   }

   else{  

     Serial.println(Pbuttonvalue);           // print LDR value on serial monitor

     delay (500);                                     // wait for 500m Seconds                    

     digitalWrite(LED,LOW);                   // turn off LED

     delay (500);                                     // Wait for 500m Seconds

   }

  

}

 

Digital output