Here introduces the Arduino analog input function, which controls the brightness of the LED light by reading input voltage values through a potentiometer.
The analog input is a pin with ADC (Analog-to-Digital Converter) function. It can convert the externally input analog signal into a digital signal that can be recognized during chip operation, so as to realize the function of reading in the analog value. The Arduino analog input function has 10-bit precision, that is, it can convert a voltage signal of 0 to 5V into an integer form of 0 to 1024. Utilize the analogRead() function to read input voltage values by the potentiometer, and then use the analogWrite() function to control the brightness of the LED light.
Experimental Materials1x Uno R3 development board
1x Bread board and supporting connecting line
1x 220Ω current limiting resistor
1x LED
1x potentiometer
Supporting USB data cable
The potentiometer is an adjustable resistor, and its operating principle is shown in the following figure:
Move the position of pin 2 by rotating the knob, changing the resistance value from pin 2 to both ends. In the experiment, connect pin 1 and pin 3 to the 5V GND of the development board, and then read the voltage of pin 2 obtained by the potentiometer through the analog input pin A0, and the range is between 0V to 5V.
Experimental Steps1) 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:
The physical connection diagram is shown in the figure below:
2) Create a new sketch, copy the following code to replace the automatically generated code and save it.
3) Connect the development board, set the corresponding port number and development board type, and download the program.
Rotating the potentiometer knob changes the brightness of the LED light.
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 AnalysisThe 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.
Recommended ReadingAnalogRead() - Arduino Reference
AnalogWrite() - Arduino Reference
Comments
Please log in or sign up to comment.