What am I doing wrong? (RGB LED with potentiometers)

Found out I have an RGB LED lying around and decided to make a thing.

I connected 3 pots to the Arduino on pins A0, A1, and A2, and connected the RGB LED to 9, 10, and 11, uploaded this code that I wrote in a few minutes, and was very surprised.

int potR;
int potG;
int potB;

int ledR;
int ledG;
int ledB;

void setup(){
  Serial.begin(9600);
  pinMode(9, OUTPUT);
  pinMode(10, OUTPUT);
  pinMode(11, OUTPUT);
}

void loop(){
  potR = analogRead(A0);
  potG = analogRead(A1);
  potB = analogRead(A2);
  ledR = map(potR, 0, 1023, 0, 255);
  ledG = map(potG, 0, 1023, 0, 255);
  ledB = map(potB, 0, 1023, 0, 255);
  analogWrite(9, ledR); 
  analogWrite(10, ledG);
  analogWrite(11, ledB);
  Serial.println(ledR);
  Serial.println(ledG);
  Serial.println(ledB);
}

The output is something like:

255
254
255
254
255
255

even with the pots turned all the way down. The LED is white. (All colors on).

I feel like I did something stupid, but can't figure out what. The LED has 2k ohm resistors on each input.

Quick elaboration on the Serial output. When the pots are turned down (all 3):

254
253
253
254
254

And all the way up:

255
255
255
255
255

print potR potG potB values

Better written

ledR = potR / 4;
// Or at worst 
ledR = map(potR, 0, 1024, 0, 256);

How are the pots wired?

(You're updating the pot and led variables each time through loop, so there's no reason for them to have global scope, and analogWrite pins don't need a pinMode - don't write more code than you have to.)

try

  ledR = potR/4;
  ledG = potG/4;
  ledB = potB/4;

This is just an improvement. But you clearly have some simple hardware problem.

I'll try that code.

The pots have power on the left, ground on the right, and the wiper pin in the middle all connected to Arduino. I'm using 10k pots.

It's not code. The sketch you posted should work.

Alright.

I feel like I'm doing something stupid but I've yet to figure out what it is...

Guess I'll figure it out later tonight (it's like 10:00PM here, IDK what time it is for you).

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.