diff --git a/05-photoresistors/05-photoresistors.ino b/05-photoresistors/05-photoresistors.ino new file mode 100644 index 0000000..2f66604 --- /dev/null +++ b/05-photoresistors/05-photoresistors.ino @@ -0,0 +1,19 @@ +#define PIN_PHOTORESISTOR 1 +#define PIN_LED 14 +#define ADC_MAX (1 << 12) +#define ADC_VOLTAGE 3300 + + +void setup() +{ + Serial.begin(115200); + ledcAttach(PIN_LED, 1000, 12); +} + +void loop() +{ + int adcvalue = analogRead(PIN_PHOTORESISTOR); + int adcvoltage = (ADC_MAX / ADC_VOLTAGE) * adcvalue; + Serial.printf("ADC Value: %d Voltage (int): %d\n", adcvalue, adcvoltage); + ledcWrite(PIN_LED, adcvoltage); +} diff --git a/05-photoresistors/README.md b/05-photoresistors/README.md index 0f0f0c8..d2a2342 100644 --- a/05-photoresistors/README.md +++ b/05-photoresistors/README.md @@ -1,15 +1,23 @@ # Photoresistors + + This is another analog to digital converter project using a photoresistor like I used a potentiometer in the last project. # Lessons Learned - Resistors in a circuit don't work the way I thought they did +- How a voltage divider actually works and why we might use one +- How a photoresistor actually works under the hood # Resistors +Resistors, if you don't know, are a device that goes inside of a circuit to (surprise) provide a resistance to the electrical current in the circuit. The common practical application is to reduce the voltage in the circuit. + I always thought resistors worked much like a hose reducer. Say you have a 2" hose with a 1 gallon per minute of water flowing through it. Now you reduce that hose to 1" with a 2" to 1" adapter. The orifice is physically 50% smaller so now you can only fit 50% as much matter through it in a given period of time. Electronics tutorials often use streams of water as an analogy for electrical current, and this makes easy sense. + + So when I looked at the circuit diagram, I wondered, "why are we measuring the voltage on the circuit UPSTREAM from the photoresistor?" Wouldn't it stand to reason that you would witness the impact of the resistor *downstream* of the resistor? For example it doesn't make sense to measure the impact of a `2" -> 1"` hose reducer on the `2"` side, right? The answer is because, as it turns out, if you take the measurement downstream of the photoresistor in this particular example, you will always read 0v. You will never see a change in the output voltage from the photoresistor. In order to see the impact of the photoresistor in the circuit, you need to measure upstream of the photoresistor. @@ -82,6 +90,12 @@ $$ Vout = 5 * \frac{10k}{10k + 10k} $$ This is also why the tutorial circuit is wired up the way it is. Because it's using a 10k resistor as the first half, and the photoresistor, as the second half, of a voltage divider. As the photoresistor reacts to light, the resistance value of the photoresistor changes, which changes the R2 value in the voltage divider, which varies the value being shown on the tap at the GPIO pin which is processed by the ADC on the ESP32. -