Project Concept
The RGB LED with 3-function DIP Switch project allows you to switch between multiple lighting patterns and colors locally, without needing to re-upload code. By using a 3-position DIP switch, we can represent up to 8 different states (2^3), providing a versatile way to control color outputs.
Hardware Configuration
- RGB LED: As with most RGB projects, the red, green, and blue pins are connected to PWM-enabled digital pins (e.g., 9, 10, 11) with 220-ohm current-limiting resistors.
- DIP Switch: The three pins of the switch are connected to digital input pins (e.g., 5, 6, 7) using internal or external pull-up resistors. Each switch position determines a specific logic state (HIGH / LOW) for that pin.
Logic and Control
The Arduino code reads the states of the three DIP switch pins using digitalRead(). Based on the combination of these three inputs, the code uses if...else or switch statements to call different color functions. For example:
- Switch 1 ON: Red light.
- Switch 2 ON: Green light.
- Switch 1 & 2 ON: Yellow (Red + Green).
- All Switches OFF: Turn off the LED.
Code Sample Logic
int sw1 = digitalRead(5);
int sw2 = digitalRead(6);
int sw3 = digitalRead(7);
if (sw1 == HIGH && sw2 == LOW && sw3 == LOW) {
setColor(255, 0, 0); // Red
} else if (sw1 == LOW && sw2 == HIGH && sw3 == LOW) {
setColor(0, 255, 0); // Green
}
Versatility
This hardware-based selection method is great for projects requiring quick, manual configuration, such as setting modes for a mood lamp, a sign, or an indicator light on a larger prototype.