Maple Lesson 06 - The potentiometer PWM controls LED brightness
ObjectiveLast lesson we use the button to generate PWM to control the LED, this lesson we will use precision potentiometer to control the LED. The difference between them is that the button use the digital voltage signal (0 and 1) to control which only has two states. When the signal changed, LED increases brightness by 5 units (0 ~ 255). Potentiometer uses the analog voltage to generate PWM which is a linear change of state, so the LED's brightness can be changed coherently and softly. Conversely, if use the button, you need consider the button shaking. EquipmentMicroduino-CoreSTM32 is an ARM development board using STM32F103CBT6 chip. It use special Upin7 interface, the size is similar with a coin and fully compatible other Microduino modules.
SchematicConnection method, LED connects to the PWM output pin, and potentiometer connects to analog port A0 ~ A5. Analog interface can measure 0-5V voltage, and the corresponding return value is 0-1024, the measurement accuracy of the voltage variation is relatively high. Potentiometer had better choose winding precision linear potentiometer, because some cheap nonlinear potentiometer on the market doesn't have a good electrical characteristic. Numerical drift is big which easy to cause the led flashing, Resistance is nonlinear variation, so the brightness change is not obvious, easy to produce the sense of hierarchy just like the button dimmer experiments, impact the test results. Programint red_pin=14;
int pwm_pin=4;
void setup()
{
pinMode(red_pin,INTPUT);
pinMode(pwm_pin,PWM);
}
void loop()
{
int val= analogRead(red_pin); //Read the analog port A0's value (0-4095)
val = map(val, 0, 4095, 0, 65535);//Mapping the analog value (0-4095) to(0-65535)
pwmWrite(pwm_pin, val);
} map() function
ResultWith the rotation of the potentiometer, LED’s brightness changes softly. Video |