Potentiometer Control LED Light
Here introduces the Arduino analog input function, which controls the brightness of the LED light by reading input voltage values through a potentiometer.
The potentiometer is an adjustable resistor, and its operating principle is shown in the following figure:
Experimental Steps
1) Build the circuit according to the schematic diagram
Connect the positive pole of the LED lamp to the current limiting resistor, connect the other end of the resistor to the 10th pin of the development board, and connect the negative pole of the LED lamp to the GND of the development board. Pin 1 and pin 3 of the potentiometer are connected to the development board 5V and GND respectively, and pin 2 is connected to the A0 pin of the development board.
The experimental schematic diagram is as follows:
Function Introduction
analogRead() Description: Read the value of the specified analog pin.
Function prototype: analogRead(pin)
Parameter: Analog input pin
Return value: an integer between 0 to 1024
map() Description: Map data from one range to another.
Function prototype: map(value, fromLow, fromHigh, toLow, toHigh)
parameter:
value: The data to be mapped.
fromLow: The lower limit of the current range.
formHigh: The upper limit of the current range.
toLow: The lower limit of the target range.
toHigh: The upper limit of the target range.
Return value: Remapped data
Experimental Analysis
The program uses the analogRead() function to read the analog input value, and the input value range is between 0 to 1024. Then use the analogWrite() function to change the LED light duty cycle, and the duty cycle range is 0 to 255. Remap the range by using the map() function.