Software Overview  |  Sitemap  |  Downloads  |  Developers  |  Forums
Small_clear_logo_llc

Recipe that Dims an LED Based on an Analog Input Pin Value

Light and Dimmer

This recipe uses a voltage level on an Arduino analog input pin to dim an LED. Dimming is accomplished by varying the pulse width of a PWM (pulse width modulated) output pin.

Ingredients

For this recipe you will need:

  • an Arduino (running Firmata, and Grove Shield, if using Grove devices)
  • a Grove Rotary Angle Sensor (or equivalent)
  • a Grove LED (or equivalent)

An easy way to create a variable input voltage is to use a variable resistor connected between 0 and 5V with its wiper pin connected to the Arduino analog input (see diagram). If you are using a Grove system, use one of their "Rotary Angle Sensor" devices.

Script

You can find the Script in the Scripts "examples" area. The Script is called "light_and_dimmer.script". The Script is as follows:

# Requires an Arduino, a 0-5V voltage source (a Grove Rotary Angle Sensor
# works well here), and an LED.
# The voltage source is on A0.  The LED is on PWM output (pulse width
# modulated output) pin 3.

# Get the file name of the Arduino device.
# On *nix systems, the file will be in the "/dev" directory with
# a name like "/dev/cu.usbmodem411" or "/dev/ttyUSB0".
# On Windows systems, the name will be something like "COM1", "COM2", ....
script_parameters("arduino_port")

# define the location of the voltage source - has to an an analog input
optional_script_parameters(dimmer_pin:"A0")

# define the location of the led - has to be a PWM output
optional_script_parameters(led_pin:3)


# Arduino device
run_script("Scripts/Device/Arduino/Arduino", port_location:arduino_port, id:"arduino")


# Create an analog input to measure the value of the rotary switch.
# Make it's output units be a percentage (0-100), so it can wire directly
# up to a PWM.
run_script("Scripts/Device/Arduino/PinTypes/AnalogInputs", names:"dimmer", pins:dimmer_pin, value_type:"percent", arduino_id:"arduino")


# Create a PWM output to control the LED.
run_script("Scripts/Device/Arduino/PinTypes/PWMOutputs", names:"led", pins:led_pin, arduino_id:"arduino")


# wire things together
wire "arduino:dimmer", "arduino:led"

To run this Script, you need 3 parameters: the arduino_port, dimmer_pin, and led_pin parameters. The arduino_port parameter is the name of the USB port where the Arduino is plugged in. On *nix hosts, the USB port name is in the "/dev" directory. To see the devices in order of their creation, type "ls -lrt /dev" at a shell prompt and look near the bottom of the list for your Arduino's USB port.

The dimmer_pin parameter defines the Arduino pin where the variable resistor is located. It has a default value of "A0" (analog input 0). The led_pin parameter defines the Arduino pin where the LED is located. It has a default value of 3.

As an example, for an Arduino at USB port "/dev/ttyUSB0" with a variable resistor at pin A0 and an LED at pin 3, the Script parameters are:

arduino_port: "/dev/ttyUSB0"
dimmer_pin: "A0"
led_pin: 3

Running the Script

Run the Script with your own parameters. Change the voltage on the Arduino analog input pin by changing the variable resistor. The LED will brighten and dim as you change the voltage value.

Catalina Computing, LLC.

Copyright © Catalina Computing, LLC. (2013-2017)




Page last updated: Fri Jan 17 16:33:56 2014 (UTC)