Recipe that Dims an LED Based on an Analog Input Pin ValueThis 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. IngredientsFor this recipe you will need:
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. ScriptYou can find the Script in the Scripts "examples" area. The Script is called "light_and_dimmer.script". The Script is as follows:
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:
Running the ScriptRun 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. |