Module 6 · Answers

Answers & explanations

Section A — Easy

01
setup() runs once when the board starts. loop() runs over and over.
02
(a) Set whether a pin is INPUT or OUTPUT.
(b) Set a digital pin HIGH or LOW.
(c) Read whether a digital pin is HIGH or LOW.
(d) Pause the program for a given number of milliseconds.
03
0 to 1023 (a 10-bit value).
04
Pulse Width Modulation. It switches a digital pin on and off very quickly to fake an analogue voltage — used to dim LEDs and control motor speed.

Section B — Medium

05
void setup() {
  pinMode(9, OUTPUT);
}

void loop() {
  digitalWrite(9, HIGH);
  delay(500);
  digitalWrite(9, LOW);
  delay(500);
}
06
Buttons "bounce" — a single press can register as several rapid HIGHs as the contacts settle. The delay (debounce) prevents the latch flipping multiple times per press.
07
void setup() {
  Serial.begin(9600);
}

void loop() {
  int v = analogRead(A0);
  Serial.println(v);
  delay(1000);
}
08
A digital pin can only supply a small current — too small to drive a motor, and the inrush could damage the Arduino. Use a transistor (e.g. NPN) as a switch, with the motor powered from a separate supply. The Arduino just controls the transistor.

Section C — Hard

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

void loop() {
  int light = analogRead(A0);
  if (light < 300) {
    digitalWrite(13, HIGH);
  } else {
    digitalWrite(13, LOW);
  }
}
10
void flash(int pin, int times, int ms) {
  for (int i = 0; i < times; i++) {
    digitalWrite(pin, HIGH);
    delay(ms);
    digitalWrite(pin, LOW);
    delay(ms);
  }
}

void loop() {
  flash(9, 4, 200);
  delay(2000);
}
11
Wiring: potentiometer's outer pins to 5V and GND, middle pin to A0. LED's positive leg through a resistor to a PWM pin (e.g. pin 9), other leg to GND.
void loop() {
  int reading = analogRead(A0);   // 0–1023
  int brightness = reading / 4;    // map to 0–255
  analogWrite(9, brightness);
}
← Back Practice Next → All modules