Mechatronics I: Exam 2 Study Guide

Prepare for your second exam with these practice problems and solutions.

← Back to Course Hub

Section 1: Conceptual Questions

1. In digital systems, information is typically represented using discrete states, commonly referred to as HIGH and LOW. True or False?

Solution

The correct answer is True. Digital systems operate on binary logic, representing information with distinct high and low voltage levels.

2. A ________ is a complete computer system on a single integrated circuit, containing a processor, memory, and programmable input/output peripherals, making it ideal for embedded applications.

Solution

The correct answer is microcontroller. Microcontrollers are designed for specific control tasks in embedded systems.

3. In an Arduino sketch, the `loop()` function is primarily used for which purpose?

  • a) To set up initial pin modes and serial communication.
  • b) To execute code continuously after initialization.
  • c) To declare global variables.
  • d) To define custom functions.
Solution

The correct answer is b) To execute code continuously after initialization. The `loop()` function runs repeatedly, making it suitable for continuous sensing, processing, and actuating tasks.

4. What is the process of converting a continuous analog signal into a discrete digital representation?

Solution

The correct answer is Analog-to-Digital Conversion (ADC).

5. The resolution of an Analog-to-Digital Converter (ADC) refers to:

  • a) The speed at which it converts signals.
  • b) The number of discrete digital values it can produce.
  • c) The maximum input voltage it can handle.
  • d) Its ability to filter out noise.
Solution

The correct answer is b) The number of discrete digital values it can produce. Higher resolution means more distinct digital levels to represent the analog input.

6. According to the Nyquist-Shannon sampling theorem, what is the minimum sampling rate required to accurately reconstruct a signal with a maximum frequency component of $F_{max}$?

Solution

The correct answer is at least $2 \times F_{max}$ (twice the maximum frequency). This minimum rate is known as the Nyquist rate.

7. Pulse Width Modulation (PWM) is a technique used to achieve an average analog voltage output from a digital pin by varying the:

  • a) Frequency of the digital signal.
  • b) Amplitude of the digital signal.
  • c) Duty cycle of the digital signal.
  • d) Phase of the digital signal.
Solution

The correct answer is c) Duty cycle of the digital signal. By changing the proportion of time the signal is HIGH versus LOW, the average voltage can be controlled.

8. Which of the following Arduino functions is used to set a digital pin as either an INPUT or an OUTPUT?

  • a) `digitalWrite()`
  • b) `analogRead()`
  • c) `pinMode()`
  • d) `Serial.begin()`
Solution

The correct answer is c) `pinMode()`. This function configures the direction of a specified pin.

9. True or False: The `if` statement in Arduino programming allows a block of code to execute only if a specified condition is true.

Solution

The correct answer is True. `if` statements are fundamental for implementing conditional logic.

10. Which type of loop is best suited for iterating a known number of times?

  • a) `while` loop
  • b) `do-while` loop
  • c) `for` loop
  • d) `if-else` statement
Solution

The correct answer is c) `for` loop. `for` loops are designed for definite iteration where the number of repetitions is predetermined.

11. What is the binary representation of the decimal number 13?

Solution

The correct answer is 11012. (8 + 4 + 0 + 1 = 13)

12. What is the purpose of the `Serial.begin()` function in Arduino programming?

Solution

The `Serial.begin()` function initializes serial communication between the Arduino board and a computer (or other serial device) at a specified baud rate. This allows the Arduino to send and receive data via the USB port for debugging or communication purposes.

13. Explain the concept of "mapping" or "scaling" in Arduino, particularly when dealing with analog sensor readings.

Solution

Mapping (or scaling) in Arduino refers to converting a value from one range of numbers to another range. For analog sensor readings, this is often used to transform the raw ADC values (e.g., 0-1023) into a more meaningful physical unit or a different output range (e.g., 0-255 for LED brightness, or actual temperature in Celsius). The `map()` function in Arduino is commonly used for this purpose.

14. What is the main difference between a microcontroller and a general-purpose computer (like a desktop PC)?

Solution

A microcontroller is designed for specific control tasks in embedded systems, typically with limited resources (CPU, memory) and optimized for real-time interaction with hardware. A general-purpose computer is designed for a wide range of tasks, with more powerful processors, larger memory, and extensive operating systems, focusing on flexibility and user interaction rather than direct hardware control.

15. True or False: A Digital-to-Analog Converter (DAC) converts a continuous analog signal into a discrete digital signal.

Solution

The correct answer is False. A DAC performs the opposite function: it converts a discrete digital signal into a continuous analog signal.

16. What is the significance of the "baud rate" in serial communication?

Solution

The baud rate defines the speed at which data is transmitted serially, measured in bits per second (bps). It is crucial that both the transmitting and receiving devices are configured to the same baud rate for successful and intelligible communication.

17. Explain why "floating" digital input pins on a microcontroller can lead to unpredictable behavior.

Solution

A "floating" digital input pin is one that is not connected to a defined voltage (HIGH or LOW). Such a pin is highly susceptible to electromagnetic interference (EMI) or static electricity, causing it to randomly switch between HIGH and LOW states. This unpredictable behavior can lead to erroneous readings and unintended actions in a microcontroller program. Pull-up or pull-down resistors are used to prevent floating states.

18. What is the purpose of the `delay()` function in Arduino programming?

Solution

The `delay()` function pauses the program for a specified number of milliseconds. It is used to introduce timed delays in the execution flow, which can be useful for blinking LEDs, waiting for sensor stabilization, or timing sequences of actions. However, it blocks other code from running during the delay.

19. What is the difference between `digitalWrite()` and `analogWrite()` in Arduino?

Solution

`digitalWrite()` is used to set a digital pin to either a HIGH (5V/3.3V) or LOW (0V) state. It's for binary outputs.
`analogWrite()` (for PWM pins) is used to generate a Pulse Width Modulation signal, effectively producing an "analog-like" output by varying the duty cycle of a square wave. It takes a value from 0-255 to control the average voltage.

20. Explain the concept of "overflow" in digital systems when dealing with numerical data.

Solution

Overflow occurs when a calculation or operation produces a result that is too large to be represented by the available number of bits or data type. For example, if an 8-bit unsigned integer can hold values from 0 to 255, adding 1 to 255 would cause an overflow, typically resulting in the value wrapping around to 0.

21. What is the hexadecimal representation of the binary number 111100002?

Solution

Group the binary number into 4-bit nibbles: 1111 0000.
11112 = F16
00002 = 016
So, the correct answer is F016.

22. How does the `const` keyword affect a variable in Arduino programming?

Solution

The `const` keyword declares a variable as "constant," meaning its value cannot be changed after it has been initialized. This is useful for defining pin numbers or fixed parameters that should not be accidentally modified during program execution, improving code robustness and readability.

23. What is the purpose of a `switch-case` statement in programming? When might it be preferred over a series of `if-else if` statements?

Solution

A `switch-case` statement is used to control the flow of execution based on the value of a single variable or expression. It allows for multiple execution paths. It is often preferred over a long series of `if-else if` statements when checking a single variable against many distinct constant values, as it can make the code more readable, organized, and sometimes more efficient.

24. Describe the concept of a "logic gate" in digital electronics. Provide an example of a basic logic gate and its function.

Solution

A logic gate is an elementary building block of digital circuits that performs a basic logical function (AND, OR, NOT, etc.) on one or more binary inputs and produces a single binary output.
Example: An AND gate produces a HIGH output only if all its inputs are HIGH; otherwise, its output is LOW.

25. What is the function of the `millis()` function in Arduino, and how does it differ from `delay()`?

Solution

The `millis()` function returns the number of milliseconds that have passed since the Arduino board began running the current program. It is non-blocking, meaning it does not pause the program execution. This differs from `delay()` which halts all program execution for a specified duration, making `millis()` more suitable for multitasking and responsive applications.

26. Explain the concept of "pull-up" and "pull-down" resistors for digital inputs.

Solution

A pull-up resistor connects an input pin to the positive voltage supply (e.g., 5V), ensuring the pin reads HIGH when left unconnected or when a switch is open.
A pull-down resistor connects an input pin to ground (0V), ensuring the pin reads LOW when left unconnected or when a switch is open.
Both are used to prevent input pins from "floating" and picking up random electrical noise, ensuring a defined logic state.

27. What is "aliasing" in the context of analog-to-digital conversion, and what causes it?

Solution

Aliasing is a distortion that occurs when an analog signal is sampled at a rate lower than twice its highest frequency component (the Nyquist rate). This causes higher frequencies in the original signal to be misrepresented as lower, incorrect frequencies in the digitized signal, making it impossible to accurately reconstruct the original waveform.

28. Describe the difference between a `while` loop and a `do-while` loop in programming.

Solution

A `while` loop checks its condition *before* executing the loop body. If the condition is false initially, the loop body will never execute.
A `do-while` loop executes its loop body *at least once* before checking the condition. The condition is checked after each iteration, and the loop continues as long as the condition is true.

29. What is the maximum decimal value that can be represented by an 8-bit unsigned integer?

Solution

For an N-bit unsigned integer, the maximum value is $2^N - 1$.
For 8 bits: $2^8 - 1 = 256 - 1 = 255$.
The correct answer is 255.

30. Explain the concept of "digital noise" in a microcontroller input and how it can be mitigated.

Solution

Digital noise refers to unwanted, random fluctuations in voltage that can cause a digital input pin to rapidly switch between HIGH and LOW states, even when the intended signal is stable. This can lead to erroneous readings (e.g., multiple button presses from a single physical press). It can be mitigated using hardware solutions (e.g., pull-up/pull-down resistors, capacitors for debouncing) or software solutions (e.g., debouncing algorithms in code that ignore rapid changes within a short time window).

31. What is the purpose of a "function" in programming?

Solution

A function (or subroutine/method) is a block of organized, reusable code that performs a specific task. Its purpose is to break down complex problems into smaller, manageable parts, promote code reusability, improve readability, and simplify debugging.

32. Describe the difference between `int` and `float` data types in Arduino programming.

Solution

An `int` (integer) data type stores whole numbers (e.g., 1, 100, -5). It uses 2 bytes of memory on most Arduinos and has a limited range.
A `float` (floating-point) data type stores numbers with decimal points (e.g., 3.14, -0.5, 10.0). It uses 4 bytes of memory and can represent a much wider range of values, but calculations with floats can be slower and less precise than with integers.

33. When using `analogRead()`, what is the typical range of digital values returned by a 10-bit ADC on an Arduino, assuming a default reference voltage?

Solution

A 10-bit ADC produces $2^{10} = 1024$ discrete values. These values typically range from 0 to 1023.

34. What is the primary benefit of using a `const` variable for pin assignments in Arduino, rather than just using the raw number directly in `pinMode()` and `digitalWrite()`?

Solution

The primary benefit is improved code readability and easier modification. By using a meaningful name (e.g., `const int ledPin = 13;`), the code becomes self-documenting. If the pin assignment needs to change, you only need to modify the `const` declaration once, rather than searching and replacing every instance of the raw number throughout the code, reducing errors.

35. Describe how a simple LED is typically connected to an Arduino digital output pin, including any necessary external components.

Solution

A simple LED is typically connected with its anode (longer leg) to the Arduino digital output pin and its cathode (shorter leg) to ground, but with a current-limiting resistor placed in series with the LED. The resistor is crucial to limit the current flowing through the LED, preventing it from burning out due to excessive current from the Arduino pin.

36. What is the purpose of the `if-else if-else` structure?

Solution

The `if-else if-else` structure allows a program to execute different blocks of code based on a series of mutually exclusive conditions. The program evaluates conditions sequentially; the first condition that evaluates to true has its corresponding code block executed, and then the rest of the structure is skipped. If none of the `if` or `else if` conditions are true, the `else` block (if present) is executed.

37. Explain the concept of "sampling rate" in Analog-to-Digital Conversion.

Solution

The sampling rate (or sampling frequency) in ADC refers to how many times per second an analog signal is measured and converted into a digital value. It is measured in Hertz (Hz) or samples per second. A higher sampling rate captures more data points from the analog signal, allowing for a more accurate digital representation, especially for rapidly changing signals.

38. What is the role of the `setup()` function in an Arduino sketch?

Solution

The `setup()` function in an Arduino sketch is executed only once when the Arduino board starts up or is reset. Its primary role is to initialize variables, set pin modes (INPUT/OUTPUT), start serial communication, and perform any other one-time configurations required for the program to run.

39. How does a `while` loop differ from a `for` loop in terms of when their conditions are checked?

Solution

A `for` loop typically has its initialization, condition check, and iteration step defined at the beginning of the loop statement, making it ideal for iterating a fixed number of times or through a known sequence. A `while` loop only has a condition check at the beginning; its initialization and iteration steps must be handled explicitly within the loop body. `while` loops are better suited for indefinite iteration, where the number of repetitions is not known beforehand and depends on a condition becoming false.

40. What is the purpose of comments in Arduino (or any) programming code?

Solution

Comments are non-executable lines of text within code that are ignored by the compiler. Their purpose is to explain the code, its logic, and its functionality to human readers (programmers). Good comments improve code readability, maintainability, and collaboration, making it easier for others (and your future self) to understand and modify the program.

Section 2: Mathematical Problems

41. A 10-bit ADC has a reference voltage of 5.0 V. What is the voltage resolution (step size) of this ADC?

Solution

Number of Steps = $2^{10} = 1024$

Resolution = Vref / Number of Steps
Resolution = 5.0 V / 1024
Resolution ≈ 0.0048828 V/step or 4.88 mV/step

Therefore, the voltage resolution is approximately 0.00488 V/step.

42. An analog temperature sensor outputs a voltage that ranges from 0.5 V (for 0°C) to 3.0 V (for 100°C). If an Arduino (with a 10-bit ADC and 5V reference) reads a digital value of 512 from this sensor, what is the temperature in °C?

Solution

First, find the analog voltage corresponding to the digital reading:

ADC Resolution = 5.0 V / 1024 ≈ 0.0048828 V/step
Analog Voltage = Digital Reading * ADC Resolution
Analog Voltage = 512 * 0.0048828 V ≈ 2.5 V

Next, use the sensor's transfer function to find the temperature. The sensor range is 3.0 V - 0.5 V = 2.5 V for 100°C.
Sensor sensitivity = 2.5 V / 100°C = 0.025 V/°C.
The voltage measured is 2.5 V, which is 2.5 V - 0.5 V = 2.0 V above the 0°C reading.

Temperature = (Analog Voltage - Vmin_sensor) / Sensor Sensitivity
Temperature = (2.5 V - 0.5 V) / 0.025 V/°C
Temperature = 2.0 V / 0.025 V/°C = 80 °C

Therefore, the temperature is 80 °C.

43. A Pulse Width Modulation (PWM) signal is generated from a 3.3 V supply. If the duty cycle of the PWM signal is 60%, what is the approximate average DC equivalent voltage output?

Solution
Average Voltage = Supply Voltage * Duty Cycle
Average Voltage = 3.3 V * 0.60
Average Voltage = 1.98 V

Therefore, the approximate average DC equivalent voltage output is 1.98 V.

44. An analog pressure sensor produces a signal with a maximum frequency component of 30 Hz. What is the theoretical minimum sampling rate (according to the Nyquist-Shannon sampling theorem) required to accurately capture this signal?

Solution
Minimum Sampling Rate = 2 * Fmax
Minimum Sampling Rate = 2 * 30 Hz = 60 Hz

Therefore, the theoretical minimum sampling rate is 60 Hz.

45. How many unique, discrete values can be represented by a 12-bit unsigned digital signal?

Solution
Number of Unique Values = 2N
Number of Unique Values = 212 = 4096

Therefore, 4096 unique, discrete values can be represented.

46. A digital signal needs to represent temperatures from 0°C to 100°C with a resolution of 0.5°C. What is the minimum number of bits required for this digital representation?

Solution

Total range = 100°C - 0°C = 100°C
Number of steps = Total range / Resolution = 100°C / 0.5°C/step = 200 steps
To find the minimum number of bits (N), we need $2^N \ge \text{Number of steps}$.

2N ≥ 200

We know:
$2^7 = 128$
$2^8 = 256$
So, N must be at least 8.
The correct answer is 8 bits.

47. An Arduino program uses the `map()` function to convert an analog sensor reading (0-1023) to a motor speed (0-255). If the analog reading is 767, what will be the mapped motor speed?

Solution

The `map()` function in Arduino performs linear scaling.

mappedValue = map(value, fromLow, fromHigh, toLow, toHigh)
motorSpeed = map(767, 0, 1023, 0, 255)

The ratio of the input range covered is: $(767 - 0) / (1023 - 0) = 767 / 1023 \approx 0.74975$
The corresponding value in the output range: $0.74975 * (255 - 0) = 0.74975 * 255 \approx 191.18$
Since motor speed is usually an integer, it would be 191.

48. A digital output pin on an Arduino is used to control an LED. If the LED is connected to a PWM pin and `analogWrite(pin, 127)` is called, and the Arduino operates on a 5V supply, what is the approximate average voltage across the LED (assuming an ideal LED with no forward voltage drop for simplicity in this calculation)?

Solution

`analogWrite()` takes a value from 0 to 255.
Duty Cycle = `analogWrite` value / 255 = 127 / 255 $\approx$ 0.498
Average Voltage = Supply Voltage * Duty Cycle

Average Voltage = 5 V * (127 / 255)
Average Voltage ≈ 5 V * 0.4980
Average Voltage ≈ 2.49 V

Therefore, the approximate average voltage across the LED is 2.49 V.

49. A sensor provides a digital output in binary. If the sensor outputs 10102, what is its decimal equivalent?

Solution
10102 = (1 * 23) + (0 * 22) + (1 * 21) + (0 * 20)
= (1 * 8) + (0 * 4) + (1 * 2) + (0 * 1)
= 8 + 0 + 2 + 0
= 10

Therefore, the decimal equivalent is 10.

50. If an Arduino sketch executes a `delay(1000)` function, how many seconds will the program pause?

Solution

`delay()` takes its argument in milliseconds.

1000 milliseconds = 1 second

Therefore, the program will pause for 1 second.

51. A 6-bit DAC (Digital-to-Analog Converter) has a maximum output voltage of 10 V. What is its voltage resolution (step size)?

Solution

Number of steps = $2^6 = 64$

Resolution = Maximum Output Voltage / Number of Steps
Resolution = 10 V / 64
Resolution ≈ 0.15625 V/step

Therefore, the voltage resolution is approximately 0.15625 V/step.

52. If a temperature sensor has a range of -20°C to 80°C and a resolution of 1°C, how many discrete temperature levels can it distinguish?

Solution

Total temperature range = 80°C - (-20°C) = 100°C
Number of discrete levels = Total range / Resolution + 1 (to include both endpoints)

Number of levels = 100°C / 1°C + 1 = 100 + 1 = 101 levels

Therefore, it can distinguish 101 discrete temperature levels.