Microcontrollers and Open Hardware
In a vegetable garden in Oaxaca, a moisture sensor signals when to water. It runs for months on batteries, costs just a few pesos, and fits in the palm of your hand. Inside is a microcontroller board. What does it contain, how is it programmed, and what does it need around it so it doesn't burn out? By the end of the page we'll work it out.
The Microcontroller and Open Hardware
A personal computer does many things at once and uses tens of watts. A microcontroller does a single task, is much smaller and slower, but uses thousandths of that energy.
Every microcontroller system has sensors (inputs: they measure the environment, such as a temperature probe, a microphone, or a distance sensor) and actuators (outputs: they do something, such as a motor, an LED strip, or a solenoid valve).
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.
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.
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.
The Board's Pins
An Arduino Uno–type board gathers its pins into groups, each with its own function.
| Group | What it is for |
|---|---|
| Digital pins (0 to 13) | They read or write only two states: $0\ \text{V}$ or $5\ \text{V}$. They are configured as input or as output. |
| Digital pins with ~ (3, 5, 6, 9, 10, 11) | In addition to the above, they can output a PWM signal. |
| Analog pins (A0 to A5) | They read an intermediate voltage and convert it into a number. |
| Power pins | $5\ \text{V}$ and $3.3\ \text{V}$ supply voltage to sensors and components; GND is the ground (the $0\ \text{V}$ reference) that every circuit shares. |
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.
(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.
Digital Inputs, Analog Inputs, and PWM
Think of a pushbutton: it is either pressed or released (digital). A potentiometer, on the other hand, gives any value between its extremes (analog). With 10 bits there are $2^{10} = 1{,}024$ possible values, from $0$ to $1023$.
A digital output can directly control only on/off things: an LED, a buzzer, or a relay. If you want an intermediate brightness or speed, a simple digital pin is not enough: that is where PWM comes in.
The inputs on the board can be digital (reading only two states: $0\ \text{V}$ or $5\ \text{V}$) or analogue (reading an intermediate value). Classify these input…
The inputs on the board can be digital (reading only two states: $0\ \text{V}$ or $5\ \text{V}$) or analogue (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.
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.
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.
The board's analogue 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 analogue 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.
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 mean voltage value "seen" by a slow load such as an LED or motor, $V_\text{media} = 5 \times 0.25$. (c) Explain what mean 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.
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.
The Program: setup and loop
This structure fits a control machine: a control system watches the environment without rest (read, decide, act), so it needs a cycle that never ends.
Period and frequency of a blink
A blinking LED repeats a cycle: on, off, on… The period $T$ is the time of one complete cycle, and the frequency $f$ is how many cycles there are in each second. They are measured in hertz (Hz) and are inverses of each other: $f = \frac{1}{T}$.
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.
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.
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 flashing period and its frequency in hertz. (c) Explain what you would change in the program to make it flash twice as fast.
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).
Interface Components: Protecting and Adapting
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}$.
A student sets up a push button by connecting one terminal to the $+5\ \text{V}$ pin of the board and the other directly to the digital input D2, without adding any…
A student sets up a push button by connecting one terminal to the $+5\ \text{V}$ pin of the board and the other directly to the digital input D2, without adding any further components. When monitoring the input, they observe that when pressing the button they read a stable logical '1', but upon releasing it, the pin oscillates chaotically between '0' and '1' in response to any nearby movement or vibration.
(a) Interpret this failure based on the concept of high input impedance in digital logic gates and explain what it physically means for the input to remain in a "floating" state.
(b) Draw or describe in detail the circuit of a $10\ \text{k}\Omega$ pull-down resistor. Justify the voltage value at pin D2 both with the push button at rest and pressed, and calculate the current consumed by the resistor in each state.
(c) The student asks whether they could use a $10\ \Omega$ resistor instead of $10\ \text{k}\Omega$, arguing that it will "divert noise to ground more strongly". Technically refute their proposal by calculating the power that such a resistor would dissipate and the risks to the power supply. Furthermore, explain how the setup could be solved without external components by activating the INPUT_PULLUP option of the microcontroller.
Controlling Power Actuators: Relays and Transistors
Microcontroller board or PLC
In a factory, the equivalent job is done by a programmable logic controller (PLC): it also reads inputs, runs a program in a scan cycle, and controls outputs, but it is built for the industrial environment.
| Criterion | Microcontroller board | PLC |
|---|---|---|
| Cost and size | Very cheap (starting at a few hundred pesos) and tiny | Much more expensive (thousands of pesos) and larger |
| Robustness | Fragile: designed for models, classrooms, and prototypes | Withstands dust, vibration, electrical noise, and extreme temperatures |
| Inputs and outputs | 5 V pins with only a few mA; needs relays or transistors | Industrial inputs and outputs (24 V, relays), ready to connect |
| Expansion and programming | Expanded with modules and programmed in text | Expanded with standard modules and programmed with standardized languages |
Rule of thumb: for a prototype, a school project, or a low-cost product, a board; for a conveyor belt or a bottling plant that cannot fail, a PLC.
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.
- 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.
- b) Write that program in pseudocode so that the LED turns on when the LDR reading exceeds a threshold.
- c) State which two essential external components are missing from the drawing for the circuit to really work.
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.
(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.
It is desired to regulate the rotational speed of a $12\ \text{V}$ direct current motor with a rated current of $800\ \text{mA}$ using a microcontroller board whose…
It is desired to regulate the rotational speed of a $12\ \text{V}$ direct current motor with a rated current of $800\ \text{mA}$ using a microcontroller board whose digital outputs provide a maximum of $5\ \text{V}$ and $20\ \text{mA}$.
(a) A student proposes powering the motor by operating a BJT transistor in its linear/active region controlled by an analog voltage, behaving as a variable series resistor. If the motor operates at half power receiving $6\ \text{V}$ and drawing $500\ \text{mA}$, calculate the power that the transistor would have to dissipate as heat ($P = V_{CE} \times I_C$). Compare this value with the dissipation of the transistor operating in saturated switching using PWM ($V_{CE(\text{sat})} \approx 0.25\ \text{V}$) with a duty cycle of $50\ \%$ and justify the energy superiority of PWM.
(b) Explain how a DC motor can rotate at a smooth, constant speed when powered by square wave pulses at $500\ \text{Hz}$ instead of a pure DC voltage.
(c) When switching an inductive load such as a motor with PWM, it is essential to add an antiparallel diode (flyback). Predict what will happen at the instant the transistor abruptly transitions from saturation to cutoff if this diode is omitted, justifying your answer based on the law of self-induction ($V_L = -L \frac{di}{dt}$).
An engineering firm must select the automation platform for two facilities: • Facility A: Industrial hydraulic press in a metalworking workshop, with $15\ \text{kW}$…
An engineering firm must select the automation platform for two facilities:
• Facility A: Industrial hydraulic press in a metalworking workshop, with $15\ \text{kW}$ three-phase motors, $24\ \text{V}$ solenoid valves, continuous mechanical vibrations, and high electromagnetic noise.
• Facility B: Distributed network of $80$ sensor nodes to monitor in real time the microclimate inside a group of greenhouses, battery-powered and connected via MQTT wireless protocol.
(a) Select and reasonably justify whether you would implement a programmable logic controller (PLC) or a microcontroller board in each of the two facilities.
(b) Analyze the requirements for electrical conditioning and noise immunity: compare how each platform manages inputs/outputs (galvanic isolation, industrial standard voltage levels versus $3.3\ \text{V}$ or $5\ \text{V}$ TTL/CMOS levels).
(c) A manager suggests replacing the PLCs in the metalworking plant with low-cost commercial microcontroller boards to reduce the initial investment by $85\ \%$. Write a technical report refuting their position, considering machinery safety regulations (CE marking, electromagnetic compatibility directive), maintainability by plant technicians, and standard languages (IEC 61131-3 versus C/C++).
Calibration, Hysteresis, and Battery Life
A temperature sensor is connected to an analogue input: the reading $0$ corresponds to $0\ ^\circ\text{C}$ and the reading $1023$, to $100\ ^\circ\text{C}$. (a)…
A temperature sensor is connected to an analogue 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.
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.
A remote agrometeorological station is powered by a $3.7\ \text{V}$ Li-ion cell with a capacity of $2400\ \text{mA}\times\text{h}$. The station takes sensor readings and…
A remote agrometeorological station is powered by a $3.7\ \text{V}$ Li-ion cell with a capacity of $2400\ \text{mA}\times\text{h}$. The station takes sensor readings and transmits them by radio: during this active phase, the circuit consumes $75\ \text{mA}$ for $4\ \text{s}$. Between transmissions, the board enters deep sleep mode (deep sleep), where the combined consumption of the board and regulator drops to $50\ \mu\text{A}$ ($0.05\ \text{mA}$). The station must operate uninterruptedly for a minimum of $180\ \text{days}$ (about $4320\ \text{h}$) without recharging the battery.
(a) Derive the expression for the average current consumption $I_{\text{med}}$ as a function of the period between measurements $T$ (in seconds). Calculate the minimum allowable sampling interval to meet the required battery life.
(b) If due to a programming error the radio module does not shut down during sleep, raising the idle consumption to $3.0\ \text{mA}$, predict the new actual battery life keeping the interval calculated in the previous section.
(c) To power the microcontroller at $3.3\ \text{V}$ from the $3.7\ \text{V}$ battery, one technician suggests a linear LDO regulator and another a step-down switching converter (buck). Justify which one is more recommendable for this application, paying special attention to the quiescent current (quiescent current) of each device.
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.
Back to the beginning: the garden sensor has a microcontroller that reads the moisture through an analog input, decides in the loop whether watering is needed, and, through a transistor or relay, turns on the pump; at rest it sleeps so the batteries last for months.