0% found this document useful (0 votes)
1 views

1 LED gradually change brightness by control on Potentiometer

The document describes an Arduino assignment where a red LED's brightness is controlled by a potentiometer. As the voltage from the potentiometer increases, the LED gradually becomes brighter. The code initializes the LED and potentiometer, reads the potentiometer value, maps it to a brightness range, and adjusts the LED accordingly in a loop.

Uploaded by

akniemdam323
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views

1 LED gradually change brightness by control on Potentiometer

The document describes an Arduino assignment where a red LED's brightness is controlled by a potentiometer. As the voltage from the potentiometer increases, the LED gradually becomes brighter. The code initializes the LED and potentiometer, reads the potentiometer value, maps it to a brightness range, and adjusts the LED accordingly in a loop.

Uploaded by

akniemdam323
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

/*

Name: Oliver Wang


Date: Mar. 18
Assignment: 1 LED gradually change brightness by control on Potentiometer
Brief Description: With the control of potentiometer, the red LED will gradually change it’s
brightness, the higher the voltage, the more bright it is
*/

const int sensor = A5; // Constantly make potentiometer connected to analog pin A5, the value
should be an integer
const int RLED = 3; // Constantly assign integer variable RLED connected to PWM pin 3
int value = 0; // Assign the integer variable to store the potentiometer value to 0 in the
beginning, will change based on the code in the loop
int brightness = 0; // Assign the integer variable "brightness" as a newValue of "value", the
identity is in the loop, but this is still set to 0 first, which ensures the brightness starts from 0
const int DLay = 25; // Constantly assign the regular delay time to an integer of 25ms

void setup() {
pinMode(RLED, OUTPUT); // Set red LED as output
pinMode(sensor, INPUT); // Set the sensor (potentiometer) as input
Serial.begin(9600); // Set baud rate for Serial Monitor to 9600
}

void loop() {
value = analogRead(sensor); // Let Arduino check the current voltage level from sensor (pin
A5), turn it into digital value, and store it in the value of variable "value"
int brightness = map(value, 0, 1023, 0, 255); // Map a new value that is different than 'value'
(0~1023), which is set to 0~255, because 0~255 is the range that analogWrite could scan, and
we need to use analogWrite in this assignment
// Display the analog value and mapped brightness value in the Serial Monitor
Serial.print("Brightness: "); // Display message "Brightness:" in serial monitor
Serial.println(brightness); // Display mapped brightness value after

// Adjust LED brightness based on potentiometer position


analogWrite(RLED, brightness); // Set brightness of RLED
delay(DLay); // Have a small gap of the time of 'DLay'
}

You might also like