Arduino circuit And Sketch Set brightness LED with potentiometer

Arduino circuit

Arduino circuit

Sketch Set brightness LED with potentiometer


// Free Tutorial Arduino
// www.ioisalman.com


// A0 pin is a pin Analog
// Pin 9 is pin PWM digital support
const int pinPot = A0;
const int pinLED = 9;

void setup () {
   pinMode (pinPot, INPUT);
   pinMode (pinLED, OUTPUT);
}

sensor int = 0;
int brightness = 0;

void loop () {
   // Read the value of A0 feet (sensor, potentiometer)
   sensor = analogRead (pinPot);
   // Convert the value 0-1023 (Analog) into 0-255 (PWM)
   brightness = map (sensor, 0, 1023, 0, 255);

// Set the LED brightness with PWM
   analogWrite (pinLED, brightness);
}

When the above program at Sketch uploaded, then we can adjust the brightness and life-die LED perfectly, unlike the manual way like circuit on the previous post. In Sketch above, there are two parts that need to be considered:


  1. On line 7, pinPot = A0. A0 is variable to Analog pins A0 to 0. Actually, the same as the pin 14. We can also use the pin as a digital pin. But you can not use the digital pin as analog pin. So, use pin A0 - A5 if it will be connected to the analog sensors.
  2. In line 22, there is a function map (). As the name implies, the function map () is used to map a value of a specific range to another range. Here are the parameters in the function map ()

    map(value, from_min, from_max, to_min, to_max);


    Then on line 22 serves to change the sensor value that originally existed in the range 0-1024 into a value with a range of 0-255. If the sensor level values ​​512 (assuming half of 1024), that value will be changed to 127 (assuming ½ of 255).


Next, let's try to set the LED blink duration based on the value on the potentiometer. If the 'volume' low potentiometers, the duration of the LED will blink rapidly. If the 'volume' High potentiometers, the duration of the LED will blink slowly. I call the 'volume' because the potentiometer is identical with the tools to adjust the volume.

Sketch Heartbeat LED with potentiometer


// Free Tutorial Arduino
// www.ioisalman.com
// A0 pin is a pin Analog
// Pin 9 is pin PWM digital support
const int pinPot = A0;
const int pinLED = 9;

void setup () {
   pinMode (pinPot, INPUT);
   pinMode (pinLED, OUTPUT);
}

sensor int = 0;

void loop () {
   // Read the value of A0 feet (sensor, potentiometer)
   sensor = analogRead (pinPot);
  
   // Duration corresponding flicker on the sensor value 0-1023
   digitalWrite (pinLED, HIGH);
   delay (sensor);
   digitalWrite (pinLED, LOW);
   delay (sensor);
}


Already tried Sketch Heartbeat LED with potentiometer?

Heartbeat LED on the Sketch Heartbeat LED with potentiometer is less responsive. That is, when the potentiometer is maximized, then the delay will be longer. But when the potentiometer is directly descended, paka heartbeat LED remains on such long duration. Why? Because the microcontroller will finish delay value given previously.

After the execution of all delay is done, the new potentiometer will change to the new duration of the delay. That's what I call less responsive. Should the LED blink duration will soon change when the potentiometer changed.

Subscribe to receive free email updates:

0 Response to "Arduino circuit And Sketch Set brightness LED with potentiometer"

Post a Comment