/*
* Potentiometer Analog Input with Arduino
* Features: LED brightness control, servo motor, RGB LED, serial output
* Demonstrates analog input reading and PWM output
*/
#include <Servo.h>
// Pin definitions
const int POT_PIN = A0; // Potentiometer connected to A0
const int LED_PIN = 9; // PWM LED on pin 9
const int LED_RED = 3; // RGB LED Red
const int LED_GREEN = 5; // RGB LED Green
const int LED_BLUE = 6; // RGB LED Blue
const int SERVO_PIN = 10; // Servo motor pin
// Variables
int potValue = 0; // Raw potentiometer value (0-1023)
int brightness = 0; // LED brightness (0-255)
int servoAngle = 0; // Servo angle (0-180)
float voltage = 0.0; // Voltage reading (0-5V)
int percentage = 0; // Percentage (0-100%)
// Servo object
Servo myServo;
// Timing variables
unsigned long lastUpdate = 0;
const int updateInterval = 100; // Update every 100ms
void setup() {
// Initialize serial communication
Serial.begin(9600);
// Configure LED pins as outputs
pinMode(LED_PIN, OUTPUT);
pinMode(LED_RED, OUTPUT);
pinMode(LED_GREEN, OUTPUT);
pinMode(LED_BLUE, OUTPUT);
// Attach servo
myServo.attach(SERVO_PIN);
// Startup message
Serial.println("=================================");
Serial.println(" Potentiometer Analog Input");
Serial.println("=================================");
Serial.println();
Serial.println("Raw\tVoltage\tPercent\tBright\tAngle");
Serial.println("-----------------------------------------");
// Initial test sequence
testSequence();
}
void loop() {
unsigned long currentMillis = millis();
// Read and process potentiometer value
readPotentiometer();
// Control outputs based on potentiometer
controlLED();
controlRGBLED();
controlServo();
// Display data on serial monitor
if (currentMillis - lastUpdate >= updateInterval) {
lastUpdate = currentMillis;
displayData();
}
delay(10); // Small delay for stability
}
// Read potentiometer value and calculate derived values
void readPotentiometer() {
potValue = analogRead(POT_PIN);
// Calculate voltage (0-5V)
voltage = potValue * (5.0 / 1023.0);
// Calculate percentage (0-100%)
percentage = map(potValue, 0, 1023, 0, 100);
// Calculate LED brightness (0-255)
brightness = map(potValue, 0, 1023, 0, 255);
// Calculate servo angle (0-180)
servoAngle = map(potValue, 0, 1023, 0, 180);
}
// Control single LED brightness with PWM
void controlLED() {
analogWrite(LED_PIN, brightness);
}
// Control RGB LED colors based on potentiometer position
void controlRGBLED() {
if (potValue < 341) {
// Range 0-33%: Red
analogWrite(LED_RED, map(potValue, 0, 341, 0, 255));
analogWrite(LED_GREEN, 0);
analogWrite(LED_BLUE, 0);
} else if (potValue < 682) {
// Range 34-66%: Green
analogWrite(LED_RED, 0);
analogWrite(LED_GREEN, map(potValue, 341, 682, 0, 255));
analogWrite(LED_BLUE, 0);
} else {
// Range 67-100%: Blue
analogWrite(LED_RED, 0);
analogWrite(LED_GREEN, 0);
analogWrite(LED_BLUE, map(potValue, 682, 1023, 0, 255));
}
}
// Control servo motor position
void controlServo() {
myServo.write(servoAngle);
}
// Display all values on serial monitor
void displayData() {
// Tabular format for Serial Monitor
Serial.print(potValue);
Serial.print("\t");
Serial.print(voltage, 2);
Serial.print("V\t");
Serial.print(percentage);
Serial.print("%\t");
Serial.print(brightness);
Serial.print("\t");
Serial.print(servoAngle);
Serial.print("°");
Serial.println();
}
// Startup test sequence
void testSequence() {
Serial.println("Running startup test...");
// Test LED brightness
for (int i = 0; i <= 255; i += 5) {
analogWrite(LED_PIN, i);
delay(10);
}
for (int i = 255; i >= 0; i -= 5) {
analogWrite(LED_PIN, i);
delay(10);
}
// Test RGB colors
analogWrite(LED_RED, 255);
delay(300);
analogWrite(LED_RED, 0);
analogWrite(LED_GREEN, 255);
delay(300);
analogWrite(LED_GREEN, 0);
analogWrite(LED_BLUE, 255);
delay(300);
analogWrite(LED_BLUE, 0);
// Test servo sweep
for (int angle = 0; angle <= 180; angle += 10) {
myServo.write(angle);
delay(20);
}
for (int angle = 180; angle >= 0; angle -= 10) {
myServo.write(angle);
delay(20);
}
Serial.println("Test complete! Ready for input.");
Serial.println();
}