Exercise: Sensor Driven LED

Objective

Read a sensor input and control several external LEDs.

Working with microcontrollers starts to get exciting when you have both input devices (i.e. sensors) and output devices (i.e. actuators) connected. In this configuration, you can use the microcontroller and the programming that is running on it to "map" inputs to outputs, thereby creating a relationship between something about the environment (e.g. temperature, lighting, humidity, noise level, etc) and some form of actuation (i.e. a robot moving, a spotlight changing, a tweet being sent, etc.) This exercise shows how you can connect a photosensor--a light sensitive resistor--to the brightness of an LED by way of a control program running on an Arduino.

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?

Other Files

  1. SensorFade.ino
  2. sensor-fade.sch