Florisjan
Posts: 19
Joined: Mon Apr 11, 2016 2:49 pm

LDR readout not working properly on RPI 3

Fri Dec 09, 2016 4:39 pm

Hello,

I have just upgraded my pi 2 b to a 3. The ldr system with a capacitor (like this one)...
https://pimylifeup.com/raspberry-pi-light-sensor/)
Image
... worked flawlessly on the b+, but on the new 3 it only gives a result 1 out of 2-6 times. So I get a result, for instance 55, then one to 5 times I get 1, then I get a result again, and so on. It looks like a pattern but I can't figure out what causes it.

For example: 55,1,1,56,1,1,1,43,1,1,1,47...

It seems somehow the capacitor is being drained way too fast during the false readouts, as my multimeter shows it raises to 3.3 volts and then drops again every cycle.

I have tried everything I can think of,

-different gpio port
-different capacitor
-checked the ldr, works fine
-Longer sleeps between measurements
-Longer capacitor charge time

this is the test code I use.:

Code: Select all

#!/usr/bin/python

import RPi.GPIO as GPIO
import time
GPIO.setmode(GPIO.BCM)

def rc_time (pin_to_circuit):
	cap=10000000
	count = 0  
	GPIO.setup(pin_to_circuit, GPIO.OUT)
	GPIO.output(pin_to_circuit, GPIO.HIGH)
	time.sleep(1)
	GPIO.setup(pin_to_circuit, GPIO.IN)
	state=GPIO.input(pin_to_circuit)
	count=0
	while (state == GPIO.HIGH) and (count < cap)
		count += 1
		print count
		time.sleep(.02)
		state=GPIO.input(pin_to_circuit)
	return count
try:
	while 1:
		x= rc_time(17)
		print str(x)
		time.sleep(1)
Is there a solution to this problem?

Thanks

Fj
Last edited by Florisjan on Mon Dec 12, 2016 11:26 am, edited 3 times in total.

User avatar
Ferdinand
Posts: 236
Joined: Sun Dec 01, 2013 2:24 pm
Location: Leiderdorp, NL

Re: LDR readout not working properly on RPI 3

Sat Dec 10, 2016 8:28 am

Hi Florisjan,

I'm very sorry to say but you did not post your latest py script because this script will never run. You have made a couple of mistakes:

1) ":" is missing in line 16
2) It is try and except (you need both). Remove try statement and adjust indentation in succeeding lines, see below
3) in line 11 you have to replace GPIO.HIGH by GPIO.LOW
4) in line 16 replace GPIO.HIGH by GPIO.LOW

Comment:
Before you enter the while (state == ...) loop you have to discharge your capacitor.
Line 11 must "short" the capacitor so replace GPIO.HIGH by GPIO.LOW

======

Main program:

Code: Select all

while 1:
  x= rc_time(17)
  print str(x)
  time.sleep(1)

OR

try:
   while 1:
      x= rc_time(17)
      print str(x)
      time.sleep(1)
except KeyboardInterrupt:
   GPIO.cleanup()
Warning
This method is not a good idea. It's better to use a mosfet to short the capacitor.
(The discharge current could be high and could fry your gpio output so use a "powermosfet".)
See http://electronics.stackexchange.com/qu ... -a-cascode

Success with your project!!!
Success with your project!
Ferdinand

Florisjan
Posts: 19
Joined: Mon Apr 11, 2016 2:49 pm

Re: LDR readout not working properly on RPI 3

Sat Dec 10, 2016 3:54 pm

Hello Ferdinand,

Thanks for your reply!

I see I completely screwed up the code I posted here, sorry about that.

For your information: The swap of high/low in line 11/16 was because I used a two way (no +/-) capacitor, allowing me to charge it and read it out with the same gpio, so a 3.3v wire is not necessary. This actually worked, I remember my amazement when setting it up. Still I should have posted the actual testing code complying with the diagram in the link I stated. I am not sure how I had it wired up to be honest and the 3.3v wire may not be entirely true. All this fiddling with wires for the past 2 days has gotten me confused :) I just looked up the code that worked on the pi b and see I used the same high/low system, proving to me it did work before.

Code: Select all

def lightMeasure (pin_to_circuit):
	try:
		global lightLevel, lightsOn
		count = 0  
		GPIO.setup(pin_to_circuit, GPIO.OUT)
		GPIO.output(pin_to_circuit, GPIO.HIGH) #raise the voltage
		time.sleep(1)
		if exit:
			return
		GPIO.setup(pin_to_circuit, GPIO.IN)
		state=GPIO.input(pin_to_circuit)
		count=0
		while (state == GPIO.HIGH) and ( count <= 160): # wait for the voltage to drop, max 4 seconds measure 
			count += 1
			time.sleep(.02)
			state=GPIO.input(pin_to_circuit)
		if exit:
			return
		if not (count==lightLevel):
			#thread.start_new_thread(log,(''.join(["Light level ",str(count)]),))		
			lightLevel=count
			if lightLevel < lightsAreOnThreshold:
				lightsOn=True
			else:
				lightsOn=False
	except Exception as inst:
		log('!exception raised while measuring light')
		logException(str(type(inst)))
		logException(str(inst.args))
		tb = traceback.format_exc()
		logException(tb)
Your reply was a confirmation for me that it should work as well on the 3, so I swapped them around and also checked and replaced every part one by one and found the problem. One of the LDR leads was dodgy and while testing its resistance seemed to work, while running the setup it didn't. I am very sorry having wasted your time, I should have done more testing to begin with.

About your warning, I have been running this setup for over a year on my B without any problems and I now got it working again on the 3. I would like to know what could cause the output voltage to be high?

In conclusion this has nothing to do with the actual upgrade from the B to the 3 (or has it, as I used a different way of measuring?). Should I delete my post to avoid future confusion?

Thank you very much for your help,

Fj

User avatar
Ferdinand
Posts: 236
Joined: Sun Dec 01, 2013 2:24 pm
Location: Leiderdorp, NL

Re: LDR readout not working properly on RPI 3

Sun Dec 11, 2016 7:54 am

Hi Florisjan,

Some remarks:
The swap of high/low in line 11/16 was because I used a two way (no +/-) capacitor, allowing me to charge it and read it out with the same gpio, so a 3.3v wire is not necessary.
You may use both methods with electrolytic/ceramic capacitors. One side of the capacitor is always positive the other side always grounded (0 V).
About your warning, I have been running this setup for over a year on my B without any problems and I now got it working again on the 3. I would like to know what could cause the output voltage to be high?
A fully charged capacitor acts like a battery of 3.3 V. The GPIO port acts like a switch. If the switch is "closed" then a discharge current could be high enough and could damage one of the output fets in your gpio stage.
An empty capacitor acts like a short circuit. A large capacitor could damage the source fet in your output stage. My advice is to use a mosfet to charge or discharge a capacitor because the output stage is not designed to drive capacitive or inductive loads. You should always try to protect your gpio's with additional circuitries.

Some links:
https://www.scribd.com/mobile/doc/10183 ... s_Control2
https://www.raspberrypi.org/documentati ... /README.md
http://elinux.org/RPi_GPIO_Interface_Circuits
Should I delete my post to avoid future confusion?
Perhaps you can add a drawing of your circuit.

Success with your project!!!
Success with your project!
Ferdinand

Florisjan
Posts: 19
Joined: Mon Apr 11, 2016 2:49 pm

Re: LDR readout not working properly on RPI 3

Mon Dec 12, 2016 11:24 am

Ferdinand wrote:Hi Florisjan,

Some remarks:
About your warning, I have been running this setup for over a year on my B without any problems and I now got it working again on the 3. I would like to know what could cause the output voltage to be high?
A fully charged capacitor acts like a battery of 3.3 V. The GPIO port acts like a switch. If the switch is "closed" then a discharge current could be high enough and could damage one of the output fets in your gpio stage.
An empty capacitor acts like a short circuit. A large capacitor could damage the source fet in your output stage. My advice is to use a mosfet to charge or discharge a capacitor because the output stage is not designed to drive capacitive or inductive loads. You should always try to protect your gpio's with additional circuitries.

Some links:
https://www.scribd.com/mobile/doc/10183 ... s_Control2
https://www.raspberrypi.org/documentati ... /README.md
http://elinux.org/RPi_GPIO_Interface_Circuits


Should I delete my post to avoid future confusion?
Perhaps you can add a drawing of your circuit.

Success with your project!!!
Ok thanks for the explanation, I'll look into it. About the drawing, I wish I made one or took a picture. I am not certain how I had it set up and now I can only get it working the usual way as described in the link. I'll add a picture of that though.

Thanks again!

User avatar
joan
Posts: 14597
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: LDR readout not working properly on RPI 3

Mon Dec 12, 2016 12:33 pm

Alternative implementation at http://abyz.co.uk/rpi/pigpio/examples.h ... pot_cap_py

You don't have to use big electrolytics. I've used 103/104 polyester caps without problems, just need a different resistor.

Return to “Troubleshooting”