Video
                                
                                
# Lab: TK04 Push Button Control
## **Objective**
Learn to read digital input from the TinkerBlock TK04 Push Button module. The button's built-in LED will light when pressed, and we'll use Serial communication to monitor button states.
## **Learning Outcomes**
- Understand digital input concepts
- Learn about pull-down resistors and active-high signals
- Master the **digitalRead()** function
- Use Serial communication for debugging and monitoring
## **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 TK04 - Push Button** - Push button module with 4-pin connector
## **Theory**
### **Digital Input**
- **Digital Input**: Arduino receives signals from external devices (like buttons)
- **Purpose**: Allows Arduino to sense the state of external components
- **Values**: Can read HIGH (5V) or LOW (0V) from connected devices
### **Push Button Basics**
A push button is a simple mechanical switch that:
- **Open State**: No electrical connection between terminals
- **Closed State**: Electrical connection between terminals when pressed
- **Bounce**: Mechanical switches can create multiple rapid on/off signals
### **Active-High vs Active-Low**
- **Active-High**: Signal is HIGH (5V) when button is pressed, LOW (0V) when released
- **Active-Low**: Signal is LOW (0V) when button is pressed, HIGH (5V) when released
### **Pull-Down Resistor**
- **Purpose**: Ensures a defined logic level when button is not pressed
- **Function**: Pulls the signal line to ground (LOW) when button is open
- **Value**: 10kΩ resistor provides stable reference without excessive current draw
- **TK04 Built-in**: The TK04 module includes a built-in 10kΩ pull-down resistor, eliminating the need for external components
### **Key Functions Used**
- **pinMode(pin, mode)**: Configures a pin as input or output
- **digitalRead(pin)**: Reads the state of a digital pin (HIGH or LOW)
- **Serial.begin(baud)**: Initializes serial communication
- **Serial.print()** and **Serial.println()**: Outputs data to Serial Monitor
- **delay(milliseconds)**: Pauses the program for a specified time
## **Circuit Diagram**
```
Lonely Binary UNO R3
    ↓
TinkerBlock UNO R3 Shield
    ↓
TinkerBlock TK04 - Push Button Module
```
## **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 Push Button Module:**
   - Take the TinkerBlock TK04 - Push Button 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
     - **Signal** pin to digital pin D5
3. **Important Notes:**
   - No breadboard or jumper wires required
   - The TK04 push button has a built-in 10kΩ pull-down resistor
   - The button's built-in LED will light when pressed
## **Code**
```cpp
// Lab 3: Push Button Control
// Push button connected to digital pin D5
// Define the button pin
const int buttonPin = 5;   // Button input pin
void setup() {
  // Configure button pin as input
  pinMode(buttonPin, INPUT);
  
  // Initialize serial communication for debugging
  Serial.begin(9600);
  
  Serial.println("Push Button Lab Started");
  Serial.println("Press the button to see the state change");
}
void loop() {
  // Read the button state
  int buttonState = digitalRead(buttonPin);
  
  // Print button state to serial monitor
  Serial.print("Button State: ");
  Serial.println(buttonState);
  
  // Add descriptive text based on button state
  if (buttonState == HIGH) {
    Serial.println("Button is PRESSED");
  }
  
  // Small delay to prevent too rapid reading
  delay(100);
}
```
## **Code Explanation**
### **Setup Function**
```cpp
void setup() {
  pinMode(buttonPin, INPUT);
  Serial.begin(9600);
  Serial.println("Push Button Lab Started");
}
```
- Configures pin D5 as input for button reading
- Initializes serial communication for debugging
- Prints startup messages to Serial Monitor
### **Loop Function**
```cpp
void loop() {
  int buttonState = digitalRead(buttonPin);
  
  if (buttonState == HIGH) {
    Serial.println("Button is PRESSED");
  }
}
```
### **Button Reading:**
- **digitalRead(buttonPin)**: Reads the state of pin D5
- Returns **HIGH** (1) when button is pressed
- Returns **LOW** (0) when button is not pressed
### **Serial Output:**
- **if (buttonState == HIGH)**: Checks if button is pressed
- Prints descriptive messages based on button state
- Provides clear feedback about button status
### **Serial Communication**
- **Serial.begin(9600)**: Starts serial communication at 9600 baud
- **Serial.print()** and **Serial.println()**: Output debug information
- Useful for monitoring button states and troubleshooting
## **Testing and Verification**
1. **Upload the code to Lonely Binary UNO R3**
2. **Open Serial Monitor** in Arduino IDE (Tools → Serial Monitor)
3. **Observe the behavior:**
   - When button is not pressed: Serial shows "Button State: 0" and "Button is NOT PRESSED"
   - When button is pressed: Serial shows "Button State: 1" and "Button is PRESSED"
   - The button's built-in LED should light when pressed
4. **Verify connections:**
   - Ensure the TK04 module is firmly connected to the shield
   - Check that the shield is properly seated on the Arduino
   - Confirm the button's built-in LED is visible and not obstructed
## **Troubleshooting**
### **Button doesn't respond:**
- Check if the TinkerBlock shield is properly seated on the Arduino
- Ensure the TK04 module is firmly connected to the shield
- Verify the module is connected to a slot that uses D5 pin
- Check if code uploaded successfully to the Lonely Binary UNO R3
- Open Serial Monitor to see if button states are being read
### **Button reads incorrectly:**
- Check Serial Monitor for unexpected button state values
- Verify the button module is connected to the correct pin (D5)
- Try reconnecting the TK04 module to a different 4-pin slot
- Check for loose connections between shield and Arduino
### **Button's built-in LED doesn't light:**
- Check if the TK04 module is properly powered (VCC and GND connections)
- Verify the module is firmly seated in the shield
- Try reconnecting the module to a different 4-pin slot
### **Serial Monitor shows no output:**
- Ensure Serial Monitor is set to 9600 baud rate
- Check that **Serial.begin(9600)** is in setup()
- Verify the correct COM port is selected in Arduino IDE
## **Experiment Variations**
### **1. Button Press Counter**
Count and display the number of button presses:
```cpp
int buttonPressCount = 0;
int lastButtonState = LOW;
void loop() {
  int buttonState = digitalRead(buttonPin);
  
  // Detect button press (transition from LOW to HIGH)
  if (buttonState == HIGH && lastButtonState == LOW) {
    buttonPressCount++;
    Serial.print("Button pressed! Total presses: ");
    Serial.println(buttonPressCount);
  }
  
  lastButtonState = buttonState;
  delay(50);  // Debounce delay
}
```
### **2. Button Hold Detection**
Detect how long the button is held:
```cpp
unsigned long pressStartTime = 0;
bool buttonWasPressed = false;
void loop() {
  int buttonState = digitalRead(buttonPin);
  
  if (buttonState == HIGH && !buttonWasPressed) {
    // Button just pressed
    pressStartTime = millis();
    buttonWasPressed = true;
    Serial.println("Button pressed - timing started");
  } else if (buttonState == LOW && buttonWasPressed) {
    // Button released
    unsigned long holdTime = millis() - pressStartTime;
    Serial.print("Button held for: ");
    Serial.print(holdTime);
    Serial.println(" milliseconds");
    buttonWasPressed = false;
  }
  
  delay(10);
}
```
### **3. Button State with Timestamp**
Add timestamps to button state changes:
```cpp
int lastButtonState = LOW;
void loop() {
  int buttonState = digitalRead(buttonPin);
  
  if (buttonState != lastButtonState) {
    unsigned long currentTime = millis();
    
    if (buttonState == HIGH) {
      Serial.print("Button PRESSED at: ");
      Serial.print(currentTime);
      Serial.println(" ms");
    } else {
      Serial.print("Button RELEASED at: ");
      Serial.print(currentTime);
      Serial.println(" ms");
    }
    
    lastButtonState = buttonState;
  }
  
  delay(10);
}
```
## **Questions for Understanding**
1. What is the difference between **digitalRead()** and **digitalWrite()**?
2. Why do we need a pull-down resistor with the push button?
3. What does "active-high" mean for the button signal?
4. How does the built-in LED on the TK04 module work?
5. What is button bounce and how can it affect your program?
6. Why do we use **delay(100)** in the main loop?
7. What is the purpose of Serial communication in this lab?
8. How can you detect a button press vs. button hold?
## **Next Steps**
- Try connecting multiple TK04 push buttons to different 4-pin slots
- Experiment with different button control patterns
- Learn about button debouncing techniques
- Explore other TinkerBlock input modules (sensors, switches)
- Try combining button input with LED output for interactive projects
- Learn about interrupt-based button handling
## **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 TK04 module prevents component damage
- Keep the Lonely Binary UNO R3 and shield in a stable position during operation
                                
                            
                            

 
	   
	 
	   
	 
	   
	 
	   
	 
	   
	 
	   
	 
	   
	