Maple Lesson 06 - The potentiometer PWM controls LED brightness

来自Microduino Wikipedia
跳转至: 导航搜索

Objective

Last 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.

Equipment

Microduino-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.

  • Other hardware equipment
    • Breadboard Jumper one box
    • Breadboard one piece
    • LED Light-emitting diodes one
    • 220Ω resistor one
    • Precision potentiometer one
    • USB Data cable one

Schematic

Connection 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.

Program

int 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

  • Function: Map a certain range values to a different range
  • Grammar:map(value, fromLow, fromHigh, toLow, toHigh)
    • value:return value
    • fromLow: Paternal interval lower limit
    • fromHigh:Paternal interval upper limit
    • toLow:Mapping range lower limit
    • toHigh:Mapping range upper limit

Result

With the rotation of the potentiometer, LED’s brightness changes softly.

Video