You have to provide 20mA of current to the LED part of the CNY70. So, you use the Ohm's law to calculate the resistor value depending on the voltage:
I = U / R
R = U / I = 5V / 20mA = 5 / 0.02 = 250 Ohm
Or for 3.3V power, R = 3.3 / 0.02 = 165 Ohm.
The other resistor, the one that connects the phototransistor emitter to the ground, in my case was 4.7k. The phototransistor will change the current that passes from its collector to the emitter based on the proximity of objects. It means the transistor acts as a resistor between its collector and emitter. So, if you connect one more resistor after its emitter, you will effectively implement the so called voltage divider (two resistors connected in series and the middle point dividing the voltage that is applied on the top resistor).
Now, here's the part the shows the collector current, depending on the distance:
![]()
When the object is as close as possible, the highest current is 1mA. So, if it were directly connected to the ground and the voltage was 5V, it would mean the transistor acts as R = U / I = 5V / 0.001A = 5000 Ohm = 5k
When the object is at a higher distance, the effective resistance increases (because the current drops).
Now, the max current of 1mA should be able to pass through the second resistor too, which means it should have a resistance that is less or equal than the effective resistance of the transistor, so it should <= 5k, which is why I used a 4.7k. You can use any value you want but the voltage drop in the measurement point (between the two resistors) would drop too much if the second resistor has a very low value. So you choose one that is comparable to the first one, hence 4.7k which is a good enough value.
If you will be powering through 3.3V, just recalculate, it's easy, the effective resistance is 3.3k, so the second resistor should be probably 3k.
I might be wrong about all that though, electricity has never been a favorite subject of mine 😃