This commit is contained in:
2026-06-04 21:19:53 -04:00
parent 2b367f343d
commit a0acc6e11d
4 changed files with 36 additions and 3 deletions

View File

@@ -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);
}

View File

@@ -1,15 +1,23 @@
# Photoresistors # Photoresistors
![breadboard](breadboard.png)
This is another analog to digital converter project using a photoresistor like I used a potentiometer in the last project. This is another analog to digital converter project using a photoresistor like I used a potentiometer in the last project.
# Lessons Learned # Lessons Learned
- Resistors in a circuit don't work the way I thought they did - 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
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. 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.
![voltage divider](voltagedivider.png)
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? 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. 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. 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.
<center> # Photoresistors
<iframe width="560" height="315" src="https://www.youtube.com/embed/mLRcBCJlfTI?si=HKVtczsoNd6J_6LD" title="YouTube video player" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share" referrerpolicy="strict-origin-when-cross-origin" allowfullscreen></iframe>
</center> Normal resistors are fixed devices. They are made of a given material that provides a fixed resistance based on the properties of the material.
Photoresistors (or **LDR**s, **Light Dependent Resistors**) are different. They provide different resistance values depending on the amount of light being applied to them. Unlike potentiometers, they don't have mechanical action and don't act like voltage dividers. Instead, photoresistors are made of a semicoductor material (such as cadmium sulfide) that is sandwiched between a conductor and an insulator.
Semiconductors, unlike solid conductors like a copper wire, don't always have free electrons available to carry current. They are usually bound up. When a LDR semiconductor is struck by a photon from a beam of light (or the ambient photons bouncing around my lab area), those electrons absorb energy from the photons, becoming a free electron, which contributes to the material's ability to conduct current. The more photons strike an LDR, the more free electrons it has, therefore the more current it can handle, therefore the less resistance it offers. Conversely, when it is in darkness, there are less photons, therefore the electrons become dormant again, leading to poor conductivity, and higher resistance.
So when we hook the photoresistor to pin 1 on our ESP32 to measure it through the ADC, what we're trying to do is get a measure of how much resistance is currentl being offered by the photoresistor. We want to know how much light is exposed to it, and that can only be measured by measuring the resistance it provides. But the ESP32 can't measure resistance, it can only measure voltage. Hence why we rig the photoresistor up into a voltage divider, and hook it to the ADC pin. With the photoresistor as the bottom leg of the voltage divider, and a constant value on the other half, we can tap the center of the voltage divider and know that any change in the voltage value there is due to a change in the amount of light hitting the photoresistor.

Binary file not shown.

After

Width:  |  Height:  |  Size: 216 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 25 KiB