Input Output
Goal
- sensor which changes the state of an output device
- output device, not a single LED
analogRead()
analogWrite()
map()
- if-statement
- write to Serial, not the same message as the example code
Schematic
The sensor is a photocell, with power provided by the 5V pin and analog reading through pin A5.
The output device is made up of 3 LEDs.
Each diode is wired to its own pin providing power through analog write.
The calculation below yielded the minimum resistances needed for each LED.
Calculation
Red and Green LEDs:
Blue LED:
Resistor in Voltage Divider with Photocell:
The calculation below yielded the target resistance for R2 in the voltage divider with the photocell as R1.
The photocell was measured to have a resistance of around 38 Ohms in the brightest environment and around 300k Ohms in the darkest environment for its intended operating range, so the needed resistance for R2 was calculated with around halfway brightness resulting in around half of voltage in as voltage out.
220 Ohm resistors were used for all 3 LED resistors.
- For red and green, it was the closest resistor value which exceeded the minimum of 160.
- For blue, the minimum was 100, but I would not use exactly 100, since fluctuations could cause the necessary resistance to actually be more than 100 at times. Thus 220 was the next smallest value available.
For the photocell, a resistor with the calculated resistance of 100k Ohms was used.
Circuit
Firmware
Notes
-
In this voltage divider, the variable resistor is currently R1 and R2 is constant, and the sensor value is inversely mapped to the output value in the firmware.
If the variable resistor is R2 and R1 is constant, then the sensor value must be directly mapped to the output value in the firmware to achieve the same behavior.
For example, if Vin=5, R2=100k:
- If R1=5k:
- Vout = R2/(R1+R2)*Vin = 100k/(5k+100k)*5 = 4.76V
- If R1=200k:
- Vout = 100k/(200k+100k)*5 = 1.67V
However, if the values for R1 and R2 are switched, ie if R1=100k:
- If R2=5k:
- Vout = 5k/(100k+5k)*5 = 0.24V
- If R2=200k:
- Vout = 200k/(100k+200k)*5 = 3.33V
This shows that if the variable and constant resistors are switched, then the mapping of input to output value must be inverted to achieve the same behavior.
-
-
map() for 10bit read to 8bit write is used as map(value, 0, 1023, 0, 255). 16bit is 65,536, which shifted 1 down to start at 0 instead of 1 is 65,535, so map() for 16bit read to 10bit write would be used as map(value, 0, 65535, 0, 1023).