Hazır proje · Arduino Uno R3

Arduino Uno'da RGB renk karıştırıcı

Her biri kendi direncinden geçen üç PWM pinindeki bir RGB LED: analogWrite onu, kırmızı yonga için dengelenmiş bir beyaz dahil on adlandırılmış rengin içinden soldurur. Tüm yapım, bir Arduino Uno ve 4 parça daha, aşağıdaki firmware üzerinde tarayıcında çalışır; kablolamayı ya da kodu değiştirmek ve yeniden çalıştırmak için düzenleyicide aç.

Öğretiyor musun? Tek tıkla sınıfına ata.

Başlangıç About 15 minutes to build and run. Tarayıcında çalışır. Ücretsiz, hesap gerekmez.

Parçalar yaklaşık $32'a satılıyor. Mokxi'de ücretsiz.

Adım adım, bağlantı ve yaygın hatalarla: Arduino RGB LED: Bağlantı ve Renk Karıştırma Kodu

RGB renk karıştırıcı · Arduino Uno R3canlı0.000 s 0.00x
Düzenleyicide açmak için tıkla
Devrenin kendisi, burada simülatörde çalışıyor. Basılabilecek şeylere bas; düzenleyicide açmak için başka bir şeye tıkla.

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: Başlangıç
  • Time: About 15 minutes
  • Board: Arduino Uno R3
  • Parts: 6

Nasıl çalışır

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.

Parça listesi

6 parça ve jumper kablolar. Yaklaşık $32'a satılıyor; Mokxi'de ücretsiz.

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.

Parça ve pinConnects 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

Kod

Düzenleyicinin açtığı şekliyle firmware. Orada bir satırı değiştir ve Çalıştır'a bas: tarayıcıda derlenir.

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. Direnç
    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. Direnç
    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.

Değiştir ve sakla

Düzenleyicide aç, devreyi ya da kodu değiştir ve sürümünü ücretsiz bir hesapta sakla.