April 14, 2009 by ranger Awhile back, my brother gave me his box of misc old/hand-me-down/salvaged parts. Most of it was junk, but I did find a few useful things... including a ton of brand new photoresistors! I was able to figure out a little by wiring one up like this: +5v > LED > Photoresistor > GND ...namely that the brighter the light on the photoresistor, the less resistant it becomes. How can I calculate the resistance on the MCU? I tried modifying the tempsensor code, with no luck... it displays a temp of 499.51 degrees no matter how much light is on it. ranger, while waiting for the Nerdsperts to answer, I will hazard a guess. The ADC works by measuring the relative voltage on the ADCn pin as compared to the voltage on AREF (actually it does "binary guessing" in ten steps). So, you need a way to turn variable resistance into variable voltage. What I'd try is put a resistor R across AREF and ADC0, and photoresistor Rp across ADC0 and GND. This will form a voltage divider between AREF and GND, with ADC0 picking up the voltage drop across the photoresistor. Depending on the values of R and Rp_max (resistance you measure on the photoresistor in complete darkness), the voltage range you can measure will go from close to 0V (when the photoresistor is illuminated sufficiently) to Vref*Rp/(R+Rp) (in the dark). Obviously this can't go all the way up to Vref, so you'll probably want to choose a sufficiently high value of R, at least an order of magnitude higher than Rp_max. I didn't try it! I wash my hands. :) Zoran A better circuit would be +5v>10K ohm resistor>photoresistor>GND. then measure the voltage across both the resistor and the photocell. The resistance of the photocell will be porportional to the voltage across it. R1/R2 = V1/V2. You might have to use a 100K ohm resistor or a 1K ohm resistor depending on the photocell. You will also need a high impedence meter for accuracy. The diode(LED) in your circuit has a relatively constant voltage across it regardless of the current going through it. The photodiode therefore has a fairly constant voltage across it too. Only the current will change much. If the processor accepts a 5 Volt input the resistor circuit will read the voltage at the point between the resistor and the photocell and will give you a porportional reading. If it takes 3.3V for the processor then you will need to supply the 3.3V to the circuit to get a correct and porportional reading. Hi! I'm looking for some advice on a small test project I'm working on: I have three phototransistors connected to the ADC of the ATmega168 (ADC0, ADC1 & ADC2). The transistors are connected as follows: +5V-- R1(1k)-- Node to ADC--Phototransistor---GND (as suggested by mikeyw above and elsewhere on the 'net). When I connect them individually, they work fine, but as soon as I connect two or all three, the ADC pins seem to get switched around (I know, it sounds fishy, but it's true) - ADC0 becomes ADC1, ADC1 becomes ADC2 and ADC2 becomes ADC0. It does not seem to be the code, because I use a flashlight to test and they really seem to switch, but I may very well miss something. Here is my code: ``````#define F_CPU 14745600 #include #include #include #include #include #include #include "../libnerdkits/delay.h" #include "../libnerdkits/lcd.h" #include "../libnerdkits/uart.h" void adc_init() { // Set ADC (analog to digital converter) // for external reference +5V, single ended input ADC0 ADMUX = 0; // Set ADC to be enabled, with a clock prescale // of 1/128 so that the ADC clock runs @ 115.2Khz ADCSRA = (1<