Menu
CoddyTech

In-Between Colours

Part of the Outputs section of Coddy's Arduino journey. Lesson 9 of 24.

Pins 11, 10 and 9 all have a ~, so each colour of the RGB LED can be dimmed with analogWrite(). Each channel goes from 0 to 255, and mixing three levels gives far more than eight colours:

  • orange: red 255, green 80, blue 0
  • purple: red 128, green 0, blue 255
  • a soft white: all three at 60

Change setColor() to use analogWrite(), and its parameters become brightness levels:

void setColor(int red, int green, int blue) {
  analogWrite(redPin, red);
  analogWrite(greenPin, green);
  analogWrite(bluePin, blue);
}

This is why the colour legs are wired to ~ pins: on another pin the colour could only be on or off.

challenge icon

Challenge

Easy

Change setColor() to use analogWrite(), then show, again and again:

  • orange (255, 80, 0) for one second
  • purple (128, 0, 255) for one second

Press Run, then Check.

Try it yourself

const int redPin = 11;
const int greenPin = 10;
const int bluePin = 9;

void setup() {
  pinMode(redPin, OUTPUT);
  pinMode(greenPin, OUTPUT);
  pinMode(bluePin, OUTPUT);
}

void setColor(int red, int green, int blue) {
  digitalWrite(redPin, red);
  digitalWrite(greenPin, green);
  digitalWrite(bluePin, blue);
}

void loop() {
  // orange (255, 80, 0) for a second, then purple (128, 0, 255) for a second
}
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Outputs

Practice on your own: Arduino playground