Dự án có sẵn · Arduino Uno R3

Trộn màu LED RGB với Arduino Uno

Một LED RGB trên ba chân PWM, mỗi chân qua một điện trở riêng: analogWrite cho nó chuyển dần qua mười màu có tên, gồm cả một màu trắng đã được cân lại cho phần LED đỏ. Toàn bộ mạch, gồm một Arduino Uno và 4 linh kiện khác, chạy ngay trong trình duyệt của bạn với firmware bên dưới; mở nó trong trình soạn thảo để đổi cách nối dây hoặc code rồi chạy lại.

Đang dạy học? Giao bài này cho lớp của bạn chỉ với một cú nhấp.

Cơ bản About 15 minutes to build and run. Chạy trong trình duyệt của bạn. Miễn phí, không cần tài khoản.

Linh kiện bán lẻ khoảng $32. Trên Mokxi thì miễn phí.

Từng bước một, kèm cách nối dây và các lỗi thường gặp: LED RGB với Arduino: cách nối dây và code trộn màu

Trộn màu LED RGB · Arduino Uno R3trực tiếp0.000 s 0.00x
Nhấp để mở trong trình soạn thảo
Chính mạch đó, đang chạy ở đây trên trình mô phỏng. Nhấn những gì nhấn được; nhấp vào chỗ khác để mở trong trình soạn thảo.

What you'll learn

  • Mixing colors with PWM on three pins
  • How an RGB LED works
  • Fading smoothly between two values
  • Structs for named colors
  • Common cathode versus common anode
  • Difficulty: Cơ bản
  • Time: About 15 minutes
  • Board: Arduino Uno R3
  • Parts: 6

Cách hoạt động

An RGB LED is three LEDs under one lens. Pins 9, 10 and 11 drive the red, green and blue legs through 220 Ω resistors, and the common leg goes to ground. Your eye mixes the three into one color.

analogWrite sets how bright each one is, from 0 to 255. The sketch fades from one named color to the next over one second, holds it for one second, and prints the color name and its three numbers.

There are two whites. With all three at 255 the light looks pinkish, because red gets more current than the others. The balanced white turns red down to 170.

Danh sách linh kiện

6 linh kiện, cộng thêm dây cắm. Bán lẻ khoảng $32; trên Mokxi thì miễn phí.

Wiring table

Every connection, pin by pin, read from the circuit itself. Each row joins the pin on the left to the pin on the right, by a jumper wire or a breadboard strip.

Linh kiện và chânConnects to
Resistor, 220 Ω (1) pin 1Arduino Uno R3 pin 11
Resistor, 220 Ω (2) pin 1Arduino Uno R3 pin 10
Resistor, 220 Ω (3) pin 1Arduino Uno R3 pin 9
RGB LED pin CArduino Uno R3 pin GND
Resistor, 220 Ω (3) pin 2RGB LED pin R
Resistor, 220 Ω (2) pin 2RGB LED pin G
Resistor, 220 Ω (1) pin 2RGB LED pin B

Code

Firmware y như khi trình soạn thảo mở ra. Sửa một dòng ở đó và nhấn Chạy: nó được biên dịch ngay trong trình duyệt.

sketch.ino · Arduino Uno R3
// RGB color mixer: one RGB LED, three PWM pins, every color in between.
//
//   pin 9   the red leg, through 220 ohm
//   pin 10  the green leg, through 220 ohm
//   pin 11  the blue leg, through 220 ohm
//   the long leg (the common cathode) to GND
//
// An RGB LED is three LEDs under one lens. analogWrite() sets how bright each
// one is, from 0 (off) to 255 (full), and your eye mixes the three into one
// color. Red and green make yellow, green and blue make cyan, all three make
// white.
//
// The sketch fades from one named color to the next over one second, holds it
// for a second, and prints its name and its three numbers when it gets there.
//
// # Why there are two whites
//
// The red die needs about 1.8 V and the green and blue ones about 3 V. On the
// same 220 ohm resistors from a 5 V pin, red gets about 15 mA and the other
// two about 10, so "all three at 255" comes out pinkish. The balanced white
// turns red down to two thirds to make up for it. On a real LED you can do
// the same in code, or give red a bigger resistor.
//
// # Common anode
//
// This is a common-cathode LED: HIGH lights a color. On a common-anode LED
// the long leg goes to 5 V and LOW lights a color, so every value is flipped:
// write 255 - value instead.

const int RED_PIN = 9;
const int GREEN_PIN = 10;
const int BLUE_PIN = 11;

struct Color {
  const char *name;
  uint8_t r, g, b;
};

const Color COLORS[] = {
    {"red", 255, 0, 0},
    {"orange", 255, 40, 0},
    {"yellow", 255, 160, 0},
    {"green", 0, 255, 0},
    {"cyan", 0, 255, 255},
    {"blue", 0, 0, 255},
    {"purple", 120, 0, 255},
    {"magenta", 255, 0, 255},
    {"white (all 255)", 255, 255, 255},
    {"balanced white", 170, 255, 255},
};
const int COUNT = sizeof(COLORS) / sizeof(COLORS[0]);

const int FADE_STEPS = 50;
const int STEP_MS = 20;
const int HOLD_MS = 1000;

int r = 0, g = 0, b = 0;

void show(int red, int green, int blue) {
  analogWrite(RED_PIN, red);
  analogWrite(GREEN_PIN, green);
  analogWrite(BLUE_PIN, blue);
}

void fadeTo(const Color &c) {
  for (int step = 1; step <= FADE_STEPS; step++) {
    show(r + (c.r - r) * step / FADE_STEPS,
         g + (c.g - g) * step / FADE_STEPS,
         b + (c.b - b) * step / FADE_STEPS);
    delay(STEP_MS);
  }
  r = c.r;
  g = c.g;
  b = c.b;
}

void setup() {
  pinMode(RED_PIN, OUTPUT);
  pinMode(GREEN_PIN, OUTPUT);
  pinMode(BLUE_PIN, OUTPUT);
  Serial.begin(115200);
  Serial.println("Mokxi Uno: RGB color mixer");
}

void loop() {
  for (int i = 0; i < COUNT; i++) {
    fadeTo(COLORS[i]);
    Serial.print(COLORS[i].name);
    Serial.print(": ");
    Serial.print(COLORS[i].r);
    Serial.print(", ");
    Serial.print(COLORS[i].g);
    Serial.print(", ");
    Serial.println(COLORS[i].b);
    delay(HOLD_MS);
  }
}

Code walkthrough

The key functions of the sketch, and what each one does.

show sketch.ino

Writes three brightness values to the red, green and blue pins with analogWrite. Everything else in the sketch goes through it.

void show(int red, int green, int blue) {
  analogWrite(RED_PIN, red);
  analogWrite(GREEN_PIN, green);
  analogWrite(BLUE_PIN, blue);
}

fadeTo sketch.ino

Moves from the current color to a new one in FADE_STEPS steps of STEP_MS each. At each step, it works out a point part of the way between the old and new value for each color. At the end it stores the new color in r, g and b.

void fadeTo(const Color &c) {
  for (int step = 1; step <= FADE_STEPS; step++) {
    show(r + (c.r - r) * step / FADE_STEPS,
         g + (c.g - g) * step / FADE_STEPS,
         b + (c.b - b) * step / FADE_STEPS);
    delay(STEP_MS);
  }
  r = c.r;
  g = c.g;
  b = c.b;
}

loop sketch.ino

Goes through the COLORS list. For each color it fades to it, prints the name and its three numbers, then holds for HOLD_MS.

void loop() {
  for (int i = 0; i < COUNT; i++) {
    fadeTo(COLORS[i]);
    Serial.print(COLORS[i].name);
    Serial.print(": ");
    Serial.print(COLORS[i].r);
    Serial.print(", ");
    Serial.print(COLORS[i].g);
    Serial.print(", ");
    Serial.println(COLORS[i].b);
    delay(HOLD_MS);
  }
}

Troubleshooting

The usual mistakes with this circuit, and the fix for each.

  • The colors are wrong, such as red showing as blue This project
    Red is on pin 9, green on 10 and blue on 11. Check each resistor goes to the matching LED leg.
  • Colors look inverted, or the LED is bright when it should be off This project
    You may have a common anode LED. Connect its common leg to 5 V and write 255 minus each value.
  • The colors are wrong or one color never lights. RGB LED
    Check whether your LED is common cathode (shared leg to ground) or common anode (shared leg to 5 V). Common anode works backwards: LOW turns a color on.
  • One color is much brighter than the others, or the LED runs too hot. RGB LED
    Use one resistor per color leg, never one for the shared leg. Typical values are 220 ohm for red and 100 to 150 ohm for green and blue on 5 V.
  • The LED is dim, burns out, or the pin reads wrong because the resistor value is off. Điện trở
    Read the color bands again. A 220 ohm to 330 ohm resistor suits a typical LED on 5 V. 10k is a common pull-up or pull-down value. Check with a multimeter if unsure.
  • A pull-up or pull-down resistor is missing, so the input floats and reads random values. Điện trở
    Tie the input to 5 V or ground through about 10k. Or use INPUT_PULLUP and wire the switch to ground.
  • Upload fails or nothing runs after wiring pins 0 and 1 to a module. Arduino Uno R3
    Pins 0 and 1 are the USB serial port. Move the module to other pins, or unplug it while uploading. Also check the right board is selected.
  • Pins misbehave or the board resets when you power motors or servos from it. Arduino Uno R3
    Each I/O pin gives about 20 mA, 40 mA max. Power motors and servos from their own supply and join its ground to the Uno's GND.

Sửa và giữ lại

Mở trong trình soạn thảo, đổi mạch hoặc code, và giữ phiên bản của bạn trong một tài khoản miễn phí.