Arduino Sketches

Arduino Sketches from the Ultimate Arduino Uno 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 – Page 45

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 – Page 71

Arduino Uno 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 an Arduino Uno
  delay(500);
  digitalWrite(12, LOW);  // set pin 12 voltage to 0V or GND
  delay(500);
}

2.7.4.1 – Page 72

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 an Arduino Uno
  delay(500);
  digitalWrite(MY_LED, LOW);  // set pin voltage to 0V or GND
  delay(500);
}

2.7.4.4 – Page 75

Arduino Uno PWM on pin 3 with 50% duty cycle.

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

  analogWrite(3, 127);
}

void loop() {
}

2.7.5.3 – Page 79

Calculating analog input voltage on Arduino Uno 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.5 – Page 80

Arduino Uno 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 – Page 106

Pull-down Resistor

Read Arduino Uno 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 – Page 108

Pull-up Resistor

Read Arduino Uno 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 – Page 109

Internal Pull-up Resistors

Read Arduino Uno 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 – Page 113

Read the first byte from an AT24C16C EEPROM connected to the Arduino Uno 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() {
}

7.4.2 – Page 172

Use all of the PWM pins on the Arduino Uno to produce PWM waveforms with different duty cycles.

void setup() {
  analogWrite(3, 51);     // PWM 20% 490Hz
  analogWrite(5, 89);     // PWM 35% 976Hz
  analogWrite(6, 127);    // PWM 50% 976Hz
  analogWrite(9, 166);    // PWM 65% 490Hz
  analogWrite(10, 204);   // PWM 80% 490Hz
  analogWrite(11, 230);   // PWM 90% 490Hz
}

void loop() {
}

7.4.3 – Page 172

Arduino Uno serial port or UART test square wave.

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

void loop() {
  Serial.print('U');
}

7.4.5 – Page 175

Send a character on the Arduino Uno SPI bus.

#include <SPI.h>

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

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