Classroom Public page

Week 9: Analog Input

521 words

The world is not just on or off. By the end of the week you can read a photoresistor (or potentiometer) with analogRead, get back a number from 0 to 1023, and map that number to a useful action.


Reading (~45 min)

  • Arduino reference for analogRead and map
  • Optional: the SparkFun "Analog to Digital Conversion" tutorial. Understand the trade-off between resolution (10-bit on most Arduino pins; the R4 supports up to 14-bit) and speed

Lecture (~1.5 hr)

  • Analog vs digital. Digital is binary (HIGH or LOW). Analog is a continuous voltage between 0 V and 5 V. The Arduino's analog input converts the voltage to a number using its built-in analog-to-digital converter (ADC)
  • analogRead. The function reads from one of the analog pins (A0 through A5 on the R4 Minima) and returns a 10-bit value: 0 maps to 0 V, 1023 maps to 5 V. Higher voltages saturate at 1023; negative voltages are not valid input
  • Sensor-as-voltage-divider. Many simple sensors (photoresistors, thermistors, force-sensitive resistors) change their resistance based on what they sense. Pair the sensor with a fixed resistor in a voltage divider; the analog input reads the divider's output; the output changes as the sensor's resistance changes
  • The map() function. Maps a value from one range to another. Useful for converting analogRead's 0-1023 into something meaningful (a temperature in Celsius; an LED brightness from 0-255; a servo angle from 0-180)

Lab exercises (~2 hr)

Lab 9.1: Photoresistor. Wire the kit's photoresistor in a voltage divider; read with analogRead; print the value to Serial Monitor. Cover the photoresistor with your hand; see the value drop. Map the reading to an LED's brightness via PWM (preview of next week). ~90 minutes.

Independent practice (~3 hr)

  • Read three different sensors from the kit (photoresistor; potentiometer; if available a thermistor). Build the appropriate voltage divider for each. Compare the readings across different conditions
  • Calibrate your photoresistor. Cover it completely; record the dark reading. Hold it near a bright lamp; record the bright reading. Use these as your map() endpoints in code
  • Build a "night light" prototype: an LED that comes on when the photoresistor reads below a threshold (dark conditions). Tune the threshold so it actually triggers when you expect

Reflection prompts

  1. The Arduino's ADC gives you 10 bits of resolution (1024 distinct values). Why 10 bits and not 8 or 16? What trade-offs make 10 a reasonable choice for a microcontroller?
  2. The photoresistor's resistance changes nonlinearly with light. Your divider's output therefore changes nonlinearly with light. What does this mean for your "night light" threshold? Build the intuition before you measure
  3. analogRead takes ~100 microseconds. If your loop is reading and reacting fast, this is invisible. If your loop is reading at audio rates (44.1 kHz), it is too slow. Where is the boundary?

What's next

Week 10 makes output analog. Just as analogRead reads a voltage, analogWrite produces one. (Actually it produces PWM, which is a digital signal that averages to the desired voltage; most analog parts cannot tell the difference.) You fade an LED smoothly from off to fully bright.