Back to homeAP® Computer Science Principles · AP Computer Science Principles · Change subject
Start for free
Notes

Exercises: Microcontrollers and open-source hardware

Practice★☆☆
Explain what a microcontroller is and what three basic blocks it integrates into a single chip (processor, program and data memory, and input/output ports). Compare its…

Explain what a microcontroller is and what three basic blocks it integrates into a single chip (processor, program and data memory, and input/output ports). Compare its size and power with those of a personal computer.

Solve
Practice★☆☆
Arduino is the best-known example of open-source hardware. Explain what it means for its hardware to be open-source (the board's schematics and designs are public and…

Arduino is the best-known example of open-source hardware. Explain what it means for its hardware to be open-source (the board's schematics and designs are public and anyone can manufacture or modify them) and name two advantages this offers to someone learning electronics.

Solve
Practice★☆☆
The inputs on the board can be digital (reading only two states: $0\ \text{V}$ or $5\ \text{V}$) or analog (reading an intermediate value). Classify these input signals:…

The inputs on the board can be digital (reading only two states: $0\ \text{V}$ or $5\ \text{V}$) or analog (reading an intermediate value). Classify these input signals: (a) a push button, (b) a potentiometer turned halfway, (c) a light sensor giving a voltage proportional to brightness, and (d) a limit switch.

Solve
Practice★☆☆
The digital outputs on the board give only two values ($0\ \text{V}$ or $5\ \text{V}$). Explain what can be controlled directly with this type of signal (an LED, a…

The digital outputs on the board give only two values ($0\ \text{V}$ or $5\ \text{V}$). Explain what can be controlled directly with this type of signal (an LED, a relay, a buzzer) and what limitation exists if an intermediate brightness or speed is desired.

Solve
Practice★☆☆
Explain what a PWM (pulse-width modulation) signal is: an output that switches very quickly between $0\ \text{V}$ and $5\ \text{V}$ and, depending on the proportion of…

Explain what a PWM (pulse-width modulation) signal is: an output that switches very quickly between $0\ \text{V}$ and $5\ \text{V}$ and, depending on the proportion of high time, simulates an intermediate value. Give the example of adjusting the brightness of an LED.

Solve
Practice★☆☆
Every program for these boards has two parts: setup and loop. Explain what is written in each (in setup, what is prepared once, such as configuring the pins; in loop,…

Every program for these boards has two parts: setup and loop. Explain what is written in each (in setup, what is prepared once, such as configuring the pins; in loop, what repeats continuously while the board is powered on) and why this structure fits a control system.

Solve
Practice★☆☆
An LED is connected to an output pin of the board ($5\ \text{V}$). Explain what component must always be placed in series (a series resistor) and what two things its…

An LED is connected to an output pin of the board ($5\ \text{V}$). Explain what component must always be placed in series (a series resistor) and what two things its absence could damage: the LED itself or the pin of the board, which can only supply around $20\ \text{mA}$.

Solve
Practice★☆☆
Classify these components as a sensor (input) or actuator (output) in a microcontroller system: (a) a temperature probe, (b) a motor, (c) a microphone, (d) an LED strip,…

Classify these components as a sensor (input) or actuator (output) in a microcontroller system: (a) a temperature probe, (b) a motor, (c) a microphone, (d) an LED strip, (e) an ultrasonic distance sensor, and (f) an irrigation solenoid valve.

Solve
Practice★★☆
A program configures pin 8 as an output in setup and, in loop, sets the pin high, waits $1\ \text{s}$, sets it low, and waits $1\ \text{s}$. (a) Describe what an LED…

A program configures pin 8 as an output in setup and, in loop, sets the pin high, waits $1\ \text{s}$, sets it low, and waits $1\ \text{s}$. (a) Describe what an LED connected to that pin does. (b) Calculate the blinking period and its frequency in hertz. (c) Explain what you would change in the program to make it blink twice as fast.

Solve
Practice★★☆
The board's analog input converts a voltage between $0\ \text{V}$ and $5\ \text{V}$ into a number between $0$ and $1023$. A program turns on an alert if the reading…

The board's analog input converts a voltage between $0\ \text{V}$ and $5\ \text{V}$ into a number between $0$ and $1023$. A program turns on an alert if the reading exceeds $600$. (a) Calculate what voltage corresponds to the threshold $600$, with $V = \frac{600}{1023} \times 5$. (b) Explain in your own words what the program does and why the threshold is chosen with a margin relative to the typical resting value.

Solve
Practice★★☆
The figure shows the diagram of an Arduino-type microcontroller board with its pin headers, with one pin marked with a question mark, as shown in the figure. (a)…

The figure shows the diagram of an Arduino-type microcontroller board with its pin headers, with one pin marked with a question mark, as shown in the figure.

USB microcontroller digital pins 0 to 13 (~ = PWM) ? analog inputs A0 to A5 5 V GND power supply via USB or external jack (7 to 12 V)

(a) Identify the pin headers on the board: digital, analog, and power. (b) The pin marked with the question mark is in the digital row and has the ~ symbol: explain what it can do besides supplying $0\ \text{V}$ or $5\ \text{V}$. (c) State which pin you would connect each component to and why: a push button, a potentiometer, and a dimmable LED.

Solve
Practice★★☆
Write in pseudocode the complete program (setup and loop) so that an LED connected to pin 9 turns on while a button connected to pin 2 is held down, and turns off when…

Write in pseudocode the complete program (setup and loop) so that an LED connected to pin 9 turns on while a button connected to pin 2 is held down, and turns off when released. Explain the configuration of each pin in setup (input or output).

Solve
Practice★★☆
A PWM output of $5\ \text{V}$ operates with a duty cycle of $25\ \%$ (a quarter of the period high). (a) Draw two periods of the signal in your notebook. (b) Calculate…

A PWM output of $5\ \text{V}$ operates with a duty cycle of $25\ \%$ (a quarter of the period high). (a) Draw two periods of the signal in your notebook. (b) Calculate the average voltage value "seen" by a slow load such as an LED or motor, $V_\text{media} = 5 \times 0.25$. (c) Explain what average value a duty cycle of $75\ \%$ would give and how brightness or speed is adjusted this way.

You can do it on paper and send a photo with your answer.

Solve
Practice★★☆
The figure shows a light measurement system with a microcontroller board: an LDR is connected to an analog input (A0) and an LED is driven from a digital pin (D9). The…

The figure shows a light measurement system with a microcontroller board: an LDR is connected to an analog input (A0) and an LED is driven from a digital pin (D9). The inside of the board is marked with a question mark.

Block diagram: an LDR connected to input A0 of a microcontroller board whose interior is marked with a question mark, and an LED connected to output D9LDRlight sensorA0Microcontroller board?D9LED
  1. a) Explain what the central box marked with the question mark contains, that is, what the program running in the loop function does between reading A0 and driving output D9.
  1. b) Write that program in pseudocode so that the LED turns on when the LDR reading exceeds a threshold.
  1. c) State which two essential external components are missing from the drawing for the circuit to really work.
Solve
Practice★★☆
A temperature sensor is connected to an analog input: the reading $0$ corresponds to $0\ ^\circ\text{C}$ and the reading $1023$, to $100\ ^\circ\text{C}$. (a) Calculate…

A temperature sensor is connected to an analog input: the reading $0$ corresponds to $0\ ^\circ\text{C}$ and the reading $1023$, to $100\ ^\circ\text{C}$. (a) Calculate what temperature corresponds to a reading of $512$, assuming the proportional relationship $T = \frac{lectura}{1023} \times 100$. (b) Calculate what reading a temperature of $35\ ^\circ\text{C}$ would give. (c) Explain why this conversion is necessary before displaying the data to a user.

Solve
Practice★★☆
A program to control an LED on pin 7 does not work: in loop, the pin is set high and low, but the LED never turns on. Checking setup, no one configured the pin as an…

A program to control an LED on pin 7 does not work: in loop, the pin is set high and low, but the LED never turns on. Checking setup, no one configured the pin as an output. Explain why this oversight prevents it from working (by default, pins remain as inputs) and rewrite the correct setup in pseudocode.

Solve
Practice★★★
A technician programs the cooling control of a server using a microcontroller board. They design the basic logic: "if $T > 30.0\ ^\circ\text{C} \rightarrow$ turn on fan;…

A technician programs the cooling control of a server using a microcontroller board. They design the basic logic: "if $T > 30.0\ ^\circ\text{C} \rightarrow$ turn on fan; otherwise $\rightarrow$ turn off fan". When testing the prototype in a climate chamber around $30.0\ ^\circ\text{C}$, they observe that the fan relay switches frantically several times per second, producing a continuous buzzing sound and overheating the actuator.

(a) Interpret the physical and computational origin of this problem, considering the noise inherent in the analog sensor reading and the thermal inertia of the system.

(b) Redesign the algorithm by implementing hysteresis control with thresholds at $31.0\ ^\circ\text{C}$ (turn on) and $28.0\ ^\circ\text{C}$ (turn off). Write the logic in pseudocode and justify why it is essential to check the current state of the fan or use a state variable.

(c) The temperature probe delivers an analog voltage of $10\ \text{mV}/^\circ\text{C}$ ($0\ \text{V}$ at $0\ ^\circ\text{C}$) connected to the board's $10\ \text{-bit}$ ADC converter ($V_{\text{ref}} = 5.0\ \text{V}$). Calculate the exact integer value ($0$ to $1023$) that the analogRead() function will read for each of the two programmed thresholds.

Solve
Practice★★★
A team of students connects a photoresistor (LDR) to the analog input A0 of a microcontroller board ($10\ \text{bits}$, range from $0$ to $1023$) using a voltage…

A team of students connects a photoresistor (LDR) to the analog input A0 of a microcontroller board ($10\ \text{bits}$, range from $0$ to $1023$) using a voltage divider. When calibrating under real conditions, they record a digital reading of $120$ in darkness and $870$ under maximum sunlight.

(a) Deduce the linear interpolation mathematical function that transforms any raw reading $L$ into a percentage of illumination $P \in [0, 100]\ \%$. Calculate the percentage value if the ADC delivers a reading of $495$.

(b) A student implements the line in the code: int porcentaje = (lectura - 120) / 750 * 100;. Upon opening the serial monitor, they find that the variable is always $0$ for any intermediate light level and suddenly jumps to $100$ in direct sunlight. Diagnose the cause of the error in processor arithmetic and write the corrected instruction maintaining integer operations.

(c) If the biasing resistor of the divider is accidentally disconnected, input A0 is left open-circuit, picking up readings outside the calibrated range (for example, $L = 50$). Predict what problem this will cause in the percentage calculation and propose how to protect the code using clamping functions.

Solve
Practice★★★
The figure shows the diagram of an automatic irrigation system using a microcontroller board: a soil moisture probe connects to A0 and output D7 controls the pump via a…

The figure shows the diagram of an automatic irrigation system using a microcontroller board: a soil moisture probe connects to A0 and output D7 controls the pump via a component marked with a question mark, as shown in the figure.

moisture probe A0 microcontroller board D7 ? pump 230 V if the soil is dry, the board turns on watering; when the probe detects sufficient moisture, it stops

(a) Identify the component marked with the question mark (the element that allows an output of $5\ \text{V}$ and low $\text{mA}$ to control a $230\ \text{V}$ pump) and explain its function. (b) Describe the inputs and outputs of the system and the complete operating cycle. (c) Propose two reasonable improvements (not watering at night or when it rains, low tank level warning), stating which sensor or condition each would add to the program.

Solve
Practice★★★
A student programs the push-button traffic light of a school pedestrian crossing. They write sequential code where the vehicular green light stays on via the call…

A student programs the push-button traffic light of a school pedestrian crossing. They write sequential code where the vehicular green light stays on via the call delay(20000); then it reads the pedestrian push button with a digitalRead() and, if it is pressed, switches to amber ($3\ \text{s}$) and red ($10\ \text{s}$). When installed, pedestrians complain that they press the button but the traffic light seems unresponsive the vast majority of the time.

(a) Interpret the technical cause of the failure by diagnosing what happens in the microcontroller CPU during the execution of the delay() functions and why button presses are missed.

(b) Redesign the program architecture using a non-blocking finite state machine controlled by the timing function millis(). Write the pseudocode of the loop(), ensuring that the push button reading is serviced continuously without halting execution.

(c) Incorporate into the design two indispensable road safety constraints: 1) ensure that vehicles always have a minimum uninterrupted green time of $15\ \text{s}$ before any pedestrian button press can trigger the switch to amber; 2) justify how to prevent mechanical contact bounce (chattering) of the push button from causing erratic triggering of the cycle.

Solve
Practice★★★
Unit Challenge. Imagine that you have to design a low-cost, open-source home automation prototype to warn of extreme temperatures in your home. 1. Real data: Go to the…

Unit Challenge. Imagine that you have to design a low-cost, open-source home automation prototype to warn of extreme temperatures in your home.

  1. Real data: Go to the official National Weather Service website and check the maximum temperature in your city over the last three days. Calculate its arithmetic mean (for example, $32.4$ °C) to set it as the critical trigger threshold.

  2. Design in Tinkercad: Create an open-hardware circuit using an Arduino Uno-type board, a TMP36 temperature sensor, and two indicators (a green LED for normal status and a red LED with a buzzer for the alarm).

  3. Programming and testing: Program the microcontroller (using blocks or C++) to convert the sensor's analog signal into degrees Celsius and activate the alarm if it exceeds your calculated threshold. Simulate its response by varying the temperature.

  4. Technical report: Write a brief proposal (one page) with the circuit schematic, the implemented code, and a critical reflection on the advantages of open hardware compared to closed proprietary systems.

Assessment criteria:

  • Rigorous collection of National Weather Service data and its correct mathematical integration into the program.

  • Functional circuit assembly and code consistency in the simulator.

  • Technical and ethical arguments regarding the use and democratization of open hardware.

Solve