Saturday, January 31, 2015

Jan 30, 2015: Electronics

This blog will mainly include important/helpful concepts in each exercise (focusing more on new material in PHYS310 rather than reviewing concepts we've learned before unless they're very important), notes and data from experiments/exercises, and answers to questions.


Breadboards
-Horizontal strips (2 at top, 2 at bottom) are called "busses" (holes in a horizontal strip connected).
-In the middle, vertical strips are connected.



Light Emitting Diodes (LEDs) 
General:
-can be used as a visual indicator to see if current is flowing in the circuit
-"visual ammeter"
-Every electron flowing through an LED produces one photon of light.
-*Note: anode pin longer than cathode pin; current flows from anode to cathode (next to the cathode pin, LED case is flattened)

LED circuit we created (1K ohm resistor used)

Discussion/Answers to Questions in Packet:
-In an LED circuit, if one forgets to include the resistor in the circuit, a very large amount of current flows through the LED because R is very small (close to zero ohms). 
-Varying R: If we double the current by cutting R in half, LED does not look twice as bright. This is because our eyes are logarithmic (vs. linear). Since the curve flattens, we are able to see faint light but also are able to see relatively bright light without damaging our eyes, although we still shouldn't intentionally stare at bright lights.
-We use 10% resistors, and the gap between the first two values is only 2 while the gap between the last two is 14 because these resistors are only accurate to 10%. Note that 10% of smaller numbers is smaller than 10% of larger numbers.

Pulse Width Modulation (PWM) of LEDs:
-Adjusting the fraction of time the LED is on, one can control the perceived brightness of the LED.
-duty cycle = percentage of time that the LED is on
-perceived brightness proportional to the duty cycle



Arduino Programming
-can implement the PWM value using "analogWrite()"
-For the LED, pwmValue = 0 corresponds to 0% duty cycle (or fully off) and pwmValue = 255 corresponds to 100% duty cycle (fully on). (Counting from 0, this is 256 levels or 8-bits of control).
-Using the digital oscilloscope to monitor the voltage across the resistor, we saw what we expected: 

(2 ms period with about 1.2 ms on "on" mode and 0.8 ms on "off" mode)


-Using the loop() function, we were able to make the Arduino brighten and dim the LED in a periodic way. To briefly describe the code we used, we set the initial values of the LED brightness and set up the circuit to let Arduino know the pin that the LED is attached to and to declare the LED pin as the output. Then, using the loop() block, we told the pwmValue (brightness) to increase by 1 and wait 30 ms before proceeding (meaning going back to the first line of the loop). But also inside the loop was an "if" condition for pwmValue to go back to 0 (fully off) if pwmValue was greater than 255 (after increasing by one in the loop). 



Voltage Divider
Vout = (R2 / (R1+R2)) Vin

but we can also write:
Vout = (1/((R1/R2)+1)) Vin

Limiting Cases:
-If R2 << R1, then Vout << Vin 
This is because if R2 is very small compared to R1, the voltage drop across R1 is significant, making the voltage drop across R2 negligible (or much less than Vin or Vp).
-If R2 >> R1, then Vout is almost equal to Vin.
This is because if R1 is very small compared to R2, the voltage drop across R1 is negligible (compared to the Vin or Vp value), making Vout close to Vin. 

Application: Light Sensor
-For our light sensor, we want R1 to be similar to the resistance of the photocell because we want sensitivity in the change in Vout. We want a relatively large Vout value but not too large that we can't tell the difference between Vin and Vout. But we also want a strong enough signal, meaning Vout isn't too small compared to Vin. Mathematically, using the second equation above, we want R1 to be relatively close to R2. 
-Trend observed: More light incident on photocell corresponded to a decrease in resistance of photocell
-Resistance of photocell we used from 2 K (more light) - 13 K (less light) ohms (depending on amount of light)

Analog to Digital Conversion (ADC)
-Analog voltage between 0 to 5 V --> 10-bit binary number (0 to 1023 in base 10)
-If you feed 0 V into the ADC, you get a digital value of 0. If you feed it 5 V, you get 1023. If ADC gives a value of 305, that corresponds to 1.5 V in analog voltage. If you feed in 4.1 V, you would get 839 in ADC value (we rounded down because the value hasn't reached 840 or overcome the "threshold" to the next higher value). In order to solve for the missing parts, we used proportions because ideally, the curve ADC value vs. analog voltage is linear.

-The best precision (smallest voltage step) is the full voltage range over the number of steps, or in this case, 5/1024. Note that there are 1024 steps because we count from 0 to 1023. 
-We then used a code that uses an analog input pin (reads the analog input voltages using "analogRead") and stores and displays (using the serial monitor) the value read by ADC. At first, we didn't have wires connected to pin A3, which meant that the voltage at the pin was not well-defined. Thus, we saw 0 on the serial monitor. 
-When we connected the 5V pin to A3 (analog input pin), or connected to a well-defined voltage, we saw the value 1023 and values very close to 1023 on the serial monitor. When we connected to a different well-defined voltage, 3.3 V, we saw values of 664 and 665. Theoretically, using the proportions we used before, we expected the value 675, but in this experiment, we were able to see a nonideal behavior of the ADC. The curve, which we thought was linear, in fact curves. 



Putting it All Together: Shadow Detector
-Using the codes that were given to us, including the code that reads the analog input values then stores and displays the values read by the ADC along with basic format for "if-then-else" statements for Arduino, we created a shadow detector. Some important things we noted while writing this script was that we can only have one setup() and loop() functions in a script. 


-On the breadboard, we creates two separate circuits (one ADC and one voltage divider circuit with a photocell as one of the resistors and following it an LED). 
-By playing around with the ADC circuit, we noted that with full light, val (variable to store the value read by ADC) was around 200-400 while without light (by covering the photocell), val was around 700-800.