I'm currently working on an assignment which has to turn on an RGB LED using a potentiometer and button.

The problem I currently have is that the button is supposed to change the color I want the LED to display (press one time: red (pin9), press again: green (pin10), press again: blue (pin11)).

As I'm changing through the ports with the potentiometer, the potentiometer will read it with as analogWrite. For example, when I move the potentiometer all the way to the left and press the button the LED will display red, and when I move it back all the way to 0 and press the button twice the LED will stay red (do nothing). If I press it one more time it will turn off.

Another example, I turn on red again, then use green and turn off blue using the potentiometer and button giving me orange. How do I make it so to accomplish all of this while using both and combining colors?

This is a HW question. I am not asking for a whole solution but rather where to start because I already got stuck and I want to restart.

  • So - if I'm understanding that right - you want your button to be the set button, which is used 3 times sequentially to mix the colors red, green and blue with the value, that the poti has at the respective stages. So building the RGB color in 3 stages out of its components. Is that, what you want? And what exactly is your problem here? Reading the poti? Outputting 3 PWM signals for the RGB LED? calculating the values? Managing the program flow?
    – chrisl
    Commented Sep 27, 2020 at 19:26
  • 1
    Oh, sorry for not making it clear. I'm kind of new to this (as shown). I believe my main problem is reading the reading the button and poti as to mix the colors as aforementioned.
    – Matt Weber
    Commented Sep 27, 2020 at 19:33
  • 1
    Edit: The current problem I'm experiencing is the next: I'm running the simulation with if and counter for the button and my first color should be green. The next part in my code is to turn on blue but that's on two presses. On the first press it seems to skip green and go straight to blue.
    – Matt Weber
    Commented Sep 27, 2020 at 19:40
  • 1
    The most common problem beginners have with buttons is coding it to be "If HIGH do this" and then wonder why it keeps happening rather than just happening once when the button is pressed. You have to learn to watch for a change in the state of the button - as in "If changed from LOW to HIGH do this".
    – Majenko
    Commented Sep 28, 2020 at 10:07
  • 2
    If you solved your own question, please post the answer or how you fixed it. That way someone who finds your question will know how you fixed it this question will show as "Answered"
    – tavis
    Commented Oct 4, 2020 at 11:13

1 Answer 1


You method sound a bit more complicated that it needs to. What I would do is have the button switch between three colors, and have the potentiometer change the brightness of the currently selected color.

For example, when first starting up, the red led is selected. Turning the potentiometer changes the brightness of the red led (in real time). Let's say you turn it 50%, so the red led is at 50% brightness. When you're happy with that, you press the button, and now green is selected. (Since the potentiometer is still at 50% from the red, the green led will immediately turn on at 50% brightness.) You can then turn the pot to change the brightness of the green led. Let's say you turn it all the way of to 0%. Then press the button to select the blue led. Turn it to 100% and the combined color would be a dark purple. Press the button again to select red again.

Here is some proof of concept code:

byte selectedColor = 0; // 0:red, 1:green, 2:blue
byte oldButtonState = HIGH;

void loop() {
  byte buttonState = digitalRead(buttonPin); // read the button
  if( buttonState==LOW && oldButtonState==HIGH ) // see if the button is pressed, but wasn't pressed the last time
    selectedColor++; // to to next color
    if( selectedColor==3 )
      selectedColor = 0;// after blue, go back to red
  oldButtonState = buttonState; // remember the buttons state for the next loop

  int brightness = map(analogRead(potPin), 0, 1023, 0, 255);

  if( selectedColor==0 )
    analogWrite(redPin, brightness);
  else if( selectedColor==0 )
    analogWrite(greenPin, brightness);
    analogWrite(bluePin, brightness);

PS I left out the pinMode stuff in the setup.

PPS make sure the led leads are connected to PWM pin on the Arduino (they have a ~ next to them on the PCB).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.