Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

PIC Photocell

Status
Not open for further replies.

EmeraldW

New Member
I know this has been beat to death with several projects but I am looking for a way to take analog readings(voltage) from a photocell and input them into a PIC which will in turn output the reading to either RS232 or USB.

I have a little experience with the 16F628a pics but I am open to some others. How would you connect the photocell to an input and how would I go about taking a reading? I mostly use Picbasic but I can look over ASM.

It doesn't have to be 100% accurate but I would like it to be able to detect and give different readings for changes in the 5% - 10% range if it makes a difference.
 
Do you want the LDR voltage to increase or decrease with dark? What you want is pretty much just a matter of placing the LDR (Photo Cell) in series with a resistor between 5 volts and common. The junction of the LDR and resistor will give the voltage out. As to increasing or decreasing with dark it is which is first between 5 volts and ground. The output would go to a PIC ADC input pin. While I am not much good with a PIC (other than a little PICAXE) that is how I would go about it.

The resistor value used would depend on the light and dark resistance of the LDR.

Ron
 
Last edited:
Up or down wouldn't matter all that much. Up would be easier to deal with once the pic sends a value to the software we are writing but if lower then I can alter the output on the software side.

The whole purpose is to place a clear container, test tube, between a fixed light source such as a 1w led and the photocell to get a reading of optical density.
 
:) Been awhile but been there and done that. We run water chemistry on grade A water and one of the test involves that same test. I forget what it was called exactly. However, I do remember once the sample tube was loaded it was covered to remove ambient light. The system used an incandescent lamp but same general principal. The system was zeroed sans water sample then the water was added and the test run.

The attached image is about what you want in a very basic form. The resistors are shown above the LDR but also can be placed below it. Experiment with the value of R2 and it would be good to use a 10 turn pot as then it can be used as a zero function. The actual values will vary based on the LDR you use.

Hope that helps.
Ron
 

Attachments

  • LDR Volts Out.gif
    LDR Volts Out.gif
    5.4 KB · Views: 216
I have some code for the 12F510 that is a simple analog to RS232 written in assembler.
It writes a specific output frame of data with ascii BCD values, but this is easy to change to any format you like.
 
This is in picbasic pro should get you going

Code:
adval	var	byte		' Create adval to store result


	TRISA = %11111111	' Set PORTA to all input
	ADCON1 = %00000010	' Set PORTA analog and LEFT justify result
	ADCON0 = %11000001	' Configure and turn on A/D Module
	Pause 500		' Wait .5 second


loop:	ADCON0.2 = 1		' Start Conversion

notdone: Pause 5
	If ADCON0.2 = 1 Then notdone	' Wait for low on bit-2 of ADCON0, conversion finished

	adval = ADRESH		' Move high byte of result to adval	

			
	Hserout  [adval]	' Display the decimal value  

	Pause 100		' Wait .1 second

	Goto loop		' Do it forever
	End

You can't use a 16f628a there no ADC on that chip This works with a 16F690
 
Last edited:
Thank you everyone, this is exactly what I am looking for. I love picbasic pro but I am stuck using an older version for now so I might not have access to some of the newer chips. Right now I am leaning toward the 877a.
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top