Unit B - Inputs & Outputs

07. Digital Outputs, RGB LEDs & Buzzers

Drive LEDs safely, mix RGB colour, and play tones on a passive buzzer.

Estimated time 3 hours

Learning outcomes

  • Use pinMode OUTPUT and digitalWrite HIGH/LOW correctly
  • Limit LED current with a series resistor on each channel
  • Explain common-cathode versus common-anode RGB wiring and logic
  • Distinguish active and passive buzzers and use tone / noTone
  • Recognise loads that need a driver instead of a bare Uno pin

Parts and preparation

Uno, common-cathode RGB LED (or separate red/green/blue LEDs), 3 x 330 ohm resistors, passive piezo buzzer, breadboard and jumpers.

Before power: inspect wiring, confirm supply voltage and ensure all connected circuits share GND.

Digital Outputs, RGB LEDs & Buzzers instructional connection diagram

What a digital output does

After pinMode(pin, OUTPUT), digitalWrite chooses a logic level. On a 5 V Uno, LOW is about 0 V and HIGH is about 5 V.

That is enough for indicator LEDs (with resistors) and for signalling other logic chips. It is not a power supply for motors, heaters or big lamps.

Two cards showing LOW near 0 V and HIGH near 5 V with digitalWrite examples
OUTPUT pins switch between roughly 0 V and 5 V on a Uno.

Safe loads vs drivers

A classic Uno pin is rated for modest current (plan around 20 mA per pin, and stay well under absolute maxima). Always use a series resistor with an LED.

Motors, relays, solenoids and heaters need a transistor, MOSFET or driver module plus a suitable supply - and often a flyback diode. Lesson 12 covers drivers in more depth.

OK loads like LED with resistor versus loads that need a driver such as motors and relays
LEDs and small piezo loads: OK with care. High current: use a driver.

RGB LEDs: three dies, three resistors

An RGB LED packs red, green and blue emitters. Each colour needs its own series resistor - sharing one resistor for all three is incorrect and uneven.

Course wiring assumes common cathode: the shared leg goes to GND, and each colour leg goes through 330 ohm to a digital pin. HIGH turns that colour on.

RGB pins through resistors into a common-cathode LED plus a passive buzzer on D6
Course pattern: RGB on D9/D10/D11 with resistors; passive buzzer on D6.
SignalCourse pinRole
RedD9Red channel via 330 ohm
GreenD10Green channel via 330 ohm
BlueD11Blue channel via 330 ohm
BuzzerD6Passive piezo + tone()

Common cathode vs common anode

Common cathode: shared pin to GND; drive colours HIGH to light. That is this lesson's default.

Common anode: shared pin to 5 V; drive colours LOW to light (active-low). If your RGB looks always-on or inverted, you may have the other type - check the datasheet and invert the logic.

Common cathode shares GND with HIGH on, versus common anode shares 5 V with LOW on
Same package shape does not guarantee the same pinout or logic.

Active vs passive buzzers

An active buzzer has a built-in oscillator: a steady HIGH makes a fixed beep. A passive piezo is closer to a speaker: it needs a square wave at an audible frequency.

Arduino tone(pin, frequency) generates that wave. An optional third argument is duration in milliseconds. noTone(pin) stops sound. This course uses a passive buzzer with tone().

Active buzzer driven by DC HIGH versus passive buzzer driven by tone frequency
Active: DC beep. Passive: tone() sets the pitch.
tone(buzzerPin, 440, 200);  // A4, 200 ms
delay(250);
noTone(buzzerPin);

What the worked sketch practises

The sketch cycles red, green and blue with a different tone for each step. Only one colour is HIGH at a time so you can confirm each channel and hear pitch changes.

Try mixing colours later by turning two pins HIGH together (yellow ≈ red+green).

Wiring and safe build sequence

Breadboard wiring for lesson 07: Digital Outputs, RGB LEDs & Buzzers
Breadboard layout for this lesson. Match colours and pins before powering the circuit. Click the image for a larger view.
  1. RGB common cathode -> GND (confirm datasheet if unmarked)
  2. Red leg -> 330 ohm -> D9
  3. Green leg -> 330 ohm -> D10
  4. Blue leg -> 330 ohm -> D11
  5. Passive buzzer + -> D6; buzzer - -> GND
  6. If colours are inverted or always on, you may have a common-anode LED - invert HIGH/LOW
Power rule: switch off before moving wires. Arduino I/O pins are control signals; high-current loads require a driver and suitable external supply.
const byte redPin = 9;
const byte greenPin = 10;
const byte bluePin = 11;
const byte buzzerPin = 6;

void allOff() {
  digitalWrite(redPin, LOW);
  digitalWrite(greenPin, LOW);
  digitalWrite(bluePin, LOW);
}

void setup() {
  pinMode(redPin, OUTPUT);
  pinMode(greenPin, OUTPUT);
  pinMode(bluePin, OUTPUT);
  // tone() does not require pinMode, but OUTPUT is fine if you also digitalWrite
  pinMode(buzzerPin, OUTPUT);
  allOff();
}

void loop() {
  allOff();
  digitalWrite(redPin, HIGH);
  tone(buzzerPin, 440, 200);
  delay(500);

  allOff();
  digitalWrite(greenPin, HIGH);
  tone(buzzerPin, 660, 200);
  delay(500);

  allOff();
  digitalWrite(bluePin, HIGH);
  tone(buzzerPin, 880, 200);
  delay(500);
}

How the code works

  1. allOff clears every colour before lighting the next - easier to see each channel.
  2. tone duration is in milliseconds; delay should be a little longer if you want a gap.
  3. Each RGB colour needs its own resistor even when only one colour is on.
  4. For common-anode RGB, swap the sense of HIGH and LOW (or write a small helper).

Test and record evidence

Expected result: Red, then green, then blue light in turn. A higher pitch sounds with each step (440, 660, 880 Hz).

Practical evidence checklist

Common faults and checks
  • Verify RGB pinout from the module datasheet - long leg is not always common.
  • A common-anode LED needs inverted HIGH/LOW logic compared with this sketch.
  • No sound: confirm a passive buzzer (not active-only) and polarity; try a known frequency like 1000.
  • Only one colour works: check that channel's resistor and pin number.
  • USB brown-out or reset when wiring: you may have shorted a pin - disconnect and recheck.
Extension challenge: Create a three-note status melody and a matching RGB colour sequence (for example red/error, green/ok, blue/busy). Put the melody in a function playStatus(byte code).

Check your understanding

Q1. Why does each RGB channel need a resistor?

Show answer

Each LED die needs its own current limiting.

Q2. What does tone control on a passive buzzer?

Show answer

The frequency of the square wave (the pitch).

Q3. How does common-anode drive differ from common-cathode?

Show answer

Shared leg to 5 V; colours turn on with LOW instead of HIGH.

Q4. Why not drive a DC motor from a digital pin directly?

Show answer

Current and inductive kick exceed what a logic pin should supply - use a driver.

Q5. What does allOff achieve in the sketch?

Show answer

It turns every colour channel LOW before the next colour so only one shows at a time.