Solved: LDR reading is not hitting anticipated levels

Go To Last Post
23 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hye AVRFreaks,
I have problem about LDR reading.
 

adc_read_8bit(ADC_LIGHT_SENSE, AVREF);

By using code above, my LDR reading only display 0x0E and 0x0D when detect the light.
It never exceed 0x0F. Already try different voltage reference but still have same problem.
Here the circuit that Im using right now. I hope someone can help me. Thanks.

 

Split from old thread. - JGM

Last Edited: Mon. Jul 10, 2017 - 12:42 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

What is the value of your LDR in the dark?  What is the value of your LDR in bright light?

If it is a very low value then you will have to reduce the value of R in order to scale the output accordingly

 

What is your Vref value?

 

Please post your entire code related to reading the LDR.

 

Jim

I would rather attempt something great and fail, than attempt nothing and succeed - Fortune Cookie

 

"The critical shortage here is not stuff, but time." - Johan Ekdahl

 

"If you want a career with a known path - become an undertaker. Dead people don't sue!" - Kartman

"Why is there a "Highway to Hell" and only a "Stairway to Heaven"? A prediction of the expected traffic load?"  - Lee "theusch"

 

Speak sweetly. It makes your words easier to digest when at a later date you have to eat them ;-)  - Source Unknown

Please Read: Code-of-Conduct

Atmel Studio6.2/AS7, DipTrace, Quartus, MPLAB user

Last Edited: Mon. Jul 10, 2017 - 01:46 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

VREF = 5V
Actually I dont want indicate value for brightness or darkness condition.
I just want get my value above 0x0F where the maximum = 255 and the minimum = 0.

static inline uint8_t adc_read_8bit(enum adc_mux_setting input,	\
		enum adc_voltage_reference vref)
{
	
/*uint8_t count = 0;*/

#if defined(MUX5)
	/* if bit 6 (MUX5) is set: */
	if (input & (1 << 5)) {
		ADCSRB = (1 << MUX5);
	} else {
		ADCSRB &= ~(1 << MUX5);
	}

	ADMUX
		= ((uint8_t)input &
			ADC_MUX_MASK) | (uint8_t)vref | ADC_ADJUSTMENT_LEFT;
#else
	ADMUX = (uint8_t)input | (uint8_t)vref | ADC_ADJUSTMENT_LEFT;
#endif
	ADCSRA |= (1 << ADSC);

	while ((ADCSRA & (1 << ADSC))) {
// 		/* wait for conversion complete */
// 				pne_delayms(1);
// 				count++;
// 				if (count == 10)
// 				{
// 					count = 0;
// 					fatal_error_to_rf(adc_fatal_error);
// 					break;
// 				}
	}

	return ADCH;
}

I think only this part that related to my LDR value.
 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

That code makes no sense, so I am going to look at the hardware.

 

The source resistance cannot be higher than 10Kohms to the ADC input of the AVR otherwise you will start to get a voltage divider effect.  Thats why I asked what is the values of of your LDR at maximum brightness and maximum darkness.

 

So, what are the Min/MAX resistances, or can you provide a link to the LDR datasheet?

 

Just a side note for myself, I never drive my inputs to the maximum ADC upper limit as I like to leave some room for possible over limit indication.  YMMV.

 

 

From a quick second look, I was wondering if you have the ADLAR set correctly so you are reading the 8MSB's. 

 

Also:

static inline uint8_t adc_read_8bit(enum adc_mux_setting input,	\
		enum adc_voltage_reference vref)

whats the purpose of the "\"?

 

Jim

 

EDIT: Striked out commentary was incorrect.

I would rather attempt something great and fail, than attempt nothing and succeed - Fortune Cookie

 

"The critical shortage here is not stuff, but time." - Johan Ekdahl

 

"If you want a career with a known path - become an undertaker. Dead people don't sue!" - Kartman

"Why is there a "Highway to Hell" and only a "Stairway to Heaven"? A prediction of the expected traffic load?"  - Lee "theusch"

 

Speak sweetly. It makes your words easier to digest when at a later date you have to eat them ;-)  - Source Unknown

Please Read: Code-of-Conduct

Atmel Studio6.2/AS7, DipTrace, Quartus, MPLAB user

Last Edited: Mon. Jul 10, 2017 - 02:44 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Increase the value of R. It is WAY too small. At least 10X larger.

 

Jim

Jim Wagner Oregon Research Electronics, Consulting Div. Tangent, OR, USA http://www.orelectronics.net

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

jgmdesign wrote:

 

whats the purpose of the "\"?

 

That symbol only for continue the next line. It is nothing effect the code.

 

ka7ehk wrote:

Increase the value of R. It is WAY too small. At least 10X larger.

 

 

I will try change the value of R.
Thanks guys...
 

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Jim,

Since the dark resistance of most LDR's is rather high, and the 'light' resistance drops significantly, sometimes into the low ohms scale teh 6.2k resistor should be good enough to get better than the readings the OP is stating they are gettings no?

 

 

East Coast Jim

I would rather attempt something great and fail, than attempt nothing and succeed - Fortune Cookie

 

"The critical shortage here is not stuff, but time." - Johan Ekdahl

 

"If you want a career with a known path - become an undertaker. Dead people don't sue!" - Kartman

"Why is there a "Highway to Hell" and only a "Stairway to Heaven"? A prediction of the expected traffic load?"  - Lee "theusch"

 

Speak sweetly. It makes your words easier to digest when at a later date you have to eat them ;-)  - Source Unknown

Please Read: Code-of-Conduct

Atmel Studio6.2/AS7, DipTrace, Quartus, MPLAB user

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

With such a small resistance, the voltage across the LDR is almost constant. So, the current will follow the theoretical curve quite closely. The down-side is that it is totally dependent on the ADC resolution. With the value posted, the ADC changes by only a few LSB bits. 

 

Just noticed that the reading is only 8 bits. Will also do a LOT better if it is shifted to 10 bit. 4X better resolution.

 

Jim

Jim Wagner Oregon Research Electronics, Consulting Div. Tangent, OR, USA http://www.orelectronics.net

Last Edited: Mon. Jul 10, 2017 - 02:49 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I grabbed a CDS photocell I had here and checked its dark, and light resistance:

 

DArk greater than or about 60k with my finger on the lens

Light about 1.6k

 

I set up the circuit in the OP and I do not have 6.2k, but I took two 3.3k and wired them in series for 6.6k.  5Vdc source.

 

Multimeter gives me 200mv in the dark, 3.9vdc with ambient light.  SO the OP should be able to get better readings than what they are getting.

 

Either the code is flakey or as you say the scale resistor needs to be changed.  But the only sure test is a multimeter at the ADC input

 

ECJ

I would rather attempt something great and fail, than attempt nothing and succeed - Fortune Cookie

 

"The critical shortage here is not stuff, but time." - Johan Ekdahl

 

"If you want a career with a known path - become an undertaker. Dead people don't sue!" - Kartman

"Why is there a "Highway to Hell" and only a "Stairway to Heaven"? A prediction of the expected traffic load?"  - Lee "theusch"

 

Speak sweetly. It makes your words easier to digest when at a later date you have to eat them ;-)  - Source Unknown

Please Read: Code-of-Conduct

Atmel Studio6.2/AS7, DipTrace, Quartus, MPLAB user

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

ka7ehk wrote:

With such a small resistance, the voltage across the LDR is almost constant. So, the current will follow the theoretical curve quite closely. The down-side is that it is totally dependent on the ADC resolution. With the value posted, the ADC changes by only a few LSB bits. 

 

Just noticed that the reading is only 8 bits. Will also do a LOT better if it is shifted to 10 bit. 4X better resolution.

 

Jim

I think so. I should try 10bit ADC resolution rather than 8 bit.

I will give you feedback if I solved this problem.
Thanks again guys.

wink

Kyrul

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 1

KYRUL,

Just measure the voltage at the ADC pin in darkness and full light.  Or post a link to the LDR datasheet.  It will save so much grief.

 

 

Jim

I would rather attempt something great and fail, than attempt nothing and succeed - Fortune Cookie

 

"The critical shortage here is not stuff, but time." - Johan Ekdahl

 

"If you want a career with a known path - become an undertaker. Dead people don't sue!" - Kartman

"Why is there a "Highway to Hell" and only a "Stairway to Heaven"? A prediction of the expected traffic load?"  - Lee "theusch"

 

Speak sweetly. It makes your words easier to digest when at a later date you have to eat them ;-)  - Source Unknown

Please Read: Code-of-Conduct

Atmel Studio6.2/AS7, DipTrace, Quartus, MPLAB user

Last Edited: Mon. Jul 10, 2017 - 03:26 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

jgmdesign wrote:

static inline uint8_t adc_read_8bit(enum adc_mux_setting input,	\
		enum adc_voltage_reference vref)

whats the purpose of the "\"?  

 

kyrul wrote:
That symbol only for continue the next line. It is nothing effect the code.

Time to go back to your 'C' textbook!

 

The line-continuation is used only in preprocessor definitions - it is not used like that!

 

See: https://www.avrfreaks.net/comment...

 

And: https://www.avrfreaks.net/comment...

 

 

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Or post a link to the LDR datasheet.

 Should I give you in private message?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

kyrul wrote:
Should I give you in private message?

Why on earth would you do that ?!?!

 

surprise

 

It is essential information - it needs to be in the public thread!

 

(it should have been in the OP)

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

kyrul wrote:

... I should try 10bit ADC resolution rather than 8 bit.

 

Are you sure that your code actually sets the left align bit? What reading do you get when connecting your ADC input to Vcc - this should be close to 0xFF.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 1

kyrul wrote:
jgmdesign wrote:

 

whats the purpose of the "\"?

 

 

That symbol only for continue the next line. It is nothing effect the code.

 

 

As previously explained, you don't need that, the C compiler treats all whitespace the same, whether tabs, spaces or newlines, except the one inside double quotes (strings). To the C compiler, the entire program might as well be single line.

In fact, the compiler never even sees the line continuation "\", because the preprocessor has already removed it and concatenated the lines.

Last Edited: Mon. Jul 10, 2017 - 10:51 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

     awniel wrote:

Why on earth would you do that ?!?!

     Sorry. Ill post any kind of datasheet that related to my problem in public after this.
 

 

     ccrause wrote:

Are you sure that your code actually sets the left align bit?

     yeah for the adc_read_8bit already set the left align bit.

 

     El Tangas wrote:

To the C compiler, the entire program might as well be single line.

      I will remove this "\" from my code.
 

Thank you so much. Teach me a lot of thing about avr. I already solved the problem by changing the bit resolution from 8-bit to 10-bit.
Here the result about my LDR value in Hex:

Full Light = 0xFE
Normal light = 0x2A

Dark room = 0x00
 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

kyrul wrote:
Ill post any kind of datasheet that related to my problem in public after this.

Just a link is sufficient.

 

      I will remove this "\" from my code.

Teach me a lot of thing about avr.

The above have nothing specifically to do with AVR - or even Atmel/Microchip

 

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

kyrul wrote:
Here the result about my LDR value in Hex: Full Light = 0xFE Normal light = 0x2A Dark room = 0x00

 

I don't see what the issue is.  You are complaining about it not hitting 0xFF and it is sitting at 0xFE?  If that last tick is so important then replace R with a potentiometer and adjust it to get 0xFF at full light.  THen measure the resistance and grab the closest match and solder it in and retest.

 

Jim

I would rather attempt something great and fail, than attempt nothing and succeed - Fortune Cookie

 

"The critical shortage here is not stuff, but time." - Johan Ekdahl

 

"If you want a career with a known path - become an undertaker. Dead people don't sue!" - Kartman

"Why is there a "Highway to Hell" and only a "Stairway to Heaven"? A prediction of the expected traffic load?"  - Lee "theusch"

 

Speak sweetly. It makes your words easier to digest when at a later date you have to eat them ;-)  - Source Unknown

Please Read: Code-of-Conduct

Atmel Studio6.2/AS7, DipTrace, Quartus, MPLAB user

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

The thread title has been changed to SOLVED, but with no explanation as to what the resolve is....

 

KYRUL,

Care to share what the solution is/was?

 

JIm

I would rather attempt something great and fail, than attempt nothing and succeed - Fortune Cookie

 

"The critical shortage here is not stuff, but time." - Johan Ekdahl

 

"If you want a career with a known path - become an undertaker. Dead people don't sue!" - Kartman

"Why is there a "Highway to Hell" and only a "Stairway to Heaven"? A prediction of the expected traffic load?"  - Lee "theusch"

 

Speak sweetly. It makes your words easier to digest when at a later date you have to eat them ;-)  - Source Unknown

Please Read: Code-of-Conduct

Atmel Studio6.2/AS7, DipTrace, Quartus, MPLAB user

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Jim,

 

In post #17, the OP stated...

I already solved the problem by changing the bit resolution from 8-bit to 10-bit.

 

... so I amended the title accordingly.

 

Ross McKenzie ValuSoft Melbourne Australia

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

valusoft wrote:
I amended the title accordingly.

Can you also add the 'this is the solution' flag to that post, or can only the OP do that?

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

valusoft wrote:

Jim,

 

In post #17, the OP stated...

I already solved the problem by changing the bit resolution from 8-bit to 10-bit.

 

... so I amended the title accordingly.

 

 

I think the OP changed the ADLAR and thats what fixed their issue.  Look at his readings:

kyrul wrote:
Here the result about my LDR value in Hex: Full Light = 0xFE Normal light = 0x2A Dark room = 0x00

 

WTHE.

 

awneil wrote:
valusoft wrote: I amended the title accordingly. Can you also add the 'this is the solution' flag to that post, or can only the OP do that?

Only the OP can do that sadly.

 

Jim

I would rather attempt something great and fail, than attempt nothing and succeed - Fortune Cookie

 

"The critical shortage here is not stuff, but time." - Johan Ekdahl

 

"If you want a career with a known path - become an undertaker. Dead people don't sue!" - Kartman

"Why is there a "Highway to Hell" and only a "Stairway to Heaven"? A prediction of the expected traffic load?"  - Lee "theusch"

 

Speak sweetly. It makes your words easier to digest when at a later date you have to eat them ;-)  - Source Unknown

Please Read: Code-of-Conduct

Atmel Studio6.2/AS7, DipTrace, Quartus, MPLAB user