Lab 3 — Analog Input

Sam Stromberg
4 min readSep 20, 2021

This lab employs the analog input pins on the Arduino board to set parameters for the output LEDs, where potentiometers (“pots”) provide variable resistance to the pin. On 5V power from the board, the signal from the pot is processed at 2¹⁰ levels of resolution, 4x more precise than the PWM output levels we explored last week. In experimenting with the demo code provided, I found that the pots not only read values 0–1024 but had range of motion beyond that in either direction, so that on the serial display the waveforms get clipped at the bottom and top.

Input never drops below 0 (or above 1023, not pictured)

The pots I used have prongs and are not designed to plug into a breadboard, so I soldered wires to each prong to connect them to the breadboard and the microcontroller. I made a rookie mistake and tried using stranded wire, which soldered together just fine but then snapped off when I tried to land it anywhere.

To build this circuit, I first simplified the three-LED setup so that they are run in parallel off of a single output pin and single connection to ground, using the terminal bars on the breadboard. Then, I created a second loop that ran from the 5V constant power pin to each of the two pots (again using the breadboard to simplify the connection), which were connected to separate analog input pins (A0 and A3) and to the ground bar. So whereas the LEDs, on the output side, are controlled by changing the amount of power sent to them, the pots, on the input side, receive constant full power and the input pins measure how the flow is split between them and the ground.

I then wrote a program that reads input from the A0 and A3 pins and uses it to set the brightness and blink delay for the LED circuit. Adjusting the pots allows these two parameters to changed independently.

The LED circuit is basically the same, while the pots are run off of the other side of the board

For simplicity, the LEDs are all being run off of pin 11 connected to the left-hand power bar, both pots get a constant 5V off the right-hand power bar, and all connections to ground for both circuits land on the terminal bar.

Components

  • Arduino Uno
  • Mini breadboard
  • 3x LEDs (although since they’re being powered off a single pin, they’re all doing the same thing at the same time)
  • 3x Resistors (I could have run all three LEDs through a single resistor, as well, but already had them on the breadboard from the previous lab, so it seemed easier to minimize changes to the circuit)
  • 2x Potentiometers
  • Hand-cut and jumper wires — the geometry of the circuit has quickly grown more complicated, and jumper wires certainly don’t help; on the other hand, fixed (stiff) wires between the breadboard and Arduino will make the object unwieldy

Code

There’s a little bit more work being done to declare variables and set up the circuit, but the output to the LEDs is relatively simple.

int blinkSensorPin = A0; // select the input pin for the potentiometer to control blink delay
int brightSensorPin = A3; // similarly for brightness
int ledPin = 11; // we employ a pwm pin so that we can explore luminosity
int blinkValue = 0; // variable to store the value coming from the first pot
int brightValue = 0; // similarly

void setup() {
// declare the ledPin as an OUTPUT:
pinMode(ledPin, OUTPUT);
}

void loop() {
// start the loop by reading the brightness value
// The analog input can read 1024 levels (based on demo output), but PWM only outputs on 256 levels
brightValue = analogRead(brightSensorPin) / 4;

// read the blink delay from the sensor:
blinkValue = analogRead(blinkSensorPin);

// turn the ledPin on
analogWrite(ledPin, brightValue);
// stop the program for <blinkValue> milliseconds:
delay(blinkValue);
// turn the ledPin off:
analogWrite(ledPin, LOW);
// stop the program for for <blinkValue> milliseconds:
delay(blinkValue);
}

Exploration

The pots provide a very intuitive way of varying input parameters, so adding a processing step that breaks the smoothness could be interesting. For example, adding 511 to smaller input values while subtracting 511 from larger input values would make the fast/slow (or bright/dark) split fall halfway along the range of motion — reminding me of Don Norman railing against a badly-designed hotel showerhead. Since the range of 1024 levels is far finer than a person can manually control, a discontinuous function (e.g., one that treats odd and even values differently) would appear to the user as randomness, since there would be no way to know in advance the exact value detected by the Arduino. That said, the board registered some small fluctuations even when the pot was left untouched — either because it was at a value between two levels (511.4 would likely bounce between 511 and 512) or due to noise from the environment (e.g., nearby electronic devices) — so sharp discontinuities might cause the device to rapidly shift between states rather than producing a single, clear effect.

--

--

Sam Stromberg

2nd-year Masters student at the UC Berkeley School of Information. Moving into Product; interested in data and uncertainty, sensor data, behavioral change.