Please advice ADC problem.

Discussion in 'Embedded Systems and Microcontrollers' started by oookey, Apr 8, 2014.

  1. oookey

    Thread Starter Active Member

    May 24, 2010
    Hi all,
    The circuit is to sense brightness at pin 7 via LDR, light ON when dark, OFF when bright.
    Pin 6, to ensure constant current.

    When the circuit powered up, the LED stayed OFF, but once the LED ON, it ON all the way, even the LDR is uncovered.

    I’m using mikroC version 6.0.0 and pickit2.

    The codes is below, please any advice?


    My source code:

    Code ( (Unknown Language)):
    2. // internal oscillator, 8Mhz
    3. //MCLR pin function disable
    5. unsigned LDR, REF;
    6.  void initmain(){
    7.          ansel=0b00011;
    8.          trisio=0b00011; //pin 7&6 inputs.
    9.          cmcon0=0;
    10.          cmcon1=0;
    11.                 }
    13. void main() {
    14.     initmain();
    15.      PWM1_Init(1000000);  //PWM F=1Mhz
    16.   while(1){
    17.        ADC_Init();
    18.        LDR= ADC_Read(0);  
    19.        LDR/=4;
    20.      while(LDR<5){   // darkness value
    21.                              //GPIO.B2=1;
    22.      ADC_Init();
    23.      REF= ADC_Read(1);  // for constant current
    24.      REF/=4;
    26.      if (REF>12) {
    27.              PWM1_Set_Duty(120);
    28.              PWM1_Start(); }
    29.      else { PWM1_Set_Duty(200);
    30.           PWM1_Start();}
    32.         ADC_Init();
    33.         LDR= ADC_Read(0);
    34.         LDR/=4;
    36.                  }
    37.            //PWM1_Stop();
    38.           GPIO.B2=0;
    39.                 }
    40.           }
    Last edited by a moderator: Apr 8, 2014
  2. ErnieM

    AAC Fanatic!

    Apr 24, 2011
    Without code tags your code is quite obfuscated.

    Without pin numbers your schematic is quite obfuscated.

    Have you done anything like stick a voltmeter on your LDR to see the voltage for light and dark? If so, you should share this secret information.

    Do NOT initialize the A2D every time you use it. Init once, read it many times.

    You state the LED is on or off. What's all the PWM stuff doing?
  3. Ian Rogers


    Dec 12, 2012
    You never turn the PWM off.... Of course the LEDs stay on... Once the LDR goes above 4 the pwm sets the duty to 120 or 200 but never turns it off....
  4. oookey

    Thread Starter Active Member

    May 24, 2010
    This circuit was working fine using Flowcode to program with the same PIC, i want to use FC as a stepping stone to learn programing in C language.
    i did try not using PWM in the source code, it ON when LDR is covered, & OFF when uncovered with light shone on LDR, but once introduced PWM problem arises.

    There is not thing to do with stopping the PWM, because when LDR<5 looping will stop GPIO.B2

    Please any advice? thanks
  5. ErnieM

    AAC Fanatic!

    Apr 24, 2011
    Don't turn that circuit on again. You may have already damaged your PIC.

    The emitter of Q1 can conduct into pin 7 thru the ESD diode, that may well be why you are always on.

    More info may follow later.
  6. oookey

    Thread Starter Active Member

    May 24, 2010
    Let me share the flowcode program that it works for the purpose:

    The MCU chip still alive, please any advice?

  7. ErnieM

    AAC Fanatic!

    Apr 24, 2011
    General rule: check the hardware out before the software. That is a chicken and the egg problem as you need software to check out the hardware, but keep it very very simple.

    1. Throw out ALL the PWM code you say you are not using.

    2. Remove the A2D code for the moment.

    3. Add a simple loop:
    Code ( (Unknown Language)):
    2.     while(1)
    3.     {
    4.         GPIO.B2=1;
    5.         delay_ms(1000);
    6.         GPIO.B2=0;
    7.         delay_ms(1000);
    8.     }
    That will let you check out the LED part. Once you see that is working you can start looking at the A2D part.
  8. oookey

    Thread Starter Active Member

    May 24, 2010
    Hi ErnieM,

    the LED on & off 1sec each.
  9. ErnieM

    AAC Fanatic!

    Apr 24, 2011
    That is good, it is supposed to blink that way, but I *hate* the connection from pin 6 to the emitter. Why is it there, what does it do?

    Do you have a voltmeter to see what is happening at pin 7 when you expose the LDR to light and dark?
  10. oookey

    Thread Starter Active Member

    May 24, 2010
    when current flow through R4 [0.62Ω] it generates voltage, i set ADC at pin6 to capture the value ["REF" in my source code], if the REF > 12, MCU will change the PWM duty cycle to a lower cycle, hence current across R4 is lowered, the main idea is to ensure the current within limit.
    in my case the max current = 0.7A
    "REF" max = 0.7x.062=0.434V

    same principal applied to pin 7 the LDR:
    when no light on the LDR, voltage at pin7 is low, due to high value of LDR, so while(LDR<5){....} it works ON/OFF with respect to dark/bright, just GPIO.B2=0 or 1, but once i introduced ADC at pin6, the circuit is not working as intended to. :(

    thanks, any advice?
  11. ErnieM

    AAC Fanatic!

    Apr 24, 2011
    Again, do you have a voltmeter? You can't set the comparison values (LDR<5 and the like) until you know what voltage is there. And people have been making digital light sensors for ever that just drive an input pin so they don't need the A2D.

    Turning on or off pin 5 has no effect on the PWM module that has control of that same pin.

    You can only use the A2D to read the Q1 current when the PWM is driving the thing ON. That means you must synch the signals together and complete the reading while the PWM is on. *WHY* you want to go thru that trouble I don't see as the load (LED) has little effect on how much current is used.

    Besides, you will always read the same value for current.

    How much current are you drawing thru the 2222A? A better driving scheme would be advised over the reading the current.

    (At the end, I don't think the PIC is really required here anyway.)