Control LED brightness using LDR of a PIC16F

Discussion in 'The Projects Forum' started by hjki, May 4, 2014.

  1. hjki

    Thread Starter New Member

    May 3, 2014
    1
    0
    Hello everyone..i'm really need your help..i have a problem to understand how to control the brightness of LED using LDR for PIC16F..can you give me the sample coding??I'm really want to learn about it..i have found a sample of coding according to the title as shown below. However, it used an arduino microcontroller instead of PIC16F..please help me..:confused: Thank you..

    Code ( (Unknown Language)):
    1.  
    2. int sensorPin = A0;
    3. int sensorPin1 = A1;
    4. int sensorPin2 = A2;
    5. int led = 11;
    6. int sensorValue = 0;
    7. int sensorValue1 = 0;
    8. int sensorValue2 = 0;
    9.  
    10.  
    11. void setup() {
    12.   Serial.begin(9600);
    13.   pinMode(led, OUTPUT);
    14.   pinMode(sensorPin, INPUT);
    15.   pinMode(sensorPin1, INPUT);
    16.   pinMode(sensorPin2, INPUT);
    17. }
    18.  
    19. void loop () {
    20.  
    21.   sensorValue  = analogRead(sensorPin);  
    22.   sensorValue1 = analogRead(sensorPin1);
    23.   sensorValue2 = analogRead(sensorPin2);
    24.  
    25.   if (sensorValue<500)
    26.   {analogWrite(led, 50);}
    27.  
    28.   if (sensorValue1<500)
    29.   {analogWrite(led, 150);}
    30.  
    31.   if (sensorValue2<500)
    32.   {analogWrite(led, 255);}
    33.  
    34.   Serial.println(sensorValue);
    35.   Serial.println(sensorValue1);
    36.   Serial.println(sensorValue2);
    37.  
    38.   delay(10);
    39. }
    40.  
     
    Last edited by a moderator: May 4, 2014
  2. THE_RB

    AAC Fanatic!

    Feb 11, 2008
    5,435
    1,305
    Why are you measuring 3 sensors to control 1 LED?

    Please show a schematic, and a good description of WHAT the circuit needs to do and WHY.
    :)
     
  3. tshuck

    Well-Known Member

    Oct 18, 2012
    3,531
    675
    You should start at the beginning and learn how to use the PIC before attempting to make this. Learn the parts and how they work, then you will be will equipped for the task at hand.

    Look at the datasheet for the PIC you plan to use and read the pertinent sections (the ADC and PWM sections). You can't hope to make anything with a PIC without opening the datasheet.
     
Loading...