Week10
# Week10 Output Devices
SSTM¶

Group Assignment: — on progress
Individual Assignment:
- Add an output device to a self-designed microcontroller board, my PCB board ready and am on searching from different source for XIAO ESP 32 C3
- Program the device to perform a specific action … I did to align the Alarm system with my FP and Buzzer as output device.
1. Introduction to Output Devices¶
Output devices are components that receive signals from a microcontroller and convert them into physical actions such as light, sound, or movement.

2. Selected Output Devices¶
- LED → Visual alert
- Buzzer → Sound alarm
3. Circuit Connection¶
- LED → Pin 8
- Buzzer → Pin 9
- Both connected to GND

4. Arduino Code to learn the output devices with code integration¶
// ===== PIN DEFINITIONS =====
int CLK = 2; // Rotary encoder CLK pin (signal A)
int DT = 3; // Rotary encoder DT pin (signal B)
int SW = 4; // Rotary encoder push button
int redPin = 9; // RGB LED - Red channel (PWM)
int greenPin = 10; // RGB LED - Green channel (PWM)
int bluePin = 11; // RGB LED - Blue channel (PWM)
// ===== STATE VARIABLES =====
int currentStateCLK; // current reading of CLK
int lastStateCLK; // previous reading of CLK
int brightness = 150; // LED brightness (0–255)
int hue = 0; // color position (0–360 degrees)
bool mode = 0; // 0 = color mode, 1 = brightness mode
// ===== SETUP FUNCTION =====
void setup() {
pinMode(CLK, INPUT); // encoder CLK as input
pinMode(DT, INPUT); // encoder DT as input
pinMode(SW, INPUT_PULLUP); // button with internal pull-up
pinMode(redPin, OUTPUT); // RGB red output
pinMode(greenPin, OUTPUT); // RGB green output
pinMode(bluePin, OUTPUT); // RGB blue output
Serial.begin(9600); // start serial communication
lastStateCLK = digitalRead(CLK); // store initial CLK state
}
// ===== MAIN LOOP =====
void loop() {
currentStateCLK = digitalRead(CLK); // read encoder rotation
// ===== DETECT ROTATION =====
if (currentStateCLK != lastStateCLK) { // if rotation detected
if (digitalRead(DT) != currentStateCLK) {
// Clockwise rotation
if (mode == 0) {
hue += 5; // change color smoothly
} else {
brightness += 10; // increase brightness
}
} else {
// Counterclockwise rotation
if (mode == 0) {
hue -= 5; // reverse color direction
} else {
brightness -= 10; // decrease brightness
}
}
// ===== LIMIT VALUES =====
brightness = constrain(brightness, 0, 255); // keep valid PWM range
hue = (hue + 360) % 360; // keep hue in 0–359
// ===== DEBUG OUTPUT =====
Serial.print("Mode: ");
Serial.print(mode); // show current mode
Serial.print(" | Brightness: ");
Serial.print(brightness); // show brightness value
Serial.print(" | Hue: ");
Serial.println(hue); // show color position
}
lastStateCLK = currentStateCLK; // update state
// ===== BUTTON MODE SWITCH =====
if (digitalRead(SW) == LOW) { // button pressed
delay(200); // debounce delay
mode = !mode; // toggle mode
}
// ===== UPDATE RGB OUTPUT =====
HSVtoRGB(hue, brightness); // convert and apply color
}
// ===== HSV → RGB FUNCTION =====
void HSVtoRGB(int hue, int value) {
float h = hue / 60.0; // divide color wheel into 6 regions
int i = floor(h); // get region index
float f = h - i; // fractional part
int p = 0; // base value (no color)
int q = value * (1 - f);
int t = value * f;
// ===== SELECT COLOR REGION =====
switch(i % 6) {
case 0: setRGB(value, t, p); break; // Red → Yellow
case 1: setRGB(q, value, p); break; // Yellow → Green
case 2: setRGB(p, value, t); break; // Green → Cyan
case 3: setRGB(p, q, value); break; // Cyan → Blue
case 4: setRGB(t, p, value); break; // Blue → Magenta
case 5: setRGB(value, p, q); break; // Magenta → Red
}
}
// ===== APPLY RGB VALUES =====
void setRGB(int r, int g, int b) {
analogWrite(redPin, r); // set red brightness
analogWrite(greenPin, g); // set green brightness
analogWrite(bluePin, b); // set blue brightness
}
Code source AI (with prompt “generate and write a code for RGB LED control with Rotary) **
5. How the Code Works¶
- The microcontroller continuously runs the
loop()function - It turns ON the LED and buzzer for 1 second
- Then turns them OFF for 1 second
- This repeats forever creating a blinking + beeping alert

7. Result¶
- LED blinks successfully
- Buzzer produces sound
- Outputs respond correctly to microcontroller signals
src=”Images/w10_image/Scanned_Mouse(1).mp4”
8. Conclusion¶
This assignment demonstrates how to:
- Control output devices
- Program microcontroller responses
- Build a simple RGB with rotary control system