Exercise: Sensor Fade

Objective

Read a sensor input and control several external LEDs.

A microcontroller isn’t very useful without some kind of hardware attached. This exercise combines connecting a photosensor and external LED outputs via a control program.

../../../_images/sensor-fade.png

Steps and observations

  1. Wire up the circuit on the breadboard. The photograph below shows a suggested layout.
  2. Load and run the SensorFade sketch.
  3. Observe the initial blink pattern to make sure both LEDs can light. If one doesn’t light, try reversing the LED polarity or checking your wiring.
  4. Observe the LED illumination after the blink pattern. Does varying the level of light on the photocell fully cross-fade between the two LEDs?
  5. Measure the voltage on A0 over the available range of photocell illumination. Adjust the values of VLOW and VHIGH in the code to this voltage range and re-load the code.
  6. Try disconnecting A0 so it is floating and see what happens when you connect your finger to it using just a jumper wire. The input is high-impedance so even very tiny skin currents can change the voltage on the input.

Comments

This example uses a number of programming features:

  1. infinite loops
  2. calibrating and scaling analog inputs
  3. pulse-width modulation of outputs
  4. use of map() and constrain()

For a challenge, see if you figure out what controls the rate and pattern of the blinking of the onboard LED. Can you produce an asymmetric pattern?

Arduino Code

  1. Documentation: SensorFade Arduino Sketch
  2. Sketch Folder: SensorFade

Suggested Layout

../../../_images/sensor-fade-layout.jpg

Other Files

  1. EAGLE file: sensor-fade.sch