02 - Lab Basic Analog Input

Video

# Lab: Disc Potentiometer - Basic Analog Input ## Objective Learn to read analog values from the TinkerBlock TK07 Disc Potentiometer module. The potentiometer provides variable resistance that can be converted to digital values, allowing for continuous input control. This lab introduces analog input concepts and demonstrates how to read and display potentiometer values. ## Learning Outcomes - Understand analog input and analog-to-digital conversion - Learn about potentiometer operation and variable resistance - Master analogRead() function for reading analog values - Create responsive analog input systems - Use Serial communication for analog value monitoring - Understand voltage division and resistance measurement ## Required Components ![](https://cdn.shopify.com/s/files/1/0331/9994/7908/files/Pasted_image_20250722144307_2f79a8a1-eedb-4b3d-bf9f-71fff7502b4f.png?v=1753823569) 1. **Lonely Binary UNO R3** - Main Arduino board 2. **TinkerBlock UNO R3 Shield** - Expansion shield that plugs onto the UNO R3 3. **TinkerBlock TK07 - Disc Potentiometer** - 1KΩ potentiometer module with 4-pin connector ## Theory ### Analog Input Concepts - **Analog Signal**: Continuous voltage values (0-5V) - **Digital Conversion**: Converting analog voltage to digital values (0-1023) - **Resolution**: 10-bit ADC provides 1024 discrete values (0-1023) - **Voltage Range**: 0V = 0, 5V = 1023, linear relationship ### Potentiometer Operation - **Variable Resistor**: Resistance changes with rotation - **Voltage Divider**: Creates variable voltage output - **1KΩ Range**: Total resistance of 1,000 ohms - **Rotation Range**: 300 degrees of rotation - **Output**: Variable voltage from 0V to 5V ### Analog-to-Digital Conversion (ADC) - **10-bit Resolution**: 2^10 = 1024 possible values - **Reference Voltage**: 5V (default for Arduino UNO) - **Conversion Time**: ~100 microseconds per reading - **Accuracy**: ±2 LSB (Least Significant Bit) ### Voltage Division Principle ``` Vout = Vin × (R2 / (R1 + R2)) ``` - **Vin**: 5V (supply voltage) - **R1**: Variable resistance (0-1KΩ) - **R2**: Fixed resistance - **Vout**: Variable output voltage (0-5V) ### Key Functions Used - `pinMode(pin, mode)`: Configures a pin as input or output - `analogRead(pin)`: Reads analog value from specified pin (0-1023) - `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 - `map(value, fromLow, fromHigh, toLow, toHigh)`: Maps a value from one range to another ## Circuit Diagram ![](https://cdn.shopify.com/s/files/1/0331/9994/7908/files/Pasted_image_20250722144522_c2799453-7ec4-4927-835d-797e816e9849.png?v=1753823574) ## 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 Potentiometer Module:** - Take the TinkerBlock TK07 - Disc Potentiometer 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 analog pin A0 3. **Important Notes:** - No breadboard or jumper wires required - The TK07 potentiometer has a built-in voltage divider circuit - The potentiometer provides smooth analog output - Multiple 4-pin slots are available on the shield for expansion - The disc potentiometer can be rotated 300 degrees ## Code ```cpp // Lab: Disc Potentiometer - Basic Analog Input // Potentiometer connected to analog pin A0 // Define the analog pin const int potPin = A0; // Potentiometer input pin // Variables for reading and processing int potValue = 0; // Raw analog value (0-1023) int potValueMapped = 0; // Mapped value (0-100) float voltage = 0.0; // Calculated voltage (0-5V) float resistance = 0.0; // Resistance from wiper to 5V end (0-1000Ω) void setup() { // Configure potentiometer pin as input (optional for analog pins) pinMode(potPin, INPUT); // Initialize serial communication for monitoring Serial.begin(9600); Serial.println("Disc Potentiometer Lab Started"); Serial.println("Rotate the potentiometer to see values change"); Serial.println("----------------------------------------"); Serial.println("Format: Raw | Mapped | Voltage | Resistance"); Serial.println("----------------------------------------"); } void loop() { // Read the analog value from the potentiometer potValue = analogRead(potPin); // Map the value to different ranges for demonstration potValueMapped = map(potValue, 0, 1023, 0, 100); // Calculate voltage (assuming 5V reference) voltage = (potValue * 5.0) / 1023.0; // Calculate resistance based on potentiometer position // For a 1KΩ potentiometer, resistance is proportional to voltage resistance = (voltage * 1000.0) / 5.0; // Display the values Serial.print("Raw: "); Serial.print(potValue); Serial.print(" | Mapped: "); Serial.print(potValueMapped); Serial.print("% | Voltage: "); Serial.print(voltage, 2); Serial.print("V | Resistance: "); Serial.print(resistance, 0); Serial.println("Ω"); // Small delay to make the output readable delay(200); } ``` ## Code Explanation ### Setup Function ```cpp void setup() { pinMode(potPin, INPUT); Serial.begin(9600); } ``` - Configures analog pin A0 as input (optional for analog pins) - Initializes serial communication for value monitoring - Sets up display format for easy reading ### Main Loop Logic ```cpp void loop() { potValue = analogRead(potPin); potValueMapped = map(potValue, 0, 1023, 0, 100); voltage = (potValue * 5.0) / 1023.0; resistance = (voltage * 1000.0) / 5.0; } ``` **Analog Reading:** - `analogRead(potPin)`: Reads raw analog value (0-1023) - **0**: Minimum rotation (0V) - **1023**: Maximum rotation (5V) - **Linear relationship**: Direct proportion to rotation **Value Mapping:** - `map(potValue, 0, 1023, 0, 100)`: Converts to percentage - **0-100%**: More intuitive for user interface - **Customizable ranges**: Can map to any desired range **Voltage Calculation:** - `voltage = (potValue * 5.0) / 1023.0` - **5.0V**: Reference voltage - **1023**: Maximum ADC value - **Result**: Actual voltage across potentiometer **Resistance Calculation:** - `resistance = (voltage * 1000.0) / 5.0` - **Direct proportion**: Resistance is proportional to voltage - **1000Ω**: Total potentiometer resistance - **Result**: Resistance from wiper to one end of potentiometer ### Key Variables - **potValue**: Raw analog reading (0-1023) - **potValueMapped**: Percentage value (0-100) - **voltage**: Calculated voltage (0-5V) - **resistance**: Resistance from wiper to one end (0-1000Ω) ### Value Ranges - **Raw ADC**: 0-1023 (10-bit resolution) - **Mapped**: 0-100 (percentage) - **Voltage**: 0.00-5.00V (2 decimal places) - **Resistance**: 0-1000Ω (wiper to 5V end) ## Testing and Verification 1. **Upload the code to Lonely Binary UNO R3** 2. **Open Serial Monitor** in Arduino IDE (Tools → Serial Monitor) 3. **Test potentiometer rotation:** - **Minimum rotation**: Should show values near 0 - **Maximum rotation**: Should show values near 1023 - **Smooth transition**: Values should change continuously - **No jumps**: Should be smooth without sudden changes 4. **Verify value ranges:** - **Raw values**: 0-1023 - **Mapped values**: 0-100% - **Voltage**: 0.00-5.00V - **Resistance**: 0-1000Ω (wiper to 5V end) 5. **Verify connections:** - Ensure the TK07 module is firmly connected to the shield - Check that the shield is properly seated on the Arduino - Confirm the potentiometer rotates smoothly ## Troubleshooting ### Potentiometer doesn't respond: - Check if the TinkerBlock shield is properly seated on the Arduino - Ensure the TK07 module is firmly connected to the shield - Verify the module is connected to a slot that uses A0 pin - Check if code uploaded successfully to the Lonely Binary UNO R3 - Open Serial Monitor to see if values are being read ### Values not changing: - Check if the potentiometer is rotating properly - Verify the module is connected to analog pin A0 - Check Serial Monitor output to see if readings are stable - Ensure the potentiometer disc is not stuck or damaged ### Values jumping or unstable: - Check if the module is properly seated in the shield - Verify all pins are making good contact - Try reconnecting the module to a different 4-pin slot - Check for loose connections or bent pins ### 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 ### Values not in expected range: - Check if the potentiometer is fully rotated - Verify the mapping calculations are correct - Ensure the reference voltage is 5V - Check if the potentiometer resistance is 1KΩ ## Experiment Variations ### 1. Different Mapping Ranges Map the potentiometer to different ranges: ```cpp // Map to 0-255 (for PWM output) int pwmValue = map(potValue, 0, 1023, 0, 255); // Map to 0-180 (for servo control) int servoAngle = map(potValue, 0, 1023, 0, 180); // Map to -100 to 100 (for bidirectional control) int bidirectionalValue = map(potValue, 0, 1023, -100, 100); ``` ### 2. Smoothing and Averaging Add smoothing to reduce noise: ```cpp const int numReadings = 10; int readings[numReadings]; int readIndex = 0; int total = 0; void loop() { // Subtract the last reading total = total - readings[readIndex]; // Read from the sensor readings[readIndex] = analogRead(potPin); // Add the reading to the total total = total + readings[readIndex]; // Advance to the next position in the array readIndex = (readIndex + 1) % numReadings; // Calculate the average int average = total / numReadings; Serial.print("Smoothed value: "); Serial.println(average); delay(100); } ``` ### 3. Threshold Detection Add threshold-based actions: ```cpp const int LOW_THRESHOLD = 200; const int HIGH_THRESHOLD = 800; void loop() { int potValue = analogRead(potPin); if (potValue < LOW_THRESHOLD) { Serial.println("LOW RANGE"); } else if (potValue > HIGH_THRESHOLD) { Serial.println("HIGH RANGE"); } else { Serial.println("MID RANGE"); } delay(500); } ``` ### 4. Calibration Mode Add calibration functionality: ```cpp int minValue = 1023; int maxValue = 0; void loop() { int potValue = analogRead(potPin); // Update min and max values if (potValue < minValue) minValue = potValue; if (potValue > maxValue) maxValue = potValue; // Map using calibrated range int calibratedValue = map(potValue, minValue, maxValue, 0, 100); Serial.print("Raw: "); Serial.print(potValue); Serial.print(" | Calibrated: "); Serial.print(calibratedValue); Serial.print("% | Range: "); Serial.print(minValue); Serial.print("-"); Serial.println(maxValue); delay(200); } ``` ## Questions for Understanding 1. What is the difference between analog and digital signals? 2. How does the analogRead() function convert voltage to digital values? 3. What is the resolution of the Arduino's ADC and what does it mean? 4. How does the map() function work and when would you use it? 5. What is the relationship between potentiometer rotation and output voltage? 6. How would you modify the code to control an LED brightness with the potentiometer? 7. What are the advantages and disadvantages of using analog vs digital inputs? ## Next Steps - Try connecting the potentiometer to control LED brightness using PWM - Experiment with different mapping ranges for various applications - Learn about analog input filtering and noise reduction - Explore other TinkerBlock analog input modules (sensors, joysticks) - Try creating analog control systems for motors or servos - Implement calibration and auto-ranging features ## 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 voltage divider in the module prevents component damage - Keep the Lonely Binary UNO R3 and shield in a stable position during operation - Do not apply voltages higher than 5V to analog inputs --- **Lab Completion Checklist:** - [ ] TinkerBlock shield properly seated on Lonely Binary UNO R3 - [ ] TK07 Disc Potentiometer module connected to shield (A0) - [ ] Code uploaded successfully to Lonely Binary UNO R3 - [ ] Potentiometer rotation produces changing values - [ ] Raw values range from 0-1023 - [ ] Mapped values range from 0-100% - [ ] Voltage calculation shows 0.00-5.00V - [ ] Resistance calculation shows 0-1000Ω (wiper to 5V end) - [ ] Serial Monitor displays all values correctly - [ ] Values change smoothly with rotation - [ ] Troubleshooting completed (if needed) - [ ] Experiment variations tried - [ ] Questions answered