Day 1: (Mon Aug 26) Welcome, Arduino Introduction

Notes for 2019-08-26. See also the Fall 2019 Calendar.

Agenda

  1. Welcome.

  2. Administrative

  3. Assignments

  4. In-class

    1. Brief introduction to the Arduino.

    2. Each person pick up an Arduino, USB cable, cluster laptop, breadboard, speaker, dropping resistor, and a few jumper wires.

    3. Crash course in Arduino basics.

    4. Write an Arduino sketch to play tones in sequence to create a siren. For now, it is easiest to use tone() and delay() inside loop().

    5. Face photos.

    6. Related exercises: Exercise: Arduino Introduction, Exercise: Coding, Compiling, Deploying, Exercise: Unipolar Drivers

    7. If time permits: rewrite your sketch without using delay(), then add a simultaneous fast-blink on the LED. For ideas, see Exercise: Event-Loop Programming

    8. At the end of class, please return the Arduinos and other components.

Figures

../_images/EDArduino-UNO-Arduino-Compatible-400x400.jpg ../_images/breadboard-connections.jpg ../_images/resistor-color-code.png

Lecture code samples

(See also Lecture Sample Code).

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
// 1. blink the onboard Arduino LED on pin 13.
// 2. demonstrate setup(), loop()
// 3. demonstrate pinMode(), digitalWrite(), delay()

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

void loop()
{
  digitalWrite(13, HIGH);
  delay(1000);

  digitalWrite(13, LOW);
  delay(1000);
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
// 1. generate tones on a speaker on pin 5
// 2. demonstrate tone(), noTone(), delay()

void setup()
{
  // empty body, tone() configures a pin for output
}
void loop()
{
  tone(5, 440);
  delay(1000);

  noTone(5);
  delay(1000);

  tone(5, 660);
  delay(1000);
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
// 1. generate chromatic tones on a speaker on pin 5
// 2. demonstrate for loops, int and float variables

void setup()
{
}

void loop()
{
  float freq = 440.0;
  for (int i = 0; i < 24; i = i + 1) {
    tone(5, freq);
    delay(200);
    freq = freq * 1.0595;
  }
  noTone(5);
  delay(1000);
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
// 1. measure execution time
// 2. demonstrate the firmware clock
// 3. demonstrate debugging on the serial port

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

long last_time = 0;

void loop()
{
  long now = micros();

  Serial.print(millis());
  Serial.print(" ");  

  Serial.print(micros());
  Serial.print(" ");

  Serial.println(now - last_time);

  last_time = now;
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
// 1. generate complex waveform on a speaker on pin 5
// 2. demonstrate execution speed, firmware clocks, bit-banging logic

void setup()
{ 
  pinMode(5, OUTPUT);
}
void loop()
{
  long now = micros();
  bool square1 = (now % 2274) < 1136;
  bool square2 = (now % 1516) < 758;
  digitalWrite(5, square1 ^ square2);
}