Digital Outputs, RGB & Buzzer
Drive several LEDs, mix colours on an RGB LED and make sound with tone() - the output half of Lesson 07.
Driving outputs
An output pin can be HIGH (about 5 V) or LOW (0 V), and can supply about 20 mA - enough for an LED with its series resistor, not enough for a motor.
The course wiring for this chapter is the Lesson 07 circuit: an RGB LED with its red, green and blue legs on pins 9, 10 and 11 through 330 Ω resistors, and a passive buzzer on pin 6. The RGB LED is common cathode, so HIGH switches a colour on.
Three LEDs in turn
Each colour is just a pin. Press Run and watch the three lamps on the virtual board.
Switch on the red LED
Make pin 9 an output and switch the red LED on so it stays on. loop() stays empty.
Alternate red and green
Switch between the red LED (pin 9) and the green one (pin 10) every 500 ms, so exactly one of them is on at any time. Start with red.
An RGB LED is three LEDs in one
A common-cathode RGB LED has one leg to GND and three legs you drive HIGH to switch each colour on. Switching two on at once mixes them:
- red + green = yellow
- red + blue = magenta
- green + blue = cyan
- all three = white
With plain digitalWrite you get these seven colours. Chapter 10 adds analogWrite for the shades in between.
Make yellow
Switch the RGB LED to yellow and leave it there: red and green on, blue off.
Colour cycle
setColour(red, green, blue) is written for you - it switches all three pins at once. Use it to cycle red → green → blue → all off, 500 ms each, over and over.
Making sound with tone()
A passive buzzer needs a signal to make sound. tone(pin, frequency) switches the pin on and off at that frequency until you call noTone(pin). tone(pin, frequency, duration) stops on its own after duration milliseconds.
Middle A is 440 Hz; middle C is about 262 Hz. The virtual board shows the frequency instead of making a noise.
A beep once a second
tone does not block: the 200 ms beep plays while the sketch carries on to the delay.
Two-tone alarm
Make the buzzer alternate between 440 Hz and 880 Hz, each for 400 ms, without gaps. Use tone(BUZZER, frequency) with no duration - the next tone call replaces the current note.
Startup chime
Play a three-note startup chime once at reset, with the RGB LED changing colour on each note: C (262 Hz) red, E (330 Hz) green, G (392 Hz) blue. Each note lasts 250 ms, and the next one starts 300 ms after the last, leaving a short silence. Switch everything off at the end.
Try it on a real Uno
Wire the Lesson 07 circuit: the RGB LED's red, green and blue legs through 330 Ω resistors to pins 9, 10 and 11, its long leg to GND, and a passive buzzer on pin 6. Upload Colour cycle, then Startup chime.
If a colour looks wrong, check which leg is which - and remember a common-anode RGB LED works the other way round: LOW switches a colour on.