Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1of 1

The code is in python and need to be executed in raspberry pi

the cone need to reed analog signal from photo resistant (and it does), parallel to it, it controls PWM - which is not right. Raspberry
pi does not read analog signal at the GPIO port and in this case this problem is solved with the code in the first part. It is same with
the output ( no analog), where do I make my mistake?
import RPi.GPIO as GPIO
import time
# instantiate GPIO as an object
GPIO.setmode(GPIO.BCM)
# define GPIO pins with variables a_pin and b_pin
a_pin = 18
b_pin = 23
c_pin =25
# create discharge function for reading capacitor data
def discharge():
GPIO.setup(a_pin, GPIO.IN)
GPIO.setup(b_pin, GPIO.OUT)
GPIO.output(b_pin, False)
time.sleep(0.005)
# create time function for capturing analog count value
def charge_time():
GPIO.setup(b_pin, GPIO.IN)
GPIO.setup(a_pin, GPIO.OUT)
count = 0
GPIO.output(a_pin, True)
while not
GPIO.input(b_pin):
count =count +1
return count
# analog read function for reading charging and discharging data
def analog_read():
discharge()
return charge_time()
# up untill here is ok, now the change in the PWM we need to control the light depending on the LDR
MY GUESS IS THAT I AM MAKING MY MISTAKE AFTER "changeDutyCycle"
#PWMs
GPIO.setup(c_pin, GPIO.OUT)
#frequency 500 Hz
led_pwm = GPIO.PWM(led_pin,500)
#duty cycle = 100
led_pwm.start(100)
while(True):
if analog_read > 1000:
led_pwm.ChangeDutyCycle(100)
elif analog_read < 1000 and analog_read >=800:
led_pwm.ChangeDutyCycle(80)
elif analog_read < 800 and analog_read >=600:
led_pwm.ChangeDutyCycle(60)
elif analog_read < 600 and analog_read >=400:
led_pwm.ChangeDutyCycle(40)
elif analog_read < 400 and analog_read >=200:
led_pwm.ChangeDutyCycle(20)
elif analog_read < 200 and analog_read >=0:
led_pwm.ChangeDutyCycle(0)

print(analog_read())
time.sleep(1)

You might also like