In-Class - Analog Input and Output

Reading the effects of a potentiometer.

Monitoring the potentiometer value (0-1023) as text.
Plotting the potentiometer value means I can have fun drawing waves.

Then, I experimented with output values as well. I read the potentiometer value, then used it to change the brightness of an LED.

Lab: Digital In/Out

Blinking LED

void setup() {
  pinMode(13, OUTPUT);
void loop() {
  digitalWrite(13, HIGH);
  digitalWrite(13, LOW);

Switching Between LEDs with a Pushbutton

const int LED_A = 13;
const int LED_B = 11;
const int BUTTON = 7;

void setup() {
  pinMode(LED_A, OUTPUT);
  pinMode(LED_B, OUTPUT);
  pinMode(BUTTON, INPUT);
void loop() {
  if (digitalRead(BUTTON) == HIGH) {
    // Switch is closed
    digitalWrite(LED_A, HIGH);
    digitalWrite(LED_B, LOW);
  } else {
    // Switch is open
    digitalWrite(LED_A, LOW);
    digitalWrite(LED_B, HIGH);

Application: Synced Speaker and LED

Finally, we had to get creative and creating a simple Arduino I/O application. I found a speaker in the scrap bin and read up on tone/frequency modulation. Then I wired up the breadboard such that turning a potentiometer would simultaneously vary the brightness on the LED and the tone on the speaker.

Here's the code:

const int POTENTIOMETER = A0;
const int LED = 6;
const int SPEAKER = 9;

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

void loop() {
  int potValue = analogRead(POTENTIOMETER);
  int toneValue = map(potValue, 0, 1023, 100, 1000); // 100Hz - 1000Hz
  int ledValue = map(potValue, 0, 1023, 0, 255);

  analogWrite(LED, ledValue);
  tone(SPEAKER, toneValue);

The first time I tried it, the LED was acting like a digital output, only turning on and off when the potentiometer crossed the halfway mark. So I'm not sure if pin 11 on the MetroX 328 does analog output properly, despite have the white dot that indicates it can do analog output. I moved the LED to pin 6, and that worked fine.

Below is a video of the setup in action. Interestingly, when I turn the dial quickly, you can hear the 1/10th of a second delay(100) - the tone jumps between frequencies, rather than varying smoothly.

An image showing the breadboard layout.