The aim of this project is to get acquainted with the way of using the potentiometer, the operation of analog inputs and reading the values from the sensors (in this case the role of the sensor is played by the potentiometer). A theoretical explanation is given at the beginning of the video. In the following, we will see what it looks like when we read the voltage that we control with a potentiometer from the analog input of the Arduino, and how we use that voltage value to control the light intensity of the LED.
Analog Arduino pins
The voltage range that can be applied to the analog pins (A0-A5) is between 0 and 5V. The input signals are fed to the A / D converters of the internal microcontroller, which convert the analog signals to the corresponding digital numbers and can take values between 0 and 1023. The analog signal of 0V corresponds to a digital value of 0, while the voltage of 5V corresponds to a value of 1023. This means that the resolution (sensitivity) is slightly less than 50mV (digital value 1 is about 50mV of input voltage).
A4 and A5 pins are used as SDA and SCL lines for I2C serial communication.
Potentiometer
A potentiometer is a variable resistor that functions as a voltage divider. On its conductive surface there is a slider whose change of position on the conductive surface changes the resistance at the ends of the potentiometer. In electrical circuit it is connected in parallel.
Required components for the project
1. Potentiometer
2. LED diode
3. Resistor 220Ω (for diode protection)
Electrical scheme and sketch
Sketch explanation
Definition section
Two constants “LED” and “POT” are defined with the values “10” and “A0”. A value of “10” corresponds to pin “10” to which the LED is connected, while “A0” represents the analog input “A0” to which the middle terminal of the potentiometer is connected.
Setup section
In the configuration section, we define pin “10” as output, the syntax looks like this:
pinMode(LED, OUTPUT)
Loop section
The voltage at the analog input “A0” is read by the function “analogRead (POT)” and stored in the variable “sensorValue”.
The “map” function changes the range of values of the “sensorValue” variable.
ledPWM = map(sensorValue, 0, 1023, 0, 255) - sensorValue - a variable that changes the value range - 0 (first 0) - the minimum value of the range from which we change - 1023 - the maximum value of the range from which we change - 0 (second 0) - minimum value of the new range - 255 - maximum value of the new range
Note: the minimum values of the range can be smaller or larger than the maximum so that the “map” function can create an inverse range, e.g. [0..100] => [100..0]
With the “analogWrite” function, the diode connected to pin 10 is switched on and illuminated by the intensity defined by the variable ledPWM.
The “delay” function defines a delay of 100 milliseconds before moving on to the next iteration of the “loop” function.
Additional explanation of “analogWrite” and “map” functions
The analogWrite function has two parameters, the first is the pin on which it will generate the PWM signal (digital pins that have this capability are marked with a ~ sign), the second parameter is an integer value between 0 and 255.
As mentioned, the voltage value from the analog inputs is converted to digital values in the range 0 to 1023. Thus, in order to control the brightness of the diode with a potentiometer, the value range of the variable “sensorValue” must be reduced from the current [0, 1023] to [0,255] ] using the “map” function.