Society of Robots - Robot Forum

Electronics => Electronics => Topic started by: byuri on July 26, 2008, 03:14:35 AM

Title: 50$ Photoresistors HELP
Post by: byuri on July 26, 2008, 03:14:35 AM
hey, i build my 50$ robot, my MCU is ATmega168. the servos work fine, but the photoresistor sensor nonworking.

i try next prog code :
Code: [Select]
#include "SoR_Utils.h" //includes all the technical stuff
int main(void)
{
int sensor_left=0;//left photoresistor

configure_ports(); // configure which ports are analog, digital, etc.
a2dInit(); // initialize analog to digital converter (ADC)
a2dSetPrescaler(ADC_PRESCALE_DIV32); // configure ADC scaling
a2dSetReference(ADC_REFERENCE_AVCC); // configure ADC reference voltage

/*********ADD YOUR CODE BELOW THIS LINE **********/
LED_off();//turn LED on
while(1)
{
//store sensor data
sensor_left=a2dConvert8bit(5);
if (sensor_left>125)
LED_on();
delay_cycles(500);//a small delay to prevent crazy oscillations
}
/*********ADD YOUR CODE ABOVE THIS LINE **********/
return 0;
}


the LED always off.

i try to change "if (sensor_left>125)" to "if (sensor_left>10)" and now the LED always on. even if i disconnect PHOTORESISTOR from the board the LED work.

anyone have idea what wrong?
Title: Re: 50$ Photoresistors HELP
Post by: bulkhead on July 26, 2008, 02:23:24 PM
If you can, use a multimeter to check if indeed the photoresistor is changing the voltage accordingly (make sure the MCU is getting good sensor data in the first place).
Title: Re: 50$ Photoresistors HELP
Post by: byuri on July 26, 2008, 10:39:10 PM
where to check?
between (Vin and Vout) or (Vin and GND) ?
Title: Re: 50$ Photoresistors HELP
Post by: byuri on July 27, 2008, 06:36:27 AM
If I cover the photoresistors with my hands the voltage on the wire will increase to 5 volt.
and if I put a light on the photoresistors the voltage will drop considerably to around 0.18 volt.
 
I think MCU doesn't read from input ports.
when I write this code :
Code: [Select]
while(1)
  {
  sensor_left=a2dConvert8bit(5);
  sensor_right=a2dConvert8bit(4);
 
  if(sensor_right = sensor_left)
  LED_on();
 
  else
  LED_off();
               delay_cycles(500);
               }

the led always on, even if I disconnect one sensor, the led is still on.
Title: Re: 50$ Photoresistors HELP
Post by: Ro-Bot-X on July 27, 2008, 01:27:10 PM
The light sensors are Light Dependant Resistors made of CadmiumSulfide. Not 2 sensors will behave the same. So you have a verry slight chance of having them returning the same value. What you should do is set a treshold like in the first example code. Try to adjust it for each sensor separately to turn on (or off) the LED only when enough light shines on them. In your first post the treshold was too high at first then you made it too low. When you remove the sensor the port is floating so there will be a false reading of the A2D converter.
Title: Re: 50$ Photoresistors HELP
Post by: byuri on July 27, 2008, 01:35:15 PM
thanks everything work fine.