Assignment 3: I/O (Input/Output)!
Demo
Sensor calibration
Demo video showing initial sensor calibration on startup. When both LEDs are on, capture the minimum reading from sensor. When both LEDs are off, capture the maximum reading from sensor. Both LEDs flashing together indicates calibration completion.
Adjusting brightness
Demo showing the potentiometer controlling LED brightness in opposite directions. Turning the knob counterclockwise increases brightness for the top LED and dims the bottom LED. Turning the knob clockwise dims the top LED and increases power (brightness) for the top LED.
Serial communications
Screen capture of data being sent over a serial connection upon initial startup & regular operation.
Circuit drawing
The circuit is is powered via USB connected to the Arudino microcontroller. The blue LEDs are connected to pin 3 and 9 respectively. Each LED is connected in series with a 220 Ω resistor to reduce the current through the LED.
The circuit also contains a potentiometer. It is connected to pin A0 and a 330 Ω resister (equivalent of a voltmeter) to limit current through the system.
Calculating resistor values
LED resistor
Because each LED used in this assignment can operate safely with $\leq 30 \text{mA}$ of current, we need to use resisters to reduce the amount of current going through each LED.
Since we are connecting the resister & the LED in series, the amount of current through the LED is the same as that for the resister ($I_\text{L} = I_\text{r}$). This also means that the sum of voltage drop across the resister and LED should be equal to the voltage provided by the Arduino microcontroller (5V).
With these information, we calculate the minimum resistance by applying Ohm’s law on the resister. $$ \begin{align*} V_\text{r} &= I_\text{r} R_\text{r} && \text{Ohm’s law}\cr V_\text{PSU} - V_\text{L} &= I_\text{L} R_\text{r} &&\text{Substitute resister V & I}\cr 5\texttt{V} - V_\text{L} &= 20\texttt{mA} \cdot R_\text{r}\cr \end{align*} $$
A 220 Ω resister is used for the blue LED (voltage drop = 3.3V). It needs an resistor that is at least $$ \begin{align*} V&=IR\cr 5\;\texttt{V} - 3.3\;\texttt{V} &=20\;\texttt{mA} \cdot \textrm{R}\cr \frac{1.7\;\texttt{V}}{0.02\;\texttt{A}} &= \textrm{R}\cr \textrm{R} &= 85 \;\Omega \end{align*} $$ Using a resistor with higher resistance than what is required here (e.g. a 100 Ω resistor) will not have an adverse effect on the longevity of the LED, it will simply cause it to appear a bit dimmer.
Potentiometer resistor
Assuming the analog in port on Arudino is an ideal voltmeter (aka. infinite resistance), the resistor and the potentiometer are effectively connected in series. Because the maximum DC current through the GND pin for the microcontroller is 200 mA, we also need to calculate the minimum resistence for the previously mentioned resistor. Since the ground pin is shared between the LEDs and the potentiometer, we need to consider the worst case scenario where current through the circuit is maximized when the potentiometer’s resistance is approximately 0 Ω and both LEDs are on. We first calculate the current through the LEDs.
$$ \begin{align*} V &= IR &&\text{Ohm’s law}\cr I &= \frac{V}{R}\cr I &= \frac{5\texttt{V}-3.3\texttt{V}}{220\Omega} &&\text{Substitution}\cr I &\approx 7.8 mA &&\text{Round up for worst-case scenario} \end{align*} $$
Because each LED and the potentio meter are connected in parallel, we can then determine that
$$ \begin{align*} I_\text{max} &> I_\text{LED} \cdot 2 + I_\text{pot}\cr 200 \texttt{mA} &> 7.8 \texttt{mA} \cdot 2 + I_\text{pot}\cr I_\text{pot} &< 184.4 \texttt{mA} \end{align*} $$
In the worst case with potentiometer having 0 Ω, the minimum resistance for the other resistor is: $$ \begin{align*} V &= I R && \text{Ohm’s law}\cr 5 \texttt{V} &= 184.4 \texttt{mA} \cdot R &&\text{Substitue V & I}\cr R &= \frac{5\texttt{V}}{0.1844\texttt{A}}\cr R &\approx 28 \;\Omega \end{align*} $$
Considering that other on-board devices might be sharing the ground pin under the hood, I decided to be extra cautious and use a 330 Ω resistor.