Arduino Sketches

Arduino Sketches from the Ultimate Arduino MEGA 2560 Hardware Manual can be found below. Select the desired sketch and copy the code, then paste it to the Arduino IDE. Sketches are displayed below under the section number that they occur in in the book, with the page number that they can be found on.

1.5.2.2 Loading a Test Sketch, Page 51

Modified Blink sketch.

void setup() {
  pinMode(LED_BUILTIN, OUTPUT);     // Configure L LED pin as output
}

void loop() {
  digitalWrite(LED_BUILTIN, HIGH);  // Switch L LED ON
  delay(200);                       // Leave L LED ON for period
  digitalWrite(LED_BUILTIN, LOW);   // Switch L LED OFF
  delay(200);                       // Leave L LED OFF for period
}

2.7.4.1 Output Pins, Page 79

Arduino MEGA 2560 pin 12 configured as an output and driven high and low.

void setup() {
  pinMode(12, OUTPUT);    // Configure digital pin 12 as an output pin
}

void loop() {
  digitalWrite(12, HIGH); // Set pin 12 voltage to 5V on a MEGA 2560
  delay(500);
  digitalWrite(12, LOW);  // Set pin 12 voltage to 0V or GND
  delay(500);
}

2.7.4.1 Output Pins, Page 80

Defining a pin with a user friendly name.

#define MY_LED  12

void setup() {
  pinMode(MY_LED, OUTPUT);    // Configure digital pin as an output pin
}

void loop() {
  digitalWrite(MY_LED, HIGH); // Set pin voltage to 5V on a MEGA 2560
  delay(500);
  digitalWrite(MY_LED, LOW);  // Set pin voltage to 0V or GND
  delay(500);
}

2.7.4.4 PWM Pins, Page 83

Arduino MEGA 2560 PWM on pin 3 with 50% duty cycle.

void setup() {
  pinMode(3, OUTPUT);

  analogWrite(3, 127);
}

void loop() {
}

2.7.5.3 Calculating Analog In Voltage, Page 88

Calculating analog input voltage on Arduino MEGA 2560 A0 pin.

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

void loop() {
  int sensorValue = analogRead(A0);                     // Read ADC value
  float voltage = (5.0 / 1024.0) * (float)sensorValue;  // Calculate voltage
  Serial.print(voltage);                                // Display voltage
  Serial.println(" V");
  delay(1);
}

2.7.5.4 Analog In Pins Used as Digital I/O, Page 89

Arduino MEGA 2560 analog pin A0 used as a digital output pin.

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

void loop() {
  digitalWrite(A0, HIGH);
  delay(500);
  digitalWrite(A0, LOW);
  delay(500);
}

3.3.1.2 Pins as Inputs, Page 120

Pull-down Resistor

Read Arduino MEGA 2560 pin 2 state with pull-down resistor.

void setup() {
  pinMode(LED_BUILTIN, OUTPUT);
  pinMode(2, INPUT);
}

void loop() {
  if (digitalRead(2)) {
    // Pin state is high or 1, switch is closed (pull-down resistor)
    digitalWrite(LED_BUILTIN, HIGH);  // Switch L LED on
  }
  else {
    // Pin state is low or 0, switch is open (pull-down resistor)
    digitalWrite(LED_BUILTIN, LOW);   // Switch L LED off
  }
}

3.3.1.2 Pins as Inputs, Page 121

Pull-up Resistor

Read Arduino MEGA 2560 pin 2 state with pull-up resistor.

void setup() {
  pinMode(LED_BUILTIN, OUTPUT);
  pinMode(2, INPUT);
}

void loop() {
  if (digitalRead(2)) {
    // Pin state is high or 1, switch is open (pull-up resistor)
    digitalWrite(LED_BUILTIN, LOW);    // Switch L LED off
  }
  else {
    // Pin state is low or 0, switch is closed (pull-up resistor)
    digitalWrite(LED_BUILTIN, HIGH);   // Switch L LED on
  }
}

3.3.1.2 Pins as Inputs, Page 122

Internal Pull-up Resistors

Read Arduino MEGA 2560 pin 2 state with internal pull-up resistor enabled.

void setup() {
  pinMode(LED_BUILTIN, OUTPUT);
  pinMode(2, INPUT_PULLUP);
}

void loop() {
  if (digitalRead(2)) {
    // Pin state is high or 1, switch is open (pull-up resistor)
    digitalWrite(LED_BUILTIN, LOW);    // Switch L LED off
  }
  else {
    // Pin state is low or 0, switch is closed (pull-up resistor)
    digitalWrite(LED_BUILTIN, HIGH);   // Switch L LED on
  }
}

3.3.4.2 Accessing TWI Devices in Software, Page 127

Read the first byte from an AT24C16C EEPROM connected to the Arduino MEGA 2560 TWI bus pins.

// Reads a single byte from a AT24C16C EEPROM on the TWI or I2C bus
#include <Wire.h>

void setup() {
  Wire.begin();         // Join TWI bus as a master
  Serial.begin(9600);   // For displaying results in Serial Monitor window
  
  // *** Perform Random Read ***
  // Address byte 0 of EEPROM
  Wire.beginTransmission((0xA0 >> 1));
  Wire.write(0x00);
  Wire.endTransmission();
  // Read 1 byte from EEPROM at address 0
  Wire.requestFrom((0xA0 >> 1), 1);
  // *** End Perform Random Read ***

  while (Wire.available()) {          // Check for response from EEPROM
    byte data8 = Wire.read();         // Read byte from the EEPROM
    Serial.print("Byte read is: ");
    Serial.println(data8, HEX);       // Display the byte
  }
}

void loop() {
}

3.3.6.1 Hardware Serial Ports, Page 132

Using the USB Port / Serial Port 0

Sketch that echos back data to the Serial Monitor window of the Arduino IDE after being sent to the Arduino MEGA 2560.

serial_echo sketch on USB Port / Serial Port 0 (TX0 and RX0)

void setup() {
  Serial.begin(9600);               // Initialize serial port 0
}

void loop() {
  if (Serial.available()) {         // Check if character received
    char rx_char = Serial.read();   // Read the character
    Serial.print("Received: ");     // Transmit a string
    Serial.println(rx_char);        // Transmit the received character
  }
}

Using Serial Port 1, Serial Port 2 and Serial Port 3

The same sketch as above can be used to test serial ports 1 to 3 with a small modification as shown below.

serial_echo_1 sketch on Serial Port 1 (TX1 and RX1)

void setup() {
  Serial1.begin(9600);               // Initialize serial port 1
}

void loop() {
  if (Serial1.available()) {         // Check if character received
    char rx_char = Serial1.read();   // Read the character
    Serial1.print("Received: ");     // Transmit a string
    Serial1.println(rx_char);        // Transmit the received character
  }
}

7.4.2 Testing for PWM Waveforms, Page 192

Use the sketch to generate PWM (Pulse Width Modulation) waveforms on each of the 15 PWM pins of the Arduino MEGA 2560.

void setup() {
  analogWrite(2, 26);   // PWM 10% 490Hz
  analogWrite(3, 38);   // PWM 15% 490Hz
  analogWrite(4, 51);   // PWM 20% 980Hz
  analogWrite(5, 64);   // PWM 25% 490Hz
  analogWrite(6, 77);   // PWM 30% 490Hz
  analogWrite(7, 89);   // PWM 35% 490Hz
  analogWrite(8, 102);  // PWM 40% 490Hz
  analogWrite(9, 115);  // PWM 45% 490Hz
  analogWrite(10, 128); // PWM 50% 490Hz
  analogWrite(11, 140); // PWM 55% 490Hz
  analogWrite(12, 153); // PWM 60% 490Hz
  analogWrite(13, 166); // PWM 65% 980Hz
  analogWrite(44, 179); // PWM 70% 490Hz
  analogWrite(45, 191); // PWM 75% 490Hz
  analogWrite(46, 204); // PWM 80% 490Hz
}

void loop() {
}

7.4.3 Testing UART Outputs, Page 194

Send a square wave out of each hardware serial port or UART on the Arduino MEGA 2560.

void setup() {
  Serial.begin(9600);
  Serial1.begin(9600);
  Serial2.begin(9600);
  Serial3.begin(9600);
}

void loop() {
  Serial.print('U');    // Pin 1, TX0
  Serial1.print('U');   // Pin 18, TX1
  Serial2.print('U');   // Pin 16, TX2
  Serial3.print('U');   // Pin 14, TX3
}

7.4.4 TWI Signals, Page 196

The simple sketch below can be used to test the TWI bus signals without the need to attach a TWI device to this bus.

#include <Wire.h>

void setup() {
  Wire.begin();
  Wire.beginTransmission((0xA0 >> 1));
  Wire.write(0x00);
  Wire.endTransmission();
}

void loop() {
}

7.4.5 SPI Signals, Page 198

The sketch simply sends a character out on the MOSI pin continually (Arduino MEGA 2560 digital pin 51).

#include <SPI.h>

void setup() {
  SPI.begin();
  SPI.beginTransaction(SPISettings(10000, MSBFIRST, SPI_MODE1));
}

void loop() {
  SPI.transfer('U');
  delay(1);
}