Video
                                
                                
# Lab: LED Dimming Control
## **Objective**
Learn to control LED brightness using analog output with the TinkerBlock TK01 - XL LED module. The LED will gradually fade from bright to dim and back, demonstrating PWM (Pulse Width Modulation) control.
## **Learning Outcomes**
- Understand the difference between digital and analog output
- Learn about PWM (Pulse Width Modulation) for LED brightness control
- Master the **analogWrite()** function
- Create smooth fading effects with LEDs
## **Required Components**
1. **Lonely Binary UNO R3** - Main Arduino board
2. **TinkerBlock UNO R3 Shield** - Expansion shield that plugs onto the UNO R3
3. **TinkerBlock TK01 - XL LED** - LED module with 4-pin connector
## **Theory**
### **Digital vs Analog Output**
- **Digital Output**: Can only be HIGH (5V) or LOW (0V) - like a light switch
- **Analog Output**: Can output varying voltage levels (0-5V) - like a dimmer switch
### **PWM (Pulse Width Modulation)**
PWM is a technique used to simulate analog output using digital pins. It works by rapidly switching a digital pin between HIGH and LOW states at different duty cycles:
- **Duty Cycle**: Percentage of time the signal is HIGH
- **Frequency**: How fast the switching occurs (usually too fast for human eye to see)
- **Result**: LED appears to have varying brightness levels
### **Key Functions Used**
- **pinMode(pin, mode)**: Configures a pin as input or output
- **analogWrite(pin, value)**: Outputs PWM signal (0-255) for analog simulation
- **delay(milliseconds)**: Pauses the program for a specified time
## **Circuit Diagram**
## **Connection Instructions**

1. **Assemble the Hardware:**
   - Place the TinkerBlock UNO R3 Shield onto the Lonely Binary UNO R3
   - Ensure all pins are properly aligned and seated
   - The shield should fit snugly on top of the Arduino board
2. **Connect the LED Module:**
   - Take the TinkerBlock TK01 - XL LED module
   - Align the 4-pin connector with any available 4-pin slot on the shield
   - Gently push the module into the slot until it clicks into place
   - The module will automatically connect:
     - **GND** pin to ground
     - **VCC** pin to 5V power
     - **NC** pin remains unconnected
     - **D3** pin to digital pin D3
## **Code**
```cpp
// Lab 2: LED Dimming Control
// LED connected to digital pin D3 (PWM capable)
// Define the LED pin
const int ledPin = 3;
void setup() {
  // Configure pin D3 as output
  pinMode(ledPin, OUTPUT);
}
void loop() {
  // Fade LED from dim to bright (0 to 255)
  for (int brightness = 0; brightness <= 255; brightness++) {
    analogWrite(ledPin, brightness);
    delay(10);  // Small delay for smooth transition
  }
  
  // Fade LED from bright to dim (255 to 0)
  for (int brightness = 255; brightness >= 0; brightness--) {
    analogWrite(ledPin, brightness);
    delay(10);  // Small delay for smooth transition
  }
}
```
## **Code Explanation**
### **Setup Function**
```cpp
void setup() {
  pinMode(ledPin, OUTPUT);
}
```
- Runs once when Arduino starts
- Configures pin D3 as an output pin
- Same as Lab 1, but now we'll use analogWrite() instead of digitalWrite()
### **Loop Function**
```cpp
void loop() {
  // Fade in
  for (int brightness = 0; brightness <= 255; brightness++) {
    analogWrite(ledPin, brightness);
    delay(10);
  }
  
  // Fade out
  for (int brightness = 255; brightness >= 0; brightness--) {
    analogWrite(ledPin, brightness);
    delay(10);
  }
}
```
### **Fade In (0 to 255):**
- **for** loop increases brightness from 0 to 255
- **analogWrite(ledPin, brightness)**: Sets PWM duty cycle
- **delay(10)**: Creates smooth transition (10ms between each brightness level)
### **Fade Out (255 to 0):**
- **for** loop decreases brightness from 255 to 0
- Same process but in reverse direction
### **PWM Values**
- **0**: LED completely off (0% duty cycle)
- **128**: LED at 50% brightness (50% duty cycle)
- **255**: LED at full brightness (100% duty cycle)
## **Testing and Verification**
1. **Upload the code to Lonely Binary UNO R3**
2. **Observe the LED behavior:**
   - The XL LED should gradually fade from off to full brightness
   - Then gradually fade from full brightness back to off
   - This cycle should repeat continuously
3. **Verify connections:**
   - Ensure the shield is properly seated on the Arduino
   - Check that the TK01 module is firmly connected to the shield
   - Confirm the LED is visible and not obstructed
## **Troubleshooting**
### **LED doesn't change brightness:**
- Check if the TinkerBlock shield is properly seated on the Arduino
- Ensure the TK01 module is firmly connected to the shield
- Verify the module is connected to a slot that uses D3 pin (PWM capable)
- Check if code uploaded successfully to the Lonely Binary UNO R3
### **LED flickers or changes brightness abruptly:**
- Check if **delay()** functions are working properly
- Verify pin number in code matches the shield's D3 connection
- Ensure the Lonely Binary UNO R3 is powered consistently
- Try reconnecting the TK01 module to a different 4-pin slot
### **LED is too dim or too bright:**
- Check power supply to the Lonely Binary UNO R3
- Ensure the shield is making good contact with all pins
- Try cleaning the module connector pins if necessary
- Verify the built-in resistor in the TK01 module is working
## **Experiment Variations**
### **1. Change Fading Speed**
Modify the delay values to change fading speed:
```cpp
delay(5);   // Faster fading
delay(20);  // Slower fading
delay(50);  // Much slower fading
```
### **2. Different Brightness Patterns**
Create breathing effect or step patterns:
```cpp
// Breathing effect (exponential fade)
for (int brightness = 0; brightness <= 255; brightness++) {
  analogWrite(ledPin, brightness);
  delay(brightness / 10);  // Slower at higher brightness
}
```
### **3. Multiple Brightness Levels**
Create a step pattern instead of smooth fade:
```cpp
// Step pattern
analogWrite(ledPin, 0);    delay(1000);  // Off
analogWrite(ledPin, 64);   delay(1000);  // 25% brightness
analogWrite(ledPin, 128);  delay(1000);  // 50% brightness
analogWrite(ledPin, 192);  delay(1000);  // 75% brightness
analogWrite(ledPin, 255);  delay(1000);  // 100% brightness
```
## **Questions for Understanding**
1. What is the difference between **digitalWrite()** and **analogWrite()**?
2. How does PWM create the illusion of varying brightness?
3. What is the range of values for **analogWrite()** and what do they represent?
4. Why do we need to use a PWM-capable pin for analog output?
5. What would happen if you used **analogWrite()** on a non-PWM pin?
6. How could you create a "breathing" effect with the LED?
## **Next Steps**
- Try connecting multiple TK01 LED modules to different 4-pin slots on the shield
- Experiment with different fading patterns and speeds
- Learn about reading analog input with **analogRead()**
- Explore other TinkerBlock modules that can be controlled with analog output
- Try creating synchronized patterns with multiple LEDs
## **Safety Notes**
- Always disconnect power before connecting or disconnecting modules
- Handle the TinkerBlock shield and modules carefully to avoid bending pins
- Ensure proper alignment when connecting modules to prevent damage
- The built-in resistor in the TK01 module prevents LED damage
- Keep the Lonely Binary UNO R3 and shield in a stable position during operation
---
**Lab Completion Checklist:**
- [ ] TinkerBlock shield properly seated on Lonely Binary UNO R3
- [ ] TK01 LED module connected to shield
- [ ] Code uploaded successfully to Lonely Binary UNO R3
- [ ] LED fades smoothly from off to bright and back
- [ ] Troubleshooting completed (if needed)
- [ ] Experiment variations tried
- [ ] Questions answered
                                
                            
                            

 
	   
	 
	   
	 
	   
	 
	   
	 
	   
	 
	   
	 
	   
	