Lab 3: 3 Potentiometers for 3 LEDs

In this lab the brightness of 3 LED lights are each capable of being independently controlled by its own coinciding potentiometer.

 

components

  • 1 Arduino
  • 1 breadboard
  • 1 red LED
  • 1 green LED
  • 1 blue LED
  • 3 potentiometers
  • 3 225-ohm resisters
  • wires

 

code

int potPinB = 0; // Analog input pin that the potentiometer is attached to
int potValueB = 0; // value read from the pot
int ledB = 9; // PWM pin that the LED is on. n.b. PWM 0 is on digital pin 9
int potPinG = 1; // Analog input pin that the potentiometer is attached to
int potValueG = 0; // value read from the pot
int ledG = 10; // PWM pin that the LED is on. n.b. PWM 0 is on digital pin 10
int potPinR = 2; // Analog input pin that the potentiometer is attached to
int potValueR = 0; // value read from the pot
int ledR = 11; // PWM pin that the LED is on. n.b. PWM 0 is on digital pin 11
void setup() {
// initialize serial communications at 9600 bps:
Serial.begin(9600);
// declare the led pin as an output:
pinMode(ledB, OUTPUT);
pinMode(ledG, OUTPUT);
pinMode(ledR, OUTPUT);
}

void loop() {
potValueB = analogRead(potPinB); // read the pot value
potValueG = analogRead(potPinG); // read the pot value
potValueR = analogRead(potPinR); // read the pot value
analogWrite(ledB, potValueB/4); // PWM the LED with the pot value (divided by 4 to fit in a byte)
analogWrite(ledG, potValueG/4); // PWM the LED with the pot value (divided by 4 to fit in a byte)
analogWrite(ledR, potValueR/4); // PWM the LED with the pot value (divided by 4 to fit in a byte)
}

 

 

 

IMG_7613

Leave a Reply