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
- Wire up the circuit on the breadboard. The photograph below shows a
suggested layout.
- Load and run the SensorFade sketch.
- 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.
- Observe the LED illumination after the blink pattern. Does varying the level
of light on the photocell fully cross-fade between the two LEDs?
- 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.
- 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:
- infinite loops
- calibrating and scaling analog inputs
- pulse-width modulation of outputs
- 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
- SensorFade.ino
- sensor-fade.sch